Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

(CurrentBar == 0) is Bars.IsFirstBarOfSession?

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

    (CurrentBar == 0) is Bars.IsFirstBarOfSession?

    Hello,

    if I set BarsRequiredToTrade = 0; in OnStateChange() within (State == State.SetDefaults), will (CurrentBar == 0) in OnBarUpdate() gives me the very first bar of the session when running Strategy Analyzer back test? I seems to be getting the second bar instead of the first bar of the session, and am wondering how to access the very first bar when the session starts?

    #2
    Hello billythekid72,

    BarsRequiredToTrade is a property that causes NinjaTrader to ignore orders until the number of bars necessary have been processed in OnBarUpdate.

    This is not related to a session, but instead counts forward from wherever the data starts.

    Try printing:

    Print(string.Format("{0} | CurrentBar: {1}", Time[0], CurrentBar));

    This should tell you what bar is the first bar processed.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello,

      OK, I just found out that, in fact I do get the first bar in a session by testing (CurrentBar == 0) in OnBarUpdate() , however this ONLY happened to the FIRST day in a multi-days back tests in Strategy Analyzer (I am running strategy analyzer for multi days). After the first day, in subsequent days, only SECOND BAR of the day show up with (CurrentBar == 0) in OnBarUpdate(). Any idea why is this happening?

      Comment


        #4
        Hello billythekid72,

        CurrentBar does not reset to 0 after a new session. It just keeps counting forward from the first historical bar that loads, which is not always the first bar of the session. CurrentBar is the current bar that is being processed in OnBarUpdate(). This is not related to the session at all.

        Are you saying that CurrentBar has skipped a bar?

        Is the print in a condition?

        Can you provide the output saved in a text file?


        If you want to know when the first bar of the session is, use Bars.IsFirstBarOfSession.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thank you Chelsea, you answered my question, I now know exactly what went wrong (it's my code). Thanks again.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Today, 05:17 AM
          0 responses
          20 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          119 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          63 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          41 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          45 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X