Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Keep getting an error of only 4 bars on chart

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

    Keep getting an error of only 4 bars on chart

    This is the full error
    Error on calling 'OnStateChange': 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.

    This is my code

    OnStateChange
    ----------------------
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "Strategy Name";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = true;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;​
    =======================
    State.Configure
    -------------------------
    double d = Stochastics(14, 14, 3).D[0];
    double k = Stochastics(14, 14, 3).K[0];​
    =============================
    OnBarUpdate
    ----------------------------
    if (CurrentBar < 1) return;

    and then just my trade executions

    #2
    Hello unpronounceable1700,

    This is because you are trying to access data in OnStateChange, you need to move that code into OnBarUpdate where bars are actually being processed. OnStateChange is only meant for setting up your script and not for anything that relates to using bar data.

    State.Configure
    -------------------------
    double d = Stochastics(14, 14, 3).D[0];
    double k = Stochastics(14, 14, 3).K[0];​​

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello unpronounceable1700,

      This is because you are trying to access data in OnStateChange, you need to move that code into OnBarUpdate where bars are actually being processed. OnStateChange is only meant for setting up your script and not for anything that relates to using bar data.

      State.Configure
      -------------------------
      double d = Stochastics(14, 14, 3).D[0];
      double k = Stochastics(14, 14, 3).K[0];​​

      Thank you, it seemed to work. Ran the backtest, but after I tried to run another one just simply changing the date range of the backtest to july - now instead of jan 1-now and it gave me an error of
      "An order placed has been ignored since the order was submitted before the strategy property BarsRequiredToTrade had been met"

      Comment


        #4
        Hello unpronounceable1700,

        That is just a warning letting you know that an order was submitted before the bars required to trade, that defaults to 20 bars. That means your entry condition became true almost immediately when the script started. the bars required to trade is used primarily to prevent trades from being placed while indicators or price data is processed initially. For example if you used a 12 period SMA the value will not be ready until at least 12 bars have processed, if your condition used that SMA and the strategy placed an order incorrectly due to the currently calculating indicator value it will be ignored.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        44 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        126 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        65 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        42 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        46 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X