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

Tick data in higher resolution is off by 1 bar of the main data series

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

    #16
    Hello Emily,

    Sorry to be a headache.

    I completely understand how bars are built.
    For the record, I'm only interested in the open price of a bar to avoid submitting orders that will result in a reject based on stop price.

    The only thing I can't get my head around is how to use high resolution in my strategy when the open price of the minute bar doesn't correspond to the first tick price for the same timestamp.
    What's more crazy to me is that the price of first tick bar corresponds exactly to next minute open price instead of current minute open price.

    So, I'm not sure how others are able to use high resolution in the unmanaged approach when there is such a thing.

    Comment


      #17
      Originally posted by kreztmok View Post
      Hello Emily,

      Sorry to be a headache.

      I completely understand how bars are built.
      For the record, I'm only interested in the open price of a bar to avoid submitting orders that will result in a reject based on stop price.

      The only thing I can't get my head around is how to use high resolution in my strategy when the open price of the minute bar doesn't correspond to the first tick price for the same timestamp.
      What's more crazy to me is that the price of first tick bar corresponds exactly to next minute open price instead of current minute open price.

      So, I'm not sure how others are able to use high resolution in the unmanaged approach when there is such a thing.
      High resolution is not an option for multi-series scripts. For a script that adds a 1-tick data series, if you'd like to see historical data process as if it were On Price Change or On Bar Close, you must enable Tick Replay.
      "What's more crazy to me is that the price of first tick bar corresponds exactly to next minute open price instead of current minute open price."
      It sounds like you are describing the behavior of processing On Bar Close rather than On Price Change or On Each Tick. What are the results if you enable Tick Replay and then set Calculate to On Price Change or On Each Tick? In real-time, you should see that the Open price of the minute bar matches the 1-tick value when IsFirstTickOfBar is true on the 1-minute series. In historical processing without Tick Replay, the first tick of bar would correspond with the next open price because of how OnBarUpdate() is processed On Bar Close historically. With Tick Replay enabled, you should see that OnBarUpdate() is called On Each Tick or On Price Change. For more information:Please read the notes about Tick Replay vs. High order fill resolution here for more information to compare/contrast the two options:Thank you for your time and patience.
      Emily C.NinjaTrader Customer Service

      Comment


        #18
        Hello kreztmok,

        Thanks for your patience.

        I would like to add the following snippet. Please try applying it in a test strategy and run it on a chart with Tick Replay enabled with Calculate On Bar Close then change Calculate to On Price Change/On Eack Tick and observe the difference:
        Code:
                private int firstTickOfBarIndex;
                protected override void OnBarUpdate()
                {
                    if (BarsInProgress == 0 && IsFirstTickOfBar && CurrentBars[0] > 5)
                    {
                        firstTickOfBarIndex = CurrentBars[1];
                        Print("STATE: " + State);
                        Print(Time[0] + " BarsInProgress 0 Open[0]: " + Open[0] + " CurrentBars[0]: " + CurrentBars[0] + " CurrentBars[1]: " + CurrentBars[1]);
                    }
                    if (BarsInProgress == 1 && CurrentBars[1] == firstTickOfBarIndex + 1)
                        Print(Time[0] + " BarsInProgress 1 Open[0]: " + Open[0] + " CurrentBars[0]: " + CurrentBars[0] + " CurrentBars[1]: " + CurrentBars[1]);
                }​
        Thanks again for your time and patience.
        Emily C.NinjaTrader Customer Service

        Comment


          #19
          Hello Emily,

          Thank you for the great examples and explanation.

          I have test your script in strategy analyzer and in chart with tickreplay and found the first tick of bar open price matching the minute open price on the chart with tick replay. This means that i have to redevelop my strategy for tick replay.

          I have been working on it during the weekend but got stuck with development of my custom indicator.
          The challenge i have is that the indicator is calculating on tick and i need it to update only on the minute bars.

          I have tried adding a tick series and using Barsinprogress==0, but within tick replay this statement is not respected. The indicator keeps on calculating on tick. I have also tried to use isfirsttick of bar to localize a new minite bar and then check the last tick price to find the minute last close price. But this delays my indicator as it checks last minute bar close instead of current close.

          Any ideas on how to get my indicator to calculate on the minute bars instead of ticks, within tick replay enviroment?

          if i get this up and running, how do i backtest my strategy as efficiently as in startegy analyzer when using tick replay on a chart?

          Again many thanks for all the help
          Last edited by kreztmok; 02-26-2024, 09:19 AM.

          Comment


            #20
            Hello kreztmok,

            Thank you for your reply.

            Please see the following reference sample that separates logic between bar close vs. each tick:


            If you include a condition that checks if BarsInProgress == 0 and if IsFirstTickOfBar is true, then the action within that condition should only occure on bar close for the primary series. I suggest testing the following:
            Code:
            if (BarsInProgress == 0 && IsFirstTickOfBar)
            Print(Time[0] + " primary series - BarsInProgress: " + BarsInProgress + " first tick of bar: " + IsFirstTickOfBar);
            "I have also tried to use isfirsttick of bar to localize a new minite bar and then check the last tick price to find the minute last close price. But this delays my indicator as it checks last minute bar close instead of current close."
            Please see the following tip in the help guide:
            • "Tip:

              In NinjaTrader's event driven framework, bar closures are signaled by the tick that opens the next bar. The price of the last tick of a bar can be referenced by checking Close[1] on IsFirstTickOfBar. For volume and tick based bars, Bars.TickCount and Volume[0] can be referenced to see if the number of ticks / volume meet the criteria to build a new bar.​"
            When IsFirstTickOfBar is true when BarsInProgress == 0 for the primary minute series, then Close[1] would be the last tick of the bar that just closed. This is the last minute bar's close price and not the current close price; the current close price when IsFirstTickOfBar is true should match with the Open price of the current bar. If you want the current close, then you would simply need to access Close[0].

            "how do i backtest my strategy as efficiently as in startegy analyzer when using tick replay on a chart?"
            When enabling a strategy on a chart, you must have Tick Replay enabled on the Data Series. Then, in the strategy properties, be sure to select the same parameters as you selected in the Strategy Analyzer. For details on the properties when running a strategy from a chart:


            You can compare those properties to the Strategy Analyzer backtest properties:


            Please feel free to reach out with any additional questions or concerns. If you are getting unexpected results, please provide a snippet of the logic in question and the behavior you are experiencing when testing it vs. what you expected the behavior to be.
            Emily C.NinjaTrader Customer Service

            Comment


              #21
              Hello Emily,

              I have put your script in my indicator for the chart with tick replay enabled and I'm getting the new minute Open price instead of last minute Close price, as you would expect from this line; if (BarsInProgress == 0) . here are results:

              script:
              if (BarsInProgress == 0 && IsFirstTickOfBar)
              {
              Print(Time[0] + " primary series - BarsInProgress: " + BarsInProgress + " first tick of bar: " + IsFirstTickOfBar + " Close[0]: " + Close[0] + " CurrentBars[0]: " + CurrentBars[0]);
              Print("Open price=" + (Open[0]) + " for currentbar= "+ CurrentBars[0]);

              Output:
              17/01/2024 00:02:00 primary series - BarsInProgress: 0 first tick of bar: True Close[0]: 4799.75 CurrentBars[0]: 1
              Open price=4799.75 for currentbar= 1
              17/01/2024 00:03:00 primary series - BarsInProgress: 0 first tick of bar: True Close[0]: 4798.75 CurrentBars[0]: 2
              Open price=4798.75 for currentbar= 2
              17/01/2024 00:04:00 primary series - BarsInProgress: 0 first tick of bar: True Close[0]: 4799 CurrentBars[0]: 3
              Open price=4799 for currentbar= 3

              ​"If you want the current close, then you would simply need to access Close[0]."
              I mentioned in my previous post that within tick replay, the condition if (BarsInProgress == 0) is not respected, as the calculation occurs on each tick regardless.​
              That's why with Open[0] or Close[0] on the same bar I'm getting the Tick price instead of minute's bar prices.

              So how do I get the current minute's bar Close price?

              Thank you.


              Click image for larger version  Name:	image.png Views:	0 Size:	13.8 KB ID:	1293208
              Last edited by kreztmok; 02-26-2024, 12:45 PM.

              Comment


                #22
                I have put your script in my indicator for the chart with tick replay enabled and I'm getting the new minute Open price instead of last minute Close price, as you would expect from this line; if (BarsInProgress == 0) . here are results:

                script:
                if (BarsInProgress == 0 && IsFirstTickOfBar)
                {
                Print(Time[0] + " primary series - BarsInProgress: " + BarsInProgress + " first tick of bar: " + IsFirstTickOfBar + " Close[0]: " + Close[0] + " CurrentBars[0]: " + CurrentBars[0]);
                Print("Open price=" + (Open[0]) + " for currentbar= "+ CurrentBars[0]);​
                "Tip:

                In NinjaTrader's event driven framework, bar closures are signaled by the tick that opens the next bar. The price of the last tick of a bar can be referenced by checking Close[1] on IsFirstTickOfBar. For volume and tick based bars, Bars.TickCount and Volume[0] can be referenced to see if the number of ticks / volume meet the criteria to build a new bar.​"

                The snippet you have shared is expected to give the you the new Open price of the newly forming bar. If you want the Close price of the minute bar that just closed, you must access Close[1]​ when IsFirstTickOfBarIsTrue.
                Code:
                // if it is the first tick of a new bar on the primary series, meaning a bar just closed
                if (BarsInProgress == 0 && IsFirstTickOfBar)
                {
                // The Close[0] price will be the price of the first tick of the new bar. This would match the Open price for the current bar.
                Print(Time[0] + " primary series - BarsInProgress: " + BarsInProgress + " first tick of bar: " + IsFirstTickOfBar + " new bar Close[0]: " + Close[0] + " equals open of current bar Open[0]: " Open[0] + " CurrentBars[0]: " + CurrentBars[0]);
                Print(Time[0] + " Close price of previous bar: " + Close[1]);
                }
                So how do I get the current minute's bar Close price?
                The Close price updates with each tick/change of price. The official close of a bar is signaled by the first tick of a new bar. This means you won't be able to get the "Close" price for a bar until IsFirstTickOfBar is true and the subsequent bar has received at least its first tick. If you want the official Close price of a bar, you must access Close[1] when IsFirstTickOfBar is true.

                Please let us know if we may be of further assistance.
                Emily C.NinjaTrader Customer Service

                Comment


                  #23
                  Hi Emily,

                  Yes, so the only way to get to know the Close of a minute bar in tick replay is by getting isfirsttickofbar to be true and then check the last bar's close price.
                  That makes sense as the calculations are done tick by tick.

                  Thanks for your help, it was of tremendous value.

                  Cheers,
                  Kreztmok

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Balage0922, Today, 07:38 AM
                  0 responses
                  5 views
                  0 likes
                  Last Post Balage0922  
                  Started by JoMoon2024, Today, 06:56 AM
                  0 responses
                  6 views
                  0 likes
                  Last Post JoMoon2024  
                  Started by Haiasi, 04-25-2024, 06:53 PM
                  2 responses
                  19 views
                  0 likes
                  Last Post Massinisa  
                  Started by Creamers, Today, 05:32 AM
                  0 responses
                  6 views
                  0 likes
                  Last Post Creamers  
                  Started by Segwin, 05-07-2018, 02:15 PM
                  12 responses
                  1,786 views
                  0 likes
                  Last Post Leafcutter  
                  Working...
                  X