Interactive Brokers管道Python断开连接失败

时间:2019-06-02 03:20:40

标签: python interactive-brokers

我正在使用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配置: enter image description here

我可以连接到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下订单?非常感谢。

1 个答案:

答案 0 :(得分:1)

您在程序结尾处调用断开连接。我猜这就是为什么它断开连接的原因;)

一些随意的想法。

  • IB提供了一个新的python API,因此,除非您想使用python 2.7,否则应使用较新的API,因为它具有更多功能,并且现在可能还会有更多用户。

  • 这不是程序的问题,但您可以调用connect,而不必等待以确保已连接。请注意,下一个有效ID是建立连接时发送的第一件事,您应该将此信号用作开始与TWS / Gateway进行交互的信号。通常,您实现nextValidId回调并在其中调用启动代码,例如placeAllOrders()之类的东西。

  • 大多数人在程序中放入sleep(),以等待其完成后再断开连接。这是一个坏主意,因为您永远不知道它什么时候完成。根据您的情况,假设您想在收到订单后断开连接。您可以实施orderStatus回调并在收到命令后断开连接。但是您可以在sleep(5)通话之前放置disconnect()或其他东西。

  • 我不知道该行的作用print(tws_conn.placeOrder(order_id, goog_contract, goog_order)) placeOrder不返回任何内容,在TWS收到订单后,它将发送一个orderStatus回调。

  • 说市场数据有效的“错误”并不是真正的错误,而只是信息。如果显示“连接断开”,则说明您没有从该特定服务器场获取数据。

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