money is coin $ flip

March 7, 2011

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

Well, sorta. More precisely, money is the sum of coin$flip divided by the number of coin$flip. But we’ll get to that later. For now, let me introduce you to a new algorithm written in R. This one is another “quote” — simple few lines of code — whose theme you can expand to include something more interesting than what I’m presenting here.

Let’s say you’re having a really bad spell trading and basically have no money left. You have a computer, but you don’t have any spare change to spend on software or data. Well, you’re in luck. R is free, open source software and Yahoo Finance offers free data. Yes the free-ness means that you get delayed quotes, but let’s not quibble about a few minutes. It’s free!

The following lines of code enable you to create a list of stocks that may be of interest to you. I’ve included the venerable Dow 30. You can include the entire S&P 500 or just some select sector ETFs that give you a broad-stroke view of the current day’s market action. In any case, it returns a value that I’ve named ‘money’. This value is a percentage of our list that is trading above yesterday’s close. Yeah, kinda boring, but as I mentioned, it’s a theme for you to play with.


bank  <- c("AA","AXP","BA","BAC","CAT","CSCO",

coin <- getQuote(bank, what=yahooQF(c("Last Trade (Price Only)", "Change"))) 

random <- function(change)
     if ( change > 0 )
         return (1)
         return (0)

coin$flip <-  mapply(random, coin[,3])

money <- sum(coin$flip)/length(coin$flip)*100


To leave a comment for the author, please follow the link and comment on their blog: Milk Trader. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, 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...


Comments are closed.

Search R-bloggers


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)