Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90


Volumetric Bar count not corresponding to base bar count

  • Filter
  • Time
  • Show
Clear All
new posts

    Volumetric Bar count not corresponding to base bar count

    Hi !

    I am having issues when working with Volumetric Bars (I have a lifetime NT license) with some instruments and I cannot figure out why.
    In my strategy I am adding a volumetric data series to use some order flow information, however there seems to be a mismatch between the number of bars retrieved by the main data series and the volumetric one.

    As you can see in the screenshot below, BarsArray[0] is way larger than BarsArray[1] so when I try to reference a bar from BarsArray[1] using the CurrentBar that called OnBarUpdate for BarsInProgress == 1, it throws the error below.
    Click image for larger version  Name:	image.png Views:	0 Size:	14.7 KB ID:	1235913

    The way I am adding the volumetric bars is as follows on the OnStateChange method (it happens the same if I hardcode the instrument name, base period type and value)
    else if (State == State.Configure) {
                    AddVolumetric(Instrument.FullName, this.BarsPeriod.BaseBarsPeriodType, this.BarsPeriod.Value, VolumetricDeltaType.BidAsk, 1);​ }

    And the way I try to get the volumetric information is as follows in OnBarUpdate method:
    if (BarsInProgress == 1) {
                VolumetricBarsType volumetricBarsType = BarsArray[1].BarsType as NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsType;
                VolumetricData volumetricBar = volumetricBarsType.Volumes[CurrentBar];
    This makes it impossible to correlate both series as you can see in the following indicator picture (The small grey boxes in the middle of the graph should sit on top of the bars)

    Click image for larger version  Name:	image.png Views:	0 Size:	130.9 KB ID:	1235914

    I have all the historical data required for the time range I am backtesting, and I cannot find why the two data series are different in size
    The instrument is FDAX 03-23 backtesting for the whole 2022 year

    Any ideas?
    Last edited by ciberian; 02-19-2023, 05:19 AM.

    Hello Ciberian,

    Thanks for your post.

    I have created a simple indicator that adds a Volumetric data series using AddVolumetric() and hard-coded the values of the added series. Note that when adding additional data series, you should hard-code the values for the AddVolumetric() method. This is stated in the AddVolumetric() help guide page.
    AddVolumetric("FDAX 03-23", BarsPeriodType.Minute, 5, VolumetricDeltaType.BidAsk, 1);
    From the help guide: "Arguments supplied to AddVolumetric() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided.​"


    After adding the Volumetric series, we added the code seen in the Order Flow Volumetric Bars help guide page and seen below.
    if (Bars == null)
    NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsType barsType = BarsArray[1].BarsType as
    if (barsType == null)

    Order Flow Volumetric Bars:

    Then, we check which BarsInProgress index is processing and print out the CurrentBar of that series.
    if (BarsInProgress == 0)
    Print("BIP0 Bars: " + CurrentBar);
    if (BarsInProgress == 1)
    Print("BIP1 Bars: " + CurrentBar);

    When adding the indicator to an FDAX 03-23 5-Minute chart, we can see that the BarsInProgress prints show that the CurrentBar value of the primary series and added volumetric series match in the Output window. See the attached screenshot.

    Note that since the added volumetric series is hard-coded to use FDAX 03-23 5-Minute, we add the test script to a chart with the same instrument and interval.

    ​I have also attached the script used to test this for you to view.

    Please let me know if I may assist further.​
    Attached Files
    Brandon H.NinjaTrader Customer Service


      Thanks for your answer Brandon!

      In my case I get the same results hardcoding the AddVolumetric values, the thing is not that the CurrentBar is not in sync between both series - you can get the same number - the issue is with the size of the Bar collection itself. As you can see in my first screenshot the volumetric series has roughly 30k less items than the 1 Minute series. Which leads to getting the wrong information when trying to correlate both series with the CurrentBar index (think of an array with the right values but skewed to the right or to the left)

      Series 0 [A, B, C, D, E, F, G]
      Series 1 [A', C', D', F', G']

      For every call to OnBarUpdate I get the CurrentBar index, but if it is not aligned and I try to map the info from Series 0 with Series 1 using that index, there is an information mismatch (this is why on the last screenshot you see a bunch of grey boxes in between the candles, as the OHLC information does not match between them)

      The complication is that this does not always happen.
      What could be causing this size difference, and more importantly how can we fix it ?


        Hello ciberian,

        Thanks for your note.

        If the CurrentBar prints are the same then this indicates that each bar object has the same number of bars in its collection.

        Are you hard-coding your AddVolumetric() secondary series to be for say a 5-minute series and comparing the added series to a 5-minute primary series as seen in my previous post?

        If you test the example script I shared in post # 2, do you see the same behavior that I shared? Or, do you see a different behavior?

        To looking into this matter further, we would need to be able to consistently reproduce the behavior. If you see a different behavior when testing the sample script I shared, please share a screenshot(s) demonstrating using prints to show that the bar values in the collection are not the same and the exact steps used to reproduce the behavior.
        • To send a screenshot with Windows 10 or newer I would recommend using the Windows Snipping Tool.
        • Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save it as a jpeg file and send the file as an attachment.
        ​I look forward to assisting further.
        Brandon H.NinjaTrader Customer Service


        Latest Posts


        Topics Statistics Last Post
        Started by ssrockstarss, 02-26-2024, 08:08 AM
        4 responses
        Last Post Ibraher
        by Ibraher
        Started by notenufftime, 05-22-2024, 07:09 PM
        12 responses
        Last Post NinjaTrader_ChelseaB  
        Started by llanqui, 05-22-2024, 06:14 AM
        18 responses
        Last Post NinjaTrader_ChelseaB  
        Started by woodyfox, 02-28-2019, 08:36 PM
        28 responses
        Last Post NinjaTrader_Gaby  
        Started by llanqui, Today, 04:57 AM
        0 responses
        Last Post llanqui
        by llanqui