Sl and Tp and Breakeven Bot HELP!

Created at 12 Apr 2024, 09:00
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!
JO

Jonathangong12

Joined 19.05.2023

Sl and Tp and Breakeven Bot HELP!
12 Apr 2024, 09:00


 

This bot is just a copy and paste of other bots combined. I'm not sure why its not working but when I open a position I am getting an “Order Execution Error Nothing to change”. Any help would be greatly appreciated
 

 

 

 

 

 

 

 

 

using System;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class CombinedTradingBot : Robot
    {
        // Parameters from the first script
        [Parameter("Modify TP", DefaultValue = true)]
        public bool TpOption { get; set; }
        [Parameter("Take Profit Pips", DefaultValue = 3)]
        public double TpPips { get; set; }
        [Parameter("Modify SL", DefaultValue = true)]
        public bool SlOption { get; set; }
        [Parameter("Stop Loss Pips", DefaultValue = 3)]
        public double SlPips { get; set; }

        // Parameters from the second script
        [Parameter("Instance Name", DefaultValue = "")]
        public string InstanceName { get; set; }
        [Parameter("Include Break-Even", DefaultValue = true, Group = "Protection")]
        public bool IncludeBreakEven { get; set; }
        [Parameter("Break-Even Trigger (pips)", DefaultValue = 100, MinValue = 1, Group = "Protection")]
        public int BreakEvenPips { get; set; }
        [Parameter("Break-Even Extra (pips)", DefaultValue = 100, MinValue = 1, Group = "Protection")]
        public int BreakEvenExtraPips { get; set; }
        [Parameter("Trail after Break-Even", DefaultValue = true, Group = "Protection")]
        public bool IncludeTrail { get; set; }
        [Parameter("Trailing Stop (pips)", DefaultValue = 100, MinValue = 1, Group = "Protection")]
        public int TrailingStopPips { get; set; }

        protected override void OnStart()
        {
            Positions.Modified += OnPositionsModified;
            Positions.Opened += OnPositionsOpened;
            Positions.Closed += OnPositionsClosed;
            PendingOrders.Filled += OnPendingOrdersFilled;
        }

        protected override void OnTick()
        {
            Settpsl();
            if (IncludeBreakEven)
            {
                BreakEvenAdjustment();
            }
        }

        private void OnPositionsModified(PositionModifiedEventArgs args)
        {
            Settpsl();
        }

        private void OnPositionsOpened(PositionOpenedEventArgs args)
        {
            Settpsl();
        }

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            Settpsl();
        }

        private void OnPendingOrdersFilled(PendingOrderFilledEventArgs args)
        {
            Settpsl();
        }

private void Settpsl()
{
    foreach (var position in Positions)
    {
        var tp = position.TradeType == TradeType.Buy ? position.EntryPrice + TpPips * Symbol.PipSize : position.EntryPrice - TpPips * Symbol.PipSize;
        var sl = position.TradeType == TradeType.Buy ? position.EntryPrice - SlPips * Symbol.PipSize : position.EntryPrice + SlPips * Symbol.PipSize;

        // Only modify if the changes are significant (more than the symbol's pip size)
        if (TpOption && Math.Abs(position.TakeProfit ?? 0 - tp) > Symbol.PipSize)
        {
            ModifyPosition(position, position.StopLoss, tp);
        }
        if (SlOption && Math.Abs(position.StopLoss ?? 0 - sl) > Symbol.PipSize)
        {
            ModifyPosition(position, sl, position.TakeProfit);
        }
    }
}

private void BreakEvenAdjustment()
{
    var allPositions = Positions.FindAll(InstanceName, SymbolName);
    foreach (Position position in allPositions)
    {
        var entryPrice = position.EntryPrice;
        var distance = position.TradeType == TradeType.Buy ? Symbol.Bid - entryPrice : entryPrice - Symbol.Ask;
        var newSL = position.TradeType == TradeType.Buy ? entryPrice + BreakEvenExtraPips * Symbol.PipSize : entryPrice - BreakEvenExtraPips * Symbol.PipSize;

        // Ensure significant change before modifying
        if (distance >= BreakEvenPips * Symbol.PipSize && Math.Abs(position.StopLoss ?? 0 - newSL) > Symbol.PipSize)
        {
            if (IncludeTrail && position.Pips >= TrailingStopPips)
            {
                position.ModifyTrailingStop(true);
            }
            else
            {
                ModifyPosition(position, newSL, position.TakeProfit);
            }
            Print($"Stop Loss adjusted for {position.TradeType} position {position.Id} to {newSL}");
        }
    }
}

        protected override void OnStop()
        {
            // Clean-up logic here, if necessary
        }
    }
}


@Jonathangong12