Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calculate VolumeBid, VolumeAsk on historical data, when level 2 is available

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

    Calculate VolumeBid, VolumeAsk on historical data, when level 2 is available

    Hello,
    I am developing indicator to calculate volume on Bid and volume on Ask (on candle).
    I need work with also historical data.

    Below is script that works on realtime data. Is there a way to solve this on historical data? Especially OnMarketData part? I thought I saw somewhere about adding another DataSeries based on ask bid? Is there an example?

    Thanks a lot

    Code:

    On realtime data, this approach works for me:

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate() {

    if(Open[0] > Close[0])
    Values[2].Set(Volume[0]);
    else if(Open[0] < Close[0])
    Values[3].Set(Volume[0]);
    else
    Values[4].Set(Volume[0]);

    if (CurrentBar < activeBar)
    {
    return;
    }
    else if (CurrentBar != activeBar)
    {
    Print(Time[0] + " buys: " + buys + " sells: " + sells + " Close[0]: " + Close[0]);

    // Sells
    Values[0].Set(sells);

    // Buys
    Values[1].Set(buys);

    buys = 0;
    sells = 0;
    activeBar = CurrentBar;

    UpdateHistogram();
    }

    if (!Historical) {

    // Sells
    Values[0].Set(sells);

    // Buys
    Values[1].Set(buys);
    }

    }

    AND:

    /// <summary>
    /// Called on each incoming real time market data event
    /// </summary>
    protected override void OnMarketData(MarketDataEventArgs e)
    {
    if (e.MarketDataType == MarketDataType.Ask)
    askPrice = e.Price;
    else if (e.MarketDataType == MarketDataType.Bid)
    bidPrice = e.Price;
    else if (e.MarketDataType == MarketDataType.Last)
    {
    if (askPrice > 0 && e.Price >= askPrice)
    buys += e.Volume;
    else if (bidPrice > 0 && e.Price <= bidPrice)
    sells += e.Volume;
    }
    }

    #2
    Hello kujista,

    Thanks for your post.

    You can add bid/ask data but please note that historically it will not call the OnMarketData().

    Here is a link to the help guide on Add(): https://ninjatrader.com/support/help.../nt7/?add3.htm

    In NinjaTrader7 you may not be able to accomplish your goal. (Can be done in NT8 with tick replay).

    We will leave this open for any community response.

    We'll look into this further and update this thread when we have further information.



    Comment


      #3
      Hello kujista,

      After further review we have these comments:

      We do not have any means to do OnMarketData() historically.

      When adding bid/ask data series, the exact sequence of a mix of bid/ask/last events will not be maintained while processing the historical data from multiple series.
      Reference: https://ninjatrader.com/support/help..._ask_serie.htm

      An approach you could take would be to create a system to record the data you need, in a format you need and create your own historical OnMarketData() to process the historical market data events. We would not have any examples of that.

      Comment

      Latest Posts

      Collapse

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