Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to get the first tick of the new bar in ninjascript

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

    How to get the first tick of the new bar in ninjascript

    Hi, i have been trading in ninjatrader for a few years and there has been a problem that plagued my code for all this time.
    In most of my strategies, I always try to wait for the current bar to completely close before I make a decision. Meaning if I act I do it on the first tick of the new bar that opens. The problem is that in code everything i tried always take the last tick of the last bar even though it tells me it's the first tick of the new bar. The last thing i used is the IsFirstTickOfBar which should do exactly what i want, but when it opens a position at that time it opens it on the last bar not on the new one.

    Here is an exemple with the code used and the result :


    If anyone knows why it works that way, I would greatly appreciate it if you could explain it to me.
    Thank you​
    Attached Files

    #2
    Hello Abeamus,

    Thank you for your post.

    What Calculate setting are you using? What BarsType is this script using?

    IsFirstTickOfBar will only report accurately if using Calculate.OnEachTick or Calculate.OnPriceChange, and when using a bar type that does not remove the last bar.

    Comment


      #3
      Thank you for your promptness.

      The chart is live and is set to ‘OnEachTick’ with ‘1 mn’ candlesticks bar.

      Comment


        #4
        Hello,

        I recommend debugging the script using prints. You should print out the Times[0][0] of the bar after your isFirstTickOfBar condition to confirm the script is only processing on the first tick of the bar.

        To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

        In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order.

        The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

        The debugging print output should clearly show what the condition is, what time the conditions are being compared, all values being compared, and how they are being compared.

        Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

        Further, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

        After enabling TraceOrders remove the instance of the strategy from the Configured list in the Strategies window and add a new instance of the strategy from the Available list.

        I am happy to assist you with analyzing the output from the output window.

        Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

        Below is a link to a support article that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.

        https://support.ninjatrader.com/s/ar...nd-TraceOrders

        Comment


          #5
          Hello again Gaby,

          The trade should be taken at 17:18:00 (first tick of the new bar), but if we look at the entryorderbuy/traceorder, it is taken at 17:17:59 (last tick of previous bar)

          Here's the screenshots.

          Is it enough to understand this issue ?

          Thank you so much for your help.

          Attached Files

          Comment


            #6
            Hello Abeamus,

            This screenshot is a bit too small to read, can you please post the actual text file? You can do this b right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

            Please use the Print(Times[0][0]); after your IsFirstTickOfBar check.

            Comment


              #7
              Yes, sorry about that.

              Here are the 2 screenshots and the .txt from the output.​
              Attached Files

              Comment


                #8
                Hello Abeamus,

                Thank you for posting the output.

                This behavior is expected, as the first tick of the bar is the same tick that closes the previous bar. This is why you are seeing IsFirstTickOfBar being true at 24/09/2024 17:53:00 (first tick of new bar), and the order seemingly submitted at 24/09/2024 17:51:59 (last tick of previous bar).

                ​Please let me know if you have any further questions.

                Comment


                  #9
                  So if i understand correctly the very first tick of a bar is also the same last tick of the previous bar ? And it's a global thing so not only IsFirstTickOfBar but if i tried some other method i would get the same result ? If so no wonder i never managed to make it work the way i wanted. Thanks a lot for your help

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by NullPointStrategies, Yesterday, 05:17 AM
                  0 responses
                  64 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  139 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  75 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  45 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  50 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X