-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprices.py
79 lines (61 loc) · 2.16 KB
/
prices.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
from datetime import datetime, timedelta
import json
import os
import requests
import sys
SYMBOLS = ['VTSAX'] # add more symbols here
API_KEY = 'YOUR_API_KEY_HERE' # get your API key at https://api.tiingo.com
PRICES_FILE = '/path/to/prices.db'
def query_data(symbol):
# Pull price data
url = f'https://api.tiingo.com/tiingo/daily/{symbol}/prices'
payload = {'token': API_KEY}
r = requests.get(url, params=payload)
return r.json()
def output(symbol, prices):
data = query_data(symbol)
# Failure response is a string. Data response is an array.
if not isinstance(data, list):
print('no data found, exiting (probably got rate limited)...')
sys.exit(0)
data = data[0]
adjusted_close = data['adjClose']
date = data['date']
if not adjusted_close or not date:
print('data not found in response, exiting...')
sys.exit(0)
# Convert date string to date object.
date = datetime.strptime(date, '%Y-%m-%dT%H:%M:%S%z').date()
# Look through existing entries to make sure a price for this date doesn't
# already exist.
for price in prices:
if len(price) != 4:
continue
# Grab date and symbol from existing entry.
parsed_date = datetime.strptime(price[1], '%Y/%m/%d').date()
parsed_symbol = price[2]
if parsed_symbol == symbol and parsed_date == date:
print(f'existing price found for {symbol} on {date}: {price[3]} (${adjusted_close})')
return None
date = date.strftime('%Y/%m/%d')
output = f'P {date} {symbol} ${adjusted_close}\n'
return output
if __name__ == '__main__':
datetime = datetime.now()
# For logging purposes
print(datetime)
with open(PRICES_FILE, 'r') as f:
prices = f.readlines()
prices = [x.strip().split() for x in prices]
lines = []
for symbol in SYMBOLS:
line = output(symbol, prices)
if line:
lines += [line]
if len(lines) > 0:
with open(PRICES_FILE, 'a') as f:
f.write('\n')
for line in lines:
print(f'writing {line.strip()}')
f.write(line)