Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unmanaged bracket orders and IB TWS work around

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

    Unmanaged bracket orders and IB TWS work around

    I've come across a baffling problem when submitting unmanaged OCA orders to TWS, and here is the work around to save you time, if you come across it too:

    The problem:

    When the strat submits the first OCA order to TWS, it works fine. All subsequent OCA orders are rejected by TWS, however, with the error message displayed in TWS as "Cancelled by system: OCA group already filled".

    Nothing gets written to the NT output window even when TraceOrders = true when the orders are cancelled. The strat works perefectly with a SIM account too.

    The cause:

    It finally dawned on me that I was using the same OCA string for every trade. TWS was then rejecting the orders as the OCA string was the same as the previous order which had already been filled.

    The solution:

    Give each OCA bracket order a unique string. I use CurrentBar, as long as the OCA trade is initiated in the same bar. It works perfectly.

    Code:
    if ( longOrder == null )
         longOrder        = SubmitOrder(0, OrderAction.Buy, OrderType.StopLimit, 1, High[1] + 2 * TickSize, High[1] + TickSize, "Entry" + CurrentBar.ToString(), "Long Limit");
    
    if ( shortOrder == null )
     shortOrder        = SubmitOrder(0, OrderAction.SellShort, OrderType.StopLimit, 1, Low[1] - 2 * TickSize, Low[1] - TickSize, "Entry" + CurrentBar.ToString(), "Short Limit");
    Chris
    Ninjascript Consultant

    #2
    Thanks for sharing your findings Chris.

    Comment


      #3
      Chris, as I was testing this scenario - you can check the IOrder native error returned in OnOrderUpdate() that would show you this error (which also would 'sit' in your log file / log tab).

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      47 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      23 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      33 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      50 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      42 views
      0 likes
      Last Post CarlTrading  
      Working...
      X