Skip to content

Instantly share code, notes, and snippets.

@audubon
Created September 24, 2015 21:27
Show Gist options
  • Save audubon/f9fd85e0cdc4ab19c857 to your computer and use it in GitHub Desktop.
Save audubon/f9fd85e0cdc4ab19c857 to your computer and use it in GitHub Desktop.
IB utli to create orders
class Utils(object):
months=['','F','G','H','J','K','M','N','Q','U','V','X','Z']
@staticmethod
def localsymbol (expiry,right,strike):
exp = Utils.months[1+ int(expiry)/100%100 ]
s= strike*1000
sym="ON%s2 %s%s" %(exp,right,str(s)[:-2])
return sym
@staticmethod
def makeOptContract(sym, exp, right, strike,mult=1000,**kwargs):
newOptContract = Contract()
newOptContract.m_symbol = sym
newOptContract.m_secType = 'FOP'
newOptContract.m_right = right
newOptContract.m_expiry = exp
newOptContract.m_strike = float(strike)
if kwargs.has_key("exchange"):
newOptContract.m_exchange = kwargs["exchange"]
else:
newOptContract.m_exchange = 'NYMEX'
newOptContract.m_currency = 'USD'
if kwargs.has_key("mult"):
newOptContract.m_multiplier = kwargs["mult"]
if kwargs.has_key("localsymbol"):
newOptContract.m_localSymbol = kwargs["localsymbol"] #'ONU2 C2700'
return newOptContract
@staticmethod
def makeContract(sym, exchange,sectype,exp=None):
newContract = Contract()
newContract.m_symbol = sym
newContract.m_secType = sectype
newContract.m_expiry = exp
newContract.m_exchange = exchange
newContract.m_currency = 'USD'
return newContract
@staticmethod
def makeOptOrder(action, oID, tif, orderType,price,qty):
newOptOrder = Order()
newOptOrder.m_orderId = oID
newOptOrder.m_clientId = 0
newOptOrder.m_permid = 0
newOptOrder.m_action = action
newOptOrder.m_lmtPrice = price
newOptOrder.m_auxPrice = 0
newOptOrder.m_tif = tif
newOptOrder.m_transmit = True
newOptOrder.m_orderType = orderType
#newOptOrder.m_goodTillDate= '20120702 08:27:22'
#newOptOrder.m_ocaType = 0
#newOptOrder.m_ocaGroup = "nernygrp"
newOptOrder.m_totalQuantity = qty
#newOptOrder.m_hidden= True
return newOptOrder
@staticmethod
def createBag( conidOne, conidTwo, symbol, exchange='NYMEX'):
leg1 = ComboLeg()
leg2 = ComboLeg()
addAllLegs = []
print "conid in bag %s" % (conidOne)
leg1.m_conId = conidOne
leg1.m_ratio = 1
leg1.m_action = "BUY"
leg1.m_exchange = exchange
leg1.m_openClose = 0
leg1.m_shortSaleSlot = 0
leg1.m_designatedLocation = ""
leg2.m_conId = conidTwo
leg2.m_ratio = 1
leg2.m_action = "SELL"
leg2.m_exchange = exchange
leg2.m_openClose = 0
leg2.m_shortSaleSlot = 0
leg2.m_designatedLocation = ""
addAllLegs.append(leg1)
addAllLegs.append(leg2)
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 = exchange
contract.m_currency = "USD"
contract.m_comboLegs = addAllLegs #including combo order in contract object
contract.m_symbol = symbol
return contract
@staticmethod
def createBracketOrder(origOrder, oID, tif, orderType,price):
#cc = copy.copy(origContract)
bracketoptOrder = Utils.makeOptOrder( Side.flipside(origOrder.m_action), oID, tif, orderType,price,origOrder.m_totalQuantity)
bracketoptOrder.m_parentId = origOrder.m_orderId
return bracketoptOrder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment