Trailing Stop

Created at 13 Jan 2025, 09:53
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!
CT

ctid5083541

Joined 25.05.2022

Trailing Stop
13 Jan 2025, 09:53


How can I integrate a trailing stop with a stop order?

What I am trying to do is place a stop order, then a trailing stop if the next bar is higher than the previous bar and a new stop loss that is 10 ticks below the new high for long positions. This would execute on every new bar close. And, obviously, the opposite for short positions.

Many thanks in advance.

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, AddIndicators = true)]
    public class SimpleMovingAverageSample : Robot
    {
        private double _volumeInUnits;

        private SimpleMovingAverage _fastSimpleMovingAverage;

        private SimpleMovingAverage _slowSimpleMovingAverage;

        [Parameter("Source", Group = "Fast MA")]
        public DataSeries FastMaSource { get; set; }

        [Parameter("Period", DefaultValue = 9, Group = "Fast MA")]
        public int FastMaPeriod { get; set; }

        [Parameter("Source", Group = "Slow MA")]
        public DataSeries SlowMaSource { get; set; }

        [Parameter("Period", DefaultValue = 21, Group = "Slow MA")]
        public int SlowMaPeriod { get; set; }

        [Parameter("Volume (Lots)", DefaultValue = 1, Group = "Trade")]
        public double VolumeInLots { get; set; }

        [Parameter("Stop Loss (Pips)", DefaultValue = 10, MaxValue = 100, MinValue = 1, Step = 1)]
        public double StopLossInPips { get; set; }

        [Parameter("Take Profit (Pips)", DefaultValue = 10, MaxValue = 100, MinValue = 1, Step = 1)]
        public double TakeProfitInPips { get; set; }

        protected override void OnStart()
        {
            _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);

            _fastSimpleMovingAverage = Indicators.SimpleMovingAverage(FastMaSource, FastMaPeriod);
            _slowSimpleMovingAverage = Indicators.SimpleMovingAverage(SlowMaSource, SlowMaPeriod);

            _fastSimpleMovingAverage.Result.Line.Color = Color.Blue;
            _slowSimpleMovingAverage.Result.Line.Color = Color.Red;
        }

        protected override void OnBarClosed()
        {
            if (_fastSimpleMovingAverage.Result.HasCrossedAbove(_slowSimpleMovingAverage.Result, 0))
            {
                PlaceStopOrderAsync(TradeType.Buy, SymbolName, VolumeInLots, Symbol.Ask + 10 * Symbol.PipSize, "Buy", StopLossInPips, TakeProfitInPips);
            }
            else if (_fastSimpleMovingAverage.Result.HasCrossedBelow(_slowSimpleMovingAverage.Result, 0))
            {   
                PlaceStopOrderAsync(TradeType.Sell, SymbolName, VolumeInLots, Symbol.Ask + 10 * Symbol.PipSize, "Sell", StopLossInPips, TakeProfitInPips);
            }
        }
    }
}

@ctid5083541