<< Click to Display Table of Contents >>

Navigation:  NinjaScript > Language Reference > Add On >


Previous page Return to chapter overview Next page


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


Note: Remember to unsubscribe if you are no longer using the subscription.





List of ask prices


List of bid prices


Instrument representing the instrument of the market depth event


Event handler for subscribing/unsubscribing to market depth events








/* Example of subscribing/unsubscribing to market depth from an Add On. The concept can be carried over

to any NinjaScript object you may be working on. */

public class MyAddOnTab : NTTabPage


    private MarketDepth<MarketDepthRow> marketDepth;


    public MyAddOnTab()


         // Subscribe to market data. Snapshot data is provided right on subscription

         // Note: "instrument" is a placeholder in this example, you will need to replace          

         // with a valid Instrument object through various methods or properties available depending

         // on the NinjaScript type you are working with (e.g., Bars.Instrument or Instrument.GetInstrument()

         marketDepth = new MarketDepth<MarketDepthRow>(instrument);

         marketDepth.Update += OnMarketDepth;



    // This method is fired on market depth events and after the snapshot data is updated.

    private void OnMarketDepth(object sender, MarketDepthEventArgs e)


         // Print the Ask's price ladder

        for (int i = 0; i < marketDepth.Asks.Count; i++)


              NinjaTrader.Code.Output.Process(string.Format("Position: {0} Price: {1} Volume: {2}", i,

                   marketDepth.Asks[i].Price, marketDepth.Asks[i].Volume), PrintTo.OutputTab1);




    // Called by TabControl when tab is being removed or window is closed

    public override void Cleanup()


         // Make sure to unsubscribe to the market data subscription

      if (marketDepth != null)

              marketDepth.Update -= OnMarketDepth;



    // Other required NTTabPage members left out for demonstration purposes. Be sure to add them in your own code.