Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How do I get a preliminary calculation before EOD?

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

    How do I get a preliminary calculation before EOD?

    I am backtesting a strategy based on 14-day stochastic, and I want to check the current value of the fast and slow stochastic line just minutes before the EOD so I can trade by EOD if the buy or sell signal is given. The problem I am having is that I am only getting the updated daily stochastic values right at the bell 3:00 pm Central, when it is too late to make the trade that day. I want to get a preliminary calculation 5 minutes before. How can I do that? A basic rule of this strategy is buying at EOD of the signal day, not waiting until the next day's open. It makes too much difference in the results.

    I even tried buying 1 minute after the bell in extended hours, but in that case it does not update the daily indicator value unitl the last minute of extended hours, so I am still in the same boat.

    I hope I dont need to code a whole new calculation of the indicator for this!

    Thanks!

    #2
    Key goal here is it has to be back-testable, so you can't just set it to Calculate.OnEachTick. I would probably add a 1-minute series with AddDataSeries so I have a back-testable 1-minute set of data, then add a Series<double> that is based on the Daily bar series on the chart and call that series IntrabarDailyClose, with the strategy set to OnBarClose. Every time the 1-minute bar closes, I would update IntrabarDailyClose[0] = Closes[1][0] so that IntrabarDailyCloses becomes a series of Daily closes, but updated as of the close of each minute. Do the same thing for the open, high, and low, if you need them for the Stochastic (you do need H and L for sure). Now that you have that, I would copy in the logic from the Stochastic indicator and change the references to High, Low, and Close to point to your new Intrabar Series variables. Now you have a "daily stochastic" that's being updated every 1-minute during the day, that is back-testable. A minute or a few minutes before 3:00 pm CST, check the stochastic calculation for the "preliminary closing value" of the daily stochastic and make your decision.
    Bruce DeVault
    QuantKey Trading Vendor Services
    NinjaTrader Ecosystem Vendor - QuantKey

    Comment


      #3
      Hello rikelleher,

      To have a script updated before the bar closes in historical, this would require TickReplay to set Calculate to OnPriceChange, as well as 1-tick intra-bar granularity for accurate order fills.

      Below is a link to a forum post with details and a link to the reference sample.


      Then trigger actions every 5 minutes, you would also want to add a 5 minute series.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Thank you Bruce and Chelsea!

        Originally posted by QuantKey_Bruce View Post
        ... Now that you have that, I would copy in the logic from the Stochastic indicator and change the references to High, Low, and Close to point to your new Intrabar Series variables. Now you have a "daily stochastic" that's being updated every 1-minute during the day, that is back-testable.
        When you say "copy in the logic from the Stochastic indicator", do you mean copy it into the strategy, or create a new version of the indicator? I am still understanding the object model here and how the events flow, and the best way to code for them.

        Thanks

        Comment


          #5
          Well, either. You could make your own customized version of the Stochastic indicator that runs on daily bars but uses 1-minute data to update it intraday and then reference that from the strategy, or you could just copy the calculation itself that Stochastic does into the strategy and modify it there. I would probably, in this case, copy it into the strategy myself just because there is already an indicator Stochastic that would show the same thing on the screen for visualization purposes, but that does violate the principle of not duplicating code. You also could do it the way support described above which is to just use the regular Stochastic indicator and tick replay it. I was describing a way that doesn't use tick replay to get the intraday/intrabar values because it uses 1-minute data, but their suggestion is actually simpler to code, so you might want to go that way. It was a good suggestion they made.
          Bruce DeVault
          QuantKey Trading Vendor Services
          NinjaTrader Ecosystem Vendor - QuantKey

          Comment


            #6
            OK, thanks. I am not familiar with TickReplay, so I dont yet understand that. If that is simpler I will check that out.

            Comment


              #7
              I read that
              "An indicator or strategy running Tick Replay needs to have been specifically designed to take advantage of Tick Replay."
              https://ninjatrader.com/support/help...sub=tickreplay

              I don't know the ramifications of this. Is this a large rewrite? What will I NOT be able to do if I rewrite for TickRelay?

              Comment


                #8
                Hello rikelleher,

                It depends on what the script is doing.

                If you are overriding OnMarketData() ensure that you are using the last event as shown in the example code.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  I believe Stochastic should be safe for a tick replay. If it were not, you would see that if you set Calculate to Calculate.OnPriceChange or Calculate.OnEachTick with the indicator on the chart, its values would go haywire. I do not believe that they do. I have not carefully audited this one but my recommendation would be to assume that it is okay until you see evidence otherwise in this particular case.
                  Bruce DeVault
                  QuantKey Trading Vendor Services
                  NinjaTrader Ecosystem Vendor - QuantKey

                  Comment


                    #10
                    Originally posted by NinjaTrader_ChelseaB View Post
                    Hello rikelleher,

                    It depends on what the script is doing.

                    If you are overriding OnMarketData() ensure that you are using the last event as shown in the example code.
                    I am not doing anything as fancy as overriding OnMarketData(), and thanks for the heads-up.

                    Comment


                      #11
                      Originally posted by QuantKey_Bruce View Post
                      I believe Stochastic should be safe for a tick replay. If it were not, you would see that if you set Calculate to Calculate.OnPriceChange or Calculate.OnEachTick with the indicator on the chart, its values would go haywire. I do not believe that they do. I have not carefully audited this one but my recommendation would be to assume that it is okay until you see evidence otherwise in this particular case.
                      Thanks, Bruce. This is what I am trying.

                      Comment


                        #12
                        I am seeing things in NT that I am not understanding. I have gone back to a simpler example to try and understand the flow and operation of NT. Please assist me.

                        I followed the video demonstrating the implementation of 1-tick intra-bar granularity: https://drive.google.com/file/d/12F6...usp=share_link
                        I am using that simple code as my starting point.

                        I see how the one tick intra bar granularity is allowing the order to be placed and fulfilled in the same bar that these signal is given. To understand the flow better, I have added logging into this code to understand what events are triggered when. To move toward replicating my situation, I turned the SMA crossover into a daily SMA period to make the events trigger faster I made the SMA's of shorter duration.

                        This is my logging code:
                        (the header)
                        Print("DateTime,Time Span,Open,Low,Close,SMA Fast,SMA Slow");

                        (the body)
                        LineToPrint = Time[0].ToString()+","+BarsInProgress+","+Open[0].ToString() +","+Low[0].ToString()+","+Close[0].ToString() +","+smaFast[0].ToString() +","+smaSlow[0].ToString();
                        Print(LineToPrint);

                        I understand that it should process the primary data series, the daily first and then the second series the tick series second. Why am I seeing multiple daily series executions of OnBarUpdate() for the same day, and with different SMA values?

                        5/5/2023 2:59:54 PM,1,412.4,412.4,412.4,408.52,410.341
                        5/5/2023 3:00:00 PM,0,408.91,408.64,412.41,408.523333333333,410.342
                        5/5/2023 2:59:54 PM,1,412.41,412.41,412.41,408.523333333333,410.342
                        5/5/2023 3:00:00 PM,0,408.91,408.64,412.4,408.52,410.341
                        5/5/2023 2:59:54 PM,1,412.4,412.4,412.4,408.52,410.341

                        5/5/2023 2:59:54 PM,1,412.41,412.41,412.41,408.523333333333,410.342
                        5/5/2023 3:00:00 PM,0,408.91,408.64,412.4,408.52,410.341
                        5/5/2023 2:59:54 PM,1,412.4,412.4,412.4,408.52,410.341
                        5/5/2023 3:00:00 PM,0,408.91,408.64,412.41,408.523333333333,410.342
                        5/5/2023 2:59:54 PM,1,412.41,412.41,412.41,408.523333333333,410.342
                        5/5/2023 3:00:00 PM,0,408.91,408.64,412.4,408.52,410.341
                        5/5/2023 2:59:54 PM,1,412.4,412.4,412.4,408.52,410.341
                        5/5/2023 3:00:00 PM,0,408.91,408.64,412.41,408.523333333333,410.342
                        5/5/2023 2:59:55 PM,1,412.41,412.41,412.41,408.523333333333,410.342

                        I will start with this question. Thanks!

                        Comment


                          #13
                          Also, why are the SMA numbers being calculated correctly from the first bar, but the lines are only drawn starting on the 21st bar? See attached screenshot.

                          Thanks

                          Click image for larger version

Name:	Screenshot 2023-05-07 a.jpg
Views:	100
Size:	239.6 KB
ID:	1250369

                          Comment


                            #14
                            Is BarsRequiredToPlot set to 20?
                            Bruce DeVault
                            QuantKey Trading Vendor Services
                            NinjaTrader Ecosystem Vendor - QuantKey

                            Comment


                              #15
                              I havent seen the BarsRequiredToPlot setting before. It is not set in this simple piece of code:

                              if(State == State.SetDefaults)
                              {
                              Fast = 10;
                              Slow = 25;
                              Calculate = Calculate.OnBarClose;
                              Name = "SampleIntrabarBacktest";
                              }​

                              Is there a default hidden somewhere? I set the similarly named "Bars Required to Trade" in the strategy settings = 11

                              Honestly, at this point my first question is my bigger concern.

                              Thank you!

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Today, 05:17 AM
                              0 responses
                              23 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              120 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