我正在使用 ibpy 向 InteractiveBrokers 中的 TWS 发送订单。我可以发送股票订单,例如 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()
上面的代码运行良好。我能够以 273 的价格在 SPY 中出价。
但是,我想购买 E-迷你期货 S&P 500 12 月合约。我做了以下定义合同:
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()
它没有通过,也没有返回错误消息。有没有人有这方面的经验?