Take the 2-minute tour ×
Quantitative Finance Stack Exchange is a question and answer site for finance professionals and academics. It's 100% free, no registration required.

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.

enter image description here

share|improve this question

migrated from stackoverflow.com Aug 3 '12 at 12:21

This question came from our site for professional and enthusiast programmers.

2  
My recommendation is to understand what exactly this package is doing in the daily case first. It is impossible to know what the problem is on an intraday basis without knowing where your potential missteps would be. For instance, one problem on an intraday basis is that the data may not be available for both securities at the same period of time. Also, not all of the securities may trade every minute of every trading day, potentially leading to gaps. There are a lot of issues that you have to be careful about when working with higher frequency data. –  John Aug 3 '12 at 15:37
    
(1) Your example is not reproducible. (2) You should NOT use all the data to calculate your hedge ratio (3) With intra-day data, you probably need to account for bid-ask spread (4) Do you really want returns? Most intraday traders use enough leverage that they care more about PnL than returns. (5) Once you have a signal for the spread, you can easily apply it to each leg separately. (6) Last I looked at the PairTrading package, I thought they got some things backwards. Maybe they just look at things differently than me, but I could not figure out how to get their results to match my own. –  user508 Aug 4 '12 at 15:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.