I am looking for some tips on how to run a simple backtest on a pairtrading strategy intraday using eg. 30minute bars.
I have calculated the spread, beta(=ratio/hedgeweight), and standard deviation.
I tried treating the spread as a simple instrument which I buy and sell, but realised I cannot do it because the returns from eg. 0 to 0.2 becomes infinite. The spread crosses zero multiple times.
I tried using the quantstrat package, but unfortunately it seems a bit complex and overkill at this point.
After lots of looking around and trial and error I ran into the PairTrading package. This works great for daily data and especially with the included data. With intraday data it seems to have problems calculating the returns correctly. It calculates the returns for the legs independently but somewhere it goes wrong.
What would be the easiest way to get the returns from a simple intraday pairtrading strategy?
Here is some sample code I have where I try to use the PairTrading package.
Backtesting code below
sig <- ifelse(sprd < -2*standarddev, 1, NA)
sig <- ifelse(sprd > 2*standarddev, -1, sig)
sig <- na.locf(sig)
ret <- Return(pair, lag(sig), lag(beta))
ret <- (100 * cumprod(1 + ret))
plot(ret)
Unfortunately this doesnt give right results. In the picture can be seen how the returns just continue up although spread still increases.
