NexusFi: Find Your Edge


Home Menu

 





Help multi-instruments to access OnMarketData(MarketDataEventArgs e) for live Ask/Bid


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Jigsaw Trading with 3 posts (0 thanks)
    2. looks_two chrisflow with 2 posts (0 thanks)
    3. looks_3 vvhg with 1 posts (0 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
    1. trending_up 3,710 views
    2. thumb_up 0 thanks given
    3. group 3 followers
    1. forum 6 posts
    2. attach_file 2 attachments




 
Search this Thread

Help multi-instruments to access OnMarketData(MarketDataEventArgs e) for live Ask/Bid

  #1 (permalink)
 
chrisflow's Avatar
 chrisflow 
New Orleans, LA
 
Experience: Intermediate
Platform: xtrader ADL, NinjaTrader7
Trading: Guitar and CL
Posts: 15 since Feb 2010
Thanks Given: 54
Thanks Received: 5

Hi,

Trying to modify "BidAskSpread.zip" in order to get the spread between TWO instruments bids & asks (calendar spread). No luck.

Why? I know there are "spread" indicators out there that show spread based on the "last trade" price. But I'm looking for one that indicates the spread based on the current bid-ask between calendar months. i.e., I want the difference between the CL June Bid vs the CL December Bid, as well as the CL June Ask vs the CL Dec Ask. I've found that the bid/asks between these two do vary, but quickly. An indicator would help to use in strategy development to place the trade. Again, I don't want the "last trade" price of the spread because the Dec trade often happens on a delay due to thin market. Run-of-the-mill spread indicators using "last trade" suffer from illusory spreads due to the trades not occurring simultaneously, where last trade is way behind the current bid-asks.

I uploaded a photo of my DOM showing that the later month CL contract last price (in yellow) is behind the current bid-ask. Most spread indicators look at the in the 2nd pic on the right (SpreadCandlesticks) isn't useful when the last price is outside of the current bid-ask - it would have me buy at the last price, a price that is no longer current with the bid-ask.

The code in "BidAskSpread.zip" shows live bid/ask on the chart using OnMarketData(MarketDataEventsArgs e):

protected override void OnMarketData(MarketDataEventArgs e)
{
if (e.MarketData.Ask == null || e.MarketData.Bid == null)
return;
double ask=e.MarketData.Ask.Price;
double bid=e.MarketData.Bid.Price;
}


Question is: How to I access OnMarketData for instrument #1 vs instrument #2?? How to I add the second instrument??

ShowBidAsk.zip


Thanks all!

PS I realize that a better solution to this is a colocated platform such as CQG spreader. I'm just testing proof of concept here for myself. Latency is probably too large to capitalize on spreads using any other type of remote PC/ modem connection.

Attached Thumbnails
Click image for larger version

Name:	bidask.jpg
Views:	207
Size:	84.3 KB
ID:	113698   Click image for larger version

Name:	bidask2.jpg
Views:	212
Size:	156.4 KB
ID:	113699  
Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
REcommedations for programming help
Sierra Chart
Exit Strategy
NinjaTrader
How to apply profiles
Traders Hideout
ZombieSqueeze
Platforms and Indicators
Increase in trading performance by 75%
The Elite Circle
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
34 thanks
Tao te Trade: way of the WLD
24 thanks
My NQ Trading Journal
14 thanks
GFIs1 1 DAX trade per day journal
11 thanks
Vinny E-Mini & Algobox Review TRADE ROOM
10 thanks
  #3 (permalink)
 
Jigsaw Trading's Avatar
 Jigsaw Trading  Jigsaw Trading is an official Site Sponsor
 
Posts: 2,988 since Nov 2010
Thanks Given: 831
Thanks Received: 10,393


Good question - I am note sure it can be don, the event handler presumes the first instrument on the chart and there's no subscribe method to subscribe to more.

How about you have 2 charts, one for each instrument and use global variables to store the bid/asks from both.

Then on one script you can just grab the global variable that the other one set....

If you have any questions about the products or services provided, please send me a Private Message or use the futures.io " Ask Me Anything" thread
Visit my NexusFi Trade Journal Reply With Quote
  #4 (permalink)
 
chrisflow's Avatar
 chrisflow 
New Orleans, LA
 
Experience: Intermediate
Platform: xtrader ADL, NinjaTrader7
Trading: Guitar and CL
Posts: 15 since Feb 2010
Thanks Given: 54
Thanks Received: 5

Thanks! Interesting solution.

But how can the indicator that pulls the global variable pull it with each change of the bid-ask? The receiving indicator may only pull every time it has an OnBarUpdate(), thus missing tons of live bid-asks. Am I right to say this?


NT website forum had this to say:

protected override void OnMarketData(MarketDataEventArgs e)
{
if (BarsInProgress == 0)
//Handle primary series bid/ask here.

if (BarsInProgress == 1)
//Handle secondary series bid/ask here.
}

from:
https://forum.ninjatrader.com/showthread.php?t=42866&highlight=onmarketdata+instruments



BUT I tried that and I'm getting an error that my var "does not exist in current context"

I've added a second instrument, and used "BarsInProgress" to parse out the bid-asks. Can you tell what is wrong? It works if I remove the "if(BarsInProgress ==0)", but that defeats the ability to get the bid-asks of each instrument separately.


protected override void Initialize()
{
Add(Symbol2, BarsPeriod.Id, BarsPeriod.Value);
}
protected override void OnMarketData(MarketDataEventArgs e)
{
if (e.MarketData.Ask == null || e.MarketData.Bid == null)
return;

if (BarsInProgress == 0) {
double aAsk=e.MarketData.Ask.Price;
double aBid=e.MarketData.Bid.Price;
}

SpreadHigh.Set( Qty1* aAsk ); // throws error "The name 'aAsk" does not exists in the current context"
SpreadLow.Set( Qty1* aBid ); // throws error "The name 'aBid" does not exists in the current context"


Edit: found solution..
if (BarsInProgress == 0) {
double aAsk = e.MarketData.Ask.Price;
double aBid = e.MarketData.Bid.Price;

SpreadHigh.Set( Qty1* aAsk );
SpreadLow.Set( Qty1* aBid );
}

Started this thread Reply With Quote
  #5 (permalink)
 
Jigsaw Trading's Avatar
 Jigsaw Trading  Jigsaw Trading is an official Site Sponsor
 
Posts: 2,988 since Nov 2010
Thanks Given: 831
Thanks Received: 10,393

OnMarketData fires every tick, so it would store the bid/ask often enough as long as you set the global var in there.

In terms of getting it, you could grab it on the other indicator in the OnMarketData method or you could have a timer that fires every 20ms to compare the latest of the values.

I've never tried to use OnMarketData with 2 instruments on 1 chart. Like I say - 2 charts, 2 scripts, each with an OnMarketData that posts to global variables both scripts can access.

If you have any questions about the products or services provided, please send me a Private Message or use the futures.io " Ask Me Anything" thread
Visit my NexusFi Trade Journal Reply With Quote
  #6 (permalink)
 
vvhg's Avatar
 vvhg 
Northern Germany
 
Experience: Intermediate
Platform: NT
Trading: FDAX, CL
Posts: 1,583 since Mar 2011
Thanks Given: 1,016
Thanks Received: 2,824


DionysusToast View Post
Good question - I am note sure it can be don, the event handler presumes the first instrument on the chart and there's no subscribe method to subscribe to more.

How about you have 2 charts, one for each instrument and use global variables to store the bid/asks from both.

Then on one script you can just grab the global variable that the other one set....

@DionysusToast, I guess you are certain that OnMarketData only fires for the primary DataSeries?

If so global vars seems the way to go, but I would write an EventHandler to them that I could subscribe to in the other indicator. That way I get all updates instantly with no overhead checking periodically.

Vvhg

Hic Rhodos, hic salta.
Reply With Quote
  #7 (permalink)
 
Jigsaw Trading's Avatar
 Jigsaw Trading  Jigsaw Trading is an official Site Sponsor
 
Posts: 2,988 since Nov 2010
Thanks Given: 831
Thanks Received: 10,393


vvhg View Post
@DionysusToast, I guess you are certain that OnMarketData only fires for the primary DataSeries?

If so global vars seems the way to go, but I would write an EventHandler to them that I could subscribe to in the other indicator. That way I get all updates instantly with no overhead checking periodically.

Vvhg

I'm not certain - I've just never used it on 2 series.

If you have any questions about the products or services provided, please send me a Private Message or use the futures.io " Ask Me Anything" thread
Visit my NexusFi Trade Journal Reply With Quote




Last Updated on May 25, 2013


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts