交互式代理断开管道python连接失败

交互式代理断开管道python连接失败,python,interactive-brokers,Python,Interactive Brokers,我正在使用VM、Ubuntu 16.04、Python 2.7.12。我从中学到了一个例子。我正在使用演示帐户和TWS 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""" prin

我正在使用VM、Ubuntu 16.04、Python 2.7.12。我从中学到了一个例子。我正在使用演示帐户和TWS

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配置:

我可以连接到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}.

管道破裂
,这是什么意思?如何通过pythonapi进行修复和订购?非常感谢。

您在节目结束时呼叫disconnect。我猜这就是它断开连接的原因;)

一些随意的想法

  • IB提供了一个新的pythonapi,因此除非您想使用python2.7,否则您应该使用更新的API,因为它现在有更多的功能,可能还有更多的用户

  • 您的程序没有问题,但您可以拨打connect,不必等待以确保已连接。请注意,下一个有效id是建立连接时发送的第一个内容,您应该使用它作为启动与TWS/Gateway交互的信号。通常,您会实现
    nextvalide
    回调,并在那里调用启动代码,比如
    placeAllOrders()
    之类的

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

  • 我不知道这行打印的是什么(tws连接placeOrder(订单id、合同、订单))
    placeOrder
    不返回任何内容,tws收到订单后会发送一个
    orderStatus
    回调

  • 说市场数据有效的“错误”并不是真正的错误,只是信息。如果它说“连接断开了”,那么您就知道您没有从特定的服务器场获取数据

  • 这对谷歌来说并不重要,但主交换从来都不是智能的。这是一个实际的交易,您使用的合同主要用于交易。这仅用于消除goog在其他国家以美元交易时的歧义


您是否尝试过使用主API客户端ID 1进行连接?我认为ID最多只能达到32。所以你的客户ID为100会被拒绝。非常感谢,我会尽快尝试possible@misantroop最多可以连接32个客户端,id可以是任意整数。非常感谢。问题是我过早地断开了连接。是的,你是对的,我应该保留连接,直到订单发出。