Пожалуйста, добавьте if __name__ == "__main__":
, чтобы оформить заказ.Кстати, основной обмен на AAPL не SMART, вы можете оставить его пустым как ''
для большинства акций США.
from ib.opt import Connection, message
from ib.ext.Contract import Contract
from ib.ext.Order import Order
import time
def make_contract(symbol, sec_type, exch, prim_exch, curr):
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 make_order(action,quantity, price = None):
if price is not None:
order = Order()
order.m_orderType = 'LMT'
order.m_totalQuantity = quantity
order.m_action = action
order.m_lmtPrice = price
else:
order = Order()
order.m_orderType = 'MKT'
order.m_totalQuantity = quantity
order.m_action = action
return order
cid = 103
def handleAll(msg):
print(msg)
if __name__ == "__main__":
conn = Connection.create(port=4002, clientId=103)
conn.connect()
conn.registerAll(handleAll)
oid = cid
cont = make_contract('AAPL', 'STK', 'SMART', 'ISLAND', 'USD')
offer = make_order('BUY', 1, 200)
conn.placeOrder(oid, cont, offer)
time.sleep(1)
conn.disconnect()