I am trying to download historical data from the IBAPI, and since I am not that savvy in coding I came up with the plan to write seperate files for each stock that I want data of, and let 1 main file run all those files. The problem is that I can only request 1 stock every time, so after the first one has finished downloading I want the main file to run the next program.
I have tried multiple different things the past few days but I am unable to get it to work because for some reason Python does not kill the first script.
What I have tried so far:
import AAPL
import GOOG
import sys
import sleep
AAPL.main()
time.sleep(10)
sys.exit(AAPL)
GOOG.main()
And a few different variations with functions such as
terminate, kill()
the weird thing is that, when i try the first line of code i shared, but replace
AAPL.main() & GOOG.main()
with
print("Running") & print("terminate")
I do receive "terminte" as result, which is not the case with GOOG.main()
Could anyone help me with this issue?
EDIT: Code of AAPL.py, is same as GOOG.py:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
import sys
def print_to_file(*args):
with open('AAPL.txt', 'a') as fh:
fh.write(' '.join(map(str,args)))
fh.write('\n')
print = print_to_file
class TestApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
Layout = "{!s:1} {!s:2} {!s:3} {!s:4} {!s:5} {!s:6} {!s:7} {!s:8} {!s:8} '\n'"
print(Layout.format("Ticker;", "Date;", "None;", "Time;", "Open;", "High;", "Low;", "Close;", "Volume"))
def historicalData(self, reqId, bar):
print("AAPL", ";", bar.date.replace(' ', '; '), ";", bar.open, ";", bar.high, ";", bar.low, ";", bar.close, ";", bar.volume)
def main():
app = TestApp()
app.connect("127.0.0.1", 7497, 0)
contract = Contract ()
contract.symbol = "AAPL"
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
contract.primaryExchange = "NASDAQ"
app.reqHistoricalData(0, contract, "20180201 10:00:00", "1 D", "1 min", "TRADES", 0, 1, False, [])
app.run()
if __name__ == "__main__":
main()
Running this as a standalone will give me the required result for just this stock, but because it does not stop the function after a few seconds it does not work when I try to run multiple