Adding Stop Loss..

Created at 14 Oct 2014, 20:45
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!
97

9718853

Joined 14.10.2014

Adding Stop Loss..
14 Oct 2014, 20:45


Hi,

I'm experimenting with reversing a trend Bot to create a consolidation Bot.. The back testing is profitable even with strong trends taking 100+ Pips of the table at a time. So if I can limit these losses it should be a great robot!!

Can anyone please advise how I to add a Stop Loss to the following code?

 

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MyRobot : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter()]
        public DataSeries SourceSeries { get; set; }

        [Parameter("Slow Periods", DefaultValue = 100)]
        public int SlowPeriods { get; set; }

        [Parameter("Fast Periods", DefaultValue = 14)]
        public int FastPeriods { get; set; }

        [Parameter(DefaultValue = 10000, MinValue = 0)]
        public int Volume { get; set; }

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

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

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

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

            var SlowMa = slowMa.Result.Last(0);
            var FastMa = fastMa.Result.Last(0);


            if (FastMa < SlowMa - 5 * Symbol.TickSize && longPosition == null)
            {
                if (shortPosition != null)
                    ClosePosition(shortPosition);
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
            }
            else if (SlowMa < FastMa - 5 * Symbol.TickSize && shortPosition == null)
            {
                if (longPosition != null)
                    ClosePosition(longPosition);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }
        }
    }
}

 

Many thanks in advance...

Ian


@9718853
Replies

tradermatrix
14 Oct 2014, 23:59

I have added "Delay Entry (Pips)" adjustable. 
to optimizer 
good trade 
kind regards 

 

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 MyRobot : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter()]
        public DataSeries SourceSeries { get; set; }

        [Parameter("Slow Periods", DefaultValue = 100)]
        public int SlowPeriods { get; set; }

        [Parameter("Fast Periods", DefaultValue = 14)]
        public int FastPeriods { get; set; }

        [Parameter("Delay Entry (Pips)", DefaultValue = 5)]
        public double DelayEntry { get; set; }

        [Parameter(DefaultValue = 10000, MinValue = 0)]
        public int Volume { get; set; }

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

        [Parameter(DefaultValue = 160)]
        public int TakeProfit { get; set; }

        [Parameter("Trigger", DefaultValue = 0)]
        public int Trigger { get; set; }

        [Parameter("TrailingStop", DefaultValue = 0)]
        public int TrailingStop { get; set; }

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

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

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

                var SlowMa = slowMa.Result.Last(0);
                var FastMa = fastMa.Result.Last(0);


                if (FastMa < SlowMa - DelayEntry * Symbol.TickSize && longPosition == null)
                {
                    if (shortPosition != null)
                        ClosePosition(shortPosition);
                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, TakeProfit);
                }
                else if (SlowMa < FastMa - DelayEntry * Symbol.TickSize && shortPosition == null)
                {
                    if (longPosition != null)
                        ClosePosition(longPosition);
                    ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, StopLoss, TakeProfit);
                }
            }
        }

        private void TRAILING()
        {
            if (TrailingStop > 0 && Trigger > 0)
            {

                Position[] positions = Positions.FindAll(label, Symbol);



                foreach (Position position in positions)
                {


                    if (position.TradeType == TradeType.Sell)
                    {

                        double distance = position.EntryPrice - Symbol.Ask;


                        if (distance >= Trigger * Symbol.PipSize)
                        {

                            double newStopLossPrice = Symbol.Ask + TrailingStop * Symbol.PipSize;


                            if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                            {

                                ModifyPosition(position, newStopLossPrice, position.TakeProfit);

                            }
                        }
                    }


                    else
                    {

                        double distance = Symbol.Bid - position.EntryPrice;


                        if (distance >= Trigger * Symbol.PipSize)
                        {

                            double newStopLossPrice = Symbol.Bid - TrailingStop * Symbol.PipSize;


                            if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                            {

                                ModifyPosition(position, newStopLossPrice, position.TakeProfit);

                            }
                        }
                    }
                }
            }
        }
    }
}

 


@tradermatrix

9718853
15 Oct 2014, 11:17

Thank you so much for this Tradermatrix...

After backtesting it seems that the stop loss is effective but a new trade is immediately placed... 

We really need to make the bot wait until the Moving Averages cross again to trigger entry, is this possible? 

Many Thanks,

Ian


@9718853