Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Volume of barsAgo clarification

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

    Volume of barsAgo clarification

    How do I properly use Vol()[x] ?
    Where is a larger integer, say 1440.

    When I use an int greater than 5, my Indicator errors.

    My ultimate goal is to chart a comparison of current bar volume and volume of bar 24 hours ago to the minute.
    So volume of bar 2018-10-23 10:00:00 compared to 2018-10-22 10:00:00

    Example code:

    //double value1 = VOL()[10];

    //Print("The last week's VOL value is " + value1.ToString());

    I have over 20000 bars loaded.

    Thank you in advance - jch
    Last edited by [email protected]; 10-23-2018, 07:52 PM.

    #2
    Hello John,

    Thanks for your post and welcome to the forums.

    For your own protection, you may want to change your forum name to not be an e-mail address as you are likely to receive unwelcome e-mails from bots that scan forums for e-mail addresses.

    The error that you are running into occurs when your lookback exceeds the number of bars the indicator has processed. When an indicator is started it will load the first bar of data and execute the code in OnBarUpdate(), then the next bar is loaded and so on. If you are accessing VOL()[10] when there are only 5 bars loaded then the indicator would error out and an error message would appear in the "log" tab of the control center.

    To prevent this, you can add a condition to prevent code execution until the appropriate minimum number of bars has been processed by the indicator.

    If your lookback is a static value, such as [10] then at the top of OnBarUpDate() you can add code to prevent execution by checking to see if the current bar count is less than 10. NinjaTrader provides the property CurrentBar which contains the number of bars processed at the moment. for example:

    if (CurrentBar < 10) return; // prevent code below this line from running until first 10 bars loaded.

    Reference: https://ninjatrader.com/support/help...currentbar.htm

    Comment


      #3
      Thank you Paul. Your feedback provides the answer I was looking for. Much appreciated.

      Comment


        #4
        Click image for larger version

Name:	2018-10-29_10Bars.png
Views:	170
Size:	197.0 KB
ID:	1037591Click image for larger version

Name:	2018-10-29_20Bars.png
Views:	182
Size:	213.7 KB
ID:	1037592 Paul et al,

        In the attached, when I reference 10 barsAgo, I get the first result as expected.

        However, in the second image, I changed the barsAgo to 20 and the script errors.

        How can this be if me CurrentBar is greater than 117580?

        Maybe I do not understand the CurrentBar variable. Is this not how many bars are available to my indicator?

        Again, my goal is to compare the most current bar volume to the volume 1400 bars ago.

        I am open to any suggestion or recommendations for further research/study.

        Comment


          #5
          Hello John,

          Thanks for your reply.

          Please add the current bar check I previously advised and make sure that the bar check matches your longest bars ago reference.

          If your lookback is a static value, such as [10] then at the top of OnBarUpDate() you can add code to prevent execution by checking to see if the current bar count is less than 10. NinjaTrader provides the property CurrentBar which contains the number of bars processed at the moment. for example:

          if (CurrentBar < 10) return; // prevent code below this line from running until first 10 bars loaded.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          656 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          371 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
          579 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X