Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

You are accessing an index with a value that is invalid since it is out-of-range

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

    You are accessing an index with a value that is invalid since it is out-of-range

    Strategy 'Stochastic': 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.
    Its always some kind of new problem.
    This started happening but only on these 2 strategies.
    They are set to Price Change.
    All the other strategies are running fine and they're using the same chart/same ticker (same bars).
    There is not a lack of bars on this chart as it goes back to 8/1/22
    This started happening after I tried to manually download price data. Could that be the cause?
    Is my strategy trying to access a bar thats 10000000 bars ago?
    Reloading history doesnt help. Restarting didnt help. thanks

    #2
    BrandonH:
    If the error is occurring while the script is loading, the issue is likely somewhere in OnStateChange(). In this situation its best to remove any logic in OnBarUpdate, remove any custom methods and classes, and remove as many properties as possible, while still being able to reproduce the error.
    yes it wont start so its occuring at loading. I'm not using FirstTickOfBar or anything.

    Comment


      #3

      Here's my OnStateChange:



      Code:
      protected override void [B]OnStateChange[/B]()
      {
      if (State == State.SetDefaults)
      {
      Description = @"";
      Name = "Stochastic";
      Calculate = Calculate.OnPriceChange;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.AllEntries;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 30;
      IsFillLimitOnTouch = false;
      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
      OrderFillResolution = OrderFillResolution.Standard;
      Slippage = 0;
      StartBehavior = StartBehavior.WaitUntilFlat;
      TimeInForce = TimeInForce.Gtc;
      TraceOrders = false;
      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      BarsRequiredToTrade = 1;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;
      EntryBar = 1;
      IntialTrade = false;
      }
      else if (State == State.Configure)
      {
      }
      else if (State == State.DataLoaded)
      {
      Difference = new Series<double>(this);
      mahKDJ1 = mahKDJ(Close, 7, 14, 3);
      MACD1 = MACD(Close, 12, 26, 9);
      Stochastics1 = Stochastics(Close, 10, 20, 8);
      ADX1 = ADX(Close, 14);
      Stochastics2 = Stochastics(Close, 7, 14, 3);
      SMA1 = SMA(Close, 3);
      SMA2 = SMA(Close, 14);
      SetStopLoss("", CalculationMode.Currency, 20, false);
      SetProfitTarget("", CalculationMode.Currency, 25);
      }
      }

      Comment


        #4

        Here's my other:

        Code:
        protected override void [B]OnStateChange[/B]()
        {
        if (State == State.SetDefaults)
        {
        Description = @"";
        Name = "MahKDJ";
        Calculate = Calculate.OnPriceChange;
        EntriesPerDirection = 1;
        EntryHandling = EntryHandling.AllEntries;
        IsExitOnSessionCloseStrategy = true;
        ExitOnSessionCloseSeconds = 30;
        IsFillLimitOnTouch = false;
        MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
        OrderFillResolution = OrderFillResolution.Standard;
        Slippage = 0;
        StartBehavior = StartBehavior.WaitUntilFlat;
        TimeInForce = TimeInForce.Gtc;
        TraceOrders = false;
        RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
        StopTargetHandling = StopTargetHandling.PerEntryExecution;
        BarsRequiredToTrade = 1;
        // Disable this property for performance gains in Strategy Analyzer optimizations
        // See the Help Guide for additional information
        IsInstantiatedOnEachOptimizationIteration = true;
        EntryBar = 1;
        IntialTrade = false;
        }
        else if (State == State.Configure)
        {
        }
        else if (State == State.DataLoaded)
        {
        Difference = new Series<double>(this);
        Stochastics1 = Stochastics(Close, 10, 20, 8);
        mahKDJ1 = mahKDJ(Close, 7, 14, 3);
        MACD1 = MACD(Close, 12, 26, 9);
        mahKDJ2 = mahKDJ(Close, 7, 10, 3);
        ADX1 = ADX(Close, 14);
        SMA1 = SMA(Close, 3);
        SMA2 = SMA(Close, 14);
        EMA1 = EMA(Close, 3);
        SetStopLoss("", CalculationMode.Currency, 20, false);
        SetProfitTarget("", CalculationMode.Currency, 30);
        }
        }

        Comment


          #5
          Hi, thanks for posting. Please use Visual Studio debugging to find the exact line of code that is throwing the exception. I don't see anything wrong with your OnStateChagned method:

          Chris L.NinjaTrader Customer Service

          Comment


            #6
            Or, since the error messages states the error occurred in
            your OnBarUpdate, you could post that code.

            First thing I'd check:
            Is your guard code correct?

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by LawrenHom, Today, 10:45 PM
            0 responses
            3 views
            0 likes
            Last Post LawrenHom  
            Started by love2code2trade, Yesterday, 01:45 PM
            4 responses
            28 views
            0 likes
            Last Post love2code2trade  
            Started by funk10101, Today, 09:43 PM
            0 responses
            7 views
            0 likes
            Last Post funk10101  
            Started by pkefal, 04-11-2024, 07:39 AM
            11 responses
            37 views
            0 likes
            Last Post jeronymite  
            Started by bill2023, Yesterday, 08:51 AM
            8 responses
            44 views
            0 likes
            Last Post bill2023  
            Working...
            X