Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to test stop loss and target strategies

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

    How to test stop loss and target strategies

    I have discovered that using SetProfitTarget and SetStopLoss functions in a strategy only work in real time. Is there a way to perform backtesting for stops and targets?

    #2
    Hello ShruggedAtlas,

    SetStopLoss and SetProfitTarget do work in backtest as well as historical data.

    These should be set before the order is placed.

    Attached is an example made with the Strategy Wizard to demonstrate.

    Please run this in the Strategy Analyzer or add to a chart. Ensure you have historical data showing in the backtest or chart.

    The strategy will only enter a new order if the strategy is in a flat position.

    This means after the first order, there will only be a second if the stop loss or profit target closes the order. (or the ExitOnClose)
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I assume this is an example stategy? I'm trying to figure out what to do with this file to allow me to open it in the Wizard or put it on a chart. I'll see if there is info on this.

      Comment


        #4
        Hello ShruggedAtlas,

        Yes, the attachment is an example strategy.

        Follow these steps to import the NinjaScript:

        1. Download the script to your desktop, keep it in the compressed .zip file.
        2. From the Control Center window select the menu File > Utilities > Import NinjaScript
        3. Select the downloaded .zip file
        4. NinjaTrader will then confirm if the import has been successful.

        Critical *Note that on any files that say "File already exists on your PC" that start with an "@" symbol are the ones that came preloaded inside of NinjaTrader so you would say "No" so that you do not override those files.


        Once imported, to view the strategy in the Strategy Wizard:
        • Click Tools -> Edit NinjaScript -> Strategy... -> StopLossTargetExampleWizard -> OK


        To add the strategy to a chart:
        • Right-click the Chart -> select Strategies...
        • Select StopLossTargetExampleWizard from the list -> click the New button
        • In the parameters on the right -> set Enabled to True
        • Click OK


        To backtest this in the Strategy Analyzer:
        • Click File -> New -> Strategy Analyzer...
        • Right-click an instrument from your instrument lists -> select Backtest
        • Click Run Backtest

        (You must have a connection with historical data to backtest a script. If you click the Charts tab in the results, there must be data on this chart)
        Last edited by NinjaTrader_ChelseaB; 02-26-2015, 01:43 PM.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Ok I see now how this works. I was applying my strategy to my chart and seeing what the performance was...this is totally different apparently to backtesting. i was assuming it was the same thing.

          thank you!

          Comment


            #6
            Hello ShruggedAtlas,

            Applying a strategy to a chart that has historical data on it and enabling will process the script historically which essentially the same as a backtest. (Ensure the strategy is enabled)

            This script, if applied to a chart with data, will have historical trades that are exited by the stop loss and profit target. You will be able to view the "backtest results" for the days to load period on the chart by right-clicking the chart -> selecting Strategy Performance -> the instance of the strategy -> Historical.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Still not working

              I have applied my strategy and backtested. Many of the losing trades show losses well beyond my stop value. My stop is set at 20 ticks. 1 YM contract = $100 loss.
              my results show some losing trades of several hundred dollars. Whats going on here?

              here is my code: am I applying the stop condition properly or is there some other trick to this?

              HTML Code:
              public class RICK3 : Strategy
                  {
                      #region Variables
                      private int stopLoss = 20; // Default setting for StopLoss
                      private int profitTarget = 4; // Default setting for ProfitTarget
                      private int barsToExit = 12; // Default setting for BarsToExit
                      private int entryOffset = 10; // Default setting for EntryOffset
                      private int timeframe2 = 30;
                      private int timeframe3 = 150;
                      private int rsiHigh = 70;
                      private int rsiLow = 30;
                      #endregion
              
              
                      protected override void Initialize()
                      {
                          SetProfitTarget("Profit", CalculationMode.Ticks, 40);
                          SetStopLoss("Loss", CalculationMode.Ticks, 20, false);
                          
                          Add(PeriodType.Minute, timeframe2);
                          Add(PeriodType.Minute, timeframe3);
              
                          CalculateOnBarClose = true;
                      }
              
              
                      protected override void OnBarUpdate()
                      {
                          if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired || CurrentBars[2] <= BarsRequired)
                                 return;
                          
                          if(BarsInProgress != 0)
                              return;
                          
                          // Entry Condition
                          if(CUMRSI(BarsArray[1],2,3)[0] < rsiLow
                              && CUMRSI(2,3)[0] < rsiLow)
                          {
                              EnterLongLimit(DefaultQuantity, Close[0] - EntryOffset * TickSize, "Long");
                          }
                          
                          // Exit Condition
                          if(CUMRSI(BarsArray[1],2,3)[0] > rsiHigh
                              && CUMRSI(2,3)[0] > rsiHigh)// || BarsSinceEntry(0,"Long",0) > barsToExit)
                              
                              ExitLong();
                      }
              Attached Files
              Last edited by ShruggedAtlas; 02-26-2015, 04:18 PM.

              Comment


                #8
                Hello ShruggedAtlas,

                At what price was the entry filled?

                At what price was the stop loss submitted?

                At what price did the stop loss fill?

                Is this in the Strategy Analyzer or on a Chart?

                If on a chart, you can find this information by right-clicking the chart -> selecting Strategy Performance -> instance name -> Historical & Real-time.

                In the Strategy Analyzer and the chart performance report click the executions tab to see the fill price, click the orders tab to see the submission price.
                Last edited by NinjaTrader_ChelseaB; 02-26-2015, 04:33 PM.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  This was in strategy Analyser - after the backtest I pulled up the list of trades...chose the worst loss and then pulled up the chart from within strategy analyser.

                  From the included picture you can see there was no stop applied. my code is identical to the one supplied earlier today as an example so I don't see what i'm doing wrong.

                  Based on the picture I've included..the order executed at 17330 long so the stop should have been placed automatically at 17310. As you can see, the traded exited at close rather than at the stop.

                  I looked at trades and execution in strategy analyzer and they are identical to the what the chart shows. no stop was implemented

                  Is this related in some way to the multi-timeframe aspects of my strategy? I'm implementing this on a 5 minute chart and using 5 and 30 minute data for entry and exit. (ignore the 150 minute - that's for later)
                  Last edited by ShruggedAtlas; 02-26-2015, 04:28 PM.

                  Comment


                    #10
                    Hi ShruggedAtlas,

                    In my example I do not use signal names.

                    You have chosen to use signal names. However the fromEntrySignal in the stop loss does not match your entry order.

                    Your entry order uses the signalName "Long".

                    The profit target however is attached to "Profit".

                    There is no entry order that uses the signal name Profit, so this profit target is not attached to anything. If you remove the fromEntrySignal, then this profit target will be applied to all orders and not a specific order using the signalName Profit.

                    EnterLongLimit(int quantity, double limitPrice, string signalName)
                    http://www.ninjatrader.com/support/h...rlonglimit.htm

                    SetProfitTarget(string fromEntrySignal, CalculationMode mode, double value)
                    http://www.ninjatrader.com/support/h...ofittarget.htm

                    Your stop loss is attached to "Loss". There is not an entry signal named Loss. This stop loss is not applied to any orders.

                    Try using no fromEntrySignal.

                    SetProfitTarget(CalculationMode.Ticks, 40);
                    SetStopLoss(CalculationMode.Ticks, 20, false);


                    Or if you want your stop loss to be applied to a specific entry, use the signal name of that entry in the fromEntrySignal of the stop loss.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Ok now i see why those labels are there...to connect the stops to the entry. It works just perfectly now.

                      thanks!

                      Comment


                        #12
                        When I right-click the Chart to select strategies I am not able to see the entire list in one go. What may be the reason?

                        Comment


                          #13
                          I discovered this problem yesterday in fact...Once the list of available strategies gets too long it won't fit in the window and NT has apparently not built in a scrolling function in that panel of that window. It's an annoying flaw but easily fixed. Just delete any old strategies you don't think you're likely to use until you are able to fit in the list.

                          Actually, i just tested this....my comment only refers to applying a strategy using the strategies tab in control panel...on that one you can't resize the window.

                          When you apply a strategy directly to a chart, you can resize the window and view all the strategies.

                          Comment


                            #14
                            Hello Donaldm1,

                            Thanks for your post and welcome to the Forum!

                            When you right mouse click on the chart and select strategies (or if you select the strategy icon at the top of the chart) you will have a "strategies" window with a vertical scroll bar so that you can scroll through the list of available strategies on the left upper side of the window (only if you have more strategies than can be shown in the window) Please advise if you do not see this.

                            When you right mouse click on the strategies tab and select "new strategy" the new strategy window appears. At the top of this window is a drop downlist that will show all available strategies again with a scroll bar (if you have more strategies than will be shown in the list window).

                            Please see attached examples for clarification.
                            Attached Files
                            Paul H.NinjaTrader Customer Service

                            Comment


                              #15
                              Thanks Paul.

                              Its working fine.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by GussJ, 03-04-2020, 03:11 PM
                              11 responses
                              3,221 views
                              0 likes
                              Last Post xiinteractive  
                              Started by andrewtrades, Today, 04:57 PM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by chbruno, Today, 04:10 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post chbruno
                              by chbruno
                               
                              Started by josh18955, 03-25-2023, 11:16 AM
                              6 responses
                              437 views
                              0 likes
                              Last Post Delerium  
                              Started by FAQtrader, Today, 03:35 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post FAQtrader  
                              Working...
                              X