我正试图打印间谍的开盘价(因此勾起了14),类似于打印以美元计的账户余额。打印帐户余额之前,添加代码试图打印间谍价格。然而,我现在得到“错误508 322错误处理请求。-‘bW’:原因-重复的代码id”。我不明白为什么我的代码中没有一个重复的滴答id,并且我已经重新启动了TWS和PyCharm。
from ibapi.client import EClient # handles outgoing requests
from ibapi.wrapper import EWrapper # handles incoming messages
from ibapi.contract import Contract
import threading
import time
class IBapi(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
self.contract_details = {}
self.bardata = {} # initialise directory to store bar data
self.USD_cash_balance = 0
self.spyprice = 0
def tickPrice(self, reqId, tickType, price, attrib): # this function prints the price
if tickType == 2 and reqId == 1: # tickType 2 is ask price: lowest price offer on the contract
print(price)
if tickType == 14 and reqId == 508:
self.spyprice = price
def nextValidId(self, orderId: int):
super().nextValidId(orderId)
self.nextorderId = orderId
print('The next valid order id is: ', self.nextorderId)
def accountSummary(self, reqId: int, account: str, tag: str, value: str,
currency: str):
if tag == "CashBalance" and reqId == 131:
self.USD_cash_balance = value
def run_loop():
app.run() # starts communication with TWS
app = IBapi()
app.nextorderId = None
app.connect('127.0.0.1', 7497, 123)
# Start the socket in a thread
api_thread = threading.Thread(target=run_loop, daemon=True) # Algo doesn't have "daemon=True"
api_thread.start()
# Check if the API is connected via orderid
while True:
if isinstance(app.nextorderId,
int): # the IB API sends out the next available order ID as soon as connection is made
# The isinstance() function returns True if the specified object is of the specified type, otherwise False.
print('connected')
break
else:
print('waiting for connection')
time.sleep(2)
# getting strike prices
spystock_contract = Contract()
spystock_contract.symbol = "SPY"
spystock_contract.secType = "STK"
spystock_contract.exchange = "SMART"
spystock_contract.currency = "USD"
while not getattr(app, 'spyprice', None):
app.reqMktData(508, spystock_contract, "", False, False, [])
time.sleep(0.5)
print("The opening price of SPY is $" + str(app.spyprice))
while not getattr(app, 'USD_cash_balance', None):
app.reqAccountSummary(131, "All", "$LEDGER:USD")
time.sleep(0.5)
print("My account balance in USD is: " + str(app.USD_cash_balance))
time.sleep(3)
print("Disconnecting.")
app.disconnect()
发布于 2021-12-06 19:05:09
您只能调用这个app.reqMktData(508, spystock_contract, "", False, False, [])
一次,而不是每秒钟调用一次.5。将其放入nextVaildId
方法中,以便它只被调用一次,并且在连接之后调用它。
不需要睡眠,只需以异步的方式来做事情,就像API打算使用的那样。一种方法是在API调用nextValidId之后进行操作。
def nextValidId(self, orderId: int):
super().nextValidId(orderId)
self.nextorderId = orderId
print('The next valid order id is: ', self.nextorderId)
spystock_contract = Contract()
spystock_contract.symbol = "SPY"
spystock_contract.secType = "STK"
spystock_contract.exchange = "SMART"
spystock_contract.currency = "USD"
app.reqMktData(508, spystock_contract, "", False, False, [])
https://stackoverflow.com/questions/70240845
复制相似问题