Now that we’ve got the Python implementation under out belts, let’s do the same thing with R. We’ll still be able to pass command-line arguments to get a quick look at what our stock of interest is doing during the day. And we start with the familiar incantation of enabling our script to run from command-line.
$ chmod +x yahoo.r
This is a further abuse of starting the story in the middle. So let’s get to the beginning. Here is the R script that we’ll be invoking from command-line.
arg <- commandArgs(trailingOnly = TRUE)
stock <- getQuote(arg)
last <- stock
high <- stock
low <- stock
pctile <- (last-low)/(high-low)
bingo <- round(pctile * 100)
b <- as.integer(bingo)
sprintf(“%s is trading at the %ith percentile of its daily range”, arg ,b)
We only need to call one package here, the inimitable quantmod. That’s were we get access to the getQuote function. From there, it’s basic boilerplate. I am by no means a professional programmer, so there are some issues with this script that I’m just living with for now including the fact that when this is invoked, R just doesn’t shut up with it’s messages. Let’s run this on ^GSPC, which is the Yahoo symbol for S&P 500. You’ll see what I mean.
$ ./yahoo.r ^GSPC
WARNING: ignoring environment value of R_HOME
Loading required package: quantmod
Loading required package: Defaults
Loading required package: xts
Loading required package: zoo
Loading required package: TTR
Loading required package: methods
 “GSPC is trading at the 3th percentile of its daily range”
First, you notice that the first line is what we typed in command-line. Starting at WARNING is where R starts to chatter. The last line is the one we expect to get returned. (Happened to catch the market at a low point). All of this Loading is necessary, but I’d like to silence it.
Our second issue is that ‘3th’ isn’t a word. It’s supposed to be ‘3rd’. This can be programmatically corrected, but then the code starts getting dense. Better to live with it or find a better way to phrase the return phrase.
Also with quantmod, you can go to the races and calculate all manner of technical analysis data and have that output to terminal. Now I need to go figure out what’s going on with the market…
There is nothing going on in the market so I decided to start googling how get rid of Loading messages. Sure enough I found suppressPackageStartupMessages.Now I have to solve the WARNING message issue. That one I’m afraid is going to be a rat’s nest.