NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 05-17-2009, 10:52 AM   #1
ScoobyStoo
Senior Member
 
Join Date: Apr 2009
Posts: 161
Thanks: 0
Thanked 5 times in 5 posts
Default MarketDepthEventArgs - Clarification needed

Hi,

Could I please get some clarification on the details provided by http://www.ninjatrader-support.com/H...DepthEventArgs, 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.
Last edited by ScoobyStoo; 05-17-2009 at 10:55 AM.
ScoobyStoo is offline  
Reply With Quote
Old 05-17-2009, 11:10 AM   #2
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

This reference sample should help: http://www.ninjatrader-support2.com/...ead.php?t=3478
NinjaTrader_Dierk is offline  
Reply With Quote
Old 05-17-2009, 11:13 AM   #3
ScoobyStoo
Senior Member
 
Join Date: Apr 2009
Posts: 161
Thanks: 0
Thanked 5 times in 5 posts
Default

Quote:
Originally Posted by NinjaTrader_Dierk View Post
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.
ScoobyStoo is offline  
Reply With Quote
Old 05-17-2009, 02:23 PM   #4
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

The example shows you how to build an order book which actually requires processing Insert, Update and Delete properly.
NinjaTrader_Dierk is offline  
Reply With Quote
Old 05-18-2009, 05:54 AM   #5
ScoobyStoo
Senior Member
 
Join Date: Apr 2009
Posts: 161
Thanks: 0
Thanked 5 times in 5 posts
Default

Quote:
Originally Posted by NinjaTrader_Dierk View Post
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
ScoobyStoo is offline  
Reply With Quote
Old 05-18-2009, 06:01 AM   #6
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

- Insert: insert record at .Position
- Remove: remove record from .Position
- Update: update record at .Position

Unfortunately there isn't more information to provide.
NinjaTrader_Dierk is offline  
Reply With Quote
Old 05-18-2009, 09:42 AM   #7
ScoobyStoo
Senior Member
 
Join Date: Apr 2009
Posts: 161
Thanks: 0
Thanked 5 times in 5 posts
Default

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?
Attached Files
File Type: txt EventLog.txt (12.0 KB, 18 views)
ScoobyStoo is offline  
Reply With Quote
Old 05-18-2009, 10:35 AM   #8
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,409
Thanks: 252
Thanked 975 times in 958 posts
Default

The Initialize() is expected to be called multiple times - http://www.ninjatrader-support.com/H...nitialize.html


If your code is sensitive to being called multiple times, please move to another location such as OnBarUdpate() or OnMarketDepth() etc..
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 05-20-2009, 04:18 AM   #9
ScoobyStoo
Senior Member
 
Join Date: Apr 2009
Posts: 161
Thanks: 0
Thanked 5 times in 5 posts
Default

Quote:
Originally Posted by NinjaTrader_Bertrand View Post
The Initialize() is expected to be called multiple times - http://www.ninjatrader-support.com/H...nitialize.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?
ScoobyStoo is offline  
Reply With Quote
Old 05-20-2009, 04:22 AM   #10
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

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.
NinjaTrader_Dierk is offline  
Reply With Quote
Old 05-20-2009, 04:57 AM   #11
ScoobyStoo
Senior Member
 
Join Date: Apr 2009
Posts: 161
Thanks: 0
Thanked 5 times in 5 posts
Default

Quote:
Originally Posted by NinjaTrader_Dierk View Post
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.
ScoobyStoo is offline  
Reply With Quote
Old 05-20-2009, 05:01 AM   #12
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

>> I'm guessing not
Correct -> you would need to code using some flag in OnBarUpdate or such.
NinjaTrader_Dierk is offline  
Reply With Quote
Old 05-20-2009, 05:53 AM   #13
ScoobyStoo
Senior Member
 
Join Date: Apr 2009
Posts: 161
Thanks: 0
Thanked 5 times in 5 posts
Default

Quote:
Originally Posted by NinjaTrader_Dierk View Post
>> 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 is offline  
Reply With Quote
Old 05-20-2009, 06:15 AM   #14
ScoobyStoo
Senior Member
 
Join Date: Apr 2009
Posts: 161
Thanks: 0
Thanked 5 times in 5 posts
Default

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?
ScoobyStoo is offline  
Reply With Quote
Old 05-20-2009, 06:55 AM   #15
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

OnMarketData() will NOT be called before Initialize().
NinjaTrader_Dierk is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Clarification on IB connection OLIVER97 Connecting 1 07-25-2008 06:21 AM
CalculateOnBarClose clarification trader_rick Automated Trading 4 06-25-2008 09:01 AM
Clarification Mike_32 Installation and Licensing 14 05-11-2008 01:49 PM
NT requirements clarification newguy05 Connecting 3 02-11-2008 10:02 PM
Clarification of GetCurrentAskVolume() Method Learning1 General Programming 8 08-14-2007 01:56 PM


All times are GMT -6. The time now is 04:44 AM.