Retrieve daily bar data for AAPL from 2020-01-01 to 2020-04-30
import requests
import json
import time
import pandas as pd
import pytz
from datetime import datetime
# retrieve alpaca keys
%run alpaca_keys.py
ticker = 'AAPL'
multiplier = '1'
date_start = '2020-01-01'
date_end = '2020-04-30'
timespan = 'day'
request_string = (
f'https://api.polygon.io/v2/aggs/ticker/{ticker}'
f'/range/{multiplier}/{timespan}/{date_start}/{date_end}'
f'?apiKey={KEY_ID}'
)
r = requests.get(request_string)
df = pd.DataFrame(r.json()['results'])
est = pytz.timezone('US/Eastern')
utc = pytz.utc
# convert index time UTC to EST
df.index = [datetime.utcfromtimestamp(ts / 1000.).replace(tzinfo=utc).astimezone(est) for ts in df['t']]
df.index.name = 'Date'
df.head()