Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Reversing a position -> Order Rejected - OCO ID cannot be reused

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

    Reversing a position -> Order Rejected - OCO ID cannot be reused

    Hi there,

    I'm coding my strategy and when EMA line crosses the Avg line, I enter a position and if I already have a position, I reverse the trade.

    Code:
    if (Position.MarketPosition == MarketPosition.Flat) { // if I don't have a position
    EnterShort(1, @"Limit");
    EnterShort(1, @"EMA");
    } else
    if (Position.MarketPosition == MarketPosition.Long) { //If I'm long, reverse to short
    EnterShort(1, @"Limit");
    EnterShort(1, @"EMA");
    } else
    if (Position.MarketPosition == MarketPosition.Short) { //if I'm short, reverse to long
    EnterLong(1, @"Limit");
    EnterLong(1, @"EMA");
    }
    I have different names for the signals because they have different stop losses.

    ​Also have this code to set the StopLoss to breakeven when I'm 30 ticks away from the entry.

    Code:
    ......
    if (highestHighSinceEntry >= (Position.AveragePrice + (Breakeven * TickSize))) {
    ///// Move the stop loss to breakeven when the price reaches the breakeven level
    SetStopLoss(@"Limit", CalculationMode.Price, Position.AveragePrice, false);
    SetStopLoss(@"EMA", CalculationMode.Price, Position.AveragePrice-15, false);
    ......
    ​However, when the lines cross again without hitting the target or the stop loss, I try to reverse the position and I get the Order Rejected - OCO error.

    Strategy 'EMACrossOver/306509206' submitted an order that generated the following error 'Order rejected'. Strategy has sent cancel requests, attempted to close the position and terminated itself.
    Order '33eb07c172354cd5b65a0de9a8098443' can't be submitted: The OCO ID ‘4b3c4ad946c644eab5c69b00d815686c’ cannot be reused. Please use a new OCO ID. affected Order: BuyToCover 1 Limit @ 15192.75


    The Stop Loss is set first on the State=StateDataLoaded and the breakeven code above is set onBarUpdate.

    SetStopLoss(@"Limit", CalculationMode.Ticks, StopLoss, false);
    SetStopLoss(@"MACD", CalculationMode.Ticks, StopLoss, false);​

    I really don't know what is going on and how to fix it. I've been stuck with this situation for a few days already.

    It only happens when I move the Stop Loss to breakeven. They all have the same name, but if I use random names, I can't control them.
    If I never move the stop, the whole code works flawlessly.

    Please let me know if anyone has a suggestion.
    Thank you

    #2
    Hello chbruno,

    SetStopLoss() does not unset. Because it does not unset, once you have used CalculationMode.Price it will continue that price even for new orders.

    When using SetStopLoss() with CalculationMode.Price, it is important to reset this to CalculationMode.Ticks before calling a new entry order.

    Likely there is an additional error message you have overlooked that the stop was rejected due to an invalid price.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      That was a very quick fix. Thank you so much!

      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