首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我要在IB API / TWS (Python)中获得"ERROR 508 322错误处理请求“。-‘bW’:原因-重复的ticker id”。

为什么我要在IB API / TWS (Python)中获得"ERROR 508 322错误处理请求“。-‘bW’:原因-重复的ticker id”。
EN

Stack Overflow用户
提问于 2021-12-06 04:20:29
回答 1查看 376关注 0票数 1

我正试图打印间谍的开盘价(因此勾起了14),类似于打印以美元计的账户余额。打印帐户余额之前,添加代码试图打印间谍价格。然而,我现在得到“错误508 322错误处理请求。-‘bW’:原因-重复的代码id”。我不明白为什么我的代码中没有一个重复的滴答id,并且我已经重新启动了TWS和PyCharm。

代码语言:javascript
运行
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-06 19:05:09

您只能调用这个app.reqMktData(508, spystock_contract, "", False, False, [])一次,而不是每秒钟调用一次.5。将其放入nextVaildId方法中,以便它只被调用一次,并且在连接之后调用它。

不需要睡眠,只需以异步的方式来做事情,就像API打算使用的那样。一种方法是在API调用nextValidId之后进行操作。

代码语言:javascript
运行
复制
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, [])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70240845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档