• 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.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Auto Trail code for strat that is backtestable

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

    Auto Trail code for strat that is backtestable

    Before I spend the time reinventing the wheel, does anyone have some code to do an auto trail that is backtestable? According to the doc, the built in ATM strategy for doing this is not testable ... which... makes it useless. If I have to spend the time reinventing it I will, but I thought I would ask here first.
    Thanks ... Ed

    #2
    Hello Ed,

    I do have an example you may find useful. This uses a secondary 1 tick series to trigger intra-bar movements.
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks so much for sharing. I'll take a look and get back to you.

      Comment


        #4
        Trailing Stop in Wizard?

        I am somewhat confused by this question but it seems to relate somewhat to my issue so rather than start a new thread, I will pile on here.

        Using the wizard in N8 I set a trailing stop of 10 ticks for starters and when running a strategy analyzer, it "seems" to work fine. Checking the trades and chart, the trailing stops seem to trigger as I intended. When running the strategy "live", the stops are not triggering and the results are catastrophic. When setting up the trailing stop there is a box under the "Misc." tab that says "From entry signal", but there is no explanation for this entry.

        see image here https://www.screencast.com/t/ONXmzHpF5Q

        The only tutorials (video or otherwise) regarding trailing stop setup that I see is regarding the ATM strategy setup. Am I suppose to make and ATM strategy first, then call it out here in the wizard? Also, why does the strategy analyzer correctly use the trailing stop, but live trading does not? Thanks in advance.

        Best regards,

        Dolfan

        Comment


          #5
          Hello Dolfan,

          Signal names and from entry signals are highlighted in the Strategy Builder 301 course.
          https://www.youtube.com/watch?v=HCyt...tu.be&t=52m25s

          If a signal name is used on an entry, this allows an exit using that same name for the from entry signal to be specific to that entry.

          When no from entry signal is used, the exits will apply to all entries.

          No from entry signal has to be used to get a trail stop to work. This would be used if you want to have multiple entries and exits and want to keep these separate.

          SetTrailStop() will not work historically. Instead, you must use an Exit order like ExitLongStop() or ExitLongStopLimit().

          For a trailing stop to work, intra-bar granularity has to be added to the script and the logic of the script changed to use this. The Strategy Builder is not capable of creating a proper Multi-Time Frame script and use the correct BarsInProgress for order submissions or logic. This would have to be manually coded.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Thanks for the link to the new video. The signal names were helpful, but I am not sure how to maximize their potential as of yet. Please explain to me this statement... "SetTrailStop() will not work historically." Specifically "historically". Note that when I set a trailing stop in the wizard and run it in a strategy analyzer, then check the performance both in the trades listed and on the chart, I see that it was used as I expected. For example; the trailing stop is set at 10 ticks as seen here https://www.screencast.com/t/J35HxB0sWWO and when I run the analyzer I get these results https://www.screencast.com/t/bnzyVYMX2 with excellent profit factor, % profitable trades and avg win/avg loss ratio. From here https://www.screencast.com/t/QwIVMAIp you can see that while my trailing stops are still a bit tight, they are triggering as intended. You can see here https://www.screencast.com/t/wCgaRMUWKmQ that all my exits are trailing stops. But when I run the strategy live they do not trigger. Why is there a difference in live and the analyzer?

            Best regards,

            Dolfan

            Comment


              #7
              Hello Dolfan,

              Whether connected to the Market Replay connection, a live connection, or the Simulated Data Feed, all scripts will process historical data before transitioning to real time. This allows the strategy to backtest the data previous to where the real-time data will begin, so that once real-time data starts the script has prepared by calculating indicator values and taking a historical position if necessary allowing the script to evaluate more intelligently when starting so that it will not have to collect real-time data for some time before being able to make calculations and satisfy conditions.

              If you run a script on the Market Replay, the script will process historical data first with the backtest engine, then begin processing for the real-time market replay playback data using the Market Replay engine.

              If you run a script on a live account, the script will process historical data first with the backtest engine, then begin processing for the real-time data from the live market.

              Below is a link to the help guide on OnStateChange().
              http://ninjatrader.com/support/helpG...tatechange.htm

              A strategy processing historical data is the equivalent of running a backtest before running the strategy in real-time.

              Orders placed in historical data are backtest orders and are not submitted live to the Sim101 account or to any funded brokerage accounts. These are theoretical virtual orders that are never submitted to any account.

              I should rephrase that SetTrailStop() does not function the same historically. When using SetTrailStop() in historical data trailing action will only occur when the primary bar closes. In real-time the script is receiving individual ticks the trailing logic can use to trigger movements. Historically, that tick data is not available. It is possible to add a secondary series for tick granularity to a script, however, SetTrailStop() can only use data from the primary series while an Exit order can be submitted to a secondary series. Because of this the trailing movement does not move tick for tick and instead updates when the primary bar closes.

              Below is a link to a thread on intra-bar granularity.
              http://ninjatrader.com/support/forum...297#post491297

              I would not be able to comment on how this would affect the performance of your strategy.

              However, you could add debugging prints to the script to understand why a stop loss is not be submitted, or does become submitted but does not get accepted, or is accepted and becomes working, but does not fill.

              A good place to start is by adding TraceOrders = true; to the script and opening the Output Window.
              Do you see any mentions of the trail stop in the output window?
              Last edited by NinjaTrader_ChelseaB; 02-26-2019, 09:44 AM.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                I'm not sure how to process all of this at this time. I will review the links and study away. Hopefully it will begin to make sense soon. I want to migrate back to Ninja for trading again but not until I can work these things out.

                I do understand how the ATM strategies work and if I am not mistaken, an ATM strategy can be added to an open position so, perhaps once an order is triggered, I can simply add what I know to work on the DOM.

                Best regards,

                Dolfan

                Comment


                  #9
                  Hello Dolfan,

                  This would not refer to Atm Strategies in any way. When calling an Atm Strategy using Atm Strategy Methods, this cannot be backtested at all. (No entry order will appear at all).

                  http://ninjatrader.com/support/helpG...gy_methods.htm

                  Using Atm Strategies is completely different than using the Set methods (SetTrailStop, SetStopLoss, or SetProfitTarget) in a strategy.

                  Further, when using Atm Strategy methods, the performance cannot be seen by the script. When in real-time only, entry orders will affect the account position but will not affect the strategy position.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Yes, I am aware that ATM strategies are not the same, nor can they be tested in a strategy. I do understand completely how they work and one can visibly see them functioning on a DOM when activated. This visibility is comforting. What I have been seeing or NOT seeing has made me uncomfortable and without that visibility and full understanding of the process, my strategies have not been successful in live trading. VERY successful in strategy analyzer though. I mean someone tell me if a strategy that produces a profit factor of 12.5 and a sharpe ration of 8.5 is bad and I will recalibrate my expectations.

                    I'll keep plugging away.

                    Dolfan

                    Comment


                      #11
                      Hello Dolfan,

                      Does your script add a 1 tick series for granularity and use Exit orders?

                      If you are using SetTrailStop() this will not function like it does in real-time as it is not meant to be used historically is meant to be used in real-time.
                      "Generates a trail stop order with the signal name "Trail stop" to exit a position. Trail stops are amended on a tick by tick basis. Trail stop orders are real working orders (unless simulated is specified in which case the stop order is locally simulated and submitted as market once triggered) submitted immediately to the market upon receiving an execution from an entry order."
                      "Trail stop orders are submitted in real-time on incoming executions from entry orders"
                      http://ninjatrader.com/support/helpG...ttrailstop.htm

                      You may be getting results you like, however, this does not mean they are accurate.

                      I would recommend removing all Set methods and using Exit orders with 1 tick granularity for accuracy when backtesting.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        So riddle me this Chelsea. I have been running this strategy constantly on 27 instruments with mixed results. One thing that I have begun to notice that is consistent is that if the price regresses within the first moments of the order opening, the trailing stops of 10 ticks is hit and the order closes. This results in a lot of small losses, obviously. However, if the trailing stop is not hit quickly, the trailing stop never gets hit. I have it seen it run on for major losses and I have seen it run on for major gains even though there were clearly times when the price action went against the direction of the trade. I have seen times when the trade was up maybe 100 ticks and there was a pull back of say 50 ticks and the stop was not hit.

                        The strategy is rather simple and I began with the default MyCustomStrategy. I did not add in any time triggers at all so I feel certain that I have not created this problem. I have used the wizard and not yet unlocked the code for editing. The Data Series is Renko Bars, the time frame is Days and the Chart Style is Candlesticks. There has to be a logical reason why the stop is hit early on in the position, but not after the position has "matured". Your thoughts?

                        Dolfan

                        Comment


                          #13
                          Hello Dolfan,

                          I do not have your script so reasons you may be getting undesired results would be speculation.

                          However, in historical data, if you are using SetTrailStop likely the trailing stop is not trailing until a bar updates. This means as the price increases the trailing stop doesn't move and becomes further and further from the current price and less likely to be hit.
                          On a new bar, this would suddenly update and trail. This could change your performance.

                          To find out why you strategy is behaving as it is behaving, I would recommend adding prints and using TraceOrders to see at what price the trail stop is working and see if the price falls below that level.

                          Printing the order object in OnOrderUpdate can tell you when a trailing stop has updated and changed price.

                          Below is a link to a video that demonstrates using prints to understand behavior.
                          https://www.youtube.com/watch?v=BA0W...tu.be&t=51m51s

                          At price is the trailing stop working where you are expecting this to fill?
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello,

                            I've had to make some corrections to this script.

                            http://ninjatrader.com/support/forum...993#post502993
                            Chelsea B.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Mykro, Yesterday, 04:58 PM
                            1 response
                            11 views
                            0 likes
                            Last Post NinjaTrader_PaulH  
                            Started by akuntysh, Today, 02:43 AM
                            0 responses
                            5 views
                            0 likes
                            Last Post akuntysh  
                            Started by thgamey, Today, 02:05 AM
                            0 responses
                            8 views
                            0 likes
                            Last Post thgamey
                            by thgamey
                             
                            Started by tpdk2012, Today, 12:51 AM
                            1 response
                            13 views
                            0 likes
                            Last Post NinjaTrader_ChrisSch  
                            Started by simonerg, Today, 12:13 AM
                            0 responses
                            9 views
                            0 likes
                            Last Post simonerg  
                            Working...
                            X