簡體   English   中英

無法從 API Interactive brokers 獲取歷史數據

[英]Unable to get the Historical data from API Interactive brokers

我正在嘗試訪問 API Interactive brokers 的歷史數據,但無法獲取數據。

我的代碼如下所示:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
import datetime


class TestApp(EClient, EWrapper):
    def __init__(self):
        EClient.__init__(self, self)

    def nextValidId(self, orderId: int):
        # Get the current year and month
        now = datetime.datetime.now()
        year = now.year
        month = now.month

        contract = Contract()
        contract.symbol = "ES"
        contract.secType = "FUT"
        contract.exchange = "GLOBEX"
        contract.currency = "USD"
        contract.localSymbol = "ESZ7"  # Set the local symbol

        self.reqHistoricalData(orderId, contract, "", "1 D", "1 hour", "TRADES", 0, 1, True, [])

    def historicalData(self, reqId, bar):
        print(f"Historical data: {bar}")

    def historicalDataEnd(self, reqId, start, end):
        print("End of HistoricalData")
        print(f"Start: {start}, End: {end}")


app = TestApp()
app.connect('127.0.0.1', 7497, 1)
app.run()

我收到以下錯誤:

ERROR 1 200 沒有找到請求的安全定義

我在期貨合約上有實時運行,我還需要激活另一個授權嗎?

如果這里有人可以幫助我解決問題,我將不勝感激。

解決辦法是:

  1. 我不得不將 contract.exchange = "GLOBEX" 更改為 contract.exchange = "CME"

  2. 我必須添加以下行:contract.lastTradeDateOrContractMonth = "202303"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM