Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Manage manual orders after set OCO orders by Bot

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Manage manual orders after set OCO orders by Bot

    Hello,
    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!!!!!

    #2
    Quiza debas trabajar con unmanaged orders, es bastante complicado, quiza haya otro metodo que no conozco.

    Comment


      #3
      Hello,

      Thank you for your post.

      This would not be possible using Set methods. You would need to use Exit methods in order to manually modify orders submitted by the strategy.

      It’s possible to manually modify stops and targets submitted by a NinjaScript Strategy by using exit methods, such as ExitLongStopMarket(), with isLiveUntilCancelled set to true.

      ExitLongStopMarket(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double stopPrice, string signalName, string fromEntrySignal)

      Help Guide: NinjaScript > Language Reference > Strategy > Order Methods > Managed Approach > ExitLongStopMarket()

      Using isLiveUntilCancelled set to true allows the orders to be submitted once without being updated again from the script. This will allow you to manually modify the orders without them moving back to the original price from the strategy. We suggest submitting the orders from OnExecutionUpdate() when the entry order fills.

      ManuallyModifiableStopTargetExample_NT8 demonstrates placing an exit stop and limit order which can be manually modified.

      However note that using Exit methods would not be using OCO. If you wanted to use OCO, you could instead use the Unmanaged Approach with SubmitOrderUnmanaged().

      https://forum.ninjatrader.com/forum/...269#post802269
      Attached Files

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      58 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      133 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      73 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      45 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      50 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X