gpt4 book ai didi

python - ibpy 交互式经纪商的 python api 不适用于下订单

转载 作者:太空宇宙 更新时间:2023-11-03 15:46:39 25 4
gpt4 key购买 nike

我有以下示例代码,当我第一次尝试运行它时,它起作用了:

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的纸质交易的配置信息。然而,当我第二次、第三次运行它时,弹出信息再也没有出现,这让我很困惑。这里有什么问题吗?

最佳答案

正如已经提到的,应该是 if name == "ma​​in":

您正在做的是运行一个无限循环,连接到 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)

关于python - ibpy 交互式经纪商的 python api 不适用于下订单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41687535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com