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

Coding Indicator Day Bar Consistency In Historical and Real-Time

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

    Coding Indicator Day Bar Consistency In Historical and Real-Time

    Hi,

    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.

    Best,
    RandanAL

    #2
    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

    Comment


      #3
      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"
      }






      Comment


        #4
        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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Mongo, Today, 11:05 AM
        2 responses
        6 views
        0 likes
        Last Post Mongo
        by Mongo
         
        Started by guillembm, Today, 11:25 AM
        0 responses
        2 views
        0 likes
        Last Post guillembm  
        Started by Tim-c, Today, 10:58 AM
        1 response
        2 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by traderqz, Yesterday, 09:06 AM
        4 responses
        27 views
        0 likes
        Last Post traderqz  
        Started by traderqz, Today, 12:06 AM
        4 responses
        7 views
        0 likes
        Last Post traderqz  
        Working...
        X