Trailing Stop in my Cbot

Created at 17 Jan 2022, 19:22
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!
UM

umadrat2012

Joined 18.09.2021

Trailing Stop in my Cbot
17 Jan 2022, 19:22


Hi, can someone put some risk management features such as trailing stop into this cbot please?

 

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class EMACross_RSI : Robot
    {
        [Parameter("Source")]
        public DataSeries SourceSeries { get; set; }
        [Parameter("Label", DefaultValue = "EMA")]
        public string label { get; set; }

        [Parameter("Slow Periods", DefaultValue = 30)]
        public int SlowPeriods { get; set; }
        [Parameter("Medium Periods", DefaultValue = 12)]
        public int MediumPeriods { get; set; }
        [Parameter("Fast Periods", DefaultValue = 5)]
        public int FastPeriods { get; set; }

        [Parameter("Stop Loss", DefaultValue = 10)]
        public int SL { get; set; }
        [Parameter("Take Profit", DefaultValue = 10)]
        public double TP { get; set; }
        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        private ExponentialMovingAverage slowMa;
        private ExponentialMovingAverage mediumMa;
        private ExponentialMovingAverage fastMa;


        protected override void OnStart()
        {

            fastMa = Indicators.ExponentialMovingAverage(SourceSeries, FastPeriods);
            mediumMa = Indicators.ExponentialMovingAverage(SourceSeries, MediumPeriods);
            slowMa = Indicators.ExponentialMovingAverage(SourceSeries, SlowPeriods);

        }


        protected override void OnBar()
        {
            int index = MarketSeries.OpenTime.Count - 2;
            var longPosition = Positions.Find(label, SymbolName, TradeType.Buy);
            var shortPosition = Positions.Find(label, SymbolName, TradeType.Sell);

            if ((fastMa.Result[index] > slowMa.Result[index]) && (mediumMa.Result[index] > slowMa.Result[index]) && (fastMa.Result[index - 1] < slowMa.Result[index - 1]) && (mediumMa.Result[index - 1] < slowMa.Result[index - 1]) && shortPosition == null)
            {
                if (longPosition != null)
                    ClosePosition(longPosition);

                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label, SL, TP);

            }
            else if ((fastMa.Result[index] < slowMa.Result[index]) && (mediumMa.Result[index] < slowMa.Result[index]) && (fastMa.Result[index - 1] > slowMa.Result[index - 1]) && (mediumMa.Result[index - 1] > slowMa.Result[index - 1]) && longPosition == null)
            {

                if (shortPosition != null)
                    ClosePosition(shortPosition);
                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label, SL, TP);

            }

        }
        private long VolumeInUnits
        {
            get { return Symbol.QuantityToVolume(Quantity); }
        }
    }
}


@umadrat2012
Replies

amusleh
18 Jan 2022, 09:20

Hi,

Please post a job request or contact one of our consultants.


@amusleh