API basics
Definition
An application programming interface is a way for two or more computer programs to communicate with each other. It can be useful to share large Datasets effectively.
Python example
You need to retrieve the Alpha Vantage API Key you received with your subscription to replace 'YOURAPIKEY'.
Import Python packages
import requests
import pandas as pd
import json
pd.set_option('display.max_columns', 500)
# Set your Alpha Vantage API Key
AV_API_Key='YOURAPIKEY'
Search a ticker by keyword
search_keyword='meta'
url = 'https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords={searchKeyword}&apikey={apiKey}'.format(apiKey=AV_API_Key, searchKeyword=search_keyword)
r_search = requests.get(url)
js_search = r_search.json()
df_search=pd.DataFrame(js_search)
display(df_search)
Add information based on GAFAM ticker
ticker_information=pd.DataFrame()
ticker_list=['GOOGL','AAPL', 'META', 'AMZN', 'MSFT']
for ticker in ticker_list :
r_overview = requests.get('https://www.alphavantage.co/query?function=OVERVIEW&symbol={ticker}&apikey={apiKey}'.format(apiKey=AV_API_Key, ticker=ticker))
js_overview=r_overview.json()
df_overview=pd.json_normalize(js_overview)
ticker_information=pd.concat([ticker_information, df_overview])
display(ticker_information.head())
Get GAFAM daily stock market
ticker_time_series=pd.DataFrame()
ticker_metadata=pd.DataFrame()
ticker_list=['GOOGL','AAPL', 'META', 'AMZN', 'MSFT']
for ticker in ticker_list :
r_stock = requests.get('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&outputsize=full&symbol={ticker}&apikey={apiKey}'.format(apiKey=AV_API_Key, ticker=ticker))
js_stock = r_stock.json()
# Ticker time series to DataFrame
df_time_series=pd.DataFrame(js_stock['Time Series (Daily)'])
df_time_series['Ticker']=ticker
ticker_time_series=pd.concat([ticker_time_series, df_time_series])
# Ticker metadata to DataFrame
df_metadata=pd.json_normalize(js_stock['Meta Data'])
df_metadata['Ticker']=ticker
ticker_metadata=pd.concat([ticker_metadata, df_metadata])
display(ticker_time_series.head())
display(ticker_metadata.head())
Reset index and melt DataFrame
ticker_time_series=ticker_time_series.reset_index().rename({'index':'Value_type'}, axis=1)
lg_ticker_ts = ticker_time_series.melt(id_vars=['Ticker','Value_type'])*
display(lg_ticker_ts.head())