Check out our Open Source Live Book project for the code used in this Article.

In this Article, we will show how to obtain free financial data including end-of-day and real-time pricing, company financials and macroeconomic data from crypto and stocks to currencies, commodities and more.

Data sources utilized in this Article include:

U.S. Securities and Exchange Commission (SEC);

Quandl;

IEX;

Alpha Vantage.

We also provide code to reproduce results as part of our Open Source Live Book Initiative.

Alpha Vantage

Alpha Vantage offers free access to pricing data including:

Stock Time Series Data;

Physical and Digital/Crypto Currencies (e.g., Bitcoin);

Technical Indicators and

Sector Performances.

The data are available in JSON and CSV formats via REST APIs. The quantmod and the alphavantager R packages offer a lightweight R interface to the Alpha Vantage API. Daily stock prices can be obtained with the quantmod::getSymbols function as follows:

getSymbols(Symbols='AAPL', src="av", output.size="full", adjusted=TRUE, api.key='your API key')

The output data is stored in an object with the same name as the corresponding symbol, in this example AAPL . The output data looks like the following:

We called the quantmod::getSymbols function with the following arguments:

Symbols='AAPL' defines a character vector specifying the names of each symbol to be loaded, here specified by the symbol of the company Apple Inc.;

defines a character vector specifying the names of each symbol to be loaded, here specified by the symbol of the company Apple Inc.; src="av" specifies the sourcing method, here defined with the value corresponding to Alpha Vantage;

specifies the sourcing method, here defined with the value corresponding to Alpha Vantage; output.size="full" specified length of the time series returned. The strings compact and full are accepted with the following specifications: compact returns only the latest 100 data points; full returns the full-length time series of up to 20 years of historical data;

specified length of the time series returned. The strings and are accepted with the following specifications: returns only the latest 100 data points; returns the full-length time series of up to 20 years of historical data; adjusted=TRUE defines a boolean variable to include a column of closing prices adjusted for dividends and splits;

defines a boolean variable to include a column of closing prices adjusted for dividends and splits; api.key specifies your Alpha Vantage API key.

IEX

The IEX Group operates the Investors Exchange (IEX), a stock exchange for U.S. equities that is built for investors and companies. IEX offers U.S. reference and market data including end-of-day and intraday pricing data. IEX offers an API with “a set of services designed for developers and engineers. It can be used to build high-quality apps and services”. Data sourced from the IEX API is freely available for commercial subject to conditions and the use of their API is subject to additional terms of use.

IEX lists the following github project as an unofficial API for R: https://github.com/imanuelcostigan/iex. We will provide examples on how to obtain intraday pricing data using this package. First, we will use the devtools to install the package directly from its github repository as follows:

library(devtools)

install_github("imanuelcostigan/iex")

The iex package provides 4 set of functions as follows:

last : Provides IEX near real time last sale price, size and time. Last is ideal for developers that need a lightweight stock quote. IEX API real time API documentation.

: Provides IEX near real time last sale price, size and time. Last is ideal for developers that need a lightweight stock quote. IEX API real time API documentation. market : Provides exchange trade volume data in near real time. IEX market API documentation.

: Provides exchange trade volume data in near real time. IEX market API documentation. stats : A set of functions that return trading statistics. IEX stats API documentation.

: A set of functions that return trading statistics. IEX stats API documentation. tops : Provides IEX’s aggregated bid and offer position in near real time for all securities on IEX’s displayed limit order book. IEX API TOPS documentation.

For instance, the last function has the following arguments:

symbols : A vector of tickers (case insensitive). Special characters will be escaped. A list of eligible symbols is published daily by the IEX. When set to NULL (default) returns values for all symbols.

: A vector of tickers (case insensitive). Special characters will be escaped. A list of eligible symbols is published daily by the IEX. When set to (default) returns values for all symbols. fields : A vector of fields names to return (case sensitive). When set to NULL (default) returns values for all fields.

: A vector of fields names to return (case sensitive). When set to (default) returns values for all fields. version : The API version number, which is used to define the API URL.

We can obtain intraday stock price data with the last function as follows:

dat <- iex::last(symbols = c("AAPL"), fields = c("symbol", "price", "size"))

The function returns an S3 object of class iex_api which has three accessible fields: path , response and content .

The path contains the corresponding IEX API path:

dat$path ## [1] "tops/last"

The response contains the unparsed IEX API response:

dat$response ## Response [https://api.iextrading.com/1.0/tops/last?symbols=AAPL&filter=symbol%2Cprice%2Csize]

## Date: 2019-08-27 02:04

## Status: 200

## Content-Type: application/json; charset=utf-8

## Size: 45 B

The content contains the parsed content from the API’s response:

dat$content ## [[1]]

## [[1]]$symbol

## [1] "AAPL"

##

## [[1]]$price

## [1] 207

##

## [[1]]$size

## [1] 100

According to the developer, this package causes R to pause 0.2 seconds after executing an API call to avoid the user being throttled by the IEX API (which enforces a 5 request per second limit). Documentation about the other set of functions can be obtained here.

Quandl

After Nasdaq’s acquisition earlier this year, Quandl is likely the largest financial and alternative data aggregator/provider today after the big players (S&P, Refinitiv, IHS Markit, Bloomberg). They leverage relationships with third-party providers to be a one-stop-shop for alternative data and traditional fundamental, pricing and estimates datasets.

Quandl offer an API which usage is free for registered users. You can obtain an API key here. After signing up, just append your API key to your call like this:

At Quandl, every dataset is identified by “Quandl code”, which is a unique id. In the above example, you downloaded a dataset with the Quandl code “WIKI/FB”.

Every Quandl code has 2 parts: the database code (“WIKI”) which specifies where the data comes from, and the dataset code (“FB”) which identifies the specific time series you want.

You can find Quandl codes using their data browser. Additional API documentation can be found here.

Quandl is also available via an R interface (Raymond McTaggart, Gergely Daroczi, and Clement Leung 2019). For instance, we can obtain Crude Oil Futures prices from 01/01/2010 to 01/01/2019 as follows:

library(Quandl)

Quandl.api_key(<<your quandl.key>>)

from.dat <- as.Date("01/01/2010", format="%d/%m/%Y")

to.dat <- as.Date("01/01/2019", format="%d/%m/%Y")

crude.oil.futures<-Quandl("CHRIS/CME_CL1", start_date = from.dat, end_date = to.dat, type="xts")

plot(crude.oil.futures$Last)

In the example above, we specified the following Database/Dataset:

Database: “CHRIS”. Continuous contracts for all 600 futures on Quandl. Built on top of raw data from CME, ICE, LIFFE etc. Curated by the Quandl community. 50 years history.

Dataset: “CME_CL1”. Historical futures prices of Crude Oil Futures, Continuous Contract #1. Non-adjusted price based on spot-month continuous contract calculations. Raw data from CME.

SEC

Official filings are freely available from the U.S. Securities and Exchange Commission’s EDGAR database. The package finreportr provides an interface in R to facilitate financial analysis from SEC’s 10K and 10K/A filings.

We can obtain company basic information with the function the CompanyInfo function by passing the ticker symbol of the target company as follows:

library("finreportr")

AAPL.Info<-CompanyInfo("AAPL")

print(AAPL.Info) ## company CIK SIC state state.inc FY.end street.address

## 1 Apple Inc. 0000320193 3571 CA CA 0930 ONE APPLE PARK WAY

## city.state

## 1 CUPERTINO CA 95014

As a result, we obtain the following information:

Company name: Apple Inc.;

SEC Central Index Key (CIK): 0000320193;

Standard Industrial Classification (SIC): 3571, which is the industry code for Electronic Computers;

Address: ONE APPLE PARK WAY, CUPERTINO CA 95014;

Most recent period of report end is 0930.

The list of company annual reports with corresponding filing dates can be obtained with the function AnnualReports as follows:

AAPL.reports<-AnnualReports("AAPL")

The accession number is a unique identifier that the SEC creates for each filing.

Company financials are organized into 3 segments: Income Statement, Balance Sheet and Cash Flow.

Income Statement

Financials from the Income Statement segment can be obtained with the GetIncome function as follows:

AAPL.IS<-GetIncome("AAPL", 2017)

The Income Statement function returns data for the following metrics:

Balance Sheet

Financials from the Balance Sheet segment can be obtained with the GetBalanceSheet function as follows:

AAPL.BS<-GetBalanceSheet("AAPL", 2017)

The Balance Sheet function returns data for several metrics including:

Cash Flow

Financials from the Cash Flow segment can be obtained with the GetCashFlow function as follows:

AAPL.CF<-GetCashFlow("AAPL", 2017)

The Cash Flow function returns data for several metrics including:

Conclusion

Not All Data Is Created Equal but some are available free for your to get started!

In this Article, we showed how to obtain financial, economic, pricing data among many other potential datasets available from sources including Quandl, IEX and the SEC. We have also made available some simple code to get you started.

References

Raymond McTaggart, Gergely Daroczi, and Clement Leung. 2019. Quandl: API Wrapper for Quandl.com. https://CRAN.R-project.org/package=Quandl.