我正在尝试访问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()
我得到了以下错误:
- 错误1 200找不到请求的安全定义**
我有期货合约的实时运行,我是否需要激活另一个授权?
如果这里有人能帮我解决这个问题,我将非常感激。
1条答案
按热度按时间pepwfjgg1#
解决方法是:
1.我不得不加上下面一行:合同最后交易日期或合同月份="202303"