Interactive Brokers Broken pipe python 连接失败

Interactive Brokers Broken pipe python connection fail

我正在使用 VM,Ubuntu 16.04,Python 2.7.12。我从 here 学习了一个例子。我正在使用模拟账户和交易平台。

from ib.ext.Contract import Contract
from ib.ext.Order import Order
from ib.opt import Connection, message

def error_handler(msg):
    """Handles the capturing of error messages"""
    print "Server Error: %s" % msg

def reply_handler(msg):
    """Handles of server replies"""
    print "Server Response: %s, %s" % (msg.typeName, msg)

def create_contract(symbol, sec_type, exch, prim_exch, curr):
    """Create a Contract object defining what will
    be purchased, at which exchange and in which currency.

    symbol - The ticker symbol for the contract
    sec_type - The security type for the contract ('STK' is 'stock')
    exch - The exchange to carry out the contract on
    prim_exch - The primary exchange to carry out the contract on
    curr - The currency in which to purchase the contract"""
    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):
    """Create an Order object (Market/Limit) to go long/short.

    order_type - 'MKT', 'LMT' for Market or Limit orders
    quantity - Integral number of assets to order
    action - 'BUY' or 'SELL'"""
    order = Order()
    order.m_orderType = order_type
    order.m_totalQuantity = quantity
    order.m_action = action
    return order

if __name__ == "__main__":
    # Connect to the Trader Workstation (TWS) running on the
    # usual port of 7496, with a clientId of 100
    # (The clientId is chosen by us and we will need
    # separate IDs for both the execution connection and
    # market data connection)
    tws_conn = Connection.create("127.0.0.1", port=7496, clientId=100)
    tws_conn.connect()

    # Assign the error handling function defined above
    # to the TWS connection
    tws_conn.register(error_handler, 'Error')

    # Assign all of the server reply messages to the
    # reply_handler function defined above
    tws_conn.registerAll(reply_handler)

    # Create an order ID which is 'global' for this session. This
    # will need incrementing once new orders are submitted.
    order_id = 1

    # Create a contract in GOOG stock via SMART order routing
    goog_contract = create_contract('GOOG', 'STK', 'SMART', 'SMART', 'USD')

    # Go long 100 shares of Google
    goog_order = create_order('MKT', 100, 'BUY')

    # Use the connection to the send the order to IB
    print(tws_conn.placeOrder(order_id, goog_contract, goog_order))

    # Disconnect from TWS
    tws_conn.disconnect()

这是我的交易平台配置:

我可以连接到 TWS,因为从日志中我可以看到:

2019-06-02 10:57:03.974 [GS] INFO  [JTS-EServerSocket-153] - [0:62:76:1:0:0:0:SYS] Server version is 76
2019-06-02 10:57:03.974 [GS] INFO  [JTS-EServerSocket-153] - [0:62:76:1:0:0:0:SYS] Client version is 62
2019-06-02 10:57:03.974 [GS] INFO  [JTS-EServerSocket-153] - [0:62:76:1:0:0:0:SYS] is 3rdParty false
2019-06-02 10:57:03.974 [GS] INFO  [JTS-EServerSocketNotifier-154] - Starting async queue thread
2019-06-02 10:57:03.977 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:0:0:SYS] Starting new conversation with client{100} at 127.0.0.1
2019-06-02 10:57:03.977 [GS] INFO  [AWT-EventQueue-0] - MDConnectionsModel: Updated [127.0.0.1:34076 CLIENT ACCEPTED 100]

但是我不能下订单,而且我从日志中得到错误:

2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:9:1:INFO] Sending next valid order id.
2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:9:1:DET] [9;1;1]
2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:4:2:DET] Sending error.
2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:4:2:DET] [4;2;-1;2104;Market data farm connection is OK:usfarm.nj]
2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:4:2:DET] Error sent.
2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:4:2:DET] Sending error.
2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:4:2:DET] [4;2;-1;2104;Market data farm connection is OK:cashfarm]
2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:4:2:DET] Error sent.
2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:4:2:DET] Sending error.
2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:4:2:DET] [4;2;-1;2104;Market data farm connection is OK:usfarm]
2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:4:2:DET] Error sent.
2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:4:2:DET] Sending error.
2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:4:2:DET] [4;2;-1;2106;HMDS data farm connection is OK:hkhmds]
2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:4:2:DET] Error sent.
2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:4:2:DET] Sending error.
2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:4:2:DET] [4;2;-1;2106;HMDS data farm connection is OK:ushmds]
2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:4:2:DET] Error sent.
2019-06-02 10:57:03.978 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:0:0:INFO] Start processing incoming messages for client {100}.
2019-06-02 10:57:03.980 [GS] INFO  [JTS-EServerSocket-153] - [100:62:76:1:0:0:0:INFO] Handling incoming PlaceOrder(3) message.
2019-06-02 10:57:03.982 [GS] INFO  [JTS-EWriter2-155] - Broken pipe (Write failed)
2019-06-02 10:57:03.982 [GS] INFO  [JTS-EWriter2-155] - [100:62:76:1:0:0:0:ERR] Unable write to socket client{100} - 
2019-06-02 10:57:03.982 [GS] INFO  [JTS-EWriter2-155] - Broken pipe (Write failed)
2019-06-02 10:57:03.982 [GS] INFO  [JTS-EWriter2-155] - [100:62:76:1:0:0:0:INFO] Close call made for client{100} socket connection.
2019-06-02 10:57:04.000 [GS] INFO  [JTS-EWriter2-155] - Cleaning up [serverId: 100]...
2019-06-02 10:57:04.000 [GS] INFO  [JTS-EWriter2-155] - Cleaning up [serverId: 100]...
2019-06-02 10:57:04.000 [GS] INFO  [JTS-EWriter2-155] - Cleaning up [serverId: 100]...
2019-06-02 10:57:04.000 [GS] INFO  [JTS-EWriter2-155] - [100:62:76:1:0:0:0:DET] closePrim called.  Stopping all mkt data and HMDS requests for client{100}.

Broken pipe,什么意思?我该如何修复它并通过 python API 下订单?非常感谢。

您在程序结束时调用断开连接。我猜这就是它断开连接的原因;)

一些随意的想法。

  • IB 提供了一个新的 python API,所以除非您想使用 python 2.7,否则您应该使用较新的 API 作为它有更多的功能,现在可能还有更多的用户。

  • 这不是您的程序的问题,但您调用了 connect 并且不要等待以确保您已连接。请注意,下一个有效 ID 是建立连接时发送的第一件事,您应该将其用作开始与 TWS/Gateway 交互的信号。通常你实现 nextValidId 回调并在那里调用你的启动代码,比如 placeAllOrders() 或其他东西。

  • 大多数人在他们的程序中放置一个 sleep() 以等待它完成后再断开连接。这是个坏主意,因为你永远不知道它什么时候完成。在您的情况下,假设您想在收到订单后断开连接。您可以实现 orderStatus 回调并在它表示收到您的订单后断开连接。但是您可以在 disconnect() 调用测试之前放置一个 sleep(5) 或其他内容。

  • 我不知道这条线是干什么的print(tws_conn.placeOrder(order_id, goog_contract, goog_order)) placeOrder 没有return任何东西,在TWS收到订单后它会发送一个orderStatus回调.

  • 'errors' 说市场数据有效并不是真正的错误,只是信息。如果显示 'connection is broken',那么您就知道您没有从该特定农场获取数据。

  • 这对 goog 来说无关紧要,但主要交换从来都不是 SMART。这是您使用的合约主要进行交易的实际交易所。这仅用于消除歧义,以防 goog 在其他国家/地区以美元进行交易。