Spreads in the TWS API

View as MarkdownOpen in Claude

As with standard contracts, you will continue to define the standard values such as symbol, secType, exchange, and currency. However, with combo orders, the “ComboLegs” field must be added and the ComboLeg objects should be attached. The example provided will showcase more detail of behavior; however, these require a conid, exchange, ratio, and an action for every ComboLeg on the order.

Contract()

Symbol: String. Specify the symbol of the CFD.

SecType: String. Specify CFD.

Exchange: String. Specify SMART.

Currency: String. Specify the base currency the CFD is traded with.

ComboLegs: List of ComboLeg objects. Create a list to contain all subsequent legs.

ComboLeg()

ConId: int. The conId, or contract Identifier, should be used to specify the exact contract. Symbols or local symbols can not be used for ComboLegs.

Ratio: int. Specify the ratio for the specific leg. The ratio will be multiplied by the totalQuantity field for of the order object.

Action: String. Specify what to do with the order. Users may choose Buy or Sell for these orders.

Exchange: String. Specify the routing exchange for the contract. SMART may be used.

1contract = Contract()
2contract.symbol = "IBKR,MCD"
3contract.secType = "BAG"
4contract.currency = "USD"
5contract.exchange = "SMART"
6
7leg1 = ComboLeg()
8leg1.conId = 43645865#IBKR STK
9leg1.ratio = 1
10leg1.action = "BUY"
11leg1.exchange = "SMART"
12
13leg2 = ComboLeg()
14leg2.conId = 9408#MCD STK
15leg2.ratio = 1
16leg2.action = "SELL"
17leg2.exchange = "SMART"
18
19contract.comboLegs = []
20contract.comboLegs.append(leg1)
21contract.comboLegs.append(leg2)