0

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?

John Suski
  • 21
  • 3

2 Answers2

1

Look at the source code. https://github.com/blampe/IbPy/blob/master/ib/ext/Contract.py m_expiry = "" So just use m_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).

brian
  • 10,619
  • 4
  • 21
  • 79
0

This is what I use for building futures contracts:

def create_contract(symbol, sec_type, exch, curr, exp = None, mult = None, localsymbol=None):
    contract = Contract()
    contract.m_symbol = symbol
    contract.m_secType = sec_type
    contract.m_exchange = exch
    contract.m_currency = curr
    contract.m_expiry = exp
    contract.m_multiplier = mult
    contract.m_localSymbol = localsymbol
    return contract

For futures I can get by with symbol and exp OR I can set symbol = None and set localsymbol (e.g. PLJ9 = Platinum April 2019).

I've never needed prim_exch.

Erock618
  • 453
  • 4
  • 10