Pairs Trading: deviation between two stocks
Author: robertwcass
Creation Date: 10/11/2012 6:46 PM
profile picture

robertwcass

#1
Hello All. New to WLP and would like to see if someone could help me with the code for an idea I have. I have two index stocks that are long and short on the same index. I have noticed that they do not follow each other perfectly. That is, if you buy both at the same time they don't always cancel each other out for a profit/loss = 0. One of the stocks goes up or down at a slightly higher percentage (or rate) than the other, creating a divergance in the percentages that can either equal a small profit/loss if you sell both simultaneously via multi-trades in ATP.

So the key is to buy both stocks simultaneously and, when the moment is right, sell them simultaneously. Here's the psuedo code:

When the deviation between stock a and stock b is < 0.25%, then buy both. When the deviation between stock and and b is > 0.5% then sell both.

Hope this makes sense and I would appreciate any help with the code.

Thanks, R
profile picture

robertwcass

#2
Just noticed a flaw in the following:

When the deviation between stock a and stock b is < 0.25%, then buy both. When the deviation between stock and and b is > 0.5% then sell both.

This should read:

When the price difference between stock a and stock b is < 0.25% then buy both. When price difference between stock a and and b is > 0.5%, and the price difference is a positive number, then sell both.
profile picture

Eugene

#3
Welcome to the forums,

For a newbie, it's always a good idea to explore heritage the community has accumulated through uploaded strategies and forum posts. Like these ones:

Pairs Trading

ActiveTrader 2010-10 | Market-neutral VIX-based pair strategy
profile picture

robertwcass

#4
Hi Eugene,

Thanks for the feed back. I actually read both those pairs trading strategy and they don't address pairs trading the way I would like to. I tried to modify the code for "Pairs Trading Strategy" to match more closely what I have written above. Both the strategies you provide links for have both long and short positions.

Any help with the code for my psuedo code above would be greatly appreciated.

Thanks,

R
profile picture

robertwcass

#5
Hi Eugene,

Should I have put this post initially in the Wealth-Lab Script section instead of in the strategy section?
profile picture

Eugene

#6
Hi Robert,

No, the placement is correct. If I understood your price difference correctly, I think it comes down to this. If both symbols are in the same DataSet, comprised of just these 2 symbols and nothing else, you can run this in multi-symbol mode. Just mind the position sizing in portfolio simulation mode to avoid skipped trades i.e. don't set at it e.g. 50% equity.

CODE:
Please log in to see this code.
profile picture

robertwcass

#7
Hi Eugene,

Thanks for this. It does run but doesn't give me the results I was expecting. Since the only time I buy is when the difference is less than 0.25% and the only time I sell is if the difference is greater than 0.50%, I feel there should be no loosing trades. Yet in the results I get losses. I would expect to have no gains and no losses, or gains but no losses.

Also, when I execute the strategy it only returns 4 trades. Any ideas what might be going on?

Thanks again for your help.

R

The results below for Sqqq as stockA and qqq as stockB

All Trades Long Trades Short Trades Buy & Hold
Net Profit ($1,430.91) ($1,430.91) $0.00 ($303.66)
Profit per Bar ($0.06) ($0.06) $0.00 $0.00
Total Commission ($31.80) ($31.80) $0.00 ($15.90)

Number of Trades 2 2 0 2
Average Profit ($715.46) ($715.46) $0.00 ($151.83)
Average Profit % -14.51% -14.51% 0.00% -4.08%
Average Bars Held 12,939.00 12,939.00 0.00 108,445.50

Winning Trades 1 1 0 1
Win Rate 50.00% 50.00% 0.00% 50.00%
Gross Profit $883.24 $883.24 $0.00 $3,959.79
Average Profit $883.24 $883.24 $0.00 $3,959.79
Average Profit % 17.71% 17.71% 0.00% 79.69%
Average Bars Held 12,954.00 12,954.00 0.00 166,156.00
Max Consecutive Winners 1 1 0 1

Losing Trades 1 1 0 1
Loss Rate 50.00% 50.00% 0.00% 50.00%
Gross Loss ($2,314.15) ($2,314.15) $0.00 ($4,263.45)
Average Loss ($2,314.15) ($2,314.15) $0.00 ($4,263.45)
Average Loss % -46.73% -46.73% 0.00% -87.86%
Average Bars Held 12,924.00 12,924.00 0.00 50,735.00
Max Consecutive Losses 1 1 0 1

Maximum Drawdown ($1,430.91) ($1,430.91) $0.00 ($3,994.54)
Maximum Drawdown Date 9/14/2012 9/14/2012 4/12/2004 11/21/2008

Profit Factor 0.38 0.38 0.00 0.93
Recovery Factor 0.00 0.00 0.00 0.00
Payoff Ratio 0.38 0.38 0.00 0.91
profile picture

robertwcass

#8
Hi again Eugene,

Not sure if this is relative in relationship to the code you provide, but this is a day trading strategy and should execute as many times as the thresholds are hit.

R
profile picture

robertwcass

#9
Also, the share quantity should always stay stable at 1000 shares (bars?) for each stock both on the buy and sell sides.

Any thoughts?

profile picture

Eugene

#10
QUOTE:
Any ideas what might be going on?

This was your request:
QUOTE:
When the price difference between stock a and stock b is < 0.25% then buy both. When price difference between stock a and and b is > 0.5%, and the price difference is a positive number, then sell both.

And this is the formula:
CODE:
Please log in to see this code.

If the results weren't as expected, you should give a more thorough explanation and a formula of price difference as you understand it.