Import Market Data with the Alpha Vantage API (100% free) !

You want to break into quant trading, and you want a clear tutorial about how to import some data. You are in the right place! To do so, we will use the Alpha Vantage API, which is 100% free.

However, as it is a free source, Alpha Vantage has some weaknesses that we need to highlight to be sure we do not suffer from them. Then, I will explain to you how to import some data easily using Python and the Alpha Vantage API.

1. PROS AND CONS ABOUT ALPHA VANTAGE

Before explaining how to import some market data using Alpha Vantage, I want to highlight the strengths and the weaknesses of this data source. The first thing we need to know is that Alpha Vantage is not limited to the stock prices data, you can find many alternatives data too.

But when it comes to asset prices, I generally advice to use your broker data when it comes to developing a trading strategy that will be put in live trading. Having said that, we can make a quick list about the advantages and disadvantages of the Alpha Vantage API.

In my opinion, which is just mine, I prefer to use my broker for the prices data or a very highly reliable source and Alpha Vantage for the alternative data like economic indicators, fundamental data….

2. CLAIM YOUR 100% FREE API KEY

To obtain your API key, there is nothing simpler, you use the following link from the Alpha Vantage documentation (https://www.alphavantage.co/support/#api-key) and you give your mail address and 2 other basic information and you will receive your API key.

Once you have your key, please, keep it safe or you will need to ask another one! You can use the following free API key, that I have created for this post if it is still available (2GCVWM36J7GVSP5X).

However, I advise you to create your own free API key to be sure not being limited because of other members requests on this key.

3. IMPORT FINANCIAL DATA USING ALPHA VANTAGE

In this section, I will make a simple import and then, we will customize it as much as possible. To find the documentation you can follow this link (https://www.alphavantage.co/documentation/). To our first import, we will use the example from the Alpha Vantage documentation importing the EURUSD prices.

We obtain the data as a dictionary but we will transform it into a pandas data frame to make  things easier. More details in the last section.

ADJUST THE NUMBER OF BARS

If we read the request, we do not see any parameter talking about how much bars I want, from which date I want the data or something like that and that’s normal. When we read the documentation, we see that there is an optional parameter called “outputsize” that can take 2 values: full or compact. Full to obtain all the data possible and, compact that gives only 100 values to have an overview about how the data are organized. And if I run the same request as before using the full value for outputsize, I will have 20 years of daily bars for the EURUSD

CHANGE THE SYMBOL

That’s great, we know how to import a lot of data. But for now, we know only how to import a lot of data for the EURUSD symbol. Let me explain to you how to change that. If we read the request, we understand easily how to switch from one currency pairs to another thanks to the parameters “from_symbol” and “to_symbol”. The question is, if I want to obtain the data for a stock price form example or a commodity, how can I do it?

To do so, we need to change the function parameter. Indeed, each asset class has its own function: DIGITAL_CURRENCY_DAILY for crypto FX_DAILY for FX, NEWS_SENTIMENT for the news sentiment data and so on. Again, if you want to know all the different function and data available, you can check the Alpha Vantage documentation which is well done with many examples. Let me show you a last example with the natural gas price to see the difference.

4. PROCESS THE DATA IS MANDATORY

At this step, we know how to import the data which is great but if we look to section 1, I said that the output format was not optimal. What was I wanted to say by that?
 
When you are importing some data, you need to NEVER trust the data source! By that, I mean even if your own mother gives you a dataset, take 10 minutes to analyze it, print it and take a quick look. It can avoid you a lot of errors.
 
For the small storytelling part, the first time I used Alpha Vantage 5 or 6 years ago, I was able to create amazing models very easily, 200%/year with 10% max drawdown, I thought I will be a billionaire. You should understand that there was an error because the live trading was so different. The problem was the chronological order. Alpha Vantage give the data from the most recent to the oldest. Something which is quite uncommon. It means I was predicting the past using the future and I lost a lot of time to find where the error came from even if it was a very simple error.
 
So never forget to follow these steps:
  • Put the index as date time 
  • Order your series in a chronological order 
  • Verify your data are numbers and not string
 
Please take a look to one of my other article using the Alpha Vantage API in python for the importation if you want to have the detail of this process.
Previous Article

Download Ticks Data using MetaTrader 5 and Python (CFD & Futures)

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *