Last active
July 19, 2022 07:53
-
-
Save chadhumphrey/872a9d8d7e1619b974754fc1d9b6fd05 to your computer and use it in GitHub Desktop.
Interactive Brokers, python code for combo trades via the IB API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Reference https://interactivebrokers.github.io/tws-api/basic_orders.html#limitordercombolegprices | |
def build_combo_contract(info): | |
contract = Contract() | |
contract.symbol = 'ACN' | |
contract.secType = "BAG" | |
contract.currency = "USD" | |
contract.exchange = "BOX" | |
leg1 = ComboLeg() | |
leg1.conId = info['contractId_0'] | |
leg1.ratio = 1 | |
leg1.action = "BUY" | |
leg1.exchange = "BOX" | |
leg2 = ComboLeg() | |
leg2.conId = info['contractId_1'] | |
leg2.ratio = 1 | |
leg2.action = "SELL" | |
leg2.exchange = "BOX" | |
contract.comboLegs = [] | |
contract.comboLegs.append(leg1) | |
contract.comboLegs.append(leg2) | |
return contract | |
def make_order_block(data): | |
quantity = 1 | |
order = Order() | |
order.action = "BUY" | |
order.orderType = "LMT" | |
order.orderComboLegs = [] | |
order.totalQuantity = quantity | |
legPrices = [18.90,15.50] | |
for price in legPrices: | |
comboLeg = OrderComboLeg() | |
comboLeg.price = price | |
order.orderComboLegs.append(comboLeg) | |
order.smartComboRoutingParams = [] | |
order.smartComboRoutingParams.append(TagValue("NonGuaranteed", "1")) | |
comboContract=order_contract_library.build_combo_contract(combo) | |
blockCombo =order_contract_library.make_order_block(combo) | |
orderId=123 | |
self.placeOrder(orderId, comboContract,blockCombo) | |
Error code: | |
Error. Id: 121316 Code: 463 Msg: You must enter a valid price. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment