Skip to content

Instantly share code, notes, and snippets.

@kperry2215
Created January 13, 2020 03:18
Show Gist options
  • Save kperry2215/8b77ab9c72c53523f74b23118d19722f to your computer and use it in GitHub Desktop.
Save kperry2215/8b77ab9c72c53523f74b23118d19722f to your computer and use it in GitHub Desktop.
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_daily_time_series_alpha_vantage(alpha_vantage_api_key, ticker_name, output_size = "compact"):
"""
Pull daily time series by stock ticker name.
Args:
alpha_vantage_api_key: Str. Alpha Vantage API key.
ticker_name: Str. Ticker name that we want to pull.
output_size: Str. Can be "full" or "compact". If "compact", then the past 100 days of data
is returned. If "full" the complete time series is returned (could be 20 years' worth of data!)
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')
data, meta_data = ts.get_daily_adjusted(ticker_name, outputsize = output_size)
data['date_time'] = data.index
return data, meta_data
#### EXECUTE IN MAIN FUNCTION ####
#Pull daily data for Berkshire Hathaway
ts_data, ts_metadata = pull_daily_time_series_alpha_vantage(alpha_vantage_api_key, ticker_name = "BRK.B", output_size = "compact")
#Plot the high prices
plot_data(df = ts_data,
x_variable = "date_time",
y_variable = "2. high",
title ="High Values, Berkshire Hathaway Stock, Daily Data")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment