Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Backtest not matching Real Time trades on Heiken Ashi candles

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

    Backtest not matching Real Time trades on Heiken Ashi candles

    Hi everyone,

    First off, this is my first post so I wanted to say that I delighted to join this forum. Being new to NinjaTrader (I use NT8), I have faced quite a few obstacles but found many answers on this forum thanks to the great support that you guys provide !

    I just created my first strategy using a SuperTrend indicator for NT8 that I found in the User Apps. The strategy looks for Buy and Sell signals from the indicator and I opted to run it on 5min Heiken Ashi candles after finding promising results for this setting in the Strategy Analyzer. I live tested this strategy on SOXL today in the Sim101 account and found that the live trades thqt were executed executed did not match the trades made by the strategy backtested on today's data.

    I noticed something: The Strategy Analyzer correctly executes simulated trades on the open of Heiken Ashi candles while the live trades were executed on the Open of "normal" candles. Note that I have correctly set the 5min Heiken Ashi data series both in the live and backtest settings of the strategy.

    Below are two screenshots illustrating this issue on one same trade:
    - The screenshot with one time series shows the trades generated by the Strategy Analyzer backtesting on today's data. Note how at 10:55 the short sell is executed at HA candle open @ 89.16
    - The other screenshot shows the live trade executed in Sim101 today with two different background data series. The upper series shows Heiken Ashi candles and we see that the trade was not executed @ 89.16 (HA candle open). Instead, if you look at the lower series showing regular candles the trade was seemingly executed at the open of the regular bar @88.57.

    Any help will be greatly appreciated.

    #2
    Hello sau92700,

    Thanks for your post.

    The Heiken-Ashi candle presents false information concerning each candle.

    Here is how the Heiken-ashi is calculated:
    HAClose = ((Open[0] + High[0] + Low[0] + Close[0]) * 0.25); // Calculate the close
    HAOpen = ((HAOpen[1] + HAClose[1]) * 0.5); // Calculate the open
    HAHigh = (Math.Max(High[0], HAOpen[0])); // Calculate the high
    HALow = (Math.Min(Low[0], HAOpen[0])); // Calculate the low

    When you place an order it can only fill on real price data.

    The strategy analyzer does not know what the base data is that created the Heiken-Ashi bar and it will backtest based on the false information presented.

    A work around, for the strategy analyzer, would be to use the indicator HeikenAshi8 in your strategy and use standard bars in the chart/analyzer so that the backtest results are based on real data points and your strategy can use the Heiken-ashi values for the conditional entries.

    The HeikenAshi8 indicator can be found here: https://ninjatraderecosystem.com/use...heiken-ashi-8/ Please note: The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Fantastic, thank you for the clarification ! I will go ahead and try this work around.

      Comment


        #4
        Hi Sau92700 I hope you are doing great.
        I came across your post online and thought you might be able to provide me with some insight regarding an issue I am having.
        I'm trying to create and backtest my own trading strategy based on Ashi Candles (Theoretical/Calculated data) but executing the strategy in actual/true data. Let me put it in simple terms, I want to backtest a strategy where you buy a certain security at the closing price of a day when the second-in-a-row green daily Ashi candle occur, and after the position is taken, I plan on holding the security until a trailing stop loss is met.
        Is there a way you can guide me on how to do this? The strategy is a bit more complex than what I just explained, but if I get that working, then I'm sure I can finish it up with the remaining details, of which of course, I can share them all with you.
        I hope you get back to me.
        Kind regards,

        LimoneroCCS

        Comment


          #5
          Hello LimoneroCCS,

          Thanks for your post.

          I know you are directly addressing member sau92700 but I just wanted to take a moment and welcome you to the NinjaTrader forums!

          As I advised in post#2, for backtesting I would not use the Heiken-Ashi bar type but instead, use the Heiken-Ashi indicator on regular price bars to ensure that your backtest represents the use of actual price data. When creating a strategy that is using the Heiken-Ashi indicator you would use the 4 indicator plots for the price data, the four plots are HA_Close, HA_Open, HA_Low, and HA_High.

          To determine if a Heiken-Ashi candle is green you would compare the HA_Close to the HA_Open and if greater then it would be a green bar, if less then it would be a red bar.
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Thank you NinjaTrader_PaulH !
            I will put that in place and if I have any issues I will contact you.
            Thanks again!!

            Comment


              #7
              Hi PaulH,
              So my strategy uses the following:
              HACLOSE[0] = Math.Abs((H1 + L1 +C1 + O1)/4); /// average
              HAOPEN[0] = Math.Abs((HAOPEN[1] + HACLOSE[1])/2); /// Sum previous and current

              if (HACLOSE[0] >= HAOPEN[0]) TrendUp = 1; else TrendUp =0;;
              if (HACLOSE[1] < HAOPEN[1]) TrendPrev= 1; else TrendPrev =0;

              So to determine trendup based on your suggestions above how would I reference HeikenAshi8 indicator in my strategy to accomplish the same trending.
              My current strategy does excellent in bascktest but not so good in live sim trading.Like you say the backtest is not getting real data.

              Comment


                #8
                Hello set2win,

                Thanks for your post.

                Here is an example of just checking the Close > Open. I'll include all of the script so you can be sure nothing else is missing but the parts bolded are what you would need to use the indicator instance in your strategy.

                namespace NinjaTrader.NinjaScript.Strategies
                {
                public class MyCustomStrategy8 : Strategy
                {
                private HeikenAshi8 HeikenAshi81; // create a private instance of the indicator

                protected override void OnStateChange()
                {
                if (State == State.SetDefaults)
                {
                Description = @"Enter the description for your new custom Strategy here.";
                Name = "MyCustomStrategy8";
                Calculate = Calculate.OnBarClose;
                EntriesPerDirection = 1;
                EntryHandling = EntryHandling.AllEntries;
                IsExitOnSessionCloseStrategy = true;
                ExitOnSessionCloseSeconds = 30;
                IsFillLimitOnTouch = false;
                MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                OrderFillResolution = OrderFillResolution.Standard;
                Slippage = 0;
                StartBehavior = StartBehavior.WaitUntilFlat;
                TimeInForce = TimeInForce.Gtc;
                TraceOrders = false;
                RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                StopTargetHandling = StopTargetHandling.PerEntryExecution;
                BarsRequiredToTrade = 20;
                // Disable this property for performance gains in Strategy Analyzer optimizations
                // See the Help Guide for additional information
                IsInstantiatedOnEachOptimizationIteration = true;
                }
                else if (State == State.Configure)
                {
                }
                else if (State == State.DataLoaded)
                {
                HeikenAshi81 = HeikenAshi8(Close); // initialize the indicator to use the close data series
                }
                }

                protected override void OnBarUpdate()
                {
                if (BarsInProgress != 0)
                return;

                if (CurrentBars[0] < 0)
                return;


                if (HeikenAshi81.HAClose[0] > HeikenAshi81.HAOpen[0]) // Close > open ?
                {
                // do something
                }

                }
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks for the response. And yes its a much more brutal real version than the rosy version I first cooked up. It looks more like the real time sim trades. Thanks for your guidance in giving us a reality check.

                  Comment


                    #10
                    Paul, I can get the HeikenAshi8 Indicator to pull up on a chart but there is no way for me to include it in the strategy analyzer for a backtest.
                    Attached Files

                    Comment


                      #11
                      Hello cmaily,

                      Thanks for your post and welcome to the Ninjatrader forums!

                      When you created a chart you selected some bar type that was not Heiken-Ashi and then you applied the Heikenashi8 indicator to the chart, correct? In the strategy analyzer, you would select the same bars as you used in the chart, and through the strategy you created, you would apply the HeikenAshi8 indicator.
                      Paul H.NinjaTrader Customer Service

                      Comment


                        #12
                        That is correct, I loaded a minute chart and it showed up fine. I don’t have the option to add the indicator to my strategy though in the strategy analyzer or at least don’t know how. I can only backtest different data series chart types instead of adding an indicator to the chart type.

                        Comment


                          #13
                          Hello cmally,

                          Thanks for your reply.

                          The HeikenAshi8 indicator, when applied to a chart will redraw the candles displayed on the chart according to the values shown in my Post#2.

                          In order to use the indicator values in your strategy, you have to add the indicator to your strategy and create conditions based on the indicator's plot values. The 4 plots from the indicator to use are identified HAClose, HAOpen, HAHigh, and HALow.

                          Please review the examples in posts 5 & 8. Post #8 is a complete Ninjascript way to work with the indicator in a strategy. You didn't say if you were using Ninjascript or the strategy builder so in case you are using the strategy builder then you would have a different but simpler process. In the conditions builder, select Indicators>HeikenAsh8 indicator and then (critical) select the "Value plot" for the condition to use.

                          Here is an example where the condition is to check if the Heiken-Ashi candle close is greater than the Open (an upbar (green bar)). Note that I also check the option to "Plot on chart", you only need to check that once as the indicator will then be added to the chart when you apply the strategy.

                          Click image for larger version

Name:	cmally-1.PNG
Views:	51
Size:	60.7 KB
ID:	1150675



                          Paul H.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello cmally,

                            Thanks for your reply.

                            To add the indicator to your strategy, please review post #8. What is shown in bold is what is added. The bold in the OnBarUpdate() is merely an example of a condition.

                            Paul H.NinjaTrader Customer Service

                            Comment


                              #15
                              The ninjascript strategy can’t be found anywhere in the editor. If there is a way to convert it from Indicator to BarsType then I’d be able to use it to backtest as I’m able to pull up bar types in the strategy analyzer with my strategy. Just needs to move folders and be converted but I couldn’t figure it out. Wouldn’t compile correctly. I can screen share over zoom or something, would be way easier to figure this out instead of being drawn out over this forum thread
                              Last edited by cmally; 04-08-2021, 04:08 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by elliot5, Today, 03:37 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post elliot5
                              by elliot5
                               
                              Started by Papercut110, Today, 03:17 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Papercut110  
                              Started by max333, Today, 02:22 AM
                              2 responses
                              8 views
                              0 likes
                              Last Post NinjaTrader_Jason  
                              Started by cincai, Today, 12:55 AM
                              3 responses
                              12 views
                              0 likes
                              Last Post cincai
                              by cincai
                               
                              Started by cincai, Today, 12:51 AM
                              1 response
                              11 views
                              0 likes
                              Last Post s.kinra
                              by s.kinra
                               
                              Working...
                              X