Sample Trend Algo - Not responding to Stop Loss and Take Profit Addition

Created at 11 Jun 2024, 17:28
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!
HE

hend.engineering

Joined 27.04.2024

Sample Trend Algo - Not responding to Stop Loss and Take Profit Addition
11 Jun 2024, 17:28


Hi all,

Very new to this so please excuse any newbie error committed.

I have altered the sample Trend cBot by copy and pasting the Stop Loss and Take profit section from another cBot, and I've also attempted to add additional parameters to fine tune the bot.

While the new parameters now appear on the Parameters tab, the SL and TP figures appear to be getting ignored during Optimisation and Backtesting.

The optimisation tab will give me a set of Pass parameters, with say a 5 tick stop loss, but when backtesting the losses will be into the 100s of ticks - clearly something isn't adding up, although the build button gives me a green light.

Can anyone help?

Cheers,

Tom


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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, AddIndicators = true)]
    public class SampleTrendcBot : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Slow MA Type", Group = "Slow Moving Average")]
        public MovingAverageType SMAType { get; set; }
        
        [Parameter("Fast MA Type", Group = "Fast Moving Average")]
        public MovingAverageType FMAType { get; set; }
        

        [Parameter("Source", Group = "Moving Average")]
        public DataSeries SourceSeries { get; set; }

        [Parameter("Slow Periods", Group = "Moving Average", DefaultValue = 10)]
        public int SlowPeriods { get; set; }

        [Parameter("Fast Periods", Group = "Moving Average", DefaultValue = 5)]
        public int FastPeriods { get; set; }
        
        [Parameter("Stop Loss", Group = "Protection", DefaultValue = 40)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", Group = "Protection", DefaultValue = 40)]
        public int TakeProfit { get; set; }


        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "Sample Trend cBot";

        protected override void OnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, FMAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, SMAType);
        }

        protected override void OnTick()
        {
            var longPosition = Positions.Find(label, SymbolName, TradeType.Buy);
            var shortPosition = Positions.Find(label, SymbolName, TradeType.Sell);

            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);

            if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null)
            {
                if (shortPosition != null)
                    ClosePosition(shortPosition);

                ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, label);
            }
            else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null)
            {
                if (longPosition != null)
                    ClosePosition(longPosition);

                ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, label);
            }
        }

        private double VolumeInUnits
        {
            get { return Symbol.QuantityToVolumeInUnits(Quantity); }
        }
    }
}

 


@hend.engineering
Replies

PanagiotisCharalampous
12 Jun 2024, 06:16

Hi there,

You don't seem to use the SL and TP in your ExecuteMarketOrder method

  ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, label);

You need to pass them as parameters.

Best regards,

Panagiotis


@PanagiotisCharalampous

hend.engineering
13 Jun 2024, 18:51 ( Updated at: 14 Jun 2024, 05:11 )

RE: Sample Trend Algo - Not responding to Stop Loss and Take Profit Addition

PanagiotisCharalampous said: 

Hi there,

You don't seem to use the SL and TP in your ExecuteMarketOrder method

  ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, label);

You need to pass them as parameters.

Best regards,

Panagiotis

Thank you for that. It took me a while but I did eventually figure that out! Embarrassing, but then again I haven't long discovered this language.

Any idea how I incorporate that function into the logic?


@hend.engineering