PanagiotisCharalampous

Created at 19 Apr 2024, 07: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!
EM

EMILIO1.1

Joined 26.02.2020

PanagiotisCharalampous
19 Apr 2024, 07:00


saludos

This program opens two positions, one to buy and one to sell, and when the Stop Loss jumps, it opens two other positions, but the error is that it also opens two positions, when the Stake Stop is activated, it can be changed

 using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HedgingWithStopLossRobot : Robot
    {
        [Parameter("Volume (Lots)", DefaultValue = 0.1)]
        public double VolumeInLots { get; set; }

        [Parameter("Stop Loss (Pips)", DefaultValue = 20)]
        public int StopLossInPips { get; set; }

        [Parameter("Take Profit (Pips)", DefaultValue = 40)]
        public int TakeProfitInPips { get; set; }

        protected override void OnStart()
        {
            // Abrir una posición de compra
            ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInLots, "Buy", StopLossInPips, TakeProfitInPips);

            // Abrir una posición de venta
            ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInLots, "Sell", StopLossInPips, TakeProfitInPips);

            // Suscribirse al evento Positions.Closed
            Positions.Closed += OnPositionClosed;
        }

        private void OnPositionClosed(PositionClosedEventArgs args)
        {
            // Verificar si la posición cerrada tenía activado el stop-loss
            if (args.Position.StopLoss != null && args.Position.StopLoss.Value != 0)
            {
                // Abrir dos nuevas posiciones, una de compra y otra de venta
                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInLots, "Buy", StopLossInPips, TakeProfitInPips);
                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInLots, "Sell", StopLossInPips, TakeProfitInPips);
            }
        }
    }
}


@EMILIO1.1
Replies

PanagiotisCharalampous
19 Apr 2024, 13:38

Hi there,

You should use args.Reason to check if the position has been closed by a stop loss.

Best regards,

Panagiotis


@PanagiotisCharalampous

EMILIO1.1
19 Apr 2024, 19:29 ( Updated at: 20 Apr 2024, 05:44 )

RE: PanagiotisCharalampous

PanagiotisCharalampous said: 

Hi there,

You should use args.Reason to check if the position has been closed by a stop loss.

Best regards,

Panagiotis

thank you 


@EMILIO1.1