Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

IB OCO trade

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

    IB OCO trade

    Is it possible to issue a OCO trade to IB and let IB system to handle the OCO?

    I would like to submit a limited short and long order at the same time and use profit target and stop loss to generate OCO trade for IB. Below is the sample code. Is that correct and possible to do it this way. The result I want is if the limit long order fill then the limit short will be cancel and use the preset profit target and stop loss to guide the trade and vice versa for the limit short order.

    Also, if use IB handle the realtime OCO trade. Do I need to resubmit the limit order everytime when calling the OnBarUpdate().

    protectedoverridevoid Initialize()
    {
    SetProfitTarget(
    "", CalculationMode.Ticks, 50)
    SetStopLoss("", CalculationMode.Ticks, 50, false)
    CalculateOnBarClose = false;
    }

    protectedoverridevoid OnBarUpdate()
    If (condition==true)
    {
    EnterLongLimit(1,18100)
    EnterShortLimit(1,18000)
    }

    #2
    jesshon, the Set() orders would be OCO'ed per default and yes if enabled in your account connection settings for IB, they would offer native OCO handling.

    For the entry limit bracket, you would need to work in NT7's unmanaged mode and then use the SubmitOrder() method to place the two entry orders linked up via OCO.

    Comment


      #3
      So, after I submit the OCO order by SubmitOrder() to the IB. If it's not filled within a bar, is it going to be cancelled automatically?

      Thanks,
      Jess

      Comment


        #4
        No there would be no auto cancellation / expiration in this mode, it's all liveUntilCancellled.

        Comment


          #5
          I'm trying to use the SubmitOrder() as you recommended but I could found any information about how to set profit target by using the SubmitOrder() function. Since I can't use SetProfitTarget(), as that is for Managed order. So, anyway I can submit the order with profit target already?

          Thanks,
          Jess

          Comment


            #6
            There's no dedicated method for this task here, you do all your order submission via the SubmitOrder() method in the unmanaged mode : if you like to place a limit order as profit target for exiting then you would place this once in a position with the SubmitOrder() as well.

            Comment


              #7
              I see. So, for stop loss order. I will need to submit the "Stop Market" or "Stop Limit" order. Right?

              Also, how could I submit the "Target Profit" and "Stop Loss" order only the initial SubmitOrder() is filled.

              Thanks,
              Jess

              Comment


                #8
                Correct, for a stoploss that would be a StopMarket / StopLimit order.

                You could submit the OCO exit bracket when you receive a incoming fill confirmation in the strategy OnExecution() : http://www.ninjatrader.com/support/h...nexecution.htm

                That would be the earliest possible point to submit your exits.

                Comment


                  #9
                  Thanks for the guidance. You're so helpful and I'm one more step close to finish my script.

                  For the OCO exit bracket, do I have to link that to the original filled order? Also IB/NT doesn't allow me to reuse the OCO ID. It gives me the error "reason:OCA group is already filled". How do I create a pair of the OCO with different OCO ID each time?

                  Thanks,
                  Jess

                  Comment


                    #10
                    You're welcome Jess, no you would link the 2 exit orders together as OCO, as we would not support IB's atomic OCO orders, so basically submitting the entry order with the OCO exit orders in one ticket. For unique OCO orders try adding for example the CurrentBar to the string, or use a unique id postfix - http://www.ninjatrader.com/support/h...gyuniqueid.htm

                    Comment


                      #11
                      I use the below command to try to submit the order to IB but it doesn't do its job. If I remove the entryOrder1 and use SubmitOrder() command. It's ok. Do you know why?

                      private IOrder entryOrder1 = null;

                      entryOrder1 = SubmitOrder(
                      0,OrderAction.Buy, OrderType.StopLimit, 1, High(1),High(1),CurrentBar.ToString()+"strategy1","");

                      Comment


                        #12
                        Hi Jess, when / how do you nullify the object as needed to get it ready for a new trade then? Otherwise it would be 'stuck' in a so called terminal state : http://www.ninjatrader.com/support/f...ead.php?t=7499

                        Comment


                          #13
                          Hi Bertrand,

                          I think I found out the problem.

                          I tried to use the strategy for realtime trade. For some reasons, the strategy doesn't just use the live bars when I first run it. I use the print() to trace that. The OnBarUpdate() looked back the history bars when I tried to run it with the live data feed. Is there any way to avoid that?

                          Also, is there any function to get the last market price?

                          Thanks,
                          Jess

                          Comment


                            #14
                            Hi Jess, to run only on realtime bars, you could work with Historical :



                            You can get the 'last' with Close[0] if you run the code with CalculateOnBarClose = false.

                            Comment


                              #15
                              Hi Bertrand,

                              The historical function works. All the strange behavior because of the historical data. Now, I can continue to work on the strategy.

                              Thanks,
                              Jess

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              88 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              48 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              31 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              34 views
                              0 likes
                              Last Post TheRealMorford  
                              Started by Mindset, 02-28-2026, 06:16 AM
                              0 responses
                              69 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Working...
                              X