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

OnBarUpdate() not called on last bar of daily data

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

    OnBarUpdate() not called on last bar of daily data

    I am running NT 8.0.25.

    Today is Monday, Jan 17th, 2022 (market closed today, MLK day)

    I have data up to Friday, Jan 14th. For example, AAPL, the closing price on Jan 14th was 173.07.

    I am using daily bar data. I get my data through a paid Kinetic account.



    However, if I run this code through today Jan 17

    Code:
    protected override void OnBarUpdate()
    {
        Print(Time[0].ToLongDateString());
    }
    It only prints up to Jan 13th (does not include the 14th)

    Thursday, January 6, 2022
    Friday, January 7, 2022
    Monday, January 10, 2022
    Tuesday, January 11, 2022
    Wednesday, January 12, 2022
    Thursday, January 13, 2022

    I have tried using Calculate.OnEachTick or Calculate.OnPriceChange but neither one is helping.

    I have written a strategy to calculate some price data values and indicator reading of the last day of the backtest's trading, but it keeps calculating the day before the last day of the backtest only.

    How can I get the measurement of price and its indicators on the last bar of a strategy backtest?


    #2
    Hello shodson,

    Is this a backtest in the Strategy Analyzer, or have you added this to a chart?

    If added to a chart is TickReplay enabled to allow Calculate.OnPriceChange to update historically?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I am using Strategy Analyzer

      Comment


        #4
        Hello shodson,

        In the Strategy Analyzer TickReplay would need to be enabled for the Calculate setting in State.SetDefaults to take effect.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          But I don't want to have to download, store, and process several weeks of tick data in my backtests just so I can get the closing values of the last bar of a backtest.

          In NT7, you could accomplish this by setting CalculateOnBarClose = false and it would work with Daily data, no need to process any intraday data.

          Code:
          namespace NinjaTrader.Strategy
          {
             public class testDay : Strategy
             {
                DateTime lastDate;
          
                protected override void Initialize()
                {
                   CalculateOnBarClose = false;
                }
          
                protected override void OnBarUpdate()
                {
                   lastDate = Time[0].Date;
                }
          
                protected override void OnTermination()
                {
                   Print(lastDate.ToShortDateString());   // prints the date of the last daily bar that was backtested
                }
             }
          }
          But with NT8, using the other Calculate enum values doesn't help.

          Comment


            #6
            Hello shodson,

            With NinjaTrader 7 the CalculateOnBarClose is always true in historical data or backtest. This can only be true in real-time, which is the same for NinjaTrader 8.

            From the help guide:
            "When indicators or strategies are running on historical data, OnBarUpdate() is only called on the close of each historical bar even if this property is set to false. This is due to the fact that with a historical data set, only the OHLCVT of the bar is known and not each tick that made up the bar."


            NinjaTrader 7 does not have the TickReplay option, which NinjaTrader 8 does.

            For Calculate OnEachTick to work historically with NT8, TickReplay must be enabled.

            However, it is the last bar. No orders should be submitted, so you shouldn't need to trigger anything on the last bar.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hello shodson,

              With NinjaTrader 7 the CalculateOnBarClose is always true in historical data or backtest. This can only be true in real-time, which is the same for NinjaTrader 8.
              This is not true. Just run the code I submitted in NT7's strategy analyzer and you will see that if it's false, it will print the date of the last bar, but if it is true, it will print the date of the second-to-last bar.

              I need OnBarUpdate() to fire on the last bar because I use strategies to gather price and indicator readings at the end of the day and write those to a database to generate end-of-day signals across thousands of stocks. It helps me find opportunities in thousands of markets without having to look at charts. With NT8, I can't do that anymore (with just daily data, not having to include tick data) because of this problem, and I will have to keep using NT7 for my needs or just stop using Ninjatrader.
              Last edited by shodson; 01-18-2022, 09:31 AM.

              Comment


                #8
                Is there an update, a solution to this problem? I'm seeing the same thing: my indicator is not getting triggered in "OnBarUpdate()" for the last day, for historical data.

                Comment


                  #9
                  See also the Stack Overflow post - maybe that'll yield an answer.

                  stackoverflow.com/questions/73423719/why-wont-ninjatrader-8-call-onbarupdate-for-the-last-day-in-my-series

                  Comment


                    #10
                    Hello dween,

                    No, while the Strategy Analyzer will close the last bar, on a chart the last bar does not close until a new bar starts to form.

                    The work around is only for NinjaTrader 7 in real-time with CalculateOnBarClose false.

                    Use NinjaTrader 8 if you want this functionality.
                    Last edited by NinjaTrader_ChelseaB; 09-13-2022, 08:25 AM.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Chelsea - maybe you can help me with a workaround. I don't actually need an exact closing price - an approximate one ("the last one") would be fine - what I want is to get NinjaTrader to make some kind of OnBarUpdate() function call to my code, with "BarBrush" and "CandleOutlineBrush" pointing at the right objects, so I can do custom coloring for the last Bar.

                      Otherwise I don't know how to access these objects for the "latest bar".

                      (I tried finding something that I could do during "OnRender()", which is later on, but I don't know how to access "BarBrush" and "CandleOutlineBrush" using a Bar index...)

                      Can you help me access these objects for the latest Bar?
                      Last edited by dween; 09-09-2022, 06:09 PM.

                      Comment


                        #12
                        Hello dween,

                        You would need to use Calculate.OnPriceChange (or .OnEachTick) to get OnBarUpdate() to update for the last bar on a chart.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Thank you! Using
                          Code:
                          Calculate.OnEachTick
                          did the trick!

                          Comment


                            #14
                            is there any other solution besides moving on tick update (cpu intensive)
                            To NinjaTrader: when does "CurrentBar" actually move to the next bar, and why would changing to tick update have any impact?
                            thankx

                            Comment


                              #15
                              Hello MisterTee,

                              You could use a BarsRequest to get the data instead.


                              With Calculate.OnBarClose the last bar does not update until a new bar opens.
                              With Calculate.OnPriceChange/.OnEachTick, the last bar will update for every price change or tick (before the bar closes).
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by gravdigaz6, Today, 11:40 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post gravdigaz6  
                              Started by MarianApalaghiei, Today, 10:49 PM
                              3 responses
                              9 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by XXtrader, Today, 11:30 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post XXtrader  
                              Started by love2code2trade, Yesterday, 01:45 PM
                              4 responses
                              28 views
                              0 likes
                              Last Post love2code2trade  
                              Started by funk10101, Today, 09:43 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post funk10101  
                              Working...
                              X