Fetching Live Stock Market Data with Python and AlphaVantage

These days accurate data is most precious asset for financial market participants. Recently I was working with a not so old python code (written less than a year ago) that I saw it is not functioning. While I was tracing back the problem, I came across a line of code that was using Yahoo finance API.

Currently the landscape is changing fast in the finance industry’s real-time data. Everybody that has it, wants to monetize it in someway or another. While Google and Yahoo finance have depreciated their API, There are very few free real time data providers left.

Alpha Vantage is one of the data providers that allows its users to have unto 500 requests per day before going premium. It provides real-time and historical (unto 20 years back) global equity, Forex and Cryptocurrencies data. Moreover, sector performances are also provided.

This article will help you get up and running with Alpha Vantage using Python Panda and Matplotlib libraries.

Installing Libraries

For using Alpha Vantage database, we use alpha_vantage library.

pip install alpha_vantage

We need Pandas and Matplotlib libraries to go fully functional. You can install it as follows:

pip install pandas
pip install matplotlib

Fetching Data

As discussed, Alpha Vantage provides different market data. Namely, Global equity, FX, Cryptocurrency and sector data. The following sections show how to fetch each one.

Global Equity

from alpha_vantage.timeseries import TimeSeries
import matplotlib.pyplot as plt
ts = TimeSeries(key='YOUR_API_KEY',output_format='pandas')
data, meta_data = ts.get_intraday(symbol='TSE:TD',interval='1min', outputsize='full')
data['4. close'].plot()
plt.title('Intraday TimeSeries Google')

Different output formats are available. You can use each of the following lines according to your needs. In our example we use “pandas” to help in ploting.

ts = TimeSeries(key='YOUR_API_KEY') #JSON
ts = TimeSeries(key='YOUR_API_KEY',output_format='pandas') #pandas
ts = TimeSeries(key='YOUR_API_KEY',output_format='csv') #CSV

Technical Indicators

Currently about 52 indicators are available as a service of Alpha Vantage. The complete list can be seen in their API Documentation.

The following code fetches the Bollinger Bands for TD bank stock price.

from alpha_vantage.techindicators import TechIndicators
import matplotlib.pyplot as plt
ti = TechIndicators(key='YOUR_API_KEY',output_format='pandas')
data, meta_data = ti.get_bbands(symbol='TSE:TD',interval='1min', time_period=60)

Crypto Currencies

Alpha Vantage provides over 530 cryptocurrencies data (frankly, I didn’t know we have so many of them). Please take into account that Alpha Vantage no longer provides intraday data for Cryptocurrencies (they haven’t yet omitted the part of API dealing with intraday call).

from alpha_vantage.cryptocurrencies import CryptoCurrencies
import matplotlib.pyplot as plt
cc = CryptoCurrencies(key='YOUR_API_KEY',output_format='pandas')
data, meta_data = cc.get_digital_currency_daily(symbol='BTC', market='CAD')
data['1a. open (CAD)'].plot()
plt.title('daily value for bitcoin (BTC) in CAD')


Alpha Vantage supports almost all of currency pairs. The following code shows how to print and plot the CAD/USD close data.

from alpha_vantage.foreignexchange import ForeignExchange
import matplotlib.pyplot as plt
cc = ForeignExchange(key='YOUR_API_KEY',output_format='pandas')
data, meta_data = cc.get_currency_exchange_intraday(from_symbol='CAD',to_symbol='USD')
data['4. close'].plot()
plt.title('Intraday CAD/USD')

Sectors Data

Alpha Vantage provides sectors performances calculated from S&P500 incumbents. The following code gets the daily sector performance and shows it as a bar chart.

from alpha_vantage.sectorperformance import SectorPerformances
import matplotlib.pyplot as plt
sp = SectorPerformances(key='YOUR_API_KEY',output_format='pandas')
data, meta_data = sp.get_sector()
data['Rank B: Day Performance'].plot(kind='bar')
plt.title('Day Performance (%) per Sector')


Alpha Vantage is not the only free provider of market data. It also lacks some other data needed from capital markets like options and bonds data. If you have experience of using any other data point, feel free to share your experience.