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
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
- Put the index as date time
- Order your series in a chronological order
- Verify your data are numbers and not string