Trailing stop not working

Created at 13 Apr 2023, 23:03
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
KH

khlopez

Joined 13.04.2023

Trailing stop not working
13 Apr 2023, 23:03


Hello everyone,

First of all, my knowledge about coding is near to zero, that's why I'm having so many difficulties.

I'm trying to build a code with a trailing stop, but after hours and hours trying by myself, reading other posts, etc., I can't manage to make it work.

This is an example of code with the trailing stop syntax that I took from another post. The code itself works, but the trailing stop is never triggered.

Could anyone please help me fix this?

using cAlgo.API;
using cAlgo.API.Internals;
using System;
using System.Linq;

namespace cAlgo
{

    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SwingTrader : Robot
    {
    
            [Parameter(DefaultValue = 1000)]
            public double Volume { get; set; }  
                    
            [Parameter(DefaultValue = 70)]
            public double TakeProfit { get; set; }  
                    
            [Parameter(DefaultValue = 6)]
            public double StopLoss { get; set; }
            
            [Parameter("Include Trailing Stop", DefaultValue = true)]      
            public bool IncludeTrailingStop { get; set; }                   
            
            [Parameter("Trailing Stop Trigger (pips)", DefaultValue = 20)]  
            public int TrailingStopTrigger { get; set; }                   
            
            [Parameter("Trailing Stop Step (pips)", DefaultValue = 10)]     
            public int TrailingStopStep { get; set; }                   
            
            [Parameter("Instance Name", DefaultValue = "SwingTrader")]     
            public string InstanceName { get; set; }
        


            

    protected override void OnStart()
    
        
        
        {
            Positions.Closed += OnPositionClosed;
        }  
        
        

        private void OnPositionClosed(PositionClosedEventArgs args)
        {
            if (args.Position.Label != InstanceName)   //Comment changed to InstanceName
                return;

            if (args.Reason == PositionCloseReason.StopLoss || args.Reason == PositionCloseReason.TakeProfit)
            {
                Print($"Position closed by {args.Reason}. Profit: {args.Position.GrossProfit}");
            }
        }
        
        protected override void OnTick()
        {
            if (IncludeTrailingStop)
            {
                SetTrailingStop();
            }
        }
            
        private void SetTrailingStop()
        {
            var sellPositions = Positions.FindAll("Swing", SymbolName, TradeType.Sell);
            foreach (var position in sellPositions)
            {
                double distance = position.EntryPrice - Symbol.Ask;
                if (distance < TrailingStopTrigger * Symbol.PipSize)
                    continue;

                double newStopLossPrice = Symbol.Ask + TrailingStopStep * Symbol.PipSize;
                if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                {
                    ModifyPosition(position, newStopLossPrice, null);
            Print("Trailing Stop Loss triggered...");
                }
            }

            var buyPositions = Positions.FindAll("Swing", SymbolName, TradeType.Buy);
            foreach (var position in buyPositions)
            {
                double distance = Symbol.Bid - position.EntryPrice;
                if (distance < TrailingStopTrigger * Symbol.PipSize)
                    continue;

                double newStopLossPrice = Symbol.Bid - TrailingStopStep * Symbol.PipSize;
                if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                {
                    ModifyPosition(position, newStopLossPrice, null);
            Print("Trailing Stop Loss triggered...");
                }
            }
        }    
            
 
        protected override void OnBar()
        {
            if (Positions.Count > 0)
                return;
         
                                

        
        
            //Bullish swing
           
            if (Bars.HighPrices.Last(8) - Bars.LowPrices.Last(18) >= 0.0015
                && Bars.HighPrices.Last(8) - Bars.LowPrices.Last(18) < 0.0025)    
                
            
            {
                ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, InstanceName, 6, 70);
            }

            //Bearish swing
            if (Bars.HighPrices.Last(18) - Bars.LowPrices.Last(8) >= 0.0015
                && Bars.HighPrices.Last(18) - Bars.LowPrices.Last(8) < 0.0025)         
          
                
            {
                ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, InstanceName, 6, 70);
            }
            
                }
            }
            
    }
    


Thank you very much.


@khlopez
Replies

PanagiotisChar
15 Apr 2023, 12:08

Hi there,

The labels you are using to open the positions do not match the labels you use to find the positions in the trailing stop method.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar

khlopez
16 Apr 2023, 14:23

RE:

PanagiotisChar said:

Hi there,

The labels you are using to open the positions do not match the labels you use to find the positions in the trailing stop method.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 

Thank you so much, PanagiotisChar! It works now.

Regards.


@khlopez