Personal Quantitative Analysis Journal

Can R Fix My IRA Account? — Day 1

My chronological journey to getting R to invest in the market for me: quantmod and devtools

Cindy S. Cheung

--

Photo by Fabian Blank on Unsplash

Dollar signs popped up in my eyes in my twenties.

The last thing I wanted was to work in my retirement year because I couldn’t afford to lay in the sun in Bora Bora or relax in my Italian villa.

So I made sure I took care of my retirement accounts right out of school.

My first encounter with financial investment was my 401k account. Studying and setting it up gave me a taste for stocks and options. I soon found myself spending thousands to learn about trading from a couple of institutions.

Boy, I was immature. The real world gave me a gigantic awakening.

Let’s just say the last 15 years did not turn out the way I dreamed.

Then life happened.

Maybe an Opportunity in Disguise

The idea of building a machine to invest in the stock market for had been on my mind for years.

The only issue was I am a mathematician, not a software developer. These days, any quantitative role involves programming, and I didn’t like programming. It’s a major reason I majored in hardware electrical engineering instead of computer science engineering.

So when I got into trading, I was hoping for something ready built.

It wasn’t meant to be. And I ended up abandoning it to let financial gurus handle it for me.

But now, as I sit here writing this, perhaps the universe is telling me this is my opportunity to get this straightened out while I look for my next job.

My Start to Quantitative Analysis

A couple of days after Christmas last year, I registered for DataCamp (http://www.datacamp.com), an online training company that specializes in teaching R and Python to data scientists. I had to succumb to the fact that I need to be more familiar with programming if I wanted to stay in the analytics space.

After completing its Data Science Tracks for R and Python, my eyes darted toward a different track. Quantitative Analysis with R.

I skimmed over the curriculum and thought, “Well, I do have time on my hands besides applying to job after job. At least this will keep my R skills current and maybe I can finally get my IRA back on track.

So I started the track.

The First Few Hours

NOTE: From here on out, this is for absolute beginners.

Installing devtools and quantmod

The first three courses of the fifteen included in the track gave me little to desire. Did I mention I don’t like programming?

It wasn’t until I reached course four, Importing and Managing Financial Data in R, that I finally learned anything of substance that I could use to start my personal R script.

As an add-on, I have decided to chronicle my journey here on Medium. I will expose the cheers and pains I encounter after every time I work on building this algorithm in R. I will include the code and the adjust I have to make.

Please note: I am working on RStudio on Windows 10.

Here is what I have thus far. I do have a Github account, but I have to figure out how to upload the script.

Oh, and by the way, if you have any suggestions, please pass it on. Thank you.

####################################################################
# Creator: Cindy S Cheung
# Script Name: RQuantAnalytics
# Purpose: This is the first R script in learning how to use R to
# evaluate stock investment. This is the core script at its
# first stages, so there are no functions yet.
# Version: 1.0
####################################################################
####################################################################
# Install packages needed to be used in script
####################################################################
# Make package development easier--needed this to update quantmod to
# work with the current Yahoo server. Otherwise, quantmod cannot
# download data from Yahoo
install.packages('devtools')
# Quantitative Financial Modeling Framework Package includes most
# trading functions and strategies
install.packages('quantmod')
####################################################################
# Load package libraries
####################################################################
library(devtools)
library(quantmod)
# Set directory
work_dir <- "C:/Users/cinji/OneDrive/Quantitative Analytics"
setwd(work_dir)
# Update quandmod to the development version created by J. Ulrich
devtools::install_github("joshuaulrich/quantmod")
# Extract stock price data of Ferrari from Yahoo
getSymbols("RACE", src = "yahoo", auto.assign = TRUE)
str(RACE)
head(RACE)

I know. It’s not much code at all.

What took so long?

  1. The lessons from DataCamp were useful, but it didn’t address the updates or other computer-related nuances that stopped the R script from working as taught. Another reason I don’t like programming.
  2. I was working on Mac at first, and it gave me all sorts of problems when trying to install packages. My understanding is that a bunch of packages were not updated. So I switched over to my PC.
  3. I had a typo in the syntax of my working directory.
  4. I couldn’t get devtools to work, but it turned out I had a typo in install_github too.
  5. I couldn’t get getSymbols to work. It appeared that Yahoo changed its URL to the financial data. At first, I tried to switch to Google Finance, but that didn’t work either. After much research, I found out that this problem had existed for the last couple years, and DataCamp failed to mention it. To rectify it, I followed the instructions on Robot Wealth (https://robotwealth.com/solved-errors-downloading-stock-price-data-yahoo-finance/).

When it finally worked, I was able to download the data for RACE, the stock ticker for Ferrari.

> str(RACE)
An ‘xts’ object on 2015–10–21/2019–06–11 containing:
Data: num [1:915, 1:6] 60 57.1 57.8 57 54.8 …
— attr(*, “dimnames”)=List of 2
..$ : NULL
..$ : chr [1:6] “RACE.Open” “RACE.High” “RACE.Low” “RACE.Close” …
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
$ src : chr “yahoo”
$ updated: POSIXct[1:1], format: “2019–06–12 16:36:47”
> head(RACE)
RACE.Open RACE.High RACE.Low RACE.Close RACE.Volume RACE.Adjusted
2015–10–21 60.00 60.97 55.000 55.00 22425300 53.13913
2015–10–22 57.07 58.20 55.700 56.75 4545100 54.82992
2015–10–23 57.77 58.00 56.270 56.38 1967600 54.47243
2015–10–26 57.00 57.00 54.535 55.02 1466200 53.15845
2015–10–27 54.80 54.99 49.360 53.85 5949200 52.02803
2015–10–28 54.02 54.16 50.100 51.87 2411300 50.11503

Finally! I got the data. Next course.

To be continued…

--

--

Cindy S. Cheung

Data Analyst. Screenwriter. Project Manager. Now, Resume Coach. A student of life and West Coast Swing. A promoter of self from within. www.sunbreakresumes.com