簡體   English   中英

Python-Ibpy返回有效的訂單ID

[英]Python - Ibpy return valid order id

我的代碼:

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)

第一個結果:(訂單已完成)

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>

如果最后兩個代碼交換了:

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

第二結果:(訂購失敗)

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>

為什么這么有趣,如何正確地做呢? 我只想獲取要訂購的有效訂單ID。 我不是一個很好的程序員,我不知道監聽器是如何工作的。 請盡可能簡單地說明。 非常感謝!

伊比(Ibpy): https//github.com/blampe/IbPy

答案很簡單:在第一個conn.placeOrder()調用中,您使用了初始oid = 0值。 您無法再次使用它-IB服務器在接受訂單時將其分配給您的訂單-因此,當您嘗試在第二次嘗試中重新使用它時,會出現錯誤。 它與換線無關。

順便說一句,您的第一次嘗試成功是一個奇跡,因為oid = 0並不總是有效的訂單ID。 如果要獲取有效的訂單ID,則必須調用conn.reqIds()並在get_valid_order_id(msg)回調中捕獲答案。 回調已經准備好了,但是我看不到reqIds調用。 只有在答案到達並且oid具有正確的值之后,您才能調用conn.placeOrder()

請注意:您的print(1)調用在錯誤處理過程中正常執行。 觀察第三行中的字符“ 1”:服務器錯誤:1 ...

根據您的結果,可以通過斷開連接然后再次重新連接來解決第二次或以后的下單失敗,請在您現有的代碼中添加以下代碼。

# disconnect
conn.disconnect()  

每次您連接到IB來檢索數據,下訂單,獲取自己的投資組合摘要等時,都需要運行while loop或其他類似的邏輯,並保持重新連接IB。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM