Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop-Loss Order Automatically Cancels After Entry Execution

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

    Stop-Loss Order Automatically Cancels After Entry Execution

    Hi,

    I am trying to place a stop-loss order (in this case, for a short position) by following the steps from the sample code "SampleOnOrderUpdate.zip". However, for some reason, after the short entry order is executed and the stop-loss order is placed, the stop-loss order is automatically closed after the close of the next bar, and I cannot figure out why this is happening.

    Code:
    [B]Variables[/B]
    private Order entryOrder = null;
    private Order stopOrder = null;
    ----------------------------------------------------------------------------------------------

    Code:
    else if [B](State == State.Realtime)[/B]
    {
    if (stopOrder != null)
    stopOrder = GetRealtimeOrder(stopOrder);
    
    if (entryOrder != null)
    entryOrder = GetRealtimeOrder(entryOrder);
    }
    ----------------------------------------------------------------------------------------------

    Code:
    [B]protected override void OnOrderUpdate[/B](Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
    if (order.OrderState == OrderState.Cancelled)
    {
    if (order == stopOrder)
    stopOrder = null;
    if (order == entryOrder)
    entryOrder = null;
    }
    }
    ----------------------------------------------------------------------------------------------​
    OnBarUpdate
    Code:
    if (Position.MarketPosition == MarketPosition.Flat
    && Trigger
    )
    {
    if (QTY1 > 0)
    entryOrder = EnterShort(Convert.ToInt32(QTY1), "ShortEntry");
    calculateDynamicSL = false;
    }​​​​
    ----------------------------------------------------------------------------------------------​

    Code:
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    if (execution.Order != null && execution.Order.OrderState == OrderState.Filled && Position.MarketPosition == MarketPosition.Short)
    {
    if (execution.Order.Name == "ShortEntry")
    {
    double stopLossPrice = execution.Order.AverageFillPrice + 40 * TickSize;
    
    if (stopOrder == null || stopOrder.OrderState == OrderState.Cancelled)
    {
    stopOrder = ExitShortStopMarket(Convert.ToInt32(QTY1), stopLossPrice, "SL", "ShortEntry");
    }
    }
    }
    }
    What am I doing wrong? because what I am looking for after that is move the stop loss to breakeven

    Thanks

    #2
    I can't see what could be wrong. Try putting a bunch of Print statements. I'd put them on everything, variables, to make sure they are being assigned properly. Doubles, for prices like your stopLossPrice. See if your prices are what they are supposed to be and aren't getting changed on bar update for whatever reason.

    Comment


      #3
      Hello,

      Orders submitted by a managed approach strategy are automatically cancelled when the submission bar closes if the order is not filled when isLiveUntilCancelled is false or the overload with the isLiveUntilCancelled parameter is not used.

      See isLiveUntilCancelled in the help guide.



      To keep limit and stop orders alive, continue re-submitting the order on each new bar for as long as you would like the order to remain alive. Save the limit or stop price to a double variable when the entry condition is true, and this can be used as the limit or stop price of the order on each resubmission.

      Below is a link to unlocked examples that demonstrate.



      Please let me know if this does not resolve your inquiry.

      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