Skip to content

Instantly share code, notes, and snippets.

@seankross
Created December 2, 2012 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seankross/4186180 to your computer and use it in GitHub Desktop.
Save seankross/4186180 to your computer and use it in GitHub Desktop.
Plots difference between two stocks
setwd('~/Desktop/R/Inputs')
getYahooCsv <- function(startDate, endDate, ticker){
  string <- paste("http://ichart.finance.yahoo.com/table.csv?s=",ticker,
                  "&a=",startDate[1]-1,"&b=",startDate[2],"&c=",startDate[3],
                  "&d=",endDate[1]-1,"&e=",endDate[2],"&f=",endDate[3],"&g=d&ignore=.csv",
                  sep="")
  
  return (read.csv(string))
}
getClose <- function(start, end, ticker){
raw <- getYahooCsv(start, end, ticker)
data <- raw[order(nrow(raw):1),]
return( data$Close[1:nrow(data)] )
}
plotStockDif <- function(start, end, tick1, tick2){
datapoints <- getClose(start, end, tick1) - getClose(start, end, tick2)
        plot(1:length(datapoints), datapoints, type='l')
}
plotStockDif(c(10,31,11),c(10,31,12),'F','GM')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment