# A Simple R Script for Traders

November 21, 2011
By

(This article was first published on Milk Trader, and kindly contributed to R-bloggers)

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 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 his blog: Milk Trader.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags:

Comments are closed.