I am using ibpy to send orders to TWS in InteractiveBrokers. I am able to send stock orders, such as SPY, but I am unable to send Futures. Here is the code I am using, copied online:
from ib.opt import Connection, message
from ib.ext.Contract import Contract
from ib.ext.Order import Order
def make_contract(symbol, sec_type, exch, prim_exch, curr):
Contract.m_symbol = symbol
Contract.m_secType = sec_type
Contract.m_exchange = exch
Contract.m_primaryExch = prim_exch
Contract.m_currency = curr
return Contract
def make_order(action,quantity, price = None):
if price is not None:
order = Order()
order.m_orderType = 'LMT'
order.m_totalQuantity = quantity
order.m_action = action
order.m_lmtPrice = price
else:
order = Order()
order.m_orderType = 'MKT'
order.m_totalQuantity = quantity
order.m_action = action
return order
orderId=300
conn = Connection.create(port=7496, clientId=999)
conn.connect()
cont = make_contract('SPY', 'STK', 'SMART', 'SMART', 'USD')
trade = make_order('BUY', 1, 273)
conn.placeOrder(orderId, cont, trade)
conn.disconnect()
The code above works well. I am able to place a bid in SPY at 273.
However, I want to buy E-mini Futures S&P 500 Dec Contract. I made the following to define the contract:
def make_fut():
Contract.m_symbol = 'ES'
Contract.m_secType = 'FUT'
Contract.m_exchange = 'GLOBEX'
Contract.m_primaryExch = 'GLOBEX'
Contract.m_currency = 'USD'
Contract.m_lastTradeDateOrContractMonth ='201812'
return Contract
cont = make_fut()
It did not go through and it did not get back error messages. Does any one have some experience in this?
Look at the source code. https://github.com/blampe/IbPy/blob/master/ib/ext/Contract.py
m_expiry = ""So just usem_expiry = '201812'It doesn't use the new name
lastTradeDateOrContractMonth. You tagged this python 2.7 but if you use python 3, you can use the python API from IB which will have some newer features. https://www.interactivebrokers.com/en/index.php?f=5041 . That does use the new field name (without the m_ styles).Also
Contract.m_primaryExch = 'GLOBEX'is unnecessary. It's for when you specify SMART for exchange and it is ambiguous. eg. I think for your SPY example you should specify ARCA, but it is also unnecessary since there's only one SPY stock(etf).