Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 tsantospinto, 04-12-2024, 07:04 PM
          7 responses
          126 views
          0 likes
          Last Post aligator  
          Started by futtrader, 04-21-2024, 01:50 AM
          5 responses
          56 views
          0 likes
          Last Post NinjaTrader_Eduardo  
          Started by PeakTry, Today, 10:49 AM
          0 responses
          2 views
          0 likes
          Last Post PeakTry
          by PeakTry
           
          Started by llanqui, Today, 10:32 AM
          0 responses
          5 views
          0 likes
          Last Post llanqui
          by llanqui
           
          Started by StockTrader88, 03-06-2021, 08:58 AM
          45 responses
          3,994 views
          3 likes
          Last Post johntraderuser2  
          Working...
          X