aboutsummaryrefslogtreecommitdiff
path: root/main.py
blob: 5ddd8af4372f9b40a7eee03944978c0790601ab2 (plain)
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
48
49
50
51
import requests
import json
import datetime

"""
First pull data from yahoo api
"""
params = { #'period1' : '1753487940', 
           #'period2' : '1753725600', 
           'interval' : '1wk', # 1m, 2m, 5m, 15m, 30m, 60m, 90m, 1h, 4h, 1d, 5d, 1wk, 1mo, 3mo
           'range' : '5y', # "1d","5d","1mo","3mo","6mo","1y","2y","5y","10y","ytd","max"
           'events' : 'div|split|earn', 
           'includePrePost' : 'false' }
headers = {'User-agent' : 'fin-backtesting-proj'}
r = requests.get("https://query2.finance.yahoo.com/v8/finance/chart/SPY", headers=headers, params=params)

print(r.url)
print("status_code:\t", r.status_code)


# decode the JSON response data into a Python object
r.raise_for_status() # raises if error before parsing
data_obj = r.json()

timestamps = data_obj['chart']['result'][0]['timestamp']
close_prices = data_obj['chart']['result'][0]['indicators']['quote'][0]['close']
print('close_price len: ', len(close_prices), 'timestamps len: ', len(timestamps))

# clean out null's and 0s
for i in range(len(timestamps)):
    if close_prices[i] == None or close_prices[i] == 0:
        del close_prices[i]
        del timestamps[i]
        i -= 1

print('close_price len: ', len(close_prices), 'timestamps len: ', len(timestamps))

# save timestamps and close prices into separate files
timestamps_encoded = json.dumps(timestamps)
close_prices_encoded = json.dumps(close_prices)

timestamps_file = open('timestamps.json', 'w')
timestamps_file.truncate(0) # erase current content in file
timestamps_file.write(timestamps_encoded)
timestamps_file.close()

close_prices_file = open('close_prices.json', 'w')
close_prices_file.truncate(0)
close_prices_file.write(close_prices_encoded)
close_prices_file.close()