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

Error on calling 'OnBarUpdate' method on bar 0

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

    Error on calling 'OnBarUpdate' method on bar 0

    Hello there,

    I'm trying to code a very simple thing (so I thought) in NinjaTrader 8.

    Code:
                if(Momentum(14)[0] < Momentum(14)[1])
                {
                    ExitLong();    
                }
    But i get this message:
    Strategy 'MomentumIndicatorTest': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
    My setting "BarsRequiredToTrade" is set to 14 in my strategy. What causes this error? I can't figure it out.

    Thanks in advance!

    #2
    Hello Aqua-Life,

    Thank you for writing in.

    You'll want to ensure that at least one bar has elapsed before checking for Momentum(14)[1]: http://ninjatrader.com/support/forum...ead.php?t=3170

    The BarsRequiredToTrade of 14 will prevent your strategy from making any trades before 14 bars have elapsed, but it won't prevent the strategy from running over non-order logic, such as the condition in your if statement.

    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      That was easy. Thank you!

      Comment


        #4
        Index out of range error on Bar 100

        if (BarsInProgress == i
        && Closes[i][1] == MIN(Closes[i],5)[1]
        && Account.Positions[i].MarketPosition == MarketPosition.Flat
        && (High[1]<High[2] & High[2]<High[3]& High[3]<High[4]& High[4]<High[5]))

        {
        EnterLong(i,100, "SwingLong"+i);
        }

        Gives the index out of range error normally for bar 100. but also bar 87/88. This is obviously a multi instrument strategy... which worked fine in NT7 but errors in NT8. Any ideas

        Comment


          #5
          Hello everington_f,

          Thank you for writing in.

          Have you ensured that you have enough bars for each of the series that you are trying to access? http://ninjatrader.com/support/forum...ead.php?t=3170

          Additionally, I would suggest printing out the value of i as well to ensure that you are not getting a value that is out of bounds.
          Zachary G.NinjaTrader Customer Service

          Comment


            #6
            Yes i have checked..... and am leaving it for now and doing some more testing. Thx for the service

            Comment


              #7
              Index out of range error on Bar 0

              Originally posted by NinjaTrader_ZacharyG View Post
              Hello everington_f,

              Thank you for writing in.

              Have you ensured that you have enough bars for each of the series that you are trying to access? http://ninjatrader.com/support/forum...ead.php?t=3170

              Additionally, I would suggest printing out the value of i as well to ensure that you are not getting a value that is out of bounds.


              for (int i = 0, h = 24; i < h; i++)
              {
              if(BarsInProgress == i)
              {


              if (BarsInProgress == i
              && Closes[i][1] == MIN(Closes[i],5)[1]
              && Account.Positions[i].MarketPosition == MarketPosition.Flat
              && Momentum(Closes[i],20)[0] < -1
              &&(Highs[i][1]<Highs[i][2] & Highs[i][2])

              {
              EnterLong(i,100, "Long"+i);
              }

              Print(i);

              strategy prints the first barinprogress 0 then disables and keep getting
              out of range error.Any ideas? I have checked that all the data is there.
              I am not sure that the data is being downloaded.... have used

              if (State != State.DataLoaded)
              {
              return;
              }

              as a check.... and it now shows no error.... but of course will not fire...
              Last edited by elliot5; 07-27-2016, 09:08 AM.

              Comment


                #8
                Hello everington_f,

                Please provide the indicator script so I may test it on my end.

                You can export the indicator by clicking on File -> Utilities -> Export NinjaScript… in the Control Center.

                In the “Export NinjaScript Archive File (.zip)” window that follows:
                1. Ensure that “Export selected source files” is selected.
                2. Enter a file name for the ZIP file.
                3. Select your indicator from the list and click on the “>” so the indicator moves to the right side of the window.
                4. Click on Export


                The ZIP file will be saved in the “\Documents\NinjaTrader 7\bin\Custom\ExportNinjaScript” folder.
                Zachary G.NinjaTrader Customer Service

                Comment


                  #9
                  It is an NT8 issue sir.

                  Comment


                    #10
                    Hello everington_f,

                    Please follow these instructions to export from NinjaTrader 8.

                    1. From the Control Center window select the menu Tools > Export > NinjaScript... to open the "Export NinjaScript" dialog window

                    2. Press "add"

                    3. Use the "Type" drop down to filter available NinjaScript types

                    4. Select all of the files that you want to export and press the "OK" button

                    5. A list of all files that will be exported will be shown

                    6. Press the "Export" button to export the selected files

                    7. A NinjaScript Archive File (.zip) file will be created in My Documents\<NinjaTrader Folder>\bin\Custom\ExportNinjaScript. You can press ".." and change the export directory if you wish
                    Zachary G.NinjaTrader Customer Service

                    Comment


                      #11
                      I have just sent the .zip to platformsupport with your name in the subject. Thx Zachary

                      Comment


                        #12
                        Hello everington_f,

                        I'll be investigating further and return with my findings on this forum thread.

                        Thank you for your patience.
                        Zachary G.NinjaTrader Customer Service

                        Comment


                          #13
                          Hello everington_f,

                          Thank you for your patience.

                          Take a look at line 97 on your code:
                          Code:
                          Print(MIN(Closes[8],5)[1]);
                          You're trying to access the previous value of MIN() on Closes[8] without first checking that you have at least two bars of data.

                          Upon adding this check before line 97:
                          Code:
                          if (CurrentBars[8] < 1)
                               return;
                          I am no longer running into a run-time error.

                          I would suggest running this check on all other series as well.

                          This particular forum posts covers more about making sure that you have have enough bars in the data series you are accessing: http://ninjatrader.com/support/forum...ead.php?t=3170

                          As you are using multiple series, you can access the CurrentBar value of each series with CurrentBars: https://ninjatrader.com/support/help...urrentbars.htm

                          Please, let us know if we may be of further assistance.
                          Last edited by NinjaTrader_ZacharyG; 07-27-2016, 10:40 AM.
                          Zachary G.NinjaTrader Customer Service

                          Comment


                            #14
                            Remove the print code and the following on lines 102-105

                            // if (State != State.DataLoaded)
                            // {
                            // return;
                            // }

                            I added your currentbar check and got an error on bar 1 index

                            Comment


                              #15
                              Hello everington_f,

                              Rather than using Account.Positions[i], please utilize PositionsAccount in your if statements: https://ninjatrader.com/support/help...onsaccount.htm

                              Example:
                              Code:
                              PositionsAccount[i].MarketPosition == MarketPosition.Flat
                              You are running into a run-time error when using Account.Positions because you are attempting to access non-existent account positions. PositionAccount objects are generated when Bars objects are added with AddDataSeries() and you will not run into this error.

                              Additionally, you need to ensure that you have at least three bars loaded before attempting to access items two bars ago in your if statements.

                              I would suggest using a for loop to automate this process for you so you do not need to manually check each CurrentBars object.

                              Code:
                              for (int j = 0; j < CurrentBars.Length; j++)
                              {
                              	if (CurrentBars[j] < 3)
                              		return;
                              }
                              Zachary G.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by alifarahani, Today, 09:40 AM
                              3 responses
                              15 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by RookieTrader, Today, 09:37 AM
                              4 responses
                              17 views
                              0 likes
                              Last Post RookieTrader  
                              Started by PaulMohn, Today, 12:36 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post PaulMohn  
                              Started by love2code2trade, 04-17-2024, 01:45 PM
                              4 responses
                              40 views
                              0 likes
                              Last Post love2code2trade  
                              Started by junkone, Today, 11:37 AM
                              3 responses
                              25 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X