Skip to content

Instantly share code, notes, and snippets.

@dragosthealex
Last active September 28, 2021 12:14
Show Gist options
  • Save dragosthealex/cb3bb24628d278f068b6811ac04329d9 to your computer and use it in GitHub Desktop.
Save dragosthealex/cb3bb24628d278f068b6811ac04329d9 to your computer and use it in GitHub Desktop.
Get historical daily data from NASDAQ by ticker
import requests
import json
import pandas as pd
import datetime
def get_from_nasdaq(ticker, date_from=None, date_to=None) -> pd.DataFrame:
"""Return historical daily data for ticker from NASDAQ
Args:
ticker (str): NASDAQ ticker
date_from (str, date-like): Start of period, YYYY-mm-dd. Default is today
date_to (str, date-like): End of period, YYYY-mm-dd. Default is date_from
Returns:
(pd.DataFrame): Dataframe with daily high, low, open, close,
volume and value
"""
if not ticker:
raise ValueError('You must include ticker')
if not date_from:
date_from = datetime.date.today().strftime('%Y-%m-%d')
if not date_to:
date_to = date_from
url = "https://api.nasdaq.com/api/quote/{}/chart".format(ticker.upper())
params = {
'assetclass': 'stocks',
'fromdate': date_from,
'todate': date_to
}
r = requests.get(url, params=params)
data = json.loads(r.content)['data']['chart']
data = [day['z'] for day in data]
data = pd.DataFrame(data)
data.set_index('dateTime', inplace=True)
data.index = pd.to_datetime(data.index).to_period('D')
data.index.names = ['date']
return data
if __name__ == '__main__':
print(get_from_nasdaq('aapl'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment