Skip to content
All library documents

Finding Historical Financial Statements for Equity Backtests in R

Article Quant Q&A · Author: Scott Nunez

Summary

The discussion addresses how to obtain historical company financial statements for testing equity screens and accounting ratios in R. The questioner has access to Datastream and Compustat and wants long histories for measures such as return on equity and inventory turnover, noting that price-and-volume sources do not provide the needed fundamentals.

The answers point to financial data services and illustrate two routes: Quandl datasets sourced from SEC filings, and Intrinio’s standardized financial statement API, which can return statement data for a selected company and reporting period for use in R. The examples show that financial statement items can be retrieved and converted into tabular data for analysis. The evidence is illustrative rather than a comparison of coverage or data quality: the Quandl answer describes its free filing-based data as covering about five years, while the Intrinio example covers a specific quarterly report. The discussion does not establish that either source meets a 20-year history requirement or address survivorship bias, restatements, or point-in-time availability, all of which matter for credible backtests.

Key ideas

  • Fundamental screens require historical accounting data, not just prices and trading volume.
  • Financial statement items can be retrieved from data providers and brought into R for analysis.
  • Coverage varies by dataset; the cited free filing-based source is described as having about five years of data.
  • A demonstrated API query retrieves standardized statements for a specified company and reporting period.
  • The examples do not establish long-history coverage or point-in-time reliability for backtesting.

Tags

Full text
# Historical Financial Statement to Backtest in R


# Historical Financial Statement to Backtest in R












I would like to preface this by saying I am preparing for an upcoming internship this summer so I am extremely new to Quant Finance.

At my university we have access to Datastream by Thomson Reuters and Compustat. With either of those two software can I download historical financial statements going back 20+ years so I can backtest screens in R.

I am looking for historical financial statements so I can backtest ratios such as ROE, Inventory turnover, and etc. This is why the free Yahoo! API which only has price changes and volume doesn't help me out.

Thank you.

## Answer by WaltS (score 6)

https://quant.stackexchange.com/a/15927

Both free and paid access to data sets conatianing company financial statement items is available from Quandl. The free data sets are sourced from the SEC based on compnay electronic filings and go back about five years. For example, you could obtain five years of MSFT's quarterly net income using the R call

```
Quandl("RAYMOND/MSFT_NET_INCOME_Q")
```

Lists of available financial data and more detail on using Quandl is available at https://www.quandl.com/help/api-for-stock-data

## Answer by Andrew Carpenter (score 0)

https://quant.stackexchange.com/a/34528

You can get that data from Intrinio in R as well. There is documentation, including how to actually make the API calls in R with examples, here. Here is an example of pulling Apple's income statement for Q1 2017- you just need to get your own API keys and add them to the code (it's free):

```
#Cleaning up the environment

rm(list=ls())

#Skip this installation if you already have httr installed. This package makes using APIs in R easier
install.packages("httr")
#Require the package so you can use it
require("httr")

#Skip this installation if you already have jsonlite installed. This package makes parsing JSON easy
install.packages("jsonlite")

#Require the package so you can use it
require("jsonlite")

#Create variables for your usename and password, get those at intrinio.com/login. 
#Replace them here or the code won't run! They need to be in ""

username <- "Your_API_Username_Here"
password <- "Your_API_Password_Here"

#These variables will be pasted together to make our API call. You can replace the "stock" variable with any US ticker symbol

base <- "https://api.intrinio.com/"
endpoint <- "financials/standardized"
stock <- "identifier=AAPL"
statement <- "statement=income_statement"
fiscal_period <- "fiscal_period=Q1"
fiscal_year <- "fiscal_year=2017"

#Pasting them together to make the API call. 
call1 <- paste(base,endpoint,"?",stock,"&", statement, "&", fiscal_period, "&", fiscal_year, sep="")

#This line of code uses the httr package's GET function to query Intrinio's API, passing your username and password as variables
get_prices <- GET(call1, authenticate(username,password, type = "basic"))

#The content function parses the API response to text. You can parse to other formats, but this format is the easiest to work with. Text is equivalent to JSON
get_prices_text <- content(get_prices, "text")

#This line of code uses the JSONlite function fromJSON to parse the JSON into a flat form. Flat means rows and coloumns instead of nested JSON
get_prices_json <- fromJSON(get_prices_text, flatten = TRUE)

#Converting the data to a dataframe
get_prices_df <- as.data.frame(get_prices_json)
```

The result is a dataframe that looks like this:

Now that you can get the statements via API (for free), you can go to town analyzing them.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.