R and The Future of Financial Data
Tags: Open Data, R Programming, Finance, Rstudio, Stock Market

The rock critic Jon Landau once wrote, way back in 1974, that he saw “The future of rock and roll and its name was Bruce Springsteen.” Now even if Bruce Springsteen doesn’t do it for you musically, you’ve got to admit Landau’s phrase was an audacious-sounding prophecy. Forty six years later, I find myself thinking along similar prophetic lines about how investment-related financial market data gets shared, and I can find no better phrasing to describe what is happening than to say, “I’ve seen the future of financial data and its name is the R ecosystem!”

The R programming language, and more specifically its ecosystem of computer packages and libraries, is the most important thing that has happened to liberalize financial market data since, like, the inception of the Internet itself; and get this, perhaps best of all, we are just getting started! The development of the R ecosystem is undeniably great for casual and professional investors, the academic finance community, educators, and those who tinker with data in their spare hours. To give you a sense of how using R works, and how powerful it is, I’ll demonstrate a few lines of code that will enable you to retrieve returns for just about any stock (for free).

Getting Set Up in R

Your journey working in R will begin with two downloads: The R language and RStudio. R is an open source programming language and RStudio is an incredibly well-designed front end (or integrated development environment) that is offered in both freeware and commercial versions. The links to R and RStudio are provided below.

R: The R Project for Statistical Computing (r-project.org)

RStudio

Once these are installed on your computer you are ready to get to work.

Getting Stock Data Into RStudio

Think of RStudio as your smartphone, and just like your phone, to maximize productivity you’ll need some apps to run on the operating system. In the R and RStudio lexicon these apps are called packages. Packages are packets of functions and code (like small computer programs or apps) that allow you to do things. To use the packages you must call them from the package library.

Fortunately to get stock data (prices, returns, etc.) into RStudio you just need one package (Tidyquant) and its corresponding library. To install the Tidyquant package on RStudio you simply type: install.packages(“tidyquant”) in the console and hit Control Enter to run the command.

Once the package installs you call the library as follows: library(tidyquant) and hit Control Enter.

After you have Tidyquant installed and have called its library you are in position to start downloading data. For an example of how this works, I’ll retrieve the daily returns for the SPY exchange traded funded (i.e., S&P 500 Index ETF).

Attached below (Sample Code 1) is the code snippet that pulls data for the SPY ETF (directly from Yahoo Finance). Once you enter the file name (spy_returns); the ticker (“spy”), the from and to dates; the period (“daily”); and the column rename (“spy.returns”),if you wish to rename it, you hit Control Enter to run.

Sample Code 1:

spy_returns <- tq_get(“spy”, get = “stock.prices”, from = “2020–9–30”, to = “2020–12–4”) %>%

tq_transmute(adjusted, mutate_fun=periodReturn, period = “daily”, col_rename = “spy.returns”)

To view the data you just downloaded, run:

head(spy_returns) and Control Enter (to have the package show what the first few rows look like).

Exporting to Excel from RStudio

In the R world, there is a package for just about everything, and several are designed to work with Excel. The code below installs the writexl package, calls its library, and with a simple command sends the data we gathered earlier to an Excel file on your drive.

Sample Code 2:

install.packages (“writexl”)

library(writexl)

write_xlsx(spy_returns, “spyFile.xlsx”)

This code saves the data to Excel on your C: drive in a file called spyFile.xlsx. So simple and yet so very effective!

The Future of Open Data

How investors retrieve investment data is at an important inflection point, in my humble view. While some old guard financial industry insiders hold on to the practices of hording and rationing data, and overcharging for it, the R ecosystem, like the Internet a few decades before, is swatting away these outdated paradigms. Accessible, open-source financial data is on the horizon, and I think the R community will be leading us in that revolution!

Reprinted from https://wordpress.com/post/dailyscreenz.home.blog/704