我对 tws API 很陌生,我正在尝试编写一个基于股票历史价格下订单的程序。我试图从一个 excel 文件中获取股票代码,然后在循环中调用 reqMktdata:类 TestApp(EClient,EWrapper):
def __init__(self):
EClient.__init__(self,self)
self.barsList=[]
def error(self, reqId, errorCode, errorString):
print(reqId,"error",errorString)
def nextValidId(self,reqId):
self.start()
def historicalData(self, reqId, bar):
print(bar)
def start(self):
contract = Contract()
wb = Workbook.load_workbook('StockTickers.xlsx')
sheet_ranges=wb['Sheet1']
empty_list=[]
empty_list.append(sheet_ranges['A1'].value)
empty_list.append(sheet_ranges['A2'].value)
empty_list.append(sheet_ranges['A3'].value)
for i in empty_list:
contract.symbol=str(i)
print(i)
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
contract.primaryExchange = "NASDAQ"
self.reqHistoricalData(1, contract, "", "26 W", "1 day", "MIDPOINT", 0, 1, False, [])
def stop(self):
self.done=True
self.disconnect()
定义主():
app = TestApp()
app.nextOrderId=0
app.connect("127.0.0.1", 7497, 0)
Timer(3, app.stop).start()
app.run()
如果名称==“主”:主()
但是,当我尝试打印历史数据时,它只打印第一个交易品种的历史数据并停止。谁能指出我在这里做错了什么,或者有人在循环中获取数据吗?非常感谢。