Example of Back Testing

 

This formula is a simple trading system using a 20 day moving average and a 60 day moving average. The idea is that the 20 day moving average represents the thinking of more recent investors whereas the 60 day moving average represents the thinking of investors that have been in the stock under consideration for at least 60 days. The idea is that if the 20 day moving average is crossing over the 60 day moving average, this means that the new investors are more bullish - and vice versa. This only takes a few lines to implement. 

 

CLOSE  = LOAD(_TICKER,CLOSE)
VOLUME = LOAD(_TICKER,VOLUME)
RANK1  = INIT(_TICKER)
RANK2  = INIT(_TICKER)
STOPIF(CLOSE,2)
MA20 = MOVAVG(ICLOSE,20)
MA60 = MOVAVG(ICLOSE,60)
RANGE(-200,_LASTDATE)
BACKTEST: SELLWHEN(SHARES,(MA20(i)>MA60(i)),"Sell all shares we have when MA averages cross")
BACKTEST: BUYWHEN((CASH/CLOSE(i)),(MA20(i)<MA60(i)),"Use all the cash we have to buy shares when MA averages cross")
BACKTEST(CLOSE(i))
RANK1 = VARIABLE(PROFIT)
GRAPHAREA(75,25,0,0,0,0)
GRAPHLINE(Axis1,CLOSE,RED)
GRAPHLINE(Axis1,MA20,BLUE)
GRAPHLINE(Axis1,MA60,GREEN)
GRAPHBAR(Axis2,VOLUME,GREEN)
GRAPHLABEL(Axis1,Price)
GRAPHLABEL(Axis2,Volume)
GRAPHTITLE(_TICKER)
GRAPHSUBTITLE(Back testing results)
GRAPHTRADES()

 

 

Click here to view output image

Click here to view output in 'Back Test Results'.