Overview
Getting access to financial time series data sets can be a hassle. Fortunately, there are a few options available on the internet for pulling financial time series data directly into Python for analysis. Even better, many of these options are free. In this tutorial, we will pull financial time series data into Python using the Alpha Vantage free API options.
With these API’s, we should be able to gain access to a vast majority of financial data sets, including daily and intraday stock price data.
Boston, Massachusetts-based Alpha Vantage is a leading provider of free API’s for historical and real-time stock data, physical currency data, and crypto-currency data.
Getting a free API key to access its data bank is simple. Go to this webpage, and fill out your contact information as directed:
Once you’re finished, Alpha Vantage will print an API key on its web page for your own personal use. You can use this key to pull data directly into Python for analysis.
Downloading Required Libraries
To illustrate, alpha Vantage has a Python library specifically for its API. Go to the command prompt and enter the following to download Alpha Vantage’s API package:
pip install alpha-vantage
Time Series Data
There are a couple of options for pulling time series data via Alpha Vantage’s API, depending on the level of data frequency that you want.
One of the methods we will cover is for intra day data. Where we want to pull a time series with a data frequency of 1 hour or less.
We use the following code to pull time series data for Google stock, with a data frequency of 15 minutes:
from alpha_vantage.timeseries import TimeSeries import pandas as pd import matplotlib.pyplot as plt alpha_vantage_api_key = "YOUR API KEY HERE" def pull_intraday_time_series_alpha_vantage(alpha_vantage_api_key,ticker_name, data_interval = '15min'):
Pull intraday time series data by stock ticker name.
Args:
alpha_vantage_api_key: Str. Alpha Vantage API key. ticker_name: Str. Ticker name that we want to pull. data_interval: String. Desired data interval for the data. Can be '1min', '5min', '15min', '30min', '60min'.
Outputs:
data: Dataframe. Time series data, including open, high, low, close, and datetime values.
metadata: Dataframe. Metadata associated with the time series.
Generate Alpha Vantage time series object
ts = TimeSeries(key = alpha_vantage_api_key, output_format = 'pandas') #Retrieve the data for the past sixty days (outputsize = full) data, meta_data = ts.get_intraday(ticker_name, outputsize = 'full', interval= data_interval) data['date_time'] = data.index return data, meta_data def plot_data(df, x_variable, y_variable, title):
Plot the x- and y- variables against each other, where the variables are columns in a pandas data frame.
Args:
df: Pandas dataframe, containing x_variable and y_variable columns.
x_variable: String. Name of x-variable column
y_variable: String. Name of y-variable column
title: String. Desired title name in the plot.
Outputs:
Plot in the console.
fig, ax = plt.subplots() ax.plot_date(df[x_variable], df[y_variable], marker='', linestyle='-', label=y_variable) fig.autofmt_xdate() plt.title(title) plt.show()
EXECUTE IN MAIN FUNCTION
ts_data,ts_metadata=pull_intraday_time_series_alpha_vantage(alpha_vantage_api_key, ticker_name = "GOOGL")
Plot the high prices
plot_data(df = ts_data, x_variable = "date_time", y_variable = "2. high", title ="High Values, Google Stock, 15 Minute Data")
Google Stock Price (High), 15 Minute Interval Data
Snapshot of the returned intraday Google stock time series data. Data includes open, high, low, close, and volume information.
We pull time series data using the pull_intraday_time_series_alpha_vantage() function. The function allows sampling frequencies of 1 minute, 5 minutes, 15 minutes, 30 minutes, and 60 minutes.
We pass our API key, stock ticker name (‘GOOGL’), and the desired sampling frequency in as parameters. The function returns a dataframe containing stock data (including open, high, low, close, and volume data) for the stock at a 15-minute data sampling frequency. As well as a metadata dataframe associated with the time series.
Read More: Learn how to import stock data into python using Quandl API.
Conclusions
In conclusion, the API’s covered in this article offer a wide range of financial data for your next time series analysis, free of charge (premium options are available if you’re looking for a specific data set). Access to high quality data is one of the biggest challenges in data science.
Luckily, API services like Alpha Vantage’s have made it easier than ever to obtain financial time series sets for data exploration and algorithm development.