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

Image for post
Image for post
Photo by Fabian Blank on Unsplash

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.

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.

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?

####################################################################
# 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)
  1. 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.
  2. I had a typo in the syntax of my working directory.
  3. I couldn’t get devtools to work, but it turned out I had a typo in install_github too.
  4. 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/).
> 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

To be continued…

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store