A Gentle Introduction to Alpha Vantage API(With Python Code)

Joe Ng
2 min readOct 18, 2019

--

As we know, accurate data is vital for better understanding of the stock market, especially if you are a professional trader or Machine Learning Developer. While getting data from Google or Yahoo finance has become more cumbersome, a great solution has emerged — the AlphaVantage API.

Alpha Vantage provides real-time and historical (unto 20 years back) global equity, Forex and Cryptocurrencies data. Moreover, sector performances are also provided.

I applaud Github user RomelTorress’ efforts in building a great module for the extraction of the data from Alpha Alpha Vantage.In this article, we are implementing his work in Python to get the data.

You need to get your free API key from http://www.alphavantage.co/support/#api-key. Then,type this line below in your command prompt to install RomelTorress’ python wrapper:

pip install alpha_vantage

Using the AlphaVantage wrapper, we can build a Python class to request the desired data.In my case, I built a class with several functions to get the close price,MACD,Bollinger Bands,RSI and SMA 30 (30 day simple moving average)respectively. These data are just a small part of the numerous types of data we can actually get from AlphaVantage and there are a lot more explorations to be done by yourself.

from alpha_vantage.techindicators import TechIndicators
import matplotlib.pyplot as plt
from click._compat import raw_input
from alpha_vantage.timeseries import TimeSeries
import pandas as pd
class TechnicalIndicators:
def __init__(self):
self.api_key= 'KMOA5YH5JT2Z2A1B'
self.stock_name=self.question()
self.macd_data=self.macd()
self.rsi_data=self.rsi()
self.bbands_data=self.bbands()
self.close_data=self.close()
self.sma_data=self.sma()
def question(self):
stock_name=raw_input("Enter stock name:")
return stock_name
def macd(self):
a = TechIndicators(key=self.api_key, output_format='pandas')
data, meta_data=a.get_macd(symbol=self.stock_name,interval='daily')
return data
def rsi(self):
b=TechIndicators(key=self.api_key,output_format='pandas')
data,meta_data = b.get_rsi(symbol=self.stock_name,interval='daily',time_period=14)
return data
def bbands (self):
c=TechIndicators(key=self.api_key,output_format='pandas')
data,meta_data=c.get_bbands(symbol=self.stock_name)
return data
def sma(self):
d= TechIndicators(key=self.api_key, output_format='pandas')
data, meta_data = d.get_sma(symbol=self.stock_name,time_period=30)
return data
def close(self):
d=TimeSeries(key=self.api_key,output_format='pandas')
data,meta_data=d.get_daily(symbol=self.stock_name,outputsize='full')
return data

OK,I admit that the indentation was terrible.Anyway, these code are available on my Github Repository: https://github.com/joeng03/LSTM-Stock-Price-Prediction-Example in the AlphaVantageExample.py file.

The following code retrieves the MACD data for a stock and plots it using Matplotlib.Just type the symbol of the stock(example:TSLA) when the “Enter Stock name:” line appears.

if __name__ == "__main__":
TI=TechnicalIndicators()
macd_data = TI.macd_data
plt.plot(macd_data)
plt.show()

Feel free to leave your views or suggestions below as well as giving claps for this article.Thanks a lot!

Link to Romel’s Github :https://github.com/RomelTorres/alpha_vantage

--

--