Trail Stop Loss don't work

Created at 07 Apr 2020, 18:35
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!
LU

luca.tocchi

Joined 25.03.2020

Trail Stop Loss don't work
07 Apr 2020, 18:35


 

Hello! my program generates random symbols.
I wanted to put a trail stop loss that started at a certain distance.

The program works and generates random symbols and opens buy or sell positions depending on the conditions. Now I have entered the trail stop loss, this is the part of the program.
The problem is that trail stop loss doesn't work

 

        [Parameter("Stop Loss", DefaultValue = 5)]
        public double StopLoss { get; set; }

        [Parameter("Trigger When Gaining", DefaultValue = 1)]
        public double TriggerWhenGaining { get; set; }

        [Parameter("Trailing Stop Loss Distance", DefaultValue = 1)]
        public double TrailingStopLossDistance { get; set; }

        [Parameter("Highest Gain", DefaultValue = 1)]
        public double _highestGain { get; set; }

        private bool _isTrailing;

thanks

this is the part of program

 

 ExecuteMarketOrder(TradeType.Sell, EURUSD, 20000, "myLabel", StopLoss, null);
                                var position1 = Positions.Find("myLabel", EURUSD);

                                if (!_isTrailing && position1.Pips >= TriggerWhenGaining)
                                {
                                    _isTrailing = true;
                                }
                                if (_isTrailing && _highestGain < position1.Pips)
                                {
                                    var newSLprice1 = symbol.Bid + (symbol.PipSize * TrailingStopLossDistance);
                                    if (newSLprice1 > position1.StopLoss)
                                    {
                                        ModifyPosition(position1, newSLprice1, null);
                                    }
                                    _highestGain = position1.Pips;
                                }
                                System.Threading.Thread.Sleep(300000)


@luca.tocchi
Replies

PanagiotisCharalampous
08 Apr 2020, 08:25

Hi Luca,

Check this condition

 if (newSLprice1 > position1.StopLoss)

If you are trailing a sell position SL then the new SL should be lower, not higher.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

luca.tocchi
08 Apr 2020, 18:10

RE:

PanagiotisCharalampous ha detto:

Ciao Luca,

Controlla questa condizione

 if (newSLprice1 > position1.StopLoss)

Se si sta dietro una posizione di vendita SL allora la nuova SL dovrebbe essere inferiore, non superiore.

Migliori saluti

Panagiotis 

Unisciti a noi su Telegram

I did this test program, but the stop loss is not changed, it remains at -15 pips

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class provatrail : Robot
    {

        [Parameter("Stop Loss", DefaultValue = 15)]
        public double StopLoss { get; set; }

        [Parameter("Trigger When Gaining", DefaultValue = 1)]
        public double TriggerWhenGaining { get; set; }

        [Parameter("Trailing Stop Loss Distance", DefaultValue = 1)]
        public double TrailingStopLossDistance { get; set; }

        private double _highestGain;
        private bool _isTrailing;

        protected override void OnStart()
        {
            ExecuteMarketOrder(TradeType.Sell, Symbol, 20000, "Miaposizione", StopLoss, null);
            var position1 = Positions.Find("Miaposizione");

            _highestGain = Positions[0].Pips;
            if (!_isTrailing && position1.Pips >= TriggerWhenGaining)
            {
                _isTrailing = true;
            }
            if (_isTrailing && _highestGain < position1.Pips)
            {
                var newSLprice1 = Symbol.Bid + (Symbol.PipSize * TrailingStopLossDistance);
                if (newSLprice1 < position1.StopLoss)
                {
                    ModifyPosition(position1, newSLprice1, null);
                }
                _highestGain = position1.Pips;
            }
        }
    }
}


@luca.tocchi

PanagiotisCharalampous
09 Apr 2020, 08:18

Hi Luca,

The code runs only on start. This is why it does not change. You need to put the login in OnTick().

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

luca.tocchi
09 Apr 2020, 12:07

RE:

PanagiotisCharalampous ha detto:

Ciao Luca,

Il codice viene eseguito solo all'avvio. Questo è il motivo per cui non cambia. È necessario inserire l'account di accesso in OnTick().

Migliori saluti

Panagiotis 

Unisciti a noi su Telegram

ok i did this, adding the multi symbol.

but it still doesn't work, I miss it so little but I don't understand the mistake

 

 

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class trailingstoplossexample1 : Robot
    {

        [Parameter("Stop Loss", DefaultValue = 15)]
        public double StopLoss { get; set; }

        [Parameter("Trigger When Gaining", DefaultValue = 1)]
        public double TriggerWhenGaining { get; set; }

        [Parameter("Trailing Stop Loss Distance", DefaultValue = 1)]
        public double TrailingStopLossDistance { get; set; }

        private double _highestGain;
        private bool _isTrailing;
        private Random random = new Random();

        protected override void OnStart()
        {
            string EURUSD = Symbols[random.Next(Symbols.Count)];
            Symbols.GetSymbol("EURUSD");
            Print(EURUSD);
            var symbol = MarketData.GetSymbol(EURUSD);

            ExecuteMarketOrder(TradeType.Sell, EURUSD, 20000, "Miaposizione", StopLoss, null);
            _highestGain = Positions[0].Pips;
        }

        protected override void OnTick()
        {
            var position1 = Positions.Find("Miaposizione");
            if (!_isTrailing && position1.Pips >= TriggerWhenGaining)
            {
                _isTrailing = true;
            }
            if (_isTrailing && _highestGain < position1.Pips)
            {
                var newSLprice1 = Symbol.Bid + (Symbol.PipSize * TrailingStopLossDistance);
                if (newSLprice1 < position1.StopLoss)
                {
                    ModifyPosition(position1, newSLprice1, null);
                }
                _highestGain = position1.Pips;
            }

        }
    }
}


@luca.tocchi

PanagiotisCharalampous
09 Apr 2020, 12:20

Hi Luca,

You seem to trade on another symbol but you use the values of the chart's symbol to calculate your trailing stop loss. That is obviously wrong.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

luca.tocchi
09 Apr 2020, 12:24

RE:

PanagiotisCharalampous ha detto:

Ciao Luca,

Ti sembra di scambiare su un altro simbolo, ma si utilizzano i valori del simbolo del grafico per calcolare il trailing stop loss. Questo è ovviamente sbagliato.

Migliori saluti

Panagiotis 

Unisciti a noi su Telegram

How can I solve the problem?
sorry but I'm learning to program


@luca.tocchi

luca.tocchi
09 Apr 2020, 12:24

RE:

PanagiotisCharalampous ha detto:

Ciao Luca,

Ti sembra di scambiare su un altro simbolo, ma si utilizzano i valori del simbolo del grafico per calcolare il trailing stop loss. Questo è ovviamente sbagliato.

Migliori saluti

Panagiotis 

Unisciti a noi su Telegram

How can I solve the problem?
sorry but I'm learning to program


@luca.tocchi

PanagiotisCharalampous
09 Apr 2020, 12:29

Hi Luca,

Save the symbol you use in a global variable and use it for the calculation of your trailing stop variables.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

luca.tocchi
09 Apr 2020, 12:30

RE:

PanagiotisCharalampous ha detto:

Ciao Luca,

Salvare il simbolo utilizzato in una variabile globale e utilizzarlo per il calcolo delle variabili di arresto finali.

Migliori saluti

Panagiotis 

Unisciti a noi su Telegram

thanks for the advice and for your patience


@luca.tocchi