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

{Ninjascript} NT8 / isFirstBarOfSession

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

    {Ninjascript} NT8 / isFirstBarOfSession

    Dear all, hope you are fine.
    Im having trouble, next code should return "First Bar Of Session" one time on the first bar of the session but returns "First Bar Of Session" 3 times every first bar of session.
    My code:

    Calculate = Calculate.OnEachTick;

    protected override void OnBarUpdate()
    {
    if(IsFirstTickOfBar)
    {

    if(Bars.IsFirstBarOfSession)
    {
    Print("First Bar Of Session");
    }

    }

    }

    The code is printing "First Bar Of Session" in the 3 first bars (please find attached).
    Should print one time, only in the first bar of the session. Can the calculate mode affect this? I need to use OnEachTick.

    Thank you somuch
    Attached Files

    #2
    Are you loading any additional data series in your script?

    Try also printing the value of BarsInProgress inside your Print statement.

    Comment


      #3
      Hello fscabrera03,

      Thanks for your post.

      As member bltdavid asked, do you have additional data series in your script?

      What bar type are you apply the script to?

      Paul H.NinjaTrader Customer Service

      Comment


        #4
        Hello, Im fscabrera03 (i cannot reset my password).
        Im using :
        playback connection
        Im not using data series
        Im using renko bars.

        Thank you

        Comment


          #5
          Hello nandocabrera87 / fscabrera03,

          Thanks for your reply.

          Here is a link to reset your password: https://ninjatrader.com/support/forum/lostpw Please note that you would have to complete registration by replying to the activation e-mail.

          I've been able to recreate your observations and we are looking into this further. I will update this thread when I have further information.

          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Thank you! I recovered the account.
            It´s great if you can recreate that.
            I await your comments.
            Regards

            Comment


              #7
              Hello fscabrera03,

              Thanks for your patience.

              The Renko bars use RemoveLastBar() method which means that once the bar has finished its price movement it is removed and redrawn as the final bar. This removal and redraw triggers the first tick and first bar of the session multiple times, as you have observed. You would not see this behavior in time-based bars or any bar that does not use IsRemoveLastBar.



              What we recommend would be to use your own bool that will change state the first time the condition occurs. You would also need to reset that bool for the session, at a later point.

              if (Bars.IsFirstBarOfSession && IsFirstTickOfBar && doitonce)
              {
              doitonce = false; // to prevent multiple first bar of session
              // your other once a session stuff here
              }


              You will need to come up with another condition to reset the bool doitonce back to true such as a specific time before the end of the session
              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Hello Paul, how are you?
                There are some kind of bar id? or some atribute that refers to a specific bar.
                I want to use this to store bar id on doitonce and ask every time that this specific bar is executed only one time.
                Thank you!

                Comment


                  #9
                  Use CurrentBar.

                  This number is unique for each bar on a per chart basis.

                  Each new bar just gets the next number. That is, the value
                  of CurrentBar advances by 1 for each newly closed bar.

                  This is an automatic thing.

                  Meaning each time your OnBarUpdate is called, the value
                  of CurrentBar will be +1 compared to the last time, assuming
                  Calculate is 'OnBarClose'.
                  Last edited by bltdavid; 07-18-2021, 12:15 PM.

                  Comment


                    #10
                    Hello fscabrera03,

                    Thanks for your post.

                    Member bltdavid has advised using CurrentBar which is the systems bar counter.

                    Reference: https://ninjatrader.com/support/help...currentbar.htm
                    Paul H.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by mjairg, 07-20-2023, 11:57 PM
                    3 responses
                    213 views
                    1 like
                    Last Post PaulMohn  
                    Started by TheWhiteDragon, 01-21-2019, 12:44 PM
                    4 responses
                    543 views
                    0 likes
                    Last Post PaulMohn  
                    Started by GLFX005, Today, 03:23 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post GLFX005
                    by GLFX005
                     
                    Started by XXtrader, Yesterday, 11:30 PM
                    2 responses
                    12 views
                    0 likes
                    Last Post XXtrader  
                    Started by Waxavi, Today, 02:10 AM
                    0 responses
                    7 views
                    0 likes
                    Last Post Waxavi
                    by Waxavi
                     
                    Working...
                    X