View Full Version : MarketDepthEventArgs - Clarification needed
ScoobyStoo
05-17-2009, 10:52 AM
Hi,
Could I please get some clarification on the details provided by http://www.ninjatrader-support.com/HelpGuideV6/helpguide.html?MarketDepthEventArgs, specifically relating to the Operation property?
The help guide states:
Operation
Represents the action you should take when building a level two book.
Possible values are:
Operation.Insert
Operation.Remove
Operation.Update
Why does this refer to the action I should take when building my own order book? All I want is the event args to tell me what volume change has occured at what price level in the book.
I've looked at the SampleMarketDepth example on these forums but am still unclear about under which exact circumstances this event gets fired, and the correct meaning/usage of the Operation (and Position) properties. For instance, does an event with e.Operation == Operation.Remove relate to the situation where a row has dropped off the top or bottom of the ladder, or when an order has been removed from the book?
I'm guessing here that this API event is a wrapper for exposing some internal functionality related to the visual SuperDOM modules. Some help in understanding it would be gratefully appreciated.
Thanks.
NinjaTrader_Dierk
05-17-2009, 11:10 AM
This reference sample should help: http://www.ninjatrader-support2.com/vb/showthread.php?t=3478
ScoobyStoo
05-17-2009, 11:13 AM
This reference sample should help: http://www.ninjatrader-support2.com/vb/showthread.php?t=3478
Thanks Dierk. But as I said, I've looked at that sample and it doesn't actually explain much about the logic behind the event. It just gives you one example of how the event can be used, which isn't much help if you don't wish to use it in that way.
NinjaTrader_Dierk
05-17-2009, 02:23 PM
The example shows you how to build an order book which actually requires processing Insert, Update and Delete properly.
ScoobyStoo
05-18-2009, 05:54 AM
The example shows you how to build an order book which actually requires processing Insert, Update and Delete properly.
I think you are missing my point. I don't want to build an order book in the same way as featured in the example. What I need is a proper understanding of the underlying event model, not how to implement it for this one example scenario. The documentation is a little light in explaining exactly what the properties correspond to, and I need a completely clear understanding of what's going on.
Could you have a look under the covers of NT's source code and just confirm exactly how these properties are set?
Thanks
NinjaTrader_Dierk
05-18-2009, 06:01 AM
- Insert: insert record at .Position
- Remove: remove record from .Position
- Update: update record at .Position
Unfortunately there isn't more information to provide.
ScoobyStoo
05-18-2009, 09:42 AM
OK, so I'll have a dig around myself to figure out how this works...
I've just quickly created the following custom indicator and added it to a chart to check out the firing of events:
namespace NinjaTrader.Indicator
{
/// <summary>
/// Dumps event sequence
/// </summary>
[Description("Dumps event sequence")]
public class DumpEventSequence : Indicator
{
protected override void Initialize()
{
Print("Initialize()");
Print("Time : " + DateTime.Now.ToLongTimeString());
Print(Environment.NewLine);
CalculateOnBarClose = false;
Overlay = false;
PriceTypeSupported = false;
}
protected override void OnBarUpdate()
{
Print("OnBarUpdate()");
Print("Time : " + DateTime.Now.ToLongTimeString());
Print(Environment.NewLine);
}
protected override void OnMarketDepth(MarketDepthEventArgs args)
{
Print("OnMarketDepth()");
Print("Time : " + DateTime.Now.ToLongTimeString());
Print("MarketDataType : " + args.MarketDataType.ToString());
Print("Operation : " + args.Operation.ToString());
Print("Position : " + args.Position.ToString());
Print("Price : " + args.Price.ToString());
Print("Volume : " + args.Volume.ToString());
Print(Environment.NewLine);
}
}
}
I've attached the output.
Can you let me please why the indicator's Initialize method (which is supposed to be called once) is being called twice (at 4:29:10 PM and 4:29:15 PM) before the 10 levels of the DOM order book are built and then again once (at 4:29:17 PM) before the historical bars on the chart are processed?
NinjaTrader_Bertrand
05-18-2009, 10:35 AM
The Initialize() is expected to be called multiple times - http://www.ninjatrader-support.com/HelpGuideV6/Initialize.html
If your code is sensitive to being called multiple times, please move to another location such as OnBarUdpate() or OnMarketDepth() etc..
ScoobyStoo
05-20-2009, 04:18 AM
The Initialize() is expected to be called multiple times - http://www.ninjatrader-support.com/HelpGuideV6/Initialize.html
If your code is sensitive to being called multiple times, please move to another location such as OnBarUdpate() or OnMarketDepth() etc..
Ok thanks.
Can I suggest you alter the indicator code template as it misleadingly states:
/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
I'm pretty sure most developers new to NT will assume that any initialisation method is called only once upon initialisation of the object. This is a convention and I think you need to make it really clear in the code template that this is not the case.
P.S. Where should I place initialisation code then that I want called only once?
NinjaTrader_Dierk
05-20-2009, 04:22 AM
There is a misunderstanding: Initialize() is called once PER INSTANCE of an indicator. As you configure an indicator/strategy multiple instances (some of them temporary) of the same indicator/strategy might be created.
ScoobyStoo
05-20-2009, 04:57 AM
There is a misunderstanding: Initialize() is called once PER INSTANCE of an indicator. As you configure an indicator/strategy multiple instances (some of them temporary) of the same indicator/strategy might be created.
OK. I think I understand now. So for example, when a user brings up the indicators dialog box it creates an instance of every indicator in order to display their default properties (obviously), but the initialise() method is also called in these cases.
Is there an event/method that I can use to perform some expensive operations, which I only want performed when an indicator is actually added to a chart? I'm guessing not as I can't find anything in the manual.
NinjaTrader_Dierk
05-20-2009, 05:01 AM
>> I'm guessing not
Correct -> you would need to code using some flag in OnBarUpdate or such.
ScoobyStoo
05-20-2009, 05:53 AM
>> I'm guessing not
Correct -> you would need to code using some flag in OnBarUpdate or such.
Yeah. I had assumed that the object was constructed into order to display/set properties but was only initialised when the indicator was actually added to a chart.
Also, I have discovered that OnMarketData() is called before Initialize(), which is not made clear.
Tell you what, some documentation on the lifecycles of NT indicators and strategies would be really helpful. Are there any other gotchas like this I need to be aware of?
ScoobyStoo
05-20-2009, 06:15 AM
My specific problem is that the OnMarketData() method is called 10 times to insert all 10 rows into the order book ladder before the Initialize() method is called.
I want to perform operations within the OnMarketData() method involving objects which I have initialised in the Initialize() method...but I am obviously unable to do this.
How do you recommend I work around this?
NinjaTrader_Dierk
05-20-2009, 06:55 AM
OnMarketData() will NOT be called before Initialize().
ScoobyStoo
05-20-2009, 09:22 AM
OnMarketData() will NOT be called before Initialize().
Dierk
Can I suggest that you create a custom indicator using the attached code and then run it against any instrument.
I have also attached the log that it generates when connected to my ZenFire feed for the 6E futures contract.
You will see that the order of events firing is:
Initialize()
Time : 16:05:19
This event is fired when the Indicators dialog box opened
Initialize()
Time : 16:05:25
This event is fired when the DumpEventSequence indicator is selected within the Indicators dialog box
OnMarketDepth()
Time : 16:05:28
MarketDataType : Ask
Operation : Insert
Position : 0
Price : 919.5
Volume : 668
This event is fired 10 times to insert the 10 rows of the order book
Initialize()
Time : 16:05:28
This event is fired when the DumpEventSequence indicator is added to the chart
OnBarUpdate()
Time : 16:05:28
This event is fired numerous times as the DumpEventSequence indicator processes the historical bars
As you can see, the OnMarketData() method is called to build the initial order book before the Initialize() method is called in response to the indicator being added to the chart.
Please can you verify this.
I am running NT v6.5.1000.10.
NinjaTrader_Dierk
05-20-2009, 01:28 PM
My apologies. Your findings are accurate. This is a bug which will be fixed with NT7: OnMarketData/Depth then only will be called AFTER Initialize() (like any other NS method).
Thanks for reporting that one.
ScoobyStoo
05-21-2009, 02:49 AM
My apologies. Your findings are accurate. This is a bug which will be fixed with NT7: OnMarketData/Depth then only will be called AFTER Initialize() (like any other NS method).
Thanks for reporting that one.
OK, thanks.
Could I also suggest for NT7 that the Initialize() method is only called when an indicator is actually added to a chart and starts to process data.
NinjaTrader_Dierk
05-21-2009, 03:53 AM
Thanks for your suggestion. We'll add it to the list of future considerations.
Ralph
05-21-2009, 04:29 AM
...Could I also suggest for NT7 that the Initialize() method is only called when an indicator is actually added to a chart and starts to process data.
If it turns out to be impossible because of compatibility issues, then please add at least an additional method which is doing exactly this.
Regards
Ralph