IB - Stop order for options

Posted By: Average

IB - Stop order for options - 01/15/21 10:59

Hey,
I am using IB and trying to send a stop order with my market order, but have had no success so far, is it not implemented for options or am I doing something wrong?
Options are even explicitly referenced in the remarks of "TradeMode" and "Stop", which is why I think it should be supported.


I followed the instructions in the IB / TWS Bridge (https://manual.zorro-project.com/ib.htm):
"Set TR_ENTRYSTOP for adding a stop order to a trade; this is for special purposes only since the stop order is not managed by Zorro either,..."

- TR_ENTRYSTOP: "Enter any trade with an additional stop order at distance Stop*StopFactor on NFA accounts, if supported by the broker API (see remarks). "
"So use this command in special cases only, for instance with option contracts or when Zorro does not control the trade."
(https://zorro-project.com/manual/en/trademode.htm)

- Stop: "For options the prices are always distances, f.i. Stop = 0.7 * contractPrice(ThisContract) sets a stop limit at 70% loss."
- StopFactor: "At 0 the stop is only controlled by software; at 1 it is only controlled by the broker."
(https://zorro-project.com/manual/en/stop.htm)

- SET_ORDERTYPE 2 - GTC (good-till-cancelled); order stays open until completely filled
+8 - STOP; add a stop order at distance Stop*StopFactor on NFA accounts (see remarks).
(https://zorro-project.com/manual/en/brokercommand.htm)


Code
setf(TradeMode,TR_ENTRYSTOP);			//also tried setf(TradeMode,TR_ENTRYSTOP|TR_GTC);
brokerCommand(SET_ORDERTYPE,8);			//with brokerCommand(SET_ORDERTYPE,2+8);

Lots = 1;
StopFactor = 1;
Stop = 0.1 * contractPrice(ThisContract);	//Stop limit at 10% loss

ThisTrade = enterLong(TMF_check_exit_conditions); 


I have also tried every combination and sequence I can think of, but I can't get anywhere.

The only thing that happens is that an MKT order is sent with 1Day expiration (no matter if GTC is selected or not, but I believe the GTC is more on Zorro's side and not meant for IB).
If I also add an OrderLimit, when I buy I get a message from Zorro that 0%@ContractAsk was bought, do I get the message because the purchase was not completed on IBs site and only the order was sent to IB? Because normally for the 0% the Lots*Multiplier should be displayed, so it says 100@ContractAsk.


PS
I have also tried to send an order using the brokerCommand(SET_ORDERTEXT ,"STP/"), but the problem is that I cannot send an additional stop value.
(http://interactivebrokers.github.io/tws-api/basic_orders.html)
Posted By: Petra

Re: IB - Stop order for options - 01/16/21 12:45

The TMF is probably not useful, you cannot check exit conditions of a stop order because its in fact 2 orders. The rest looks ok, but I dont know if stop orders work with options at all. Maybe IB support can tell that. 0% means that 0% of that order was filled, it was probably a GTC order.
Posted By: Average

Re: IB - Stop order for options - 01/18/21 15:16

IB does support stop orders for options. (https://www1.interactivebrokers.com/en/index.php?f=609)
Zorro seems to offer stop orders for options for at least one broker as well, otherwise it probably wouldn't be mentioned everywhere, the question is if Zorro also supports it for IB and I just can't get it to work.


Your comment regarding my TMF raised a few more questions though:

1. Assuming the stop order would work, how do I even manage this additional order with Zorro.
There is a brokerCommand "DO_CANCEL" in the new beta to close open orders, but I assume this only works for partial fills of the same TradeID, but here it should theoretically be a different TradeID, since it is a different trade in the opposite direction, or is it a bracket order with the same ID (https://interactivebrokers.github.io/tws-api/bracket_order.html)?

2. Do I also get open orders with GET_TRADES or only trades? So if I have multiple orders open for an asset (e.g. because of limit or stop orders), do I get the TradeID's and then how can I find out if they are trades yet or still orders? (maybe using Lot differences?)

3. If I bought an option via enterLong(TMF) and use a TMF on a tick basis, will the TMF be updated based on the ticks of the underlying (i.e. stock prices) or with new price information of the option?


Sorry for all my questions, working with Zorro regarding backtesting, optimizing etc. was a lot clearer and easier (probably because there was no BrokerAPI involved), but since I want to get my scripts running live as well, I still lack a bit of understanding how some things are implemented.
Posted By: Petra

Re: IB - Stop order for options - 01/19/21 10:52

DO_CANCEL cancels the open order, but I dont know if the IB API then also cancels the connected stop order. I have not yet had that case. Maybe IB support can tell that. GET_TRADES gets trades, not orders. I know the TMF will run on any incoming underlying price, even when the options price has not changed.
© 2024 lite-C Forums