Created
January 26, 2021 14:09
-
-
Save aprsn/733ed5ac3b30b35717bda2fe6283a50e to your computer and use it in GitHub Desktop.
Collect historical stock prices and write in to Excel with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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