Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to directly access MarketDepth<T> ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to directly access MarketDepth<T> ?

    I've used the example "samplecustomevents.cs" to read the DOM ladders when the DOM update event fires. So far so good.
    But I don't want to waste CPU cycles updating a separate list "LadderRows" on each DOM update event, when I only need to read the DOM ladder rows every 5 seconds.

    I'm wondering if there is a way to directly access the current state of the marketDepth list (or dictionary, whatever type of generic <T> it is) ?


    when my event timer fires every 5 seconds, I try...

    Code:
    for (int i = 0; i < NinjaTrader.Data.MarketDepth<NinjaTrader.Data.MarketDepthRow>.Asks.Count; i++)
    {
        myAskList[i].Price = NinjaTrader.Data.MarketDepth<NinjaTrader.Data.MarketDepthRow>.Asks[i].Price;
        myAskList[i].Size  = NinjaTrader.Data.MarketDepth<NinjaTrader.Data.MarketDepthRow>.Asks[i].Volume;
    }
    but I can't get access to it through that path.


    Is there a way during State.DataLoaded to instantiate a variable that points to the MarketDepth List object, so it can be read directly?

    I also read the code in AddOnFramework.cs, but I don't understand how the variable marketDepth is pointed to the data source so it can be read when that button in the add on is clicked.


    Thanks for any help.

    #2
    Hello balltrader.

    The way shown in the sample would be the correct way to access the data for an indicator, I would not suggest trying to use the addon data events from within the indicator as it has its own events for that purpose.

    From an indicators perspective you would never collect data from where it originates in the platform as you may start doing out of thread operations. The script also has an override for this specific data so adding a different subscription would not be suggested.

    Updating the list is a very small operation in terms of CPU use and will be even less if this is only being done every 5 seconds so this should not be a concern for taking too much resources. If you are doing a very complex operation inside the loop while collection of data, you may look at moving that logic outside the loop or simplifying it if possible.

    The addon event handlers are using a different concept, instead of "NinjaTrader" automatically calling your scripts OnMarketDepth override you would subscribe to that event directly as shown in the sample and manage it yourself. The addon framework samples all tie in together, the event shown in the sample is part of the larger addon sample which also includes instrument handling for its instrument picker. This is not intended to be used from an indicator but would be intended to be used in a custom addon class. The sample you are using now is the correct and suggested way to work with market depth data from an indicator or strategy.


    Please let me know if I may be of further assistance.

    Comment


      #3
      Thanks Jesse,

      re: "out of thread", the example cs file uses "Lock" on the indicator List instance, so is that in compliance with the override you mention?

      re CP cycles...
      I am being hyper-conservative with CPU cycles. The List is updated on every OnMarketDepth event, but the list that is build from scanning the MarketDepth List is only read from every 5 seconds, so from my perspective, the OMDepth event is a waste, if there is a direct access to the Asks and Bids public Lists in the MarketDepth class that would be the way to read it..

      I see this structure:
      Code:
      public class MarketDepth<T> where T : MarketDepthRow, new()
        {
          internal double _lastPrice;
          internal DateTime _lastTimeUtc;
          internal T _marketDepthRow;
          private Dispatcher dispatcher;
          private EventHandler<MarketDepthEventArgs> handler;
      
          public List<T> Asks { get; }
      
          public List<T> Bids { get; }
      but I don't yet know how to specify the type for the T object of type MarketDepthRow class, using new.so i can access it in my first post pseudocode.

      But I'll keep reading C sharp tutorial websites to see if I can figure it out.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      650 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      370 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      109 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      574 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      577 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X