Skip to content

Instantly share code, notes, and snippets.

@milktrader
Created March 25, 2011 02:18
Show Gist options
  • Save milktrader/886250 to your computer and use it in GitHub Desktop.
Save milktrader/886250 to your computer and use it in GitHub Desktop.
Plot how much today's close is different from the close 24 days ago in % terms
blueberry <- function(sym="SLV", file="~/Desktop/24_days_ago.png"){
require("quantmod")
ticker <- getSymbols(sym, auto.assign=FALSE)
ticker <- merge(ticker[,4], lag(ticker[,4],24))
ticker <- na.locf(ticker, na.rm=TRUE)
lag_diff <- function(x,y){
pdiff <- (x - y)/y
return(pdiff)
}
ticker <- cbind(ticker, apply(ticker,1, function(x)lag_diff(x[1],x[2])))
change <- ticker[,3]
png(file)
# plot(change, main="% Change From Close Today & Close 24 Days Ago")
# hist(change, main="% Change From Close Today & Close 24 Days Ago")
plot(density(change), main="% Change From Close Today & Close 24 Days Ago")
polygon(density(change), col="blue")
dev.off()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment