In [11]:
import pandas as pd
#import pandas_datareader.data as pdr
from datetime import datetime, timedelta
import requests as re
import json
import urllib as ur
In [21]:
start=datetime.now()-timedelta(days =256)
end = datetime.now()
Historical Data¶
1. Quandl¶
In [5]:
pdr.DataReader("AAPL","quandl",start,end).head()
Out[5]:
2. ALPHA VANTAGE¶
The historical data here are all adjusted price
In [19]:
api = "R9KJYWUBMIDAXNW7"
function ="TIME_SERIES_DAILY_ADJUSTED" #daily
#function="TIME_SERIES_INTRADAY" #intraday
#function="TIME_SERIES_WEEKLY_ADJUSTED" #weekly
data_size = "compact"
ticker = "AAPL"
api_url = 'https://www.alphavantage.co/query?' + 'function={}&symbol={}&apikey={}&outputsize={}&datatype=csv'\
.format(function, ticker, api,data_size)
In this case, the API returns CSV file
In [20]:
pd.read_csv(api_url).head()
Out[20]:
3.Barchart OnDemand Database¶
In [22]:
api_key = '95b5894daf3abced33fe48e7f265315e'
#you need to phrase the date format
start=start.strftime("%Y%m%d%H%M%S")
end=end.strftime("%Y%m%d%H%M%S")
# This is the required format for datetimes to access the API
api_url = 'http://marketdata.websol.barchart.com/getHistory.csv?' + \
'key={}&symbol={}&type={}&startDate={}&endDate={}&interval={}'\
.format(api_key, "AAPL", "daily", start,end,1)
#list of available frequency: minutes, nearbyMinutes, formTMinutes, daily, dailyNearest, dailyContinue, weekly,
# weeklyNearest, weeklyContinue, monthly, monthlyNearest, monthlyContinue, quarterly,
# quarterlyNearest, quarterlyContinue, yearly, yearlyNearest, yearlyContinue
temp = pd.read_csv(api_url, parse_dates=['timestamp'])
temp.set_index('timestamp', inplace=True)
temp
Out[22]:
The only drawback for Alpha Vantage is that you can not set a date range for the data
Stock Quote¶
In [4]:
ticker_1="AAPL"
In [95]:
quote = "https://financialmodelingprep.com/api/company/price/{}".format(ticker_1)
In [108]:
result = re.get(quote).text
result=result.replace("\n","")
result = result.replace("<pre>","")
result= json.loads(result)[ticker_1]["price"]
In [110]:
result
Out[110]:
Stock Profile¶
In [92]:
profile="https://financialmodelingprep.com/api/financials/income-statement/{}".format(ticker_1)
In [112]:
result = re.get(profile).text
result=result.replace("\n","")
result = result.replace("<pre>","")
result= json.loads(result)
result = DataFrame(result["AAPL"])
result
Out[112]: