money is coin $ flip

[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.

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.

require("quantmod")

bank  <- c("AA","AXP","BA","BAC","CAT","CSCO",
           "CVX","DD","DIS","GE","HD","HPQ",            
           "IBM","INTC","JNJ","JPM","KFT",              
           "KO","MCD","MMM","MRK","MSFT","PFE",         
           "PG","T","TRV","UTX","VZ","WMT","XOM")       

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

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

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

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

money

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)