Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy behavior

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

    Strategy behavior

    A simple question regarding Backtesting vs. Realtime strategy behavior.

    A 15 min bar strategy that calculates on bar close

    Backtest
    Backtesting will calculate the first OnBarUpdate after the close of the first 15 min bar and then execute code accordingly.

    Realtime
    Strategy will execute OnBarUpdate at session start and execute code accordingy. Strategy does not wait for the close of the first 15 min bar.

    Question:
    What is the best way to execute OnBarUpdate after the close of the first bar of the session in the realtime strategy?

    #2
    Using CalculateOnBarClose = true in real-time means your strategy will only process at the end of each bar just like how it does in a backtest.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Josh View Post
      Using CalculateOnBarClose = true in real-time means your strategy will only process at the end of each bar just like how it does in a backtest.
      Well, yes..simple question, but not that simple..sorry! CalculateOnBarClose is set to true in the strategy.

      Comment


        #4
        I assure you when CalculateOnBarClose = true all processing happens at the end of the bar. There is no intrabar processing so it will for sure process all historical bars and then wait for the real-time bars to close before processing those.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Josh View Post
          I assure you when CalculateOnBarClose = true all processing happens at the end of the bar. There is no intrabar processing so it will for sure process all historical bars and then wait for the real-time bars to close before processing those.
          As an example, I set session start at 8.30 am central on the live strategy. The strategy executed a trade at 8.30 as the market opened. It did not wait for the first bar of the session to close. What other code or paramaters could cause this?

          Comment


            #6
            sublime,

            A trade to be executed at 8:30 would mean the trade was placed in on the prior bar. When you are using CalculateOnBarClose = true, you can't trade on the bar you are processing because it is closed and so all orders are then placed to the next bar. In your example the order you had originated from the bar before that. You will need to check your strategy if you do not want this order.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Josh View Post
              sublime,

              A trade to be executed at 8:30 would mean the trade was placed in on the prior bar. When you are using CalculateOnBarClose = true, you can't trade on the bar you are processing because it is closed and so all orders are then placed to the next bar. In your example the order you had originated from the bar before that. You will need to check your strategy if you do not want this order.
              So, the question evolves...

              Given
              1. CalculateOnBarClose = true
              2. Session begins at 8.30
              3. All indicator logic on current bar
              4. No code accessing previous bar

              Why would the OnBarUpdate() ever calculate the 15 min bar of the previous day?

              Furthermore, how can OnBarUpdate() ever get triggered from a bar in the past? I can see how code can access bars in the past, but how does a bar that occurred at 3 pm the previous day trigger the OnBarUpdate() of the current day in separate sessions? Logically, I would think that OnBarUpdate() can only be triggered in realtime.

              Even if I activate a live strategy at 8.30 am, and declare my session start is 8 am, the first trigger of the OnBarUpdate() should occur at 8.45 am (the close of the first live bar). Sure, I could get info from bars before activating the strategy, but the OnBarUpdate() for these old bars should never happen.. IMO.

              So we know the close of a historical 15 min bar is triggering the OnBarUpdate(). How do I avoid this with the given assumptions above? Am I missing something?

              Comment


                #8
                sublime,

                Code always processes all historical bars first before reaching real-time bars. Your strategy is processing since the beginning of the chart. It is in a backtesting state till it reaches real-time bars. Then real-time actions can be taken from that point forward.

                If you don't want OnBarUpdate() to process historical bars you need to explicitly tell it not to in your code.

                Code:
                if (Historical) return;
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Josh View Post
                  sublime,

                  Code always processes all historical bars first before reaching real-time bars. Your strategy is processing since the beginning of the chart. It is in a backtesting state till it reaches real-time bars. Then real-time actions can be taken from that point forward.

                  If you don't want OnBarUpdate() to process historical bars you need to explicitly tell it not to in your code.

                  Code:
                  if (Historical) return;
                  So let me elaborate:

                  All live strategies load a certain amount of necessary historical data which gets processed in a backtesting state. The amount of historical data can vary and be set through some option, correct? In the event the OnBarUpdate() of the last historical bar triggers an action, that action will take place at the beginning of the next live session. Is this true?

                  As it would explain what I've witnessed. Basically, the OnBarUpdate() of the 3pm 15 min bar results in a Buy order. And this Buy order is executed at the beginning of the next bar, which is the 15 bar beginning at 8.30am. As this functionality is not desired, I'll try your easy fix and hope that takes care of it. Thanks!

                  Comment


                    #10
                    The amount of data loaded is determined by what you have on your chart as you throw a strategy onto the chart. Yes, action on last historical bar will be taken on first real-time bar if still relevant.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Josh, I'm sorry to say it didn't work. For testing, I created an extremely simple strategy where the buy condition is basically always true. With the Historical logic employed, I started the strategy with a session begin time in the future, using 5 min bars. On each occasion, the strategy buys immediately when the session begins. It does not wait for the close of the 5 min bar. Here is the simple strategy...

                      publicclass HistoricalBar : Strategy
                      {
                      #region Variables
                      // Wizard generated variables
                      // User defined variables (add any user defined variables below)
                      #endregion
                      ///<summary>
                      /// This method is used to configure the strategy and is called once before any strategy method is called.
                      ///</summary>
                      protectedoverridevoid Initialize()
                      {
                      CalculateOnBarClose =
                      true;
                      }
                      ///<summary>
                      /// Called on each bar update event (incoming tick)
                      ///</summary>
                      protectedoverridevoid OnBarUpdate()
                      {
                      if (Historical) return;

                      // Condition set 1
                      if (SMA(50)[0] > SMA(200)[0])
                      {
                      EnterLong(DefaultQuantity,
                      "");
                      }
                      }
                      #region Properties
                      #endregion
                      }
                      There has to be a simple explanation.

                      Comment


                        #12
                        sublime,

                        Just print the Time[0] of the bar and you will see it does not process until the bar closes. I guarantee you no order can be placed till a bar has been processed. If the bar has not closed it won't process that bar.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Josh View Post
                          sublime,

                          Just print the Time[0] of the bar and you will see it does not process until the bar closes. I guarantee you no order can be placed till a bar has been processed. If the bar has not closed it won't process that bar.
                          I don't have to print the time, I have the order right here. And I've tested this 4 times. My last strategy I set to start at exactly 2:15 pm. Here is the first trade. 4 seconds after the session starts...


                          QLD Buy Market 6547 38.78999847 Sim101 TD HistoricalBar 6/4/2009 2:15:04 PM

                          I mean, its not like this is even close...

                          Comment


                            #14
                            No, you need to see the timestamp of the bar being processed. An order is impossible to be placed until a bar is processed.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_Josh View Post
                              No, you need to see the timestamp of the bar being processed. An order is impossible to be placed until a bar is processed.
                              We can agree on that - the point is what bar is being processed. I think we can see by my last example that its not the current 5 min bar. It's not waiting for the 2:15 min bar to close. Clearly it is placing the trade on the last historical bar. The question is, why? The strategy certainly couldn't be any easier. If not that... then what? Another parameter in the options?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              630 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              364 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              105 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              566 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              568 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X