how to setup a futures option vertical spread contract?

Discuss, post your questions about TWSLink here.

Users who just registered, please write an email to
forum@trade-commander.de
with your username mentioned.
So we can distinguish you from bots.
Thank you
Post Reply
vjsworld
Posts: 7
Joined: Thu Jun 07, 2018 2:56 pm

how to setup a futures option vertical spread contract?

Post by vjsworld »

Could you please show me how to register a simple futures vertical spread option contract for retrieving bid/ask/last, as well as placing orders on this spread-contract?

I am successfully registering the two separate FOP contracts, which I suppose need to be legs that are combined into one tradable instrument?

I am ultimately using Tradestation, but for now I am using the TWSLink Batch Testing tool, so please show me how to get it done with the batch tool and I can translate that into TS code.

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: how to setup a futures option vertical spread contract?

Post by board_admin »

Hello,
never did this before.

The general approach is: Pick the contract in TWS. Right click on contract, get information / description or details.
Use the information provided there for the register contract functions.

vjsworld
Posts: 7
Joined: Thu Jun 07, 2018 2:56 pm

Re: how to setup a futures option vertical spread contract?

Post by vjsworld »

I built the spread in TWS, and it looks like this:

https://www.dropbox.com/s/hanmpeb73sosd ... 1.PNG?dl=0

Just don't see how to get this symbol (combo) and/or contract details I need to register this contract

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: how to setup a futures option vertical spread contract?

Post by board_admin »

Honestly,
i never tested combo orders and also i am not familar with combo orders.

For details of TWSLink functions, use the help file please

file:///C:/Program%20Files%20(x86)/trade-commander/twslink2/Documentation/html/tbl_orderval.html

From what i see in the code, a combo order is created in multpiple steps.

0. Create an order with transmit=-1

1. Define a combo leg.
Use function ADD_COMBO_LEG.
You get an uid for this leg returned (you can use a leg in more than just one order)

2. Add the uid of the leg to the order which uses leg:
Use function SET_ORDER_VAL with attrib id 1013 to add leg

3. Add a leg prices to the order
Use function SET_ORDER_VAL with attrib id 77 to add price

4. Call last SET_ORDER_VAL transmit=1 to place the order

Please let me know, if it works

vjsworld
Posts: 7
Joined: Thu Jun 07, 2018 2:56 pm

Re: how to setup a futures option vertical spread contract?

Post by vjsworld »

Ok, I will try this and let you know

vjsworld
Posts: 7
Joined: Thu Jun 07, 2018 2:56 pm

Re: how to setup a futures option vertical spread contract?

Post by vjsworld »

I am able to successfully register the separate option contracts, and then I can also create legs using the ADD_COMBO_LEG function. But when trying to create the order, the first input is "contract id". We do not have a spread contract id yet, and I do not believe you put either of these leg's IDs in this input. I believe we need to create an actual spread contract for this spot, just as they have done here under "Options Spread":
https://interactivebrokers.github.io/tw ... racts.html

These spread contracts could be used to retrieve quotes and place orders.

Could you please add the input property "ComboLegs" to the "REGISTER_CONTRACT" ?

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: how to setup a futures option vertical spread contract?

Post by board_admin »

What do you mean by contract id. The ADD_COMBO_LEG expects the conid, not the contract uid.
The conid is the unique id provided by IB.
So for each leg, just provide its contract conid.

You get the contract id by right click on contract in TWS. Then search for info menu. Select details. A website should open
where you can find the conid.

I don't think you need a special spread contract. From what i see in the code, an order gets created with ONE contract.
Then, if there are legs associated to the order, the legs are added. Each leg has its own conid (contract, price, action),
short, what you can provide by ADD_COMBO_LEG

So use conid. Could you please check this out.

vjsworld
Posts: 7
Joined: Thu Jun 07, 2018 2:56 pm

Re: how to setup a futures option vertical spread contract?

Post by vjsworld »

Before I even get to the combo leg part, I need to be able to trade a single futures option contract. I can successfully register the contract like this:

Code: Select all

uidESopt1 = REGISTER_CONTRACT("ES","FOP","USD","GLOBEX","","20180927","P",2725,"50",0,0.0); 
which returns 50008 (success.)

But then when I try to place an untransmitted order using this, it returns a -3 (invalid contract):

Code: Select all

uidESoptOrder = PLACE_ORDER(uidESopt1,0, "BUY", "MKT",1,0.0,0.0,"DAY", -1,0);
I read your post that I must right click on the contract in TWS and retrieve the contract id from their contract details webpage, and use that and not the uid, so I then tried it like this using that number instead and it also returns a -3 (invalid contract.)

Code: Select all

uidESoptOrder = PLACE_ORDER(310738187,0, "BUY", "MKT",1,0.0,0.0,"DAY", -1,0);
I can transmit stock orders fine.....
For instance, when I register a stock like this:

Code: Select all

uidMSFT = REGISTER_CONTRACT("MSFT","STK","USD","SMART","","","",0.0,"",0,0.0);
And then create an untransmitted order like this, it works (returns 14, success.):

Code: Select all

uidOrderMSFT = PLACE_ORDER(uidMSFT,0, "BUY", "MKT",1,0.0,0.0,"DAY", -1,0);

Can you see why this single, untransmitted futures option order fails?

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: how to setup a futures option vertical spread contract?

Post by board_admin »

Hi,
i tested first part of your code and found the expiry is wrong.

change

Code: Select all

uidESopt1 = REGISTER_CONTRACT("ES","FOP","USD","GLOBEX","","20180927","P",2725,"50",0,0.0); 
to

Code: Select all

uidESopt1 = REGISTER_CONTRACT("ES","FOP","USD","GLOBEX","","20180921","P",2725,"50",0,0.0); 
or, if you can get conid, call

Code: Select all

REGISTER_CONTRACT3(289843850, "GLOBEX");
Also, recommended order of calls:

// register contracts before connect (only once per contract)
REGISTER_CONTRACT...
REGISTER_CONTRACT...
REGISTER_CONTRACT...
// connect
CONNECT... (only once per runtime, with long timeout, e.g. 20 seconds)
// do the trading

vjsworld
Posts: 7
Joined: Thu Jun 07, 2018 2:56 pm

Re: how to setup a futures option vertical spread contract?

Post by vjsworld »

Ok, I am registering all contracts correctly, pulling in market data on them, and can issue orders individually on them (futures options too. Yay!)... Making progress...

I am also able to register a combo leg.

I then create the order for the first leg, with a transmit = -1.

I then add the previously registered combo leg to this order with Set_Order_Val 1013.

I then add a price to this order with Set_Order_Val 77, and also set transmit to 1 right here. But the order does not transmit. IB shows nothing, but I am returned a 1 as though it worked. Here is the code:

Code: Select all

uidES = REGISTER_CONTRACT("ES","FUT","USD","GLOBEX","","201809","",0.0,"50",0,0.0); 
uidESopt1 = REGISTER_CONTRACT("ES","FOP","USD","GLOBEX","","20180702","P",2725,"50",0,0.0); 
uidESopt2 = REGISTER_CONTRACT("ES","FOP","USD","GLOBEX","","20180702","P",2730,"50",0,0.0); 

uidESopt2Leg = ADD_COMBO_LEG(0, uidESopt2, 1, 1, "SELL", "GLOBEX", 0, "", 0);
Print("uidESopt2Leg: ", uidESopt2Leg);
	
uidOrderEScombo = PLACE_ORDER(uidESopt1,0, "BUY", "LMT",1,1.0,0.0,"DAY", -1,0);
Print("uidOrderEScombo: ", uidOrderEScombo);
	
// Add combo leg to the order
tmp1 = SET_ORDER_VAL( uidOrderEScombo, 1013, NumToStr(uidESopt2Leg, 0), -1 );
Print("Leg Added: ", tmp1);
	
// Add combo price to the order and transmit
tmp2 = SET_ORDER_VAL( uidOrderEScombo, 77, "1.00", 1 );
Print("Set combo prc and transmit: ", tmp2);

Every step above gets returned >= 1 so they appear to be working, except the final combo order never gets placed in IB. (all of my other tests with individual ones were getting placed successfully.) It seems that the last Set_Order_Val is not transmitting even though I set transmit to one on that final step.

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: how to setup a futures option vertical spread contract?

Post by board_admin »

I don't think you can register a leg contract.
Only single contracts.

You create a leg by ADD combo leg. For this, just need a conid, not uid. I think this is still a problem in your code.

Add_COMBO_LEG returns a leg uid. This can be added to an order by SET_ORDER_VAL.

vjsworld
Posts: 7
Joined: Thu Jun 07, 2018 2:56 pm

Re: how to setup a futures option vertical spread contract?

Post by vjsworld »

Code: Select all

uidESopt1 = REGISTER_CONTRACT("ES","FOP","USD","GLOBEX","","20180702","P",2725,"50",0,0.0); 

uidESopt2Leg = ADD_COMBO_LEG(0, 301447454, 1, 1, "SELL", "GLOBEX", 0, "", 0);
Print("uidESopt2Leg: ", uidESopt2Leg);
	
uidOrderEScombo = PLACE_ORDER(uidESopt1,0, "BUY", "LMT",1,1.0,0.0,"DAY", -1,0);
Print("uidOrderEScombo: ", uidOrderEScombo);
	
// Add combo leg to the order
tmp1 = SET_ORDER_VAL( uidOrderEScombo, 1013, NumToStr(uidESopt2Leg, 0), -1 );
Print("Leg Added: ", tmp1);
	
// Add combo price to the order and transmit
tmp2 = SET_ORDER_VAL( uidOrderEScombo, 77, "1.00", 1 );
Print("Set combo prc and transmit: ", tmp2);
This does the same thing. Everything registers correctly, and tmp2 returns a one, but no order gets transmitted. Does it work on your end?

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: how to setup a futures option vertical spread contract?

Post by board_admin »

When i executed your code i get no order placed.
The logfiles show an error message:

Code: Select all

20180630 14:50:19:240 [IN ERROR]	uid=100	BUY	E1AN8 P2725	MKT	1	DAY	GLOBEX	lmt=0	aux=0	Invalid	acc_?	clientid=0	transmit=IB	ref=	nref=	oca=	filled=0	lastfill=0	fa group=	fa profile=	fa method=	fa percentage=	gat=	gtd=	outside rth=1	hidden=0	algo=	algo-param=	oid=1	permid=0	uidp=0	oidp=0	cid=50005	comment=Error validating request:-'bh' : cause - [b]The combo details for leg '0' are invalid. - CodeMsgPair::[m_code=10055m_msg=Only LMT or REL+LMT order allows using per-leg prices.]m_sysMsg=Only LMT or REL+LMT order allows using per-leg prices.[/b]]
I am not familar with combo orders. From what the message says, it seems that a price for leg is not accepted because your order is MKT.

How to open the logfile:
Right click on TWSLink tray icon. Then click on "Show logfile".

The logfiles are the best place to get information when something is not working as expected.

board_admin
Site Admin
Posts: 244
Joined: Mon Apr 24, 2017 3:24 pm

Re: how to setup a futures option vertical spread contract?

Post by board_admin »

When i remove the SET_ORDER_VAL for price and execute again, i get a new error:

Code: Select all

API-error: error validating request:-'bh' : cause - the combo details for leg '0' are invalid. -  conid, ratio, side: -1, 1, 2 code=321 id=1 
It points to wrong parameters provided by ADD_COMBO_LEG.

So far, i don't think this is a TWSLink problem. You just need to figure out what are the correct parameters.
Function ADD_COMBO_LEG and SET_ORDER_VAL should be sufficient to accomplish combo orders.

Please mind your own link to IBs manual:

Code: Select all

 ComboLeg leg1 = new ComboLeg();
            leg1.ConId = 43645865;//IBKR STK
            leg1.Ratio = 1;
            leg1.Action = "BUY";
            leg1.Exchange = "SMART";
Function ADD_COMBO_LEG is used to set those parameters.

Post Reply