On Uncharted Territory...
A few months ago, I wrote about the R package “tidyquant” - a streamlined way of pulling stock data and visualizing it in candlesticks like the one below. My background in trading, starting in undergrad, makes experimenting with such packages especially fun. And the best part is: “tidyquant” is not the only R package devoted to processing financial data. There’s also “quantmod”, which I will be writing about today.
Starting with “quantmod”…
…is pretty simple. As you know, the first step is to download the package.
install.packages(“quantmod”)
library(quantmod)
With “quantmod”, you can get basic statistics on any stock you want with the commands:
getSymbols(‘RSX’)
head(RSX)
It will give you a simple breakdown like this:
This simple command will give you a chart for the stock of your choice in a second:
barChart(RSX)
A chart like this will tell you the story of a stock’s price while simultaneously documenting changes in volume. When I bought RSX, the volume was a lot higher than it is now and the price was slightly lower.
Zeroing In…
What if I want to look more closely at RSX’s activity during the time within which I bought my shares? The answer lies by combining the above functions of “quantmod” with those from another package, XTS.
install.packages(“xts”)
library(xts)
This will allow you to encode stock data for the timeframe you want into a variable as such:
RSX1 <- RSX['2015-06::2015-09']
After doing this, you can simply use the barChart command to make a new plot, which as a plus, will be way less cluttered than the initial plot that incorporated all of RSX’s data.
barChart(RSX1)
There’s so much more you can do here, but that’s all we’ll do today. I’ll make a tutorial next week involving next steps in “quantmod”, “tidyquant” or both.
Have a preference?
Tell me which package you’d rather hear about!