I have the following trailing stop code in my strategy in which the stop loss does not move with the trailing stop and always remains static and I do not know what the error is in the code to solve it.
#region TRAILING STOP POR PUNTOS
if (EnableTrailingStopByPoints && Position.MarketPosition == MarketPosition.Long)
{
// Calcular el nuevo precio del stop loss
double newStopPrice = Close[0] - (TicksToAdjustTrailing * TickSize);
// Verificar si el precio ha avanzado lo suficiente para mover el stop loss
if (Close[0] >= entryPrice + (TicksToMoveTrailing * TickSize))
{
// Asegurarse de que el nuevo stop loss esté por debajo del precio de entrada y por encima del stop loss actual
if (newStopPrice > entryPrice && newStopPrice > stopLossPrice)
{
// Cancelar la orden de stop loss existente si existe
foreach (var order in Account.Orders)
{
if (order.Name == "myStop" && order.OrderState == OrderState.Working)
{
CancelOrder(order);
Print("Orden de stop loss cancelada: " + stopLossPrice);
break;
}
}
// Actualizar el stop loss
stopLossPrice = newStopPrice;
ExitLongStopMarket(0, true, Position.Quantity, stopLossPrice, "myStop", "longEntry");
// Imprimir un mensaje en el log para depuración
Print("Trailing stop movido a: " + stopLossPrice);
}
}
}
#endregion
THANK YOU!!!!

Comment