PanagiotisCharalampous
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);
}
}
}
}