PDA

View Full Version : Accessing Incoming Tick Data - continued


richard
04-26-2006, 04:44 AM
Hi. I finally got around to coding an indicator to track the incoming ticks.

in the OnBarUpdate method, I have the following code:

if (MarketData!=null)

{

if (MarketData.Bid!=null)

{

Print(MarketData.Bid.Time.ToString()+" "+MarketData.Bid.Price.ToString()+" "+MarketData.Ask.Price.ToString()+" "+MarketData.Bid.Volume.ToString()+"x"+MarketData.Ask.Volume.ToString());

Log(MarketData.Bid.Time.ToString("MMddyyyy"),MarketData.Bid.Time.ToString("yyyy/MM/dd")+","+MarketData.Bid.Time.ToString("HH:mm:ss")+","+MarketData.Bid.Price.ToString()+","+MarketData.Ask.Price.ToString()+","+MarketData.Bid.Volume.ToString()+","+MarketData.Ask.Volume.ToString());

}

if (MarketData.Last!=null)

{

Print(MarketData.Last.Time.ToString()+" "+MarketData.Last.Price.ToString()+" "+MarketData.Last.Volume.ToString());

Log(MarketData.Last.Time.ToString("MMddyyyy"),MarketData.Last.Time.ToString("yyyy/MM/dd")+","+MarketData.Last.Time.ToString("HH:mm:ss")+","+MarketData.Last.Price.ToString()+","+MarketData.Last.Volume.ToString());

}

}



The Log method will just write to a file.

My problem is this:

I've noticed that the bid/ask does not always agree with my esignal "Time and Sales" window which issupposed to show the same thing. I also noticed that in esignal, all changed bid/sales (without any previous price traded) will be displayed, but I think the OnBarUpdate() filters these changes and only returns the final bid/ask. Is this correct?

The other problem I'm seeing is that the Last traded object does not add up with esignal. Also when I am inspecting the Bid object, it seems like the Last object may have been from a previous trade and not a part of this "tick" data (i.e., the bid/ask data). Is that correct? And if so, how can I insure that the Last object that I'm inspecting is really a new trade that occurred and not from a previous trade?

Thanks.

NinjaTrader_Ray
04-26-2006, 05:48 AM
You are coding this against the OnBarUpdate method which only fires -

- On change in the last trade (not bid/ask)
- There is optimization under the hood that may prevent the OnBarUpdate() method to fire on every change in last trade. For example, the last trade and price change you get the event, if only the last trade size changes and its value is the same as the prior, the event will not fire. This is because most chart indicators's values and plots will not change so therefore there is no reason to have NT re-calcualte values.

Because of the above, you will only log the bid/ask at the current time that the OnBarUpdate() event is fired which is less frequent than NT's internal market data events. I will check to see if there is a way to code against market data events.

Ray

NinjaTrader_Ray
04-26-2006, 07:34 AM
You can attach an event handler for all incoming market data events.

MarketData.MarketDataItem += new MarketDataItemEventHandler(MarketData_MarketDataIt em);

I am not sure if you can add that to the Initalize() method or if you will have to do it in the OnBarUpdate(). MarketData may still be null in the Initalize() method.

Then create a method:

private void MarketData_MarketDataItem(object sender, MarketDataEventArgs e)
{
if (e.MarketDataType == Data.MarketDataType.Ask)
// Do something with e.Price, e.Volume or e.Time
}

Don't forget, the indictor display will only update when the OnBarUpdate() method fires therefore any values calculated in the market data handler need to be stored.

richard
04-26-2006, 07:41 AM
Thanks Ray for your reply.

Please check to see if there is a way for me to read the raw tick data that's coming in. I need to be able totrack the bid/ask (even if there is no trade in between) and each trade that occurs (without any filtering).

Thanks.

NinjaTrader_Ray
04-26-2006, 07:54 AM
Richard,

The replies I gave you provide a way to read the incoming market data events unfiltered in real-time. If you want to update the chart display for each incoming market data event...this is not possible since the chart refresh methods are outside of the indicator class.


Ray

richard
04-27-2006, 04:11 AM
Ray,

Sorry, I didn't see your 3:34 post.

I tried the following code:

private void MarketData_MarketDataItem(object sender, MarketDataEventArgs e)

{

if (e.MarketDataType == Data.MarketDataType.Ask)

{

// Do something with e.Price, e.Volume or e.Time

Print("ASK: "+e.Time.ToString()+" "+e.Price.ToString()+" "+e.Volume.ToString());

}

else if (e.MarketDataType == Data.MarketDataType.Bid)

{

// Do something with e.Price, e.Volume or e.Time

Print("BID: "+e.Time.ToString()+" "+e.Price.ToString()+" "+e.Volume.ToString());

}

else if (e.MarketDataType == Data.MarketDataType.Last)

{

// Do something with e.Price, e.Volume or e.Time

Print("LAST: "+e.Time.ToString()+" "+e.Price.ToString()+" "+e.Volume.ToString());

}

Print("1");



}





/// <summary>

/// Called on each bar update event (incoming tick)

/// </summary>

protected override void OnBarUpdate()

{

// Use this method for calculating your indicator values. Assign a value to each

// plot below by replacing 'Close[0]' with your own formula.

Plot0.Set(Close[0]);

Print("barupdate:");

MarketData.MarketDataItem += new MarketDataItemEventHandler(MarketData_MarketDataIt em);

}

When I put

MarketData.MarketDataItem += new MarketDataItemEventHandler(MarketData_MarketDataIt em);

in Initialize(), I get no error messages, but when I run my indicator, OnBarUpdate() is not firing.

So I put it in OnBarUpdate() and now the indicator just hangs. Sorry, I am not a C# programmer. I am a Visual Studio 6.0 programmer. Maybe, I'm just doing something stupid here? But it looks like you are creating a new event handler everytime in OnBarUpdate()?

Thanks for your help.

NinjaTrader_Ray
04-27-2006, 05:58 AM
richard wrote: But it looks like you are creating a new event handler everytime in OnBarUpdate()?



Could be the case. Add some code so that it is only created once.

private bool handlerCreated

OnBarUpdate()
{
if (!handerCreated)
MarketData.MarketDataItem += new MarketDataItemEventHandler(MarketData_MarketDataIt em);
}

Something like that.

richard
04-27-2006, 02:28 PM
Ray,

I added the check and now only create the event handler once in OnBarUpdate(). It looks like I am now getting the correct bid/ask tick data. But if you look at my Print from the Output window, it looks like the Bid, ask and Last will always fire exactly twice everytime. Why is that? Can I prevent that?

Also, if you look, there are some lines with just a "1" printed. This means the event handler fired and the object passed was not the Ask, Bid or Last object. Is there any documentation on what else is in the MarkletData object?

Thanks again.



Dump of some output window lines:

barupdate:

BID: 4/27/2006 10:20:56 PM 1312 9

1

BID: 4/27/2006 10:20:56 PM 1312 9

1

ASK: 4/27/2006 10:20:56 PM 1312.25 39

1

ASK: 4/27/2006 10:20:56 PM 1312.25 39

1

BID: 4/27/2006 10:21:12 PM 1312 10

1

BID: 4/27/2006 10:21:12 PM 1312 10

1

ASK: 4/27/2006 10:21:12 PM 1312.25 39

1

ASK: 4/27/2006 10:21:12 PM 1312.25 39

1

LAST: 4/27/2006 10:21:27 PM 1312.25 1

1

LAST: 4/27/2006 10:21:27 PM 1312.25 1

1

1

1

barupdate:

barupdate:

BID: 4/27/2006 10:21:27 PM 1312 9

1

BID: 4/27/2006 10:21:27 PM 1312 9

1

ASK: 4/27/2006 10:21:27 PM 1312.25 38

1

ASK: 4/27/2006 10:21:27 PM 1312.25 38

1

BID: 4/27/2006 10:21:29 PM 1312 11

1

BID: 4/27/2006 10:21:29 PM 1312 11

1

ASK: 4/27/2006 10:21:29 PM 1312.25 38

1

ASK: 4/27/2006 10:21:29 PM 1312.25 38

1

NinjaTrader_Ray
04-28-2006, 12:15 AM
It would mean that there are likely two event handlers registered. This stuff we are getting into is really purposely undocumented sincewe arenot prepared to support this at this time. There could be two event handlers due to your code or because of something on our side. There are numerous events that would fire like daily high, low,allof which areundocumneted at this time.

Ray

richard
04-28-2006, 04:32 AM
Ray,

Weird. The problem went away this morning. Not sure, if its because I rebooted or the previous code (without checking if the event handler was created or not) was causing this.

But either way, its working for now. And I do truly appreciate your help in getting me setup on this. As a programmer, I know supporting about undocumented features.

Barring any further problems, what I have now should work for me. A BIG Thank You again!

NinjaTrader_Ray
04-28-2006, 04:57 AM
Great Richard and you are welcome!