I have a strategy that opens a Long order with a take profit and a stop loss when it finds a green candle. After opening the OCO order, I am interested in being able to move the stop loss and take profit manually to be able to adjust them as the chart evolves, but I don't know how to do it (Ninja trader 8). Can you help me please?
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class GreenCandleStrategy : Strategy
{
// Variables de clase
private double entryPrice;
private double stopLossPrice;
[NinjaScriptProperty]
[Display(Name = "Enable Trailing Stop", Order = 1, GroupName = "Parameters")]
public bool EnableTrailingStop { get; set; } = false; // Input para habilitar/deshabilitar trailing stop
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "Open a position when the previous candle is green, with a take profit, stop loss, and optional trailing stop.";
Name = "GreenCandleStrategy";
Calculate = Calculate.OnEachTick; // Calcular en cada tick
EntriesPerDirection = 1;
EntryHandling = EntryHandling.UniqueEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsInstantiatedOnEachOptimizationIteration = false;
}
}
protected override void OnBarUpdate()
{
// Asegurarse de que hay suficientes velas cargadas
if (CurrentBar < 1)
return;
// Comprobar si ya hay una posición abierta
if (Position.MarketPosition != MarketPosition.Flat)
return;
// Comprobar si la vela anterior es verde (Close > Open)
if (Close[1] > Open[1])
{
// Entrar en posición larga
EnterLong();
// Guardar el precio de entrada
entryPrice = Close[0];
}
}
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
// Verificar que la orden se haya llenado
if (execution.Order.OrderState == OrderState.Filled && marketPosition == MarketPosition.Long)
{
// Calcular Take Profit y Stop Loss iniciales
double takeProfitPrice = entryPrice + 30 * TickSize;
stopLossPrice = entryPrice - 40 * TickSize;
// Configurar Take Profit y Stop Loss inicial
SetProfitTarget(CalculationMode.Price, takeProfitPrice);
SetStopLoss(CalculationMode.Price, stopLossPrice);
}
}
protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
{
// Solo ajustar el trailing stop si está habilitado
if (!EnableTrailingStop)
return;
// Verificar que hay una posición larga activa
if (Position.MarketPosition == MarketPosition.Long)
{
// Si el precio actual es al menos 3 ticks mayor al precio de entrada
if (marketDataUpdate.Price >= entryPrice + 20 * TickSize)
{
// Ajustar el stop loss al precio actual menos 3 ticks
double newStopLossPrice = marketDataUpdate.Price - 3 * TickSize;
// Asegurarse de que el nuevo stop loss es mayor que el stop loss actual
if (newStopLossPrice > stopLossPrice)
{
stopLossPrice = newStopLossPrice;
SetStopLoss(CalculationMode.Price, stopLossPrice);
}
}
}
}
}
}
THANK YOU BOYS!!!!!

Comment