anyone know how to create a butterfly spread through IB's api (java or python)? I can create a 2 legged BAG. I can create a 3 legged BAG but not at different ratio sizes. here's what I have Code: class BagBuilder(object): def __init__(self): self.addAllLegs = [] def addLeg (self,cid,ratio,side,exchange): leg = ComboLeg() #print "conid in bag %s" % (cid) leg.m_conId = cid leg.m_ratio = ratio leg.m_action = side leg.m_exchange = exchange leg.m_openClose = 0 leg.m_shortSaleSlot = 0 leg.m_designatedLocation = "" self.addAllLegs.append(leg) def create(self,symbol): contract = Contract() contract.m_symbol = "USD" # For combo order use ?USD? as the symbol value all the time contract.m_secType = "BAG" # BAG is the security type for COMBO order contract.m_exchange = self.addAllLegs[0].m_exchange contract.m_currency = "USD" contract.m_comboLegs = self.addAllLegs #including combo order in contract object contract.m_symbol = symbol return contract bb = BagBuilder() bb.addLeg(113982825,1,"SELL",'ECBOT') bb.addLeg(113982910,1,"SELL",'ECBOT') bb.addLeg(113983022,2,"BUY",'ECBOT') bg = bb.create('ZN')
Here's some vba that does it - translating it to java should be pretty straightforward. There are four legs here that allow sending either an iron condor or butterfly (depending on arguments passed for conId2 and conId3 - the meat strikes). Code: Sub SubmitOrder(sym As String, exch As String, lmt As Double, qty As Long, conId1 As Long, conId2 As Long, conId3 As Long, conId4 As Long, ratio2 As Integer, ratio3 As Integer, ref As String) Dim comboLeg1 As TWSLib.IComboLeg Dim comboLeg2 As TWSLib.IComboLeg Dim comboLeg3 As TWSLib.IComboLeg Dim comboLeg4 As TWSLib.IComboLeg ' create contract structure Set objTWSControl.m_contractInfo = objTWSControl.m_TWSControl.createContract() ' create order structure Set objTWSControl.m_orderInfo = objTWSControl.m_TWSControl.createOrder() ' contract info With objTWSControl.m_contractInfo .symbol = sym .secType = "BAG" .exchange = exch .currency = "USD" End With ' order info With objTWSControl.m_orderInfo .action = "BUY" .totalQuantity = qty .orderType = "LMT" .lmtPrice = lmt .timeInForce = "DAY" .orderRef = ref .transmit = True End With ' create combo leg list objTWSControl.m_contractInfo.comboLegs = objTWSControl.m_TWSControl.createComboLegList() ' create order combo leg list 'objTWSControl.m_orderInfo.orderComboLegs = objTWSControl.m_TWSControl.createOrderComboLegList() If conId1 > 0 Then Set comboLeg1 = objTWSControl.m_contractInfo.comboLegs.Add() comboLeg1.conId = conId1 comboLeg1.ratio = 1 comboLeg1.action = "BUY" comboLeg1.exchange = exch End If If conId2 > 0 Then Set comboLeg2 = objTWSControl.m_contractInfo.comboLegs.Add() comboLeg2.conId = conId2 comboLeg2.ratio = ratio2 comboLeg2.action = "SELL" comboLeg2.exchange = exch End If If conId3 > 0 Then Set comboLeg3 = objTWSControl.m_contractInfo.comboLegs.Add() comboLeg3.conId = conId3 comboLeg3.ratio = ratio3 comboLeg3.action = "SELL" comboLeg3.exchange = exch End If If conId4 > 0 Then Set comboLeg4 = objTWSControl.m_contractInfo.comboLegs.Add() comboLeg4.conId = conId4 comboLeg4.ratio = 1 comboLeg4.action = "BUY" comboLeg4.exchange = exch End If orderId = objTWSControl.m_orderId objTWSControl.m_orderId = orderId + 1 'place order Call objTWSControl.m_TWSControl.placeOrderEx(orderId, objTWSControl.m_contractInfo, objTWSControl.m_orderInfo) End Sub