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.

    https://www.screencast.com/t/swMx4fAA

    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?
    https://ninjatrader.com/support/help...ick_replay.htm
    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."
            https://ninjatrader.com/support/help...onbarclose.htm

            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

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by emillieany, Today, 01:28 AM
              0 responses
              2 views
              0 likes
              Last Post emillieany  
              Started by GilbeCarra, Today, 01:22 AM
              0 responses
              3 views
              0 likes
              Last Post GilbeCarra  
              Started by oprahwinfreyketogummies, Today, 01:20 AM
              0 responses
              3 views
              0 likes
              Last Post oprahwinfreyketogummies  
              Started by ntbone, Today, 01:02 AM
              0 responses
              1 view
              0 likes
              Last Post ntbone
              by ntbone
               
              Started by nikholasjain, Today, 12:52 AM
              0 responses
              2 views
              0 likes
              Last Post nikholasjain  
              Working...
              X