THIS IS ONLY AN EXAMPLE AND IS NOT INVESTMENT ADVICE. ACTING ON THIS WILL LOSE LOTS OF MONEY.
Systematic Investor Blog (be sure to check out the site) offers extremely good examples of how to use R in finance. Since I firmly believe more examples are always better, I wanted to provide an additional very simple example of how to use his Systematic Investor Toolbox (SIT) for systems development. This will provide a building block for a series of posts similar to my A Quantstrat to Build On Part 6. We’ll use our good old count up/down (CUD) indicator on the S&P 500 Index and compare it to Mebane Faber’s 10 month moving average.
I know this is not pretty, but I wanted to start with as simple a base as possible. Any loyal readers will already have known that CUD is not so great at making money.
![]() |
From TimelyPortfolio |
#highlight the very fine work of http://systematicinvestor.wordpress.com/ | |
#adapted some of his code to provide an addtional example for | |
#those that might be interested | |
############################################################################### | |
# Load Systematic Investor Toolbox (SIT) | |
# http://systematicinvestor.wordpress.com/systematic-investor-toolbox/ | |
############################################################################### | |
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb')) | |
source(con) | |
close(con) | |
############################################################################### | |
require(quantmod) | |
#***************************************************************** | |
# Load historical data | |
#****************************************************************** | |
tickers = spl('^GSPC') | |
data <- new.env() | |
getSymbols(tickers, src = 'yahoo', from = '1896-01-01', env = data, auto.assign = T) | |
bt.prep(data, align='keep.all', dates='1896::2011') | |
#***************************************************************** | |
# Code Strategies | |
#****************************************************************** | |
prices = data$prices | |
# Buy & Hold | |
data$weight[] = 1 | |
buy.hold = bt.run(data) | |
# MA Cross | |
#Meban Faber 10 month or approximately 200 day moving average entry | |
sma = bt.apply(data, function(x) { SMA(Cl(x), 200) } ) | |
data$weight[] = NA | |
#when price crosses 200 day moving average enter | |
data$weight[] = iif(prices >= sma, 1, 0) | |
sma.cross = bt.run(data, trade.summary=T) | |
# just do the cud function with ttrTests optimized value of 110 days | |
cud = bt.apply(data, function(x) {runSum(ifelse(ROC(x,1,type="discrete") > 0,1,-1),n=110)}) | |
data$weight[] = NA | |
# buy an 100% if cud greater than 10 | |
# buy 50% if cud between 0 and 10 | |
# exit below 0 | |
# this is not advice and is a bad system | |
# please do not use for real money | |
data$weight[] = iif(cud >= 10, 1, iif(cud >= 0 & cud <10, 0.5, 0)) | |
cud.plus = bt.run(data, trade.summary=T) | |
#***************************************************************** | |
# Create Report | |
#****************************************************************** | |
plotbt.custom.report(cud.plus, sma.cross) | |
#to compare to buy hold then | |
plotbt.custom.report(cud.plus, buy.hold) |