MarketDepth

View as Markdown

Definition

MarketDepth can be used to access snapshot market depth and for subscribing to market depth events.

  1. Remember to unsubscribe if you are no longer using the subscription.
  2. You should only unsubscribe to a market depth event if you are actually subscribed.
  3. You must unsubscribe from the same thread where the subscription is made. It is therefore recommended to use an Instrument’s Dispatcher to ensure this is handled properly.

Properties

PropertyDescription
AsksList of ask prices
BidsList of bid prices
InstrumentInstrument representing the instrument of the market depth event
UpdateEvent handler for subscribing/unsubscribing to market depth events

Syntax

MarketDepth

Examples

1/* Example of subscribing/unsubscribing to market depth from an Add On. */
2public class MyAddOnTab : NTTabPage
3{
4 private Instrument instrument;
5
6 public MyAddOnTab()
7 {
8 instrument = Instrument.GetInstrument("AMD");
9
10 if (instrument == null)
11 return;
12
13 // Follow this pattern to subscribe to MarketDepth events so they may be unsubscribed from the same instrument thread
14 if (!instrument.Dispatcher.HasShutdownStarted)
15 instrument.Dispatcher.InvokeAsync(() => instrument.MarketDepth.Update += OnMarketDepth);
16
17 // Print the Ask's price ladder
18 for (int i = 0; i < instrument.MarketDepth.Asks.Count; i++)
19 {
20 NinjaTrader.Code.Output.Process(string.Format("Position: {0} Price: {1} Volume: {2}", i,
21 instrument.MarketDepth.Asks[i].Price, instrument.MarketDepth.Asks[i].Volume), PrintTo.OutputTab1);
22 }
23 }
24
25 // This method is fired on market depth events and after the snapshot data is updated.
26 private void OnMarketDepth(object sender, MarketDepthEventArgs e)
27 {
28 return;
29 }
30
31 // Called by TabControl when tab is being removed or window is closed
32 public override void Cleanup()
33 {
34 // Follow this pattern to subscribe to MarketDepth events so they may be unsubscribed from the same instrument thread
35 if (instrument != null && !instrument.Dispatcher.HasShutdownStarted)
36 instrument.Dispatcher.InvokeAsync(() => instrument.MarketDepth.Update -= OnMarketDepth);
37 }
38
39 // Other required NTTabPage members left out for demonstration purposes. Be sure to add them in your own code.
40}