IbPy无法发送订单 - 必须指定一个帐户

时间:2016-03-15 10:02:00

标签: python ibpy

我尝试使用以下代码尝试向TWS发送订单以放置在Google共享上。我不明白为什么它继续要求帐户,我打开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帐户中运行代码,订单将被放置并填充,所以一切正常!但是当我在纸质交易账户中运行相同的代码时,就是我收到上述错误消息。

有没有人知道“指定一个帐户”,即在某个地方输入我的帐号?

1 个答案:

答案 0 :(得分:2)

我现在有了工作代码,感谢Brian,请看上面对原始问题的评论。我想我会发布整个脚本以防其他人在尝试实现文章中的代码时遇到同样的问题&#34;使用PYTHON,IBPY和交互式经纪人API来自动化交易&#34;来自Quantstart的网站,btw是一个很棒的网站。

所以它只是通过在订单定义中添加额外的参数来命令给订单一个帐号。此参数允许在提交订单时指定帐号。在下面的脚本中,我没有提供我的真实账号,只是写了&#39; DUxxxxxx&#39;其中&#39; x是数字,对于我自己,这个帐号在TWS GUI的右上角清晰可见。

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, account):
    order = Order()
    order.m_orderType = order_type
    order.m_totalQuantity = quantity
    order.m_action = action
    order.m_account = account
    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', 'DUxxxxxx')

    tws_conn.placeOrder(order_id, goog_contract, goog_order)

    time.sleep(1)

    tws_conn.disconnect()

还有一点需要注意的是,对于某些人来说可能是ob ob,但首先不是我,因为order_id每次都要改变。每个订单都是唯一的,并且由order_id引用,因此如果使用上面的代码或添加的内容来增加它,则需要手动更改。

快乐交易!