Skip to content

Instantly share code, notes, and snippets.

@aprsn
Created January 26, 2021 14:09
Show Gist options
  • Save aprsn/733ed5ac3b30b35717bda2fe6283a50e to your computer and use it in GitHub Desktop.
Save aprsn/733ed5ac3b30b35717bda2fe6283a50e to your computer and use it in GitHub Desktop.
Collect historical stock prices and write in to Excel with Python
import requests
import pandas
API_KEY = 'YOUR_API_KEY' # get your free API key from https://moon.finage.co.uk/register?subscribe=API00
symbol = input('Symbol: ')
start_date = input('Start date: ')
end_date = input('End date: ')
response = requests.get('https://api.finage.co.uk/agg/stock/'+ symbol +'/1/day/'+ start_date +'/'+ end_date +'?apikey=' + API_KEY)
print('Total Result(s): ' + str(response.json()['totalResults']))
json_list = []
for item in response.json()['results']:
json_element = {
'open': item['o'],
'high': item['h'],
'low': item['l'],
'close': item['c'],
'volume': item['v'],
'date': pandas.to_datetime(item['t'], unit='ms')
}
json_list.append(json_element)
df = pandas.DataFrame(json_list).to_excel(symbol+".xlsx", index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment