我对tws API很陌生,我试图编写一个程序,根据股票的历史价格下订单。我尝试从 excel 文件中获取股票代码,然后在循环中调用 reqMktdata: class 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()
def main((:
app = TestApp()
app.nextOrderId=0
app.connect("127.0.0.1", 7497, 0)
Timer(3, app.stop).start()
app.run()
如果名称==">主要": 主((
但是,当我尝试打印历史数据时,它只打印第一个交易品种的历史数据并停止。谁能指出我在这里做错了什么,或者有人在循环中获取数据?谢谢。
您需要按合约执行此操作:
def start(self):
wb = Workbook.load_workbook('StockTickers.xlsx')
sheet_ranges = wb['Sheet1']
for cell in ['A3', 'A4', 'A5']:
contract = Contract()
contract.symbol = str(sheet_ranges[cell].value)
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
contract.primaryExchange = "NASDAQ"
self.reqHistoricalData(1, contract, "", "26 W", "1 day", "MIDPOINT", 0, 1, False, [])