ibpy interactive broker 的 python api 无法下订单
ibpy interactive broker's python api not working for placing order
我有以下示例代码,当我第一次尝试 运行 它时,它起作用了:
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的界面弹出一个window,说是来自API的模拟交易的配置信息。但是第二次,第三次我 运行 它,弹出信息再也没有出现,这让我很困惑。这里有什么问题吗?
如前所述,应该是 if name == "main":
你在那里做的是 运行 连接到 IB API 的无限循环,下订单,断开连接并重复相同的过程。
弹出窗口可能是 API 警告之一,一旦被接受就不会再出现,这就是为什么您再也看不到它的原因。
您的订单很可能已经下达并且应该显示在交易平台中,除非它导致了您不会在交易平台中看到的错误。
正如其他人提到的,您首先需要做的是不要使用 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)
我有以下示例代码,当我第一次尝试 运行 它时,它起作用了:
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的界面弹出一个window,说是来自API的模拟交易的配置信息。但是第二次,第三次我 运行 它,弹出信息再也没有出现,这让我很困惑。这里有什么问题吗?
如前所述,应该是 if name == "main":
你在那里做的是 运行 连接到 IB API 的无限循环,下订单,断开连接并重复相同的过程。
弹出窗口可能是 API 警告之一,一旦被接受就不会再出现,这就是为什么您再也看不到它的原因。
您的订单很可能已经下达并且应该显示在交易平台中,除非它导致了您不会在交易平台中看到的错误。
正如其他人提到的,您首先需要做的是不要使用 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)