• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.


No announcement yet.

Partner 728x90


Coding Indicator Day Bar Consistency In Historical and Real-Time

  • Filter
  • Time
  • Show
Clear All
new posts

    Coding Indicator Day Bar Consistency In Historical and Real-Time


    I'm currently working to transition a strategy from historical testing to live trading. A snag that I've hit is the difference in the way historical day bars are treated in backtesting vs. real-time trading.

    The strategy uses a couple of Daily EMAs as indicators. In back testing, a daily EMA only updates once the bar has closed. However, when running the strategy in real-time, the EMA will start calling on the current day's non-completed bar per... https://ninjatrader.com/support/help...?calculate.htm

    Calculate.OnBarClose creates consistency between the historical test and real-time but I have one concern with doing this... The strategy uses simulated stops which I adjust... I'm fine with the stops only adjusting at the end of each bar but my concern is that if during a minute bar the market spikes materially higher, the stop order will not trigger until the completion of the minute bar.

    I'm currently playing around with forcing the daily EMA to update on bar close within the indicator itself but it seems like this "is not recommended" per:

    It seems like the most elegant solution is to have the indicator only update once a new day bar closes, but in the forum and help guide I haven't found an example of how to handle this.


    Hello RandanAL,

    Thank you for your note.

    It sounds like you could perhaps add a daily data series to your strategy, then when it's processing in on bar update, assign the EMA value for that bar to a variable which can then be referenced in other calculations.

    Do you have an example of how you're currently trying to handle this daily EMA?

    Thanks in advance; I look forward to assisting you further.
    Kate W.NinjaTrader Customer Service


      Thanks Kate,

      Can you link me to example code of the process you're describing? It sounds kind of like going the opposite direction from what I'm currently doing.

      I think the easiest thing for us will be for me to describe exactly what I'm trying to achieve and what I'm currently doing, then you can point me in the right direction or explain what I'm tripping up on. Please do not just simply link me to the help guide because I've been through it and the forum extensively. Here is what I'm trying to do:

      (a) Create a strategy that shorts when the price of an instrument is below its 1 or 2 day SMA of its lows, and another value is below 30 day highs
      (b) Have the 1 and 2 day SMAs and orders display on a chart during live trading
      so I can visually confirm there are no errors
      (c) Current day prices should not impact the SMAs
      (d) Have consistent behavior between historical analysis and live trading (i.e. in regards to impact of current day prices on the SMAs and the MAX)
      (e) Have the strategy load enough historical data such that I can load it to a chart in the morning and start trading

      It's that simple... here is how I'm currently coding it.
      This works perfectly in strategy analyzer (once the days have been loaded). It also seems to work when loading a strategy from the "Strategies" tab and loading ample bars. However, on a chart, even with 365 days of minute and day data in the chart, and lookback set to "infinite"... this doesn't work when trading live or in playback mode.

      Adding the Day Bars

      else if (State == State.Configure)
      AddDataSeries(BarsPeriodType.Day, 1);
      AddDataSeries("Instrument", BarsPeriodType.Day, 1);

      Creating the EMAs

      else if (State == State.DataLoaded)
      EMA1 = EMA(Lows[1],1);
      EMA2 = EMA(Lows[1],2);
      MAX1 = MAX(Highs[2],30;

      Creating Private Values

      private EMA EMA1;
      private EMA EMA2;
      private MAX MAX1;

      Creating Plots

      protected override void OnStateChange()
      if (State == State.SetDefaults)
      AddPlot(Brushes.Yellow, "PlotA"); // Defines the plot for Values[0]
      AddPlot(Brushes.Yellow, "PlotB"); // Defines the plot for Values[0]


      Giving Value to Plots

      protected override void OnBarUpdate()

      Values[0][0] = EMA1[0]; // Yellow "Plot A"
      Values[1][0] = EMA2[0]; // Yellow "Plot B"


        Hello RandanAL, thanks for your reply.

        IsFirstTickOfBar can be used to separate logic from OnBarUpdate logic and OnEachTick logic. The only way to get OnBarUpdate to be called intrabar is to run the strategy OnEachTick and use IsFirstTickOfBar. I attached a sample strategy that implements this idea. It adds the daily series and plots it to the primary series. Comments in the script indicate where the code will run OnEachTick and where it will run OnBarClose. Add the attached file within Documents\NinjaTrader 8\bin\Custom\Strategies and then compile.

        Please let me know if you have any questions.
        Attached Files
        Chris L.NinjaTrader Customer Service


        Latest Posts


        Topics Statistics Last Post
        Started by rudccv, Today, 08:42 PM
        0 responses
        Last Post rudccv
        by rudccv
        Started by Newtrader101, Today, 03:54 PM
        1 response
        Last Post NinjaTrader_ShawnB  
        Started by williamzz, Today, 03:28 PM
        0 responses
        Last Post williamzz  
        Started by Robert87, Today, 11:07 AM
        0 responses
        Last Post Robert87  
        Started by phta123, Today, 10:24 AM
        0 responses
        Last Post phta123
        by phta123