Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SetTrailStop general questions

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

    SetTrailStop general questions

    Hello. I'm trying to write a test a strategy that uses SetStopLoss() and SetTrailStop(). I'm not getting the behavior I expect. In the docs, it is explained that SetTrailStop should be "reset," but I can't find a good example of how to do this. My solution below is to set the trailing stop wider than the stop loss when the trade is entered, then set it where I need it when the trade is profitable. I can't see it working in the backtest results, though. It just looks like the stop loss is always what gets hit. And I don't see it at all in the ExitName column of the trades tab of the strategy analyzer.

    Are there any glaring errors in my syntax, or logic for that matter? (Btw I know I have some redundant code and could use a couple of helper methods):

    Code:
    #region Variables
            // Wizard generated variables
            private int regPeriod = 1; // Default setting for RegPeriod
            private double stDev = 2; // Default setting for StDev
            private int countOfOutside = 1; // Default setting for CountOfOutside
            private int ticksInProfitForInitialStop = 1; // Default setting for TicksInProfitForInitialStop
            private int trailStopTicks = 1; // Default setting for TrailStopTicks
            private int secondSeriesMins = 1; // Default setting for SecondSeriesMins
            // User defined variables (add any user defined variables below)
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                Add(PeriodType.Minute,secondSeriesMins);
                CalculateOnBarClose = true;
            }
            
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            
            //1
            protected override void OnBarUpdate()
            {
                if (CurrentBars[0] <= BarsRequired) return;
                
                int countOfOut = 0;
                
                //is the 2ndary series uptrending? (50 per lin reg)
                if(RegressionChannel(Closes[1],50,2)[1] > RegressionChannel(Closes[1],50,2)[5])
                {
                    if(Low[1] < RegressionChannel(regPeriod,stDev).Lower[1])
                    {
                        while(Low[countOfOut + 1] < RegressionChannel(regPeriod,stDev).Lower[countOfOut + 1])
                        {
                            countOfOut++;
                        }
                        
                        if(countOfOut == countOfOutside) 
                        {
                            EnterLong(1,"Signal");
                            SetStopLoss("Signal",CalculationMode.Ticks,8,true);
                            SetTrailStop("Signal",CalculationMode.Ticks,10,true);
                        }
                    }
                }
                
                //is the 2ndary series downtrending? (50 per lin reg)
                else if(RegressionChannel(Closes[1],50,2)[1] < RegressionChannel(Closes[1],50,2)[5])
                {
                    if(High[1] > RegressionChannel(regPeriod,stDev).Lower[1])
                    {
                        while(High[countOfOut + 1] > RegressionChannel(regPeriod,stDev).Upper[countOfOut + 1])
                        {
                            countOfOut++;
                        }
                        
                        if(countOfOut == countOfOutside) 
                        {
                            EnterShort(1,"Signal");
                            SetStopLoss("Signal",CalculationMode.Ticks,8,true);
                            SetTrailStop("Signal",CalculationMode.Ticks,10,true);
                        }
                    }
                }
                
                if (Position.MarketPosition == MarketPosition.Long)
                {
                    //check the current profit in ticks
                    double profitInTicks = (Close[0] - Position.AvgPrice) / .25;
                    
                    //is the last close greater than the initial profit price to set the first stop, + the trailing stop ticks?
                    if(profitInTicks >= ticksInProfitForInitialStop + trailStopTicks)
                    {
                        SetTrailStop("Signal",CalculationMode.Ticks,trailStopTicks,true);
                    }
                    else if(profitInTicks >= ticksInProfitForInitialStop)
                    {
                        SetStopLoss("Signal",CalculationMode.Ticks,ticksInProfitForInitialStop,true);
                    }
                }
                
                if(Position.MarketPosition == MarketPosition.Short)
                {
                    //check for profit target
                    double profitInTicks = (Position.AvgPrice - Close[0]) / .25;
                    
                    //is the last close greater than the initial profit price to set the first stop, + the trailing stop ticks?
                    if(profitInTicks >= ticksInProfitForInitialStop + trailStopTicks)
                    {
                        SetTrailStop("Signal",CalculationMode.Ticks,trailStopTicks,true);
                    }
                    else if(profitInTicks >= ticksInProfitForInitialStop)
                    {
                        SetStopLoss("Signal",CalculationMode.Ticks,ticksInProfitForInitialStop,true);
                    }
                }
            }
    Last edited by CSharpTrader; 01-19-2013, 08:18 AM.

    #2
    Originally posted by CSharpTrader View Post
    Hello. I'm trying to write a test a strategy that uses SetStopLoss() and SetTrailStop(). I'm not getting the behavior I expect. In the docs, it is explained that SetTrailStop should be "reset," but I can't find a good example of how to do this. My solution below is to set the trailing stop wider than the stop loss when the trade is entered, then set it where I need it when the trade is profitable. I can't see it working in the backtest results, though. It just looks like the stop loss is always what gets hit. And I don't see it at all in the ExitName column of the trades tab of the strategy analyzer.

    Are there any glaring errors in my syntax, or logic for that matter? (Btw I know I have some redundant code and could use a couple of helper methods):

    Code:
    #region Variables
            // Wizard generated variables
            private int regPeriod = 1; // Default setting for RegPeriod
            private double stDev = 2; // Default setting for StDev
            private int countOfOutside = 1; // Default setting for CountOfOutside
            private int ticksInProfitForInitialStop = 1; // Default setting for TicksInProfitForInitialStop
            private int trailStopTicks = 1; // Default setting for TrailStopTicks
            private int secondSeriesMins = 1; // Default setting for SecondSeriesMins
            // User defined variables (add any user defined variables below)
            #endregion
     
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                Add(PeriodType.Minute,secondSeriesMins);
                CalculateOnBarClose = true;
            }
     
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
     
            //1
            protected override void OnBarUpdate()
            {
                if (CurrentBars[0] <= BarsRequired) return;
     
                int countOfOut = 0;
     
                //is the 2ndary series uptrending? (50 per lin reg)
                if(RegressionChannel(Closes[1],50,2)[1] > RegressionChannel(Closes[1],50,2)[5])
                {
                    if(Low[1] < RegressionChannel(regPeriod,stDev).Lower[1])
                    {
                        while(Low[countOfOut + 1] < RegressionChannel(regPeriod,stDev).Lower[countOfOut + 1])
                        {
                            countOfOut++;
                        }
     
                        if(countOfOut == countOfOutside) 
                        {
                            EnterLong(1,"Signal");
                            SetStopLoss("Signal",CalculationMode.Ticks,8,true);
                            SetTrailStop("Signal",CalculationMode.Ticks,10,true);
                        }
                    }
                }
     
                //is the 2ndary series downtrending? (50 per lin reg)
                else if(RegressionChannel(Closes[1],50,2)[1] < RegressionChannel(Closes[1],50,2)[5])
                {
                    if(High[1] > RegressionChannel(regPeriod,stDev).Lower[1])
                    {
                        while(High[countOfOut + 1] > RegressionChannel(regPeriod,stDev).Upper[countOfOut + 1])
                        {
                            countOfOut++;
                        }
     
                        if(countOfOut == countOfOutside) 
                        {
                            EnterShort(1,"Signal");
                            SetStopLoss("Signal",CalculationMode.Ticks,8,true);
                            SetTrailStop("Signal",CalculationMode.Ticks,10,true);
                        }
                    }
                }
     
                if (Position.MarketPosition == MarketPosition.Long)
                {
                    //check the current profit in ticks
                    double profitInTicks = (Close[0] - Position.AvgPrice) / .25;
     
                    //is the last close greater than the initial profit price to set the first stop, + the trailing stop ticks?
                    if(profitInTicks >= ticksInProfitForInitialStop + trailStopTicks)
                    {
                        SetTrailStop("Signal",CalculationMode.Ticks,trailStopTicks,true);
                    }
                    else if(profitInTicks >= ticksInProfitForInitialStop)
                    {
                        SetStopLoss("Signal",CalculationMode.Ticks,ticksInProfitForInitialStop,true);
                    }
                }
     
                if(Position.MarketPosition == MarketPosition.Short)
                {
                    //check for profit target
                    double profitInTicks = (Position.AvgPrice - Close[0]) / .25;
     
                    //is the last close greater than the initial profit price to set the first stop, + the trailing stop ticks?
                    if(profitInTicks >= ticksInProfitForInitialStop + trailStopTicks)
                    {
                        SetTrailStop("Signal",CalculationMode.Ticks,trailStopTicks,true);
                    }
                    else if(profitInTicks >= ticksInProfitForInitialStop)
                    {
                        SetStopLoss("Signal",CalculationMode.Ticks,ticksInProfitForInitialStop,true);
                    }
                }
            }
    The framework does not allow us to use SetStopLoss() and SetTrailStop() together for the same position.

    From the NT Help:
    The SetStopLoss() method can NOT be used concurrently with the SetTrailStop() method for the same position, if both methods are called for the same position (fromEntrySignal) the SetStopLoss() will always take precedence. You can however, use both methods in the same strategy if they reference different signal names.
    ref: http://www.ninjatrader.com/support/h...etstoploss.htm

    .

    Comment


      #3
      Koganam... thanks. This is the case, even if the stop loss is always below the trailing stop? I can't find a good example of how to use both within the same strategy.

      Comment


        #4
        Originally posted by CSharpTrader View Post
        Koganam... thanks. This is the case, even if the stop loss is always below the trailing stop? I can't find a good example of how to use both within the same strategy.
        The SetStopLoss() method can NOT be used concurrently with the SetTrailStop() method for the same position, if both methods are called for the same position (fromEntrySignal) the SetStopLoss() will always take precedence. You can however, use both methods in the same strategy if they reference different signal names.
        The quote from the NT Help seems pretty definitive to me. Absolutely no wiggle room.

        IOW, if you want to use both methods in the same strategy, they will have to reference different initiating orders.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        672 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        379 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        111 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        577 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        582 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X