Grid Robot with Trailing Stop

Created at 17 Dec 2024, 15:16
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!
PA

paulomsduarte89

Joined 31.08.2021

Grid Robot with Trailing Stop
17 Dec 2024, 15:16


Can Someone help me to change this things in this code?

1º Open orders always with the selected volume (not duplicated)
2º Remove the TP from the grid (just a option to adjust the distance to open new orders)
3º Add a Trailing Stop equal to grid adjustable distance


Thank you!!!

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class JCSimpleGrid : Robot
    {
        [Parameter("Long position?", Group = "Basic Setup", DefaultValue = true)]
        public bool IfOpenBuy { get; set; }

        [Parameter("First order volume", Group = "Basic Setup", DefaultValue = 1000, MinValue = 1, Step = 1)]
        public double InitialVolume { get; set; }

        [Parameter("Grid size in points", Group = "Basic Setup", DefaultValue = 200, MinValue = 10)]
        public int GridSizeInPoints { get; set; }

        private string thiscBotLabel;

        protected override void OnStart()
        {
            // Set position label to cBot name
            thiscBotLabel = GetType().Name;
            // Normalize volume in case a wrong volume was entered
            if (InitialVolume != (InitialVolume = Symbol.NormalizeVolumeInUnits(InitialVolume)))
            {
                Print("Initial volume entered incorrectly, volume has been changed to ", InitialVolume);
            }
        }

        protected override void OnTick()
        {
            if (IfOpenBuy)
            {
                if (Positions.Count(x => x.TradeType == TradeType.Buy && x.SymbolName == SymbolName && x.Label == thiscBotLabel) == 0)
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, InitialVolume, thiscBotLabel,null, GridSizeInPoints * Symbol.TickSize / Symbol.PipSize);
                else if (Symbol.Ask <= Positions.FindAll(thiscBotLabel, SymbolName, TradeType.Buy).Last().EntryPrice - GridSizeInPoints * Symbol.TickSize)
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, InitialVolume * (Positions.Count(x => x.TradeType == TradeType.Buy && x.SymbolName == SymbolName && x.Label == thiscBotLabel) + 1), thiscBotLabel, null, GridSizeInPoints * Symbol.TickSize / Symbol.PipSize);
            }
            else
            {
                if (Positions.Count(x => x.TradeType == TradeType.Sell && x.SymbolName == SymbolName && x.Label == thiscBotLabel) == 0)
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, InitialVolume, thiscBotLabel, null, GridSizeInPoints * Symbol.TickSize / Symbol.PipSize);
                else if (Symbol.Bid >= Positions.FindAll(thiscBotLabel, SymbolName, TradeType.Sell).Last().EntryPrice + GridSizeInPoints * Symbol.TickSize)
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, InitialVolume * (Positions.Count(x => x.TradeType == TradeType.Sell && x.SymbolName == SymbolName && x.Label == thiscBotLabel) + 1), thiscBotLabel, null, GridSizeInPoints * Symbol.TickSize / Symbol.PipSize);
            }
        }

        protected override void OnStop()
        {
            // Close all open positions opened by this cBot on stop during backtesting
            if (IsBacktesting)
            {
                foreach (var position in Positions)
                {
                    if (position.SymbolName == SymbolName && position.Label == thiscBotLabel)
                        ClosePosition(position);
                }
            }
        }
    }
}


@paulomsduarte89