Announcement

Collapse

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

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
    Paul H.NinjaTrader Customer Service

    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:	154
Size:	197.0 KB
ID:	1037591Click image for larger version

Name:	2018-10-29_20Bars.png
Views:	164
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.
          Paul H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Christopher_R, Today, 12:29 AM
          0 responses
          9 views
          0 likes
          Last Post Christopher_R  
          Started by sidlercom80, 10-28-2023, 08:49 AM
          166 responses
          2,235 views
          0 likes
          Last Post sidlercom80  
          Started by thread, Yesterday, 11:58 PM
          0 responses
          3 views
          0 likes
          Last Post thread
          by thread
           
          Started by jclose, Yesterday, 09:37 PM
          0 responses
          8 views
          0 likes
          Last Post jclose
          by jclose
           
          Started by WeyldFalcon, 08-07-2020, 06:13 AM
          10 responses
          1,415 views
          0 likes
          Last Post Traderontheroad  
          Working...
          X