A Simple R Script for Traders

[This article was first published on Milk Trader, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.


#!/usr/bin/Rscript
require(quantmod)
arg    <- commandArgs(trailingOnly = TRUE)
stock  <- getQuote(arg)
last   <- stock[2]
high   <- stock[6]
low    <- stock[7]
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
[1] “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…

EDITOR NOTE:

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.

To leave a comment for the author, please follow the link and comment on their blog: Milk Trader.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)