forked from SaminSarker05/HFT-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.py
48 lines (40 loc) · 1 KB
/
fetch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
sumarry
"""
import threading
import yfinance as yf
import time
import json
import os
SLEEP_TIME = 60
# download stock data using yfinance API
def make_api_call(ticker):
try:
data = yf.download(tickers=ticker, interval="1m", period="1d")
return data
except Exception as e:
print(f'error with API: {e}')
return None
# save in CSV format
def save_to_json(data, filename):
if data is not None:
data.to_csv(filename, index=True)
# fetch data every minue
def fetch_data(ticker):
while True:
data = make_api_call(ticker)
save_to_json(data, f'{ticker}.csv')
print(f'wrote to file for {ticker}')
time.sleep(SLEEP_TIME)
def main():
tickers = ['AAPL', 'MSFT']
threads = []
# create threads to concurrently grab data
for ticker in tickers:
thread = threading.Thread(target=fetch_data, args=(ticker,))
threads.append(thread)
thread.start()
for thread in threads:
thread.join() # wait for child threads before exiting main
if __name__ == "__main__":
main()