Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time
#!/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
PRICES_FILE = '/path/to/prices.db'
def query_data(symbol):
# Pull price data
url = f'{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)...')
data = data[0]
adjusted_close = data['adjClose']
date = data['date']
if not adjusted_close or not date:
print('data not found in response, exiting...')
# 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:
# 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 =
# For logging purposes
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:
for line in lines:
print(f'writing {line.strip()}')