Thinking in SIT20 Nov 2014
The best way to understand how SIT works, in my opinion, is to do a side by side strategy test in Excel and one using SIT.
Let’s do step by step in investigating following simple strategy:
- allocate 50% to Stock if it’s prices is above 20 day moving average
- allocate 50% to Bond if it’s prices is above 20 day moving average
To code this strategy in Excel, we for example can downloaded historical prices for SPY and TLT. For simplicity, I generated in columns B and C Stock and Bond time series. To simulate each asset i used:
- Stock: 10% return and 20% volatility
- Bond: 4% return and 2% volatility
Next create columns D and E to hold 20 period moving averages for Stock and Bond. I.e. the formula in column D is D21 =AVERAGE(B2:B21) and in column E is E21 = AVERAGE(C2:C21)
Next create columns F and G to hold signals. I.e. F21 = IF(B21>D21,0.5,0) and G21 = IF(C21>E21,0.5,0)
To run back-test, we also need to compute asset returns, let column H and I hold Stock and Bond returns. I.e. H3 =B3/B2-1 and I3 = C3/C2-1
Finally store strategy returns in column J. I.e. J21 = F21H21+G21I21 (RET = W.Stock * RET.Stock + W.Bond * RET.Bond) and convert strategy returns into levels in column K. I.e. K3 = K2*(1+J3)
Let’s for the moment think that Excel is one big matrix, in this case:
- columns B and C hold asset’s prices
- columns D and E hold asset’s 20 day moving averages
- columns F and G hold asset’s signal. I.e. allocate 50% if prices > 20 day moving average
- columns H and I hold asset’s returns
- column J holds strategy’s return: RET = W.Stock * RET.Stock + W.Bond * RET.Bond
- column K holds strategy’s price
To convert this matrix into SIT, we need:
- download historical prices for SPY and TLT and align them so that dates on both series match
- compute 20 day moving averages for both assets
- compute signal: allocate 50%, if price > 20 day moving average
- compute strategy’s returns
Load historical data for SPY and TLT, and align it, so that dates on both time series match. We also adjust data for stock splits and dividends.
Next let’s compute 20 day moving averages and signals. Please note that we are working with matrices below, so all computations are done for both time series with just single statement.
Now we ready to back-test our strategy:
Finally create report:
|Period||Jul2002 - Dec2014|
Ok we done.
So what is easier Excel worksheet or R back-test, both take some time to get used to. However, in the long run, R back-test will save you time because if you want to re-run it another day, you don’t need to worry about updating data and recalling strategy rules.
The data is auto update and adjusted. The strategy rules are stored in code with your own comments.
Dan at Theta Trend posted a great example and tutorial on backtesting system in Excel. Please read Dan’s post at Backtesting A Basic ETF Rotation System in Excel Free Download
Let’s see how this strategy can be backtested using SIT:
|Period||Nov2004 - Dec2014||Nov2004 - Dec2014||Nov2004 - Dec2014|
I think there is less chance of data error using SIT, and also update is automatic and takes no time on your part.
The key take away is to think about Excel operations as matrix operations to create a matrix with signals / allocations. This matrix with signals / allocations is used by SIT to create and display back-test(s).
(this report was produced on: 2014-12-07)