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

Strategy Analyzer back test with stop loss and profit target orders

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

    Strategy Analyzer back test with stop loss and profit target orders

    Hello,

    Getting my head around Strategy Analyzer back-test and came across an issue. I can not figure out how to submit stop loss and profit target order during back test once my entry order gets filled.

    I choose Historical fill as High and Type as Minute (please see attached screen shot). The strategy trades off 1 hour bars, and submits a stop limit order to go long, for example, 10 ticks above current bar high. So if next bar hits this level the order gets executed and if not, my logic would cancel it on the following bar.

    I assume the option in screen shot tells strategy analyzer to go granular to 1 min intervals to check if my order gets hit, essentially 1 min bars represent how 1 hour bar was built. In real time when bar is being created with every ticks, the moment my stop limit gets executed and full quantity is filled, my logic will submit bracket order for stop loss and profit target. In order to so the same in strategy analyzer I need access to those 1 min intervals to know when order gets filled. How can I do it?

    Am I understanding how analyzer works correctly?

    Thanks,
    redduke
    Attached Files

    #2
    Hello redduke,

    Thank you for your post.

    You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by submitting your orders to a more granular secondary bar series to achieve an "intrabar" fill in your backtests.

    I suggest you take a look at this example from our help guide:



    You should expect that a strategy running real-time (live brokerage account, live market simulation, Market Replay etc...) will produce different results than the performance results generated during a backtest. This difference may be more easily seen on certain Bars types (e.g. Point and Figure) than others due to their inherent nature in bar formation.

    During a backtest you can select different fill algorithms which will produce different results. Fills are determined based on 4 data points, OHLC of a bar since that is the only information that is known during a backtest and there will be no intra-bar data. This means actions cannot happen intra-bar, fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the information that is known.

    Because of this, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick'.

    Also, here is a link to the differences on real-time vs backtest (historical).


    Adding intra-bar granularity as in the above sample can help with this.

    Intra-bar granularity adds a second data series such as a 1 tick series so that the strategy has finer granularity in the historical data in between the OHLC of the primary series. This allows for more accurate trades by supplying the correct price at the correct time for the order to fill with.

    In NinjaTrader 8, there have been two new enhancements so that programmers do not have to manually add this secondary series and code the script to for high accuracy fills (Order Fill Resolution) and for intra-bar actions (TickReplay).

    Here is a link to our forums that goes into depth on using Order Fill Resolution and Tick Replay to ensure your backtests are as close to real time results as possible:

    https://ninjatrader.com/support/forum/forum/ninjatrader-8/strategy-development/100192-comparing-real-time-historical-and-replay-performance?t=102504

    High Fill Order Resolution and TickReplay cannot be used together. If it is necessary to have both, it is still possible to add intra-bar granularity to a script in the code itself for order fill accuracy and use TickReplay to update indicators with Calculate set to OnPriceChange or OnEachTick historically.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi Kate,

      Thanks for your response. It made things much clearer. I am still confused about back test option that I attached in my original post: Historical fill as High and Type as Minute. Does this option mean that strategy analyzer will drop to 1 min data series to simulate the fills? If I manually add 1 min data series, while using this option, I get the following error (see attached screen shot).

      My goal is to make sure that fills are simulated on 1 min bars when main chart is 60 or 120 min. This will get back test result as close to real market conditions as possible. The real market can navigate many times from low to high, and it is not visible when you just see 1 or 2 hour complete bar, where 1 min granular data will add a dose of reality.

      Thanks,
      redduke
      Attached Files

      Comment


        #4
        Hello RedDuke,

        Thank you for your reply.

        High order fill resolution adds the more granular data series for you only if your strategy uses a single data series, so if you've added the data series manually and made sure the orders are correctly being placed to the 1 minute series like in the example from my previous post, and then tried to use the High fill resolution you'd get the error you've posted. You'd want to use it if you hadn't added that dataseries, but since you did you can use the standard order fill processing.

        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Makes perfect sense, and I am totally fine with using single data series to be able to use Historical fill as High and Type as Minute. But how can I insert my logic (cancelling orders, submitting stop loss and etc) if this options is chosen on that granular level? When I use multi data series strategy, I can use BarsInProgress to control which series I am processing.

          Comment


            #6
            Hello RedDuke,

            Thank you for your reply.

            High order fill resolution takes care of that for you - so basically it's like adding the second data series manually. You don't need to worry about it because on bar update will trigger based on that 1 minute time period.

            Please let us know if we may be of further assistance to you.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              If I put Print(Time[0]) in onbarupdate it is only triggered once per hour. If historical is set to high, how it triggered every min. I need to put my code in 1 min series so to speak.

              Thank you
              Last edited by RedDuke; 07-18-2019, 06:30 PM.

              Comment


                #8
                Hello RedDuke,

                Thank you for your reply.

                That's because high order fill resolution is specific to order methods - the Print statement would only be triggered by the primary series. If you want a print statement to print out every minute, you'd need to take the step of adding the secondary data series instead.

                Please let us know if we may be of further assistance to you.
                Kate W.NinjaTrader Customer Service

                Comment


                  #9
                  Kate,

                  Thanks for explaining it.

                  Regards,
                  redduke

                  Comment


                    #10
                    Hello,

                    I came across a different issue after I added 1 min as additional data series to get a more granular execution. I ran it on 1 hour charts. The way my code is written, I have an indicator that produces a signal and strategy that calls it. Once signal is received, order gets submitted.

                    Attached files have a super easy example of the issue. Both indicator and strategy just output bar time for each data series (main 1 hour and 1 min). If you run strategy through back test, it will first run through every bar as strategy and than through every bar as indicator. I would have expected the logic to be triggered on each bar, first strategy and than indicator it calls.

                    Thanks
                    Attached Files

                    Comment


                      #11
                      Hello RedDuke,

                      Thank you for your reply.

                      How are you backtesting? When I run your example strategy, I do not get any prints at all from your example indicator, only the strategy. If, however, I attach the indicator to the chart directly, I get the behavior you're describing. This would be normal - I would expect separate objects updating separately to be initially calculated separately as well for historical data.

                      Thanks in advance; I look forward to assisting you further.

                      Kate W.NinjaTrader Customer Service

                      Comment


                        #12
                        Hi Kate,

                        The strategy calls the indicator. If I remove 1 min time frame all works well. I get 1 output line from strategy, on each bar, followed by 1 output line from indicator:
                        Strategy
                        Indicator
                        Strategy
                        Indicator
                        Strategy
                        Indicator......

                        However, once 1 min data series is added, strategy first outputs a line for every bar, and only that calls indicator for every bar:
                        Strategy
                        Strategy
                        Strategy
                        Indicator
                        Indicator
                        Indicator......

                        Thanks

                        Comment


                          #13
                          Hello RedDuke,

                          Thank you for your reply.

                          I'm unable to reproduce this with your sample strategy - I get no prints from the indicator at all, only the strategy. If you're uncomfortable with posting the full strategy here, might you be able to send it in an email to platformsupport[at]ninjatrader[dot]com? Please include a link to this forum post and "2215973 ATTN Kate" in the body of the email. If not, please revise your example so I can reproduce your results.

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

                          Comment


                            #14
                            Hi Kate,

                            I created zip file, so it is easier for you to set it up. here are the steps to reproduce:

                            1) Import zip file
                            2) Open Strategy analyzer and use the same setting as in attached screen shot (analyzer.jpg)

                            The output windows will first display what is in screen shot "Strategy series.jpg", and only after it runs through every bars on the chart, it will run again through every bar and you will what is in "Indicator series.jpg"


                            Since strategy calls indicator on every bar, I would have expected this behaviors in output windows:
                            ....
                            Series 1 minute - Strategy 5/9/2019 10:58:00 PM
                            Series 1 minute - Indicator 5/9/2019 10:58:00 PM
                            Series 1 minute - Strategy 5/9/2019 10:59:00 PM
                            Series 1 minute - Indicator 5/9/2019 10:59:00 PM
                            Series 1 hour - Strategy 5/9/2019 11:00:00 PM
                            Series 1 hour - Indicator 5/9/2019 11:00:00 PM
                            ......

                            Instead I get this:

                            ......................
                            ​​​​​​​Series 1 minute - Strategy 5/9/2019 10:58:00 PM
                            Series 1 minute - Strategy 5/9/2019 10:59:00 PM
                            Series 1 hour - Strategy 5/9/2019 11:00:00 PM
                            ......................
                            .
                            .
                            .
                            .......................
                            Series 1 minute - Indicator 5/9/2019 10:58:00 PM
                            Series 1 minute - Indicator 5/9/2019 10:59:00 PM
                            Series 1 hour - Indicator 5/9/2019 11:00:00 PM
                            .......................

                            Thanks,
                            redduke
                            Attached Files

                            Comment


                              #15
                              Sorry, zip fie is here.
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by DJ888, 04-16-2024, 06:09 PM
                              4 responses
                              12 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by terofs, Today, 04:18 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post terofs
                              by terofs
                               
                              Started by nandhumca, Today, 03:41 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post nandhumca  
                              Started by The_Sec, Today, 03:37 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post The_Sec
                              by The_Sec
                               
                              Started by GwFutures1988, Today, 02:48 PM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Working...
                              X