| 1 | /* Example of subscribing/unsubscribing to market depth from an Add On. */ |
| 2 | public 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 | } |