我尝试使用以下代码尝试向 TWS 发送订单以放置在谷歌股票上。我不明白为什么它一直要求一个帐户,我打开了 TWS 并检查了启用 ActiveX 等。我还检查了套接字编号和客户端 ID 是否正确。
from ib.ext.Contract import Contract
from ib.ext.Order import Order
from ib.opt import Connection, message
import time
def error_handler(msg):
print "Server Error: %s" % msg
def reply_handler(msg):
print "Server Response: %s, %s" % (msg.typeName, msg)
def create_contract(symbol, sec_type, exch, prim_exch, curr):
contract = Contract()
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 create_order(order_type, quantity, action):
order = Order()
order.m_orderType = order_type
order.m_totalQuantity = quantity
order.m_action = action
return order
if __name__ == "__main__":
tws_conn = Connection.create(port=7496, clientId=100)
tws_conn.connect()
tws_conn.register(error_handler, 'Error')
tws_conn.registerAll(reply_handler)
order_id = 200
goog_contract = create_contract('GOOG', 'STK', 'SMART', 'SMART', 'USD')
goog_order = create_order('MKT', 5, 'BUY')
tws_conn.placeOrder(order_id, goog_contract, goog_order)
time.sleep(1)
tws_conn.disconnect()
我收到以下错误
Server Response: error, <error id=200, errorCode=321, errorMsg=Error validating request:-'ie' : cause - You must specify an account.
如果我在演示 IB 账户中运行代码,订单就会下达并执行,所以一切正常!但是当我在模拟交易账户中运行相同的代码时,就会收到上述错误消息。
有谁知道“指定一个帐户”,即在某处输入我的帐号?