简体   繁体   English

Interactive Brokers管道Python断开连接失败

[英]Interactive Brokers Broken pipe python connection fail

I am using VM, Ubuntu 16.04, Python 2.7.12. 我正在使用VM,Ubuntu 16.04,Python 2.7.12。 I learn an example from here . 我从这里学到一个例子。 I am using demo account and 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()

Here is my TWS configuration: 这是我的交易平台配置: 在此处输入图片说明

I can connect to the TWS since from the log I can see that: 我可以连接到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]

But the I cannot make an order, and I get errors from log: 但是我无法下订单,并且从日志中得到错误:

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 , what does it means? Broken pipe ,是什么意思? How can I fix it and make order through python API? 我该如何修复它并通过python API下订单? Thank you very much. 非常感谢你。

You call disconnect at the end of your program. 您在程序结束时调用断开连接。 I'm guessing that's why it gets disconnected;) 我猜这就是为什么它断开连接的原因;)

A few random thoughts. 一些随意的想法。

  • There is a new python API available from IB so unless you want to use python 2.7 you should use the newer API as it has more features and probably more users as well by now. IB提供了一个新的python API,因此,除非您想使用python 2.7,否则应使用较新的API,因为它具有更多功能,并且到目前为止可能还会有更多用户。

  • It's not a problem with your program but you call connect and don't wait to make sure you're connected. 这不是程序的问题,但是您可以调用connect,而不必等待以确保已连接。 Notice that next valid id is the first thing sent when the connection is established, you should use this as a signal to start your interaction with TWS/Gateway. 请注意,下一个有效ID是建立连接时发送的第一件事,您应该将此信号用作开始与TWS / Gateway进行交互的信号。 Usually you implement the nextValidId callback and put a call to your startup code there, like placeAllOrders() or something. 通常,您实现nextValidId回调,然后在其中对启动代码进行调用,例如placeAllOrders()或类似的东西。

  • Most people put a sleep() in their program to wait for it to finish before disconnecting. 大多数人在他们的程序中放置一个sleep()来等待它完成,然后再断开连接。 This is a bad idea since you never know when it's finished. 这是一个坏主意,因为您永远不知道它什么时候完成。 In your case, imagine you want to disconnect after the order is received. 根据您的情况,假设您想在收到订单后断开连接。 You can implement the orderStatus callback and disconnect after it says it got your order. 您可以实现orderStatus回调并在收到您的订单后断开连接。 But you can put a sleep(5) or something just before your disconnect() call for testing. 但是,您可以在disconnect()调用之前放置sleep(5)或其他东西进行测试。

  • I don't know what this line does print(tws_conn.placeOrder(order_id, goog_contract, goog_order)) placeOrder doesn't return anything, after TWS receives the order it will send an orderStatus callback. 我不知道此行会print(tws_conn.placeOrder(order_id, goog_contract, goog_order))什么内容print(tws_conn.placeOrder(order_id, goog_contract, goog_order)) placeOrder不返回任何内容,在TWS收到订单后,它将发送orderStatus回调。

  • The 'errors' saying that the market data is working aren't really errors, just information. 说市场数据有效的“错误”并不是真正的错误,而只是信息。 If it says 'connection is broken' then you know you're not getting data from that particular farm. 如果显示“连接断开”,则说明您没有从该特定服务器场获取数据。

  • It won't matter for goog but primary exchange is never SMART. 对于goog而言,这无关紧要,但是主交换绝不是SMART。 That's an actual exchange where the contract you're using primarily trades. 那是您主要使用的合约进行交易的实际交易所。 This is only used for disambiguation in case goog trades in USD in some other country. 这仅用于歧义,以防其他国家的goog交易美元。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM