简体   繁体   中英

Python - Ibpy return valid order id

My code:

from ib.opt import Connection
from ib.ext.Contract import Contract
from ib.ext.Order import Order
from time import sleep


def get_valid_order_id(msg):    
    global oid
    oid = msg.orderId


def error_handler(msg):    
    print ("Server Error:", 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(action, quantity):
    order = Order()
    order.m_orderType = 'MKT'
    order.m_totalQuantity = quantity
    order.m_action = action
    return order


oid = 0
cid = 100
port = 7498
conn = None

# connection
conn = Connection.create(port=port,clientId=cid)
conn.connect()

# register
conn.register(get_valid_order_id, 'NextValidId')
conn.register(error_handler, 'Error')

#order
contract = create_contract('TSLA','STK','SMART','SMART','USD')
order = create_order('buy', 1)

print(1)
conn.placeOrder(oid, contract, order)

1st Result: (order completed)

Server Version: 76
TWS Time at connection:20171101 02:07:03 CST
1Server Error:
 <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:usfuture>
Server Error: <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:cashfarm>
Server Error: <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:usfarm.us>
Server Error: <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:usfarm>
Server Error: <error id=-1, errorCode=2106, errorMsg=HMDS data farm connection is OK:ushmds>

If the last two code swapped:

conn.placeOrder(oid, contract, order)
print(1)

2nd Result: (order failed)

Server Version: 76
TWS Time at connection:20171101 02:11:20 CST
Server Error: 1<error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:usfuture>

Server Error: <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:cashfarm>
Server Error: <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:usfarm.us>
Server Error: <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:usfarm>
Server Error: <error id=-1, errorCode=2106, errorMsg=HMDS data farm connection is OK:ushmds>
Server Error: <error id=0, errorCode=10149, errorMsg=Invalid order id: 0>

Why so funny and how to do it properly? I just want to get a valid order id to order. I am not a very good programmer, i don't know how a listener is work. Please explain it as easy as possible. Thanks a lot!

Ibpy: https://github.com/blampe/IbPy

The answer is simple: In the first conn.placeOrder() call you used the initial oid=0 value. You can't use it again - the IB server assigned it to your order when it was accepted - so you got an error when you try to reuse it in the second attempt. It has nothing to do with the line swap.

By the way, it is a wonder that your first attempt was successful, because oid=0 is not always a valid order id. If you want to get a valid order id, you have to call conn.reqIds() and catch the answer in get_valid_order_id(msg) callback. The callback is prepared, but I don't see the reqIds call. You can call conn.placeOrder() only after the answer arrived and oid has the proper value.

Just a remark: your print(1) call was executed normally in the middle of the error processing. Observe the character '1' in the 3rd line: Server Error: 1...

According to your result, the failure of 2nd or later order placings would be solved by disconnecting and then reconnecting again, please add the below code into your existing.

# disconnect
conn.disconnect()  

For every time you are connecting to IB to retrieve data, place orders, get your own portfolio summary etc, you need to run while loop or other kinds of similar logic and keep reconnecting IB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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