Skip to content

Instantly share code, notes, and snippets.

Created September 24, 2014 16:24
What would you like to do?
DVI Indicator Backtesting with 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)
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)
data$weight[] = NA
data$weight[] = bt.apply(signals, function(xx) {dvi.indicator(Cl(xx))[,1]})
dvi.regular =, 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')
Copy link

f1nasf1 commented Oct 16, 2015


Can you please share the gspc.csv file?


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment