Created
September 24, 2014 16:24
-
-
Save ivannp/ddaddfe2991d52184c71 to your computer and use it in GitHub Desktop.
DVI Indicator Backtesting with SIT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Zdravei,
Can you please share the gspc.csv file?
Blagodarq