My Stock in Tidyquant
In my business school Investments course, we used macros in Excel to pull Yahoo Finance data and simulate portfolios. No stranger to trading, but new to macros, I thought it was the coolest thing. But, as usual, anything Excel can do R can do better, as proven by my new favorite package: “tidyquant.”
In this tutorial, I will show you a few basic operations that tidyquant can do in hopes that you a) continue to explore and b) circle back to me with your findings. The stock I’ll use as an example is GE, of which I bought four shares today on margin. I hadn’t been trading since the crash, but got inspired by my friend Kellen’s day-trading ventures and decided to dip back in. Why GE? Because its price at $6.36 today allowed me to buy more than one share in hopes that it will rise this week.
First off, make sure you have the right packages installed: “tidyquant”, “ggplot2”, “dplyr” and “magrittr”. I had to install “quantmod” separately even though it should have downloaded with tidyquant.
GE <- tidyquant::tq_get(GE)
The above command will import GE’s opening, high, low, closing and adjusted closing prices for each day from January 4, 2010 to April 24, 2020 along with the daily volume. This data frame (shown below) can then be further analyzed and visualized.
The next step was to visualize the data, which I chose to do with a candlestick chart. My friend Matt Dancho, the Founder and CEO of Business Science University, has a great tutorial on Github illustrating this process.
GE %>% ggplot(aes(x = date, y = close)) +
tidyquant::geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
labs(title = "GE Candlesticks", y = "Closing Price", x = "") + tidyquant::theme_tq()
This coded resulted in the plot below.
I expect to explore tidyquant further in the future, so if any functions stick out to you in the meantime, let me know and I will place a specific focus on them.
Did you like what you saw here?
Invest in the creation of more tutorials like this one by becoming a patron of DiKayo Data on Patreon!