Systematic Edge Backtest Engine

In this post, I’d like to share some tools that the individual investors can use to backtest trading strategies. R is a powerful computing language that is composed of many statistical tools. I have taken and compiled several open source code and put together what I believe to be a easy to use backtesting interface. My functions act as wrappers so that it covers the unnecessary details for the end user.

The current frame work is very slow but it is very powerful as it has the capabilities of testing multiple strategies across multiple portfolios. For the user who want more customization, please visit my github page for the detail codes.

This is an example of simulating two trading strategies on a single portfolio; both are based on Faber moving average. First strategy is long when close > SMA(100) and for the sake of simplicity, the second one is when close > SMA(200). Note, the strategy is for demonstration purposes only. Here are the summary images created automatically.

The tools are currently in its infancy. There are a lot more features that I would like to add to the toolbox. The code is open source and I will organize it to put it up on github to share it. The following is the code that generated the backtest and it can be done in less than 20 lines of code! Any comments would be appreciated.

</p>
# Multi-Strategy Testing

#################################################################################
# 1. initialize backtest parameters
# (start date, end date, initial equity)
# and test name
#################################################################################
test.name<- "test1"
testParameters<-bt.testParameter("2011-01-01","2012-01-01",100000)
#################################################################################
# 2. create a list of symbols
#################################################################################
symbols<-c("SPY")
#################################################################################
# 3. get the adjusted data
#################################################################################
bt.dataSetup(symbols, "USD", 1, testParameters,adjust="True")

SPY.Strategy1<-SPY
SPY.Strategy2<-SPY

#################################################################################
# 4. configure indicator
# use ttr package to link indicators to each instrument
#################################################################################
SPY.Strategy1$SMA.100 = SMA(Cl(SPY.Strategy1), 100)
SPY.Strategy2$SMA.200 = SMA(Cl(SPY.Strategy2), 200)
n<-200
symbols<-c("SPY.Strategy1","SPY.Strategy2")

#################################################################################
# 5. Set up trading rules
# signal based traiding rule
#################################################################################

#for multi strategy
SPY.Strategy1$signal =(Cl(SPY) > SPY.Strategy1$SMA.100) + 0
SPY.Strategy2$signal =(Cl(SPY) > SPY.Strategy2$SMA.200) + 0
#################################################################################
# 6. Setup Portfolio and Account Object in Blotter
#################################################################################
bt.globalTestSetup(test.name, symbols, testParameters)
#################################################################################
# 7. Run Strategy
#################################################################################
bt.run(symbols,test.name,n)

#################################################################################
# 8. Performance Graphing
#################################################################################

equity<-getAccount(test.name)$summary$End.Eq
perf<-computePerformanceStatistics(equity)
bench<-generate.benchmark(c("SPY"),testParameters)
plot.calendarReturn(perf)
plot.equity(equity,bench,testParameters)

<strong>
Advertisements

2 comments

  1. This looks good – is the source available on github or any place else? I tried copying & pasting the code above, but it fails at bt.testParameter – I guess it’s from some package I haven’t installed

    1. Hi Patric

      Sorry to disappoint but I’ve stopped contributing to this project. It was initially a project to get myself acquainted with back testing in R. Now I do most if my analysis in SIT which is another open source environment for financial back testing in R. It’s is a lot more mature compared to what you see here.

      M

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s