繁体   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