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 NullPointStrategies, Today, 05:17 AM
    0 responses
    49 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
    67 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