├── requirements.txt ├── README.md ├── data ├── yahoo.py ├── iexcloud.py └── alpaca.py └── chart.py /requirements.txt: -------------------------------------------------------------------------------- 1 | plotly 2 | pandas 3 | requests 4 | yfinance -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Stock Historical Data Examples 2 | 3 | * IEXCloud 4 | * Yahoo 5 | * Alpaca / Polygon 6 | 7 | # Charting 8 | 9 | * Plotly Candlestick Chart 10 | -------------------------------------------------------------------------------- /data/yahoo.py: -------------------------------------------------------------------------------- 1 | import yfinance as yf 2 | 3 | work = yf.Ticker("WORK") 4 | 5 | # get historical market data 6 | history = work.history(period="max") 7 | 8 | # show pandas dataframe 9 | print(history) 10 | 11 | # show csv 12 | print(history.to_csv()) -------------------------------------------------------------------------------- /data/iexcloud.py: -------------------------------------------------------------------------------- 1 | import requests, json, csv 2 | 3 | TOKEN = "YOUR IEX CLOUD KEY" 4 | SYMBOL = "PTON" 5 | 6 | r = requests.get("https://sandbox.iexapis.com/stable/stock/{}/chart/max?token={}".format(SYMBOL, TOKEN)) 7 | 8 | items = json.loads(r.content) 9 | 10 | csv_file = open('stock.csv', 'w') 11 | csv_writer = csv.writer(csv_file) 12 | 13 | csv_writer.writerow(['date', 'open', 'high', 'low', 'close']) 14 | 15 | for item in items: 16 | csv_writer.writerow([item['date'], item['open'], item['high'], item['low'], item['close']]) 17 | 18 | csv_file.close() -------------------------------------------------------------------------------- /data/alpaca.py: -------------------------------------------------------------------------------- 1 | import requests, json, csv 2 | from datetime import datetime 3 | 4 | ALPACA_API_KEY = "YOUR APLACA KEY" 5 | TICKER = 'AAPL' 6 | START_DATE = '2019-01-01' 7 | END_DATE = '2019-12-27' 8 | POLYGON_URL = 'https://api.polygon.io/v2/aggs/ticker/{}/range/1/day/{}/{}?apiKey={}' 9 | 10 | r = requests.get(POLYGON_URL.format(TICKER, START_DATE, END_DATE, ALPACA_API_KEY)) 11 | 12 | data = json.loads(r.content) 13 | 14 | print("date,open,high,low,close") 15 | 16 | for item in data['results']: 17 | formatted_date = datetime.fromtimestamp(item['t'] / 1000) 18 | date_only = formatted_date.strftime('%Y-%m-%d') 19 | 20 | print("{},{},{},{},{}".format(date_only, item['o'], item['h'], item['l'], item['c'])) -------------------------------------------------------------------------------- /chart.py: -------------------------------------------------------------------------------- 1 | import plotly.graph_objects as go 2 | import pandas 3 | from datetime import datetime 4 | 5 | df = pandas.read_csv('aapl.csv') 6 | 7 | candlestick = go.Candlestick(x=df['date'], 8 | open=df['open'], 9 | high=df['high'], 10 | low=df['low'], 11 | close=df['close']) 12 | 13 | fig = go.Figure(data=[candlestick]) 14 | 15 | # ignore weekends 16 | fig.layout.xaxis.type = 'category' 17 | 18 | shapes = [ 19 | dict(x0='2019-01-02', x1='2019-01-02', y0=0, y1=1, xref='x', yref='paper'), 20 | dict(x0='2019-05-05', x1='2019-05-05', y0=0, y1=1, xref='x', yref='paper'), 21 | dict(x0='2019-07-30', x1='2019-07-30', y0=0, y1=1, xref='x', yref='paper'), 22 | dict(x0='2019-10-30', x1='2019-10-30', y0=0, y1=1, xref='x', yref='paper'), 23 | ] 24 | 25 | annotations=[ 26 | dict(x='2019-01-03', y=0.01, xref='x', yref='paper', showarrow=False, xanchor='left', text='Apple Cuts Guidance'), 27 | dict(x='2019-05-05', y=0.5, xref='x', yref='paper', showarrow=False, xanchor='left', text='Trump Tariff Tweet'), 28 | dict(x='2019-07-30', y=0.3, xref='x', yref='paper', showarrow=False, xanchor='left', text='Trump Tweets "China is doing very badly"'), 29 | dict(x='2019-10-30', y=0.3, xref='x', yref='paper', showarrow=False, xanchor='left', text='Apple Q4 Earnings'), 30 | ] 31 | 32 | fig.update_layout(title='AAPL', annotations=annotations, shapes=shapes) 33 | 34 | fig.show() 35 | 36 | fig.write_html('aapl.html', auto_open=False) 37 | --------------------------------------------------------------------------------