Python module to get stock data from the Alpha Vantage API
Alpha Vantage delivers a free API for real time financial data and most used finance indicators in a simple json or pandas format. This module implements a python interface to the free API provided by Alpha Vantage (http://www.alphavantage.co/). It requires a free API, that can be requested on http://www.alphavantage.co/support/#api-key. You can have a look at all the api calls available in their documentation http://www.alphavantage.co/documentation
To install the package use:
pip install alpha_vantage
If you want to install from source, then use:
git clone https://github.com/RomelTorres/alpha_vantage.git
pip install -e alpha_vantage
To get data in a python, simply import the library and call the object with your api key and get ready for some awesome free realtime finance data.
from alpha_vantage.timeseries import TimeSeries
ts = TimeSeries(key='YOUR_API_KEY')
# Get json object with the intraday data and another with the call's metadata
data, meta_data = ts.get_intraday('GOOGL')
Internally there is a retries counter, that can be used to minimize connection errors (in case that the api is not able to respond in time), the default is set to 5 but can be increased or decreased whenever needed.
ts = TimeSeries(key='YOUR_API_KEY',retries='YOUR_RETRIES')
Finally the library supports giving its results as json dictionaries (default) or as pandas dataframe, simply pass the parameter output_format='pandas' to change the format of the output for all the api calls.
ts = TimeSeries(key='YOUR_API_KEY',output_format='pandas')
Using pandas support we can plot the intra-minute value for 'MSFT' stock quite easily:
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='MSFT',interval='1min', outputsize='full')
data['close'].plot()
plt.title('Intraday Times Series for the MSFT stock (1 min)')
plt.show()
The same way we can get pandas to plot technical indicators like Bolliger Bands®
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='MSFT', interval='60min', time_period=60)
data.plot()
plt.title('BBbands indicator for MSFT stock (60 min)')
plt.show()
We can also plot sector performance just as easy:
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 A: Real-Time Performance'].plot(kind='bar')
plt.title('Real Time Performance (%) per Sector')
plt.tight_layout()
plt.grid()
plt.show()
In order to run the tests you have to first export your API key so that the test can use it to run.
export API_KEY=YOUR_API_KEY
cd alpha_vantage
nosetests
The code documentation can be found at https://alpha-vantage.readthedocs.io/en/latest/
- Separate tests between unittests and integration test.
- Should support for csv also be added?
If you like or use this project, consider showing your support by staring it.