Skip to content

Instantly share code, notes, and snippets.

@ivannp
Created September 24, 2014 16:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ivannp/ddaddfe2991d52184c71 to your computer and use it in GitHub Desktop.
Save ivannp/ddaddfe2991d52184c71 to your computer and use it in GitHub Desktop.
DVI Indicator Backtesting with SIT
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()
}
@f1nasf1
Copy link

f1nasf1 commented Oct 16, 2015

Zdravei,

Can you please share the gspc.csv file?

Blagodarq

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