我有以下示例代码,当我第一次尝试运行它时,它起作用了:
from ib.opt import Connection, message
from ib.ext.Contract import Contract
from ib.ext.Order import Order
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 = 100
while __name__ == "__main__":
conn = Connection.create(port=7496, clientId=999)
conn.connect()
oid = cid
cont = make_contract('AAPL', 'STK', 'SMART', 'SMART', 'USD')
offer = make_order('BUY', 1, 200)
conn.placeOrder(oid, cont, offer)
conn.disconnect()
x = raw_input('enter to resend')
cid += 1
当我第一次运行脚本时,IB的界面会弹出一个窗口并显示来自API的纸质交易的配置信息。然而,第二次,第三次我运行它,弹出信息再也不会出现让我感到困惑。这里有什么不对吗?
答案 0 :(得分:1)
如前所述,应该是名称 ==" 主要":
你正在做的是运行一个连接到IB API的无限循环,下订单,断开连接并重复相同的过程。
弹出窗口可能是API警告之一,一旦被接受,它就不会再出现,这就解释了为什么你再也看不到它了。
您的订单很可能放在TWS中,除非它导致您在TWS中看不到错误。
正如其他人提到你需要做的是首先不使用iPython笔记本,因为它不会让你很好地了解正在发生的事情。将代码更改为这样,您就能看到正在发生的事情:
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 = 100
def handleAll(msg):
print msg
if __name__ == "__main__":
conn = Connection.create(port=7496, clientId=999)
conn.connect()
conn.registerAll(handleAll)
oid = cid
cont = make_contract('AAPL', 'STK', 'SMART', 'SMART', 'USD')
offer = make_order('BUY', 1, 200)
conn.placeOrder(oid, cont, offer)
while 1:
time.sleep(1)