Reputation: 213
I am quite new to tws API and I was trying to write a program that places order base on the historical price of shares. I tried to acquire the stock tickers from an excel file and then call the reqMktdata in loop: 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()
if name=="main": main()
however, when I tried to print historical data, it only prints the historical data for the first symbol and stops. Can anyone point out what I did wrong here or have anyone worked with acquiring data in loops? Thank you very much.
Upvotes: 0
Views: 627
Reputation: 2563
You need to do it per contract:
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, [])
Upvotes: 0