我正在使用ibpy将订单发送到InteractiveBrokers中的交易平台。我可以发送股票订单,例如SPY,但是我不能发送期货。这是我使用的代码,在线复制:
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()
上面的代码运行良好。我可以在SPY出价273。
但是,我想购买E-迷你期货S&P 500 Dec合约。我做了以下定义合同:
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()
它没有通过,也没有返回错误消息。有人在这方面有经验吗?
答案 0 :(得分:1)
查看源代码。
https://github.com/blampe/IbPy/blob/master/ib/ext/Contract.py
m_expiry = ""
因此,只需使用m_expiry = '201812'
它不使用新名称lastTradeDateOrContractMonth
。您标记了此python 2.7,但如果使用python 3,则可以使用IB中的python API,它将具有一些较新的功能。 https://www.interactivebrokers.com/en/index.php?f=5041。确实会使用新的字段名称(不包含m_样式)。
也不需要Contract.m_primaryExch = 'GLOBEX'
。它用于您指定要交换的SMART且不明确。例如。我认为对于您的SPY示例,您应该指定ARCA,但这也是不必要的,因为只有一个SPY股票(etf)。
答案 1 :(得分:0)
这是我用来建立期货合约的方法:
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
对于期货,我可以使用symbol
和exp
来解决,或者我可以设置symbol = None
并设置localsymbol
(例如PLJ9 =铂金2019年4月)。
我从不需要prim_exch
。