Skip to content

Instantly share code, notes, and snippets.

@markcheno
Forked from ivannp/dvi.r
Last active August 29, 2015 14:06
Show Gist options
  • Save markcheno/7f3ab77af35e2ff58dfe to your computer and use it in GitHub Desktop.
Save markcheno/7f3ab77af35e2ff58dfe to your computer and use it in GitHub Desktop.
require(quantmod)
require(SIT)
dvi.indicator = function(prices) {
dvi = TTR:::DVI(prices)[,3]
indicator.long = ifelse(dvi <= 0.5, 1, 0)
indicator.short = ifelse(dvi > 0.5, -1, 0)
indicator = reclass(cbind(indicator.short + indicator.long), prices)
return(indicator)
}
bt.dvi = function(ticker='GSPC', file="gspc.csv", dates='1990::2013', prefix="") {
# The data is consumed from the *data* environment. The *signals* environment, contains
# the data adjusted only for splits - this is what the signals are based upon. For the
# actuall trading, the prices are adjusted both for splits and dividends.
data = new.env()
signals = new.env()
gspc = xts(read.zoo(file=file, format="%Y-%m-%d", header=T, sep=","))
signals[[ticker]] = gspc
data[[ticker]] = adjustOHLC(gspc, use.Adjusted=T)
bt.prep(data, align='keep.all', dates=dates)
bt.prep(signals, align='keep.all', dates=dates)
# DVI
data$weight[] = NA
data$weight[] = bt.apply(signals, function(xx) {dvi.indicator(Cl(xx))[,1]})
dvi.regular = bt.run(data, trade.summary=T)
print(round(dvi.regular$trade.summary$stats, 4))
png(filename=paste(prefix, 'plot.png', sep=""), width=1200, height=800, units='px', pointsize=12, bg='white')
plotbt.custom.report.part1(dvi.regular)
plotbt.custom.report.part2(dvi.regular)
dev.off()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment