Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

strange error on Barupdate() with OnEachTick

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

    strange error on Barupdate() with OnEachTick

    I have encounter strange error. I am running the the following code on range chart. it works well before 15;00PM in beijing time zone. However, it would report error and stop itself after 15;00PM in beijing time zone. the error is error on calling " onbarupdate" method on bar 0:you are accessing an index a value that is invalid since it is out of range. it seems it error in (lastExecutionTime.Minute != Time[1].Minute || lastExecutionTime.Hour != Time[1].Hour . if I replace it with "true", the code would works well. But , it would run on each tick, which is not wanted. I just want it run one time for one bar .

    region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    using System.Timers;
    #endregion

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class new78 : Strategy
    {
    // Input parameters with attributes for the window



    private Account myAccount;
    private DateTime lastExecutionTime = DateTime.MinValue;
    private int counter=0;



    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "new7";
    Name = "new78";
    Calculate = Calculate.OnEachTick;
    IsOverlay = true;

    // Set default parameter values

    EntryHandling = EntryHandling.AllEntries;
    }
    else if (State == State.Configure)
    {

    myAccount = Account;
    }
    else if (State == State.DataLoaded)
    {

    }
    else if (State == State.Terminated)
    {


    }
    }



    protected override void OnBarUpdate()
    {
    DateTime currentTime = DateTime.Now;
    //Print("124");

    if (lastExecutionTime.Minute != Time[1].Minute || lastExecutionTime.Hour != Time[1].Hour)
    {
    //if (State < State.Realtime) // Skip historical data
    // return;



    //Print("789");
    DateTime currentTime1 = Time[0];

    Print("NinjaTrader current time: " + DateTime.Now.ToString("HH:mm:ss"));
    Print("counter22:"+counter);



    lastExecutionTime = Time[0];


    }


    //mainTimer.Interval = 30 * 1000;
    // mainTimer.Start();
    }






    }
    }

    #2
    Hello steinberg123,

    Ensure for any indexes you are accessing the index is less than the size of the collection.

    For barsAgo indexes, these must be less than CurrentBar.

    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.




    Try adding to the top of the OnBarUpdate() block:

    if (CurrentBar < 1)
    return;
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    87 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    132 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    65 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    118 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    67 views
    0 likes
    Last Post PaulMohn  
    Working...
    X