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 the strategy but not on the indicator

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

    Error on the strategy but not on the indicator

    I'm getting this error when loading a strategy i made copying over the code from an indicator that works without this error.

    Error on calling 'OnBarUpdate' method on bar 347: Index was outside the bounds of the array.

    The data series is #1 in the array and is being called via BarsInProgress == 1. The indicator using this same code works fine but when I run this using a strategy it's giving the above error and I'm not sure why.

    I'm adding the time and period via int in the ui and it's being loaded with on state.configure like this:

    else if (State == State.Configure)
    {
    AddDataSeries(HTFPeriodType, HTFPeriod);

    }

    Also I'm sure there is enough data for the strategy to run because it only needs 26 bars and I've set it to 50 which is the same as it was in the indicator version.

    if(CurrentBar < 50 || CurrentBars[1] < 50) return;

    When it gets to the line in the code if(BarsInProgress == 1) that's when I get the error. I don't see this being a syntax issue. Maybe I can't add a data series that way in a strategy. This is the first time I've created a strategy so I'm assuming this is just a simple oversight. Please assist.
    Last edited by gordongekko; 01-24-2018, 03:27 PM.

    #2
    Hello gordongekko,

    Is the error you have posted the only error that appears?

    The HTFPeriodType and HTFPeriod are variables and we are not able to see what values are being used for this.

    If you print the time and BarsInProgress without any other code, do you see the prints showing that it is processing for the secondary series?

    The line if(BarsInProgress == 1) is not causing the error.

    There is no index being called here.

    The error is coming from an index.

    [0] <-- this is an index

    CurrentBars[1] <-- this is the index of the secondary series

    Close[0] <-- this is the index of one bar ago

    I would recommend you add prints to your script and find which line is causing the error.

    The issue is not a syntax issue. The issue is a logic issue. You are trying to use an index that doesn't exist.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      It's the only error posted and it is when i attempt to run the strategy on a chart. There are no errors when compiling. You're right about barsinprogress not being the error. I just looked at it again and the barsinprogress method starts on line 348. There is nothing on line 347 that I can see which is even more confusing. I don't know what prints are. Is this listed in the ninjascript tutorial anywhere?

      Comment


        #4
        Hello gordongekko,

        Below is a publicly available link to the help guide on Print().


        As well as a link to a forum post that demonstrates using prints to understand behavior.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          The error is actually right before this line:

          if(BarsInProgress == 0)

          I thought it was for the higher time frame but it's actually for the main data series. I don't see how this would cause a problem but I'm not an expert. I just start writing code recently.

          Comment


            #6
            Hello gordongekko,

            What is the line before the line 'if(BarsInProgress == 0)'?

            Would you like assistance with that line?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I just noticed the error is on bar 347 not line 347 which explains why there is nothing on line 347 of the code. I have no idea whats causing this error. Do you have any suggestions?

              Comment


                #8
                Hello gordongekko,

                I would suggest using prints to find which line is causing the error.

                If you add prints, the last appearing print will be the last working line before the error appears, showing which line has the error.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  It says error on a bar not a line. When the globex reopens I'll try it again on a few different contracts and see if its still happening.

                  Comment


                    #10
                    I tried it on a bunch of other contracts/time frames and I'm getting the same bar error on random bars (bar 298, 572, etc). I'm assuming this has to do with using multiple time frames and its trying to reference data from the higher time frame that doesn't exist yet or something like that. I have never seen this type of error before. If it was just giving me a line of code saying that was the issue i would have something to work with. The order execution is being done on the main data series which is always the same contract and a lower time frame than the 2nd data series. Do you have any ideas on what could be causing this?

                    Comment


                      #11
                      Hello gordongekko,

                      On some particular line of your code, you are attempting to use an invalid index with a series (or an array if you have arrays).

                      Possibly a barsAgo value used in the index of a series is greater than the bar count for that BarsInProgress.

                      Possibly an invalid BarsInProgress index is used somewhere in the CurrentBars or BarsArray collections.

                      I am saying that is highly unlikely that the lines of code you have posted so far is causing that error. The error is coming from a different line of code that has an index.

                      I recommend adding prints on the lines before any indexes are used. Which ever print is the last print to appear before the error appears is likely one line above the line that has the invalid index error.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        The single time frame version works fine so the issue appears to be solely related to the the additional data series which is the same as the primary data series - just a higher time frame. The max bars required to run this code is 26 on both time frames because the macd requires the most data of any of the variables. I have barsrequiredtotrade and current bars for both time frames at 50 minimum which should be overkill and works on the indicator version of the same code. The strategy code is basically verbatim minus the alert code which was swapped out for order entry and profit target/stops, etc. Does barsinprogress function differently in strategies vs indicators and is currentbars < x redundant in a strategy when referring to the primary data series? When getting this type of error in the past coding indicators it was because it was running a method before there were enough bars loaded to calculate the variables being used but unless I'm missing some additional bars setting that is exclusive to strategies that isn't the case here.
                        Last edited by gordongekko; 01-25-2018, 09:56 AM.

                        Comment


                          #13
                          Please disregard my previous post. I figured it out. I forgot to remove this from the properties:

                          [Browsable(false)]
                          [XmlIgnore]
                          public Series<double> Signal
                          {
                          get { return Values[0]; }
                          }

                          It was trying to plot signals on triggers when the plot code wasn't loaded in State.SetDefaults. That explains why the error was on some random bar number because it was probably getting the error when it encountered a trigger it needed to plot on historical data.

                          Comment


                            #14
                            Hello gordongekko,

                            The invalid index would be the Values collection.

                            The index [0] does not exist.

                            The easiest way to find out, is to use prints to find the line that causes the error. Likely it is a line that calls Signal[0].
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              I thought i removed the entire signal but i left it in the properties and in the trigger methods without adding this to State.SetDefaults.

                              AddPlot(new Stroke(Brushes.Brown, 2), PlotStyle.Bar, "Signal");

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by guillembm, Today, 11:25 AM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by owensd, 04-21-2024, 11:34 PM
                              9 responses
                              34 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by trilliantrader, 04-10-2024, 09:33 PM
                              7 responses
                              25 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by traderqz, Today, 12:06 AM
                              5 responses
                              11 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by Mongo, Today, 11:05 AM
                              2 responses
                              10 views
                              0 likes
                              Last Post Mongo
                              by Mongo
                               
                              Working...
                              X