Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Ensure new OCO ID is created before submittion

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

    Ensure new OCO ID is created before submittion

    Hello, is there any way to ensure a new OCO ID is created before submitting an order? I keep getting the error shown in the attached image. This has happened to me before with previous algorithms and I have always just created my own "simulated stops" in the code instead of using SetProfitTarget and SetStopLoss, however this strategy trades too frequently to create my own simulated stops in good practice. Any help is appreciated
    Attached Files

    #2
    Hello rbeckmann05,

    Thank you for your post.

    OCO (One-Cancels-Others) IDs need to be always unique for each grouping and cannot be re-used. When an OCO ID has been used with an order that is filled, rejected, or cancelled, any future orders using the same OCO ID will result in this error.

    This often occurs with SetStopLoss() and SetProfitTarget() as well as with Atm Strategies, should one of the exit orders be rejected. For example, the stop is submitted with OCO ID ‘12345’ at an invalid price and is rejected. The target is attempted to be submitted with the same OCO ID ‘12345’, however errors the OCO cannot be re-used as this was used with the stop order, which was rejected.

    NinjaTrader submits the stop, and then the target. The stop gets a rejection, and the OCO becomes invalid. The profit target then gets rejected with the error you noted because its OCO paired order is already filled/cancelled/rejected.

    To prevent the strategy from being deactivated, you can set the strategy's RealtimeErrorHandling property to IgnoreAllErrors or StopCancelCloseIgnoreRejects. With this property set, you can then trap order rejections in OnOrderUpdate if you would like to have the strategy attempt a different action. For example, you could call ExitLong() to exit the position, or you could use ExitLongStopMarket/ExitLongLimit to place other protective orders.

    RealtimeErrorHandling - https://ninjatrader.com/support/help...orhandling.htm

    OnOrderUpdate() - https://ninjatrader.com/support/help...rderupdate.htm


    This can also occur with the unmanaged approach, if OCO IDs are not unique. When using the unmanaged approach, create and assign unique OCO IDs, and ensure there are no other orders that have been filled, cancelled, or rejected that have used this ID. A simple way is to use the GetAtmStrategyUniqueId() method which will produce unique strings which are valid for OCO IDs.

    Help guide: NinjaScript > Language Reference > Strategy > ATM Strategy Methods > GetAtmStrategyUniqueId()

    Below is a link to an example strategy that creates unique OCO IDs and uses these for a bracket entry order.

    Developer example: UnmanagedOCOBracketExample_NT8

    Below is a link to an example strategy that uses unique OCO IDs for pairing an exit stop market order (stop loss) and limit order (profit target).

    Developer example: ProfitChaseStopTrailUnmanagedExample

    Please let us know if you have any questions.​

    Comment


      #3
      Exactly what I needed, thank you!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      66 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      141 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      76 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      47 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      51 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X