0

私のコード:

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)

1 番目の結果: (注文完了)

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>

最後の 2 つのコードが入れ替わった場合:

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

2 番目の結果: (注文に失敗しました)

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

4

2 に答える 2