R

Best Free API for Historical Stock Data – Examples in R

Best Free API for Historical Stock Data – Examples in R

We live in a very nice world where access to data becomes more and more affordable. In this article I will show you 3 best free stocks API, in my opinion, you can use to get historical data for stocks. Some of these APIs are freemium, they have paid both free and paid plans. But still, with the free subscription, you can get access to a pretty good amount of data.

I will also provide you with code examples in R how to access and load this data in data frames so you can use it next in your analysis or backtesting.

Yahoo Finance

https://finance.yahoo.com

Yahoo Finance is the most obvious choice from free API. It’a very well known free API with a pretty big coverage in Stocks, ETF, Options.

To get data from Yahoo Finance you can use quantmod package, with it downloading data will take you only one function call:

library(tidyverse)
library(quantmod)

# Simplest call. "AAPL" object will be created
getSymbols("AAPL")
AAPL %>% head

# Example without auto assign
d <- getSymbols("AAPL", auto.assign = F)
d %>% head

# Specifying from/to dates
d <- getSymbols("AAPL", auto.assign = F, from = "2020-01-01", to = "2020-06-01")
d %>% head 

# Specifying return class
d <- getSymbols("AAPL", auto.assign = F, from = "2020-01-01", to = "2020-06-01", return.class='data.frame')
d %>% head  

IEX Cloud

https://iexcloud.io/

IEX Cloud is also a pretty obvious choice for best API. It’s created by IEX exchange guys, featured in the famous “Big Short” book.

IEX Cloud API has plenty of different functions. You can get with them almost everything about US stocks, from historical data to custom signal or board meetings.

In a free account, you will get 0.5 M messages. Every API call will cost you a certain amount of messages. There are cheap API like quotes or historical data, but there are quite expensive ones like sentiment signals and advanced information about the company.

To work with IEX cloud in R you can use httr package for requests and jsonlite package to parse JSON format. Here is a simple example:

library(tidyverse)
library(jsonlite)
library(httr)

symbol = "AAPL"
iex_token  = "your_iex_cloud_key_here"
url <- str_c("https://cloud.iexapis.com/stable/stock/", symbol , "/chart/1m?token=", iex_token)

d <- url %>% 
  GET %>%
  content(as = "text") %>%
  fromJSON 

d %>% head

Alpha Vantage

https://www.alphavantage.co/

Alpha Vantage is not so fancy as IEX Cloud but still nice and easy to use API. If you’re looking only for market data you will find in Alpha Vantage almost all you need. You can get daily, intraday data for stocks, crypto, and forex. Also, you can get already precalculated technical indicators.

It is really easy to work with AlphaVantage in R. It supports CSV as an output, so you can use standard functions to read CSV files to load data from Alpha Vantage. Here is an example:

library(tidyverse)
library(jsonlite)
library(httr)

symbol = "IBM"
av_key = "demo"

url <- str_c("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=", symbol ,"&apikey=", av_key, "&datatype=csv")

d <- read_csv(url)

d %>% head

4 thoughts on “Best Free API for Historical Stock Data – Examples in R”

  1. Great info Quant!
    do you have any free API for EMINI future data (minute bar level as well)?

    E.

    1. Tricky question, I don’t think I know a free source of minute E-mini futures. Check Barchart, maybe you can find there pretty cheap option.

  2. Great info Quant!
    do you have any free API for EMINI future data (minute bar level as well)?

    E.

    1. Tricky question, I don’t think I know a free source of minute E-mini futures. Check Barchart, maybe you can find there pretty cheap option.

Leave a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Pine Script Programming Courses
Pine Script Programming Courses
Learn to build your own TradingView Indicators and Strategies
Sidebar Signup Form
If you want to be the first in this business, subscribe to the latest news