OCO Orders

Have a QScript to share, or need help with programming? Post your comment here.
Post Reply
sbank
Posts: 174
Joined: Tue Jul 21, 2015 9:35 pm
Contact:

OCO Orders

Post by sbank » Fri Mar 30, 2018 5:05 pm

Hi All,

Any trick to setting up OCO orders via QScript? I had this part of code:

Code: Select all

            ib_cancelall();
            mp = abs(ib_marketposition());
            ref1 = IB_PlaceOrder("buy",mp,low_offset,"limit","gtc","myoco");
            ref2 = IB_PlaceOrder("buy",mp,high_offset,"limit","gtc","myoco");  
            
(The offsets have a price that was calculated elsewhere)

When my script ran, it seemed to do nothing. My only thought was that the initial entry of the position was not done via the script, but via live off the chart. Perhaps "mp" was not set? So therefore ignored? Or do I need to do something more to tie these orders together?

(I also remember that there was discussion about OCO orders on this forum before, but I could not find anything via the search. So perhaps it was the old forum?)

User avatar
earik
Site Admin
Posts: 474
Joined: Mon Dec 01, 2014 12:41 pm
Contact:

Re: OCO Orders

Post by earik » Mon Apr 02, 2018 5:44 pm

Hi sbank,

That code looks OK to me. A couple things that might be throwing it off...

1) If you are flat, then mp is going to be 0, and nothing will happen.

2) Your ocotag ("myoco" in your case) needs to be unique for every set of orders you place. Otherwise, IB is going to get totally confused and might or might not get the orders placed. If an old "myoco" tag is recorded as placed, I'm not sure what they would do if you try and place another order with the same tag. Probably cancel it. I'd use a timestamp, or a combination of a timestamp and a barnumber here instead.

3) Orders don't have to all be entered via the script or on the chart. They should all work together. So if you're long already, and run this, you'll add to your position, even though you didn't originally get long using the script.

Hopefully that helps. Definitely check out point 2. That's probably what the issue is.

Earik

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests