Tidyquant: An Open-Access Tool for Studying the Stock Market

They say that knowledge is power. Well, in capitalism, knowledge is also money … but only if you keep it under lock and key. Hence, researchers who study capitalism are often faced with a dilemma: the best data is kept behind a hefty paywall. The only way to get access is if your institution pays for a subscription. And if you’re not lucky enough to work for a university, you’re out of luck.

Given this problem, I’m always on the look out for open-access data, and for tools that facilitate access to said data. This week I discovered an R package called ‘Tidyquant’ that does a nice job of giving access to stock market data (which it pulls from Yahoo Finance).

Here’s a brief tutorial. Install Tidyquant with this command:

install.packages('tidyquant')

Then load it:

library(tidyquant)

Next, find the stock ticker for the company you want to look up. As an example, let’s do Exxon Mobile, who’s ticker is XOM. The code below will pull daily Exxon stock prices from the beginning of 1990 to today.


getSymbols("XOM", 
           from = '1990-01-01',
           to = today(),
           warnings = FALSE,
           auto.assign = TRUE
)

The function returns a table called XOM that looks like this:

That’s pretty nifty. Let’s plot some of the data. To plot the closing price, we enter:

plot(XOM$XOM.Close)

Here’s what the plot looks like. It’s not something I’d publish. But it’s good enough to explore the data.

Suppose we want to compare Exxon’s stock price to the S&P 500. That’s easy enough. On Yahoo Finance, the S&P 500 symbol is ^GSPC. Plug that into our function:

getSymbols("^GSPC", 
           from = '1990-01-01',
           to = today(),
           warnings = FALSE,
           auto.assign = TRUE
)

You’ll get a table called GSPC containing daily data for S&P 500 prices.

Next, let’s do some simple differential analysis. Let’s see how Exxon’s stock price has changed relative to the S&P 500. That’s another one liner:

plot(XOM$XOM.Close / GSPC$GSPC.Close)

Here’s the result, the ratio of Exxon’s price to the S&P 500:

As you can see, Exxon hasn’t been doing so well since oil prices peaked in 2008.

Tidyquant is a nifty little tool that gives you quick access to Yahoo Finance data. While it’s no replacement for the paywalled data that is closely guarded by Bloomberg and the likes, it’s a decent package for doing exploratory research.


Support this blog

Economics from the Top Down is where I share my ideas for how to create a better economics. If you liked this post, consider becoming a patron. You’ll help me continue my research, and continue to share it with readers like you.

patron_button


Stay updated

Sign up to get email updates from this blog.



This work is licensed under a Creative Commons Attribution 4.0 License. You can use/share it anyway you want, provided you attribute it to me (Blair Fix) and link to Economics from the Top Down.


Leave a Reply