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

How to include last close in realtime SMA calc

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

    How to include last close in realtime SMA calc

    Hi,
    I realize that there is a difference in trading live vs. backtesting for multi-timeframe strategies and I have read the NT docs. But I have a simple question(s) in this regard. Here is an example....


    1) I open a 1 minute bar chart with 30 days of historical data and also it is running in realtime (Instrument 0). US Equities RTH
    2) I build an IndicatorA that adds a 1 Day bar (Instrument 1)
    3) IndicatorA also plots Closes[1][0], Closes[1][1] and PriorDayOHLC.PriorClose[0]
    4) IndicatorA sets CalculateOnBarClose = true; I would like to use this so that my backtesting and live strategies behave similarly. I am not using ticks, only 1 Minute so using close data is fine.

    Historical Plotting:
    Closes[1][0] : Plots the Close from the session day before the current 1 minute bar
    Closes[1][1]: Plots the Close from two session days before the current 1 minute bar
    PriorDayOHLC.PriorClose[0]: Plots the synthetic Close from the session day before the current 1 minute bar

    Realtime Plotting:
    Closes[1][0] : Plots the Close from two session days before the current 1 minute bar. My guess is that this is because the current day has not closed yet and so it is behaving as though it is still living within the last complete session which makes the Last session Close 2 days ago.
    Closes[1][1]: Plots the Close from three session days before the current 1 minute bar. Same reason as above
    PriorDayOHLC.PriorClose[0]: Plots the synthetic Close from the session day before the current 1 minute bar

    This is very important since I use day SMAs in my 1 Minute Bar strategies. For instance, I would like the 10 Day SMA to be calculated to include the prior session's day Close. But it appears from the above that if I am running in realtime that it will only include the Close starting from 2 days ago.

    If my description of the plots above and SMA calcs is true (I built the simple indicator to do these plots and it is true), then how do I included yesterdays Close in a realtime SMA calc?

    Secondarily, if I disable my strategies (e.g. for backup or housecleaning or NT/feed probs) with open positions and I want to restart them with historical data, I need them to come up in the same position state as when I disabled them. But according to the above that can never truly happen.

    Help.....

    Thanks

    #2
    Hello,

    It is possible to separate your logic so that you can run with your strategy to only execute at the after the close of the bar, like you see in backtesting, but in real-time use the indicators to calculate on every incoming tick

    Please see our Reference Sample on this topic for more information:




    You can also set your strategy to "Immediate submit live working historical orders". With this option, should the strategy detect it would have been in position at the time you started the strategy, it will submit a simulated historical order to simulate that position. You then have the option to either sync this with your account position or not. Please review our Help Guide article on this topic for more information:

    MatthewNinjaTrader Product Management

    Comment


      #3
      Hi Mathew,
      Thank you for your response. The sample code is not really applicable to what I am asking. I think that I will work around the issue using synthetic day bars.

      However, I would really appreciate it if you could respond directly to my example and questions since it probably affects others and/or I have a misunderstanding of how NT works. The question really comes down to this: if I am running a realtime minute bar strategy in the middle of the trading session and I want to get a series of day close bars ending with yesterday's close. How do I do it using out of the box NT functions (e.g. not a synthetic series)? When I use Closes[][0] it seems to provide the day close from 2 days ago.

      I have tried the same thing with 1 hour bars, instead of day bars and it seems to work as expected - where the Closes[][0] is the close of the most recent last hour bar.

      Maybe this is a problem with my data provider timestamps? Kinetick

      Best,
      Joe

      Comment


        #4
        To get the current days Daily data, you would need to run on COBC = false.

        On COBC = true, Close[0] would give you the daily data from one day ago.

        Can you please clarify if you're using EOD data with Kinetick or do you have a real-time subscription?
        MatthewNinjaTrader Product Management

        Comment


          #5
          Hi Mathew,
          I have a realtime subscription. The following code solves the problem and provides the same SMA for historical and realtime. The issue is only related to the current day historical calculation of day close SMA which NT provides the same day close SMA as the previous day. E.g. SMA[0] = SMA[1]. When NT switches to realtime then SMA[0] <> SMA[1] which is probably preferable for most folks. I have attached a simple indicator which demonstrates the issue/feature and solution. Run it on a minute bar graph with 20 days historical and realtime and use the defaults.

          Please let me know if I am off on this since this issue is fundamental to my strategies.
          Thanks

          Best,
          Joe

          if(Historical && Time[0].Date == DateTime.Now.Date)
          {
          newSMAValue = (SMA(Closes[thisDataSeries],thisSMAPeriod)[barsAgo1] * thisSMAPeriod - Closes[thisDataSeries][thisSMAPeriod -
          1] + PriorDayOHLC(Closes[0]).PriorClose[barsAgo1])/thisSMAPeriod;
          }
          else
          {
          newSMAValue = SMA(Closes[thisDataSeries],thisSMAPeriod)[barsAgo1];
          }
          Attached Files

          Comment


            #6
            Hello,

            Thank you for providing me with the script.

            We would recommend you use the 1440 minute bars to replicate the daily session. This should give you real-time data and the most current information you are looking for. As you have demonstrated, there are subtle items that need to be reworked with using Daily bars to get the closing value. Using the real-time minute data would avoid this.
            MatthewNinjaTrader Product Management

            Comment


              #7
              I just wanted to follow up on this with some things that I have (un)discovered:

              While I thought that was a general issue with how day bar Closes[][0] is treated in multi-timeframe strategies, I no longer believe it to be a general issue. The issue seems very reproducable when switching between MarketReplay and my feed while the same chart and indicators are open. So maybe it has something to do with how NT caches historical and MarketReplay data. I do not want to spend any more time trying to reproduce it in other instances.

              Either way, I have decided that all SMA day bar calcs in all of my strategies will replace the last day bar close with the PriorDayOHLC close value. That way I am immune to any undiscovered treatment or nuances of the last day bar, Closes[][0].

              While I appreciate the notion of using an aggregated minute bar to simulate day bars, I would prefer to use as much broadly available market data as possible (e.g the actual day close data for all but the PriorDay) in my strategies.

              The snippet that I posted earlier was incorrect. It should be as follows and without any condition in terms use for Historical or realtime.

              newSMAValue = (SMA(Closes[thisDataSeries],thisSMAPeriod)[barsAgo1] * thisSMAPeriod - Closes[thisDataSeries][0] + PriorDayOHLC(Closes[0]).PriorClose[barsAgo1])/thisSMAPeriod;

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Aviram Y, Today, 05:29 AM
              0 responses
              2 views
              0 likes
              Last Post Aviram Y  
              Started by quantismo, 04-17-2024, 05:13 PM
              3 responses
              25 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by ScottWalsh, 04-16-2024, 04:29 PM
              7 responses
              34 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by cls71, Today, 04:45 AM
              0 responses
              6 views
              0 likes
              Last Post cls71
              by cls71
               
              Started by mjairg, 07-20-2023, 11:57 PM
              3 responses
              217 views
              1 like
              Last Post PaulMohn  
              Working...
              X