Code covered by the BSD License  

Highlights from
Classical Pairs Trading Using MatLab

4.66667

4.7 | 9 ratings Rate this file 138 Downloads (last 30 days) File Size: 115.53 KB File ID: #13171
image thumbnail

Classical Pairs Trading Using MatLab

by Marcelo Perlin

 

29 Nov 2006 (Updated 10 Sep 2010)

Performs the classical pairs trading strategy with user defined parameters

| Watch this File

File Information
Description

This function performs the classical pairs trading framework in a given set of prices
  
From Wikipedia, the free encyclopedia:
“The pairs trade was developed in the late 1980s by quantitative analysts and pioneered by Gerald Bamberger while at Morgan Stanley. With the help of others at Morgan Stanley at the time, including Nunzio Tartaglia, Bamberger found that certain securities, often competitors in the same sector, were correlated in their day-to-day price movements. When the correlation broke down, i.e. one stock traded up while the other traded down, they would sell the outperforming stock and buy the underperforming one, betting that the "spread" between the two would eventually converge.”

Source: http://en.wikipedia.org/wiki/Pairs_trade#Algorithmic_pairs_trading
See also the great book “Demon of Our Own Design” by Richard Bookstaber, which provides an interesting background for the pairs trading strategy.

There are many ways you can implement the pairs trading framework. For this package, I used a very simple set of rules. Details can be found within code’s comments.

Please note that this package has been developed over the years and it will no longer exactly replicate the results from my 2007 paper.

Qualities of the package:
- Handles any number of assets
- Outputs separately the plot for the total cumulative profit from the long, short and combined positions.
- Outputs all trades, including traded prices and time of trades.
- Provides the user a large amount of input choices for the pairs trading algorithm, including:
* amount of money to put in each position (long and short)
* value of transaction cost (in money unit)
* size of moving window for finding the pairs over the price data
* periodicity of pairs updates
* maximum number of days to keep any trade (without convergence).
* value of threshold variable
  
Main references related to the pairs trading strategy are:
  
NATH, P. (2003) "High Frequency Pairs Trading with U.S. Treasury Securities: Risks and Rewards for Hedge Funds", Working Paper, London Business School.

GATEV, E., GOETZMANN, W. N., ROUWENHORST, K. G. (1999) ‘Pairs Trading: Performance of a Relative Value Arbitrage Rule’, Working Paper, Yale School of Management. Available at SSRN: http://ssrn.com/abstract=141615.
  
PERLIN, M. S. (2009) ‘Evaluation of Pairs Trading Strategy at the Brazilian Financial Market’
Journal of Derivatives & Hedge Funds, V. 15, N. 2, pp. 122-136.

PERLIN, M. S. (2007b) "M of a Kind: A Multivariate Approach at Pairs Trading". Working Paper. available at SSRN: http://ssrn.com/abstract=952782.

Required Products Statistics Toolbox
MATLAB release MATLAB 7 (R14)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (24)
10 Sep 2007 vincent leclercq

hi Marcelo,

Good work, well explained. Could you please tell where the data provided with the example is coming from ?

20 Nov 2007 Marcelo Perlin

The data is from the brazilian equity Market (the paper at SSRN).

But not the full period of the research, just a small part and for a portion of the assets.

Honestly, this was a long time ago. I don't remember which period and which assets were included, but the data is from real assets (not simulated).

11 Feb 2008 Gabri Jo

Excellent

19 Feb 2008 Jon Zimmerman

I think there is an error in pairs.m when the correlation method is used as many stocks seem to be paired with themselves.

you may want to replace:

    corr=corrcoef(x);
    [idx1,idx2]=find(corr==1);
    corr(idx1,idx2)=0;

with corr = corr - diag(diag(corr)) to zero out the diagnol;
    

20 Feb 2008 Marcelo Perlin

Hi Jon,

You're right. I wrote this function for using it in the ssrn paper. In there I didn't used the correlation method. And I never bother to check if it was working. The 'correlation' method is not even an option to the main algortihm. If you wanted to change it, you would had to go to the m file and write it.

Somehow the line corr(idx1,idx2)=0; doesn't take the diagonal out as it should be. Your approach will work, but I'm changing it to corr(corr==1)=0; which I think its a little more intuitive. The new files whould be up tomorrow.

Thanks for your message and rating. I really appreciate it.

Cheers.
Marcelo.

01 Apr 2008 Jia Geng

Hey, Marcelo,
 this is a great program. And I am working on this topic too. I always have a question about the data. As Gatev mentioned in his paper, he used the data covering from 1962 to 2002. How did he(how did you)handle the missing values? There got to be some missing values(for example, trading closed for a particular stock). Thanks again for your grogram.

06 Apr 2008 Modiri Kgomari  
23 Dec 2008 bob

hello everybody,

marcelo you normalise equities values by ''(value-mean)/std deviation'' it seems to me it's not realistic since all equities do not follow a normal distribution.

how can you justify such a behavior ??

nevertheless the technical code are very well done, congratulations.

maxime

05 Jan 2009 Marcelo Perlin

Hi Bob,

Thanks for you comment.

Sorry for the late reply (vacation was great!).

I agree with you that the way I normalized the time series of prices is not realistic. At the time I wrote the paper (which produced the matlab code) that seemed to be a good way of bringing all stock prices to the same unit but, today, I can see the drawbacks.

I cannot change the paper (it is already published) but I'm going to add another option of normalization to the algorithm.

Regards,
Marcelo.

01 Apr 2009 V

edited your "pairs.m" file. noticed that when you use the least-squares sum method, the matrix you get is symmetric. therefore, I calculated the lower triangular only, 'nan' on diagonals, and copied over the transpose cell pair. When I set ut very low, this speedup really shows:

for i=1:n2
    for j=1:(i-1)
        Qdist(i,j)=sum((x(:,i)-x(:,j)).^2);
        Qdist(j,i)=Qdist(i,j);
    end
    Qdist(i,i)=nan;
end

last note: i don't think you use pairs(:,2)=m'; anywhere in the parent scripts, maybe you could eliminate it.

03 May 2009 Marcelo Perlin

Hi V,

Thanks for you comment.

Sorry for the late reply. I just realized you comment on file.

Yes, this would speed up the algorithm, but, it also degenerates the Qdist matrix with respect of the infomration I need to find the pairs with the pairs(:,1)=n; command. .

So, I employed a quick fix based on your idea. See new updated file for details.

Again, thanks.
Cheers.
Marcelo.

27 Oct 2009 Felipe

Ola Marcelo,
gostei muito do seu trabalho, e como trabalho no mercado, gostaria de saber mais sobre sua metodolgia de aplicacao. Como estou apenas comecando a me aprofundar nos estudos talvez precise de sua ajuda futuramente. Tenho interesse em aplicar seus estudos na pratica.
Se tiver interesse de discutir algumas ideias ficaria grato.

Obrigado,

Felipe
felipecfreitas@hotmail.com

29 Oct 2009 Marcelo Perlin

Oi Felipe,

Agradeco pelo interesse no programa.
Favor entrar em contato pelo meu email.

Abraco.
Marcelo.

19 Nov 2009 ?ahinder

And I have a problem about to creating mat-file . I must to use my excell data .How can I solve this problem. I think that "Creating Mat file in C "; but I can not solve my problem because I don't have enough source . Could you help me?? Thanks for your all attention.

Şahinder Kaya
İstanbul Technical University

Mathematical Engineer

kayasah@itu.edu.tr

19 Nov 2009 ?ahinder

Hello Marcelo,
I am student in Turkey, at Istanbul Technical University.And I have a problem about to create mat-file . I must to use my excell data .How can I solve this problem. I think that "Creating Mat file in C "; but I can not solve my problem because I don't have enough source . Could you help me?? Thanks for your all attention.

Şahinder Kaya
İstanbul Technical University

Mathematics Engineering
kayasah@itu.edu.tr

20 Nov 2009 Marcelo Perlin

Hi Sahinder,

HAve a look in matlab help files. There you can find all the information about importing xls data into matlab.

Cheers.
Marcelo.

20 Jan 2010 Ahmet Seker

Hi Marcelo,

I have a question or comment. In the main file, just after line 150 you check whether a position is open and enter a trade if not. It seems to me that if say 1 is paired with 3 and 3 paired with 1, then these will be entered as two different trades. You will enter the same trade twice. Isn't this problematic?

Thanks,

Erdal

20 Jan 2010 Ahmet Seker

Another thing is that the t-parameter is not explained clearly in the code. Since all values are normalized and the difference of two standard normal is distributed N(0,2) so setting t=2 means spread is one standard deviation away from the mean, and t=4 two std away from mean. Am I right?

Thanks,

Erdal

20 Jan 2010 Ahmet Seker  
21 Jan 2010 Marcelo Perlin

Hi Ahmet,

My replies by email.

Regards.

14 Apr 2011 Fuzhi Cheng

Hi, Marcelo:

Would it better to build a cointegration analysis into the pair selection process? Basically you pick the pairs, triples or whatever, that are cointegrated. That seems to be more statistically sound.

Thanks. Fuzhi

14 Apr 2011 Marcelo Perlin

Hi Fuzhi,

Econometrically speaking, yes, it would be more intuitive to pick pairs according to a cointegration analysis since it implies a stationary residual from the linear transformation.

But, what I set out to do in the code was to have the simplest case of pairs trading. This justify the use of quadratic error and correlation to find the pairs.

Regards,
Marcelo.

19 Apr 2011 Nicolasillo

Hi Marcelo,

It may sound like a stupid question, as I just started using MatLab, but in the description of the algorith is says that "Outputs all trades, including traded prices and time of trades. "

Actually, I am having difficulties in finding the actual file that reports these.

Thank you!

19 Apr 2011 Nicolasillo

Actually I found the info I needed.
Thank you!

Please login to add a comment or rating.
Updates

I've sended the wrong file at the last update...

08 Dec 2006

Update on Zip File

11 Dec 2006

Another update on zip file

20 Dec 2006

Update on Description file to include SSRN Paper location

22 Jan 2007

Another change.

26 Feb 2007

The last version's output was too messy. I'm simplifying it by using structures.

11 Apr 2007

Update on Description

02 Jul 2007

* Added outputs to screen

09 Jul 2007

* Corrected minor bug at error checking lines. * Added Example_Script.m

05 Nov 2007

Organized directories in zip file

27 Dec 2007

* Updated a few things

20 Feb 2008

Fixed bug Pointed by Jen Zimmerman by erasing parts of the code. Again, thanks for the message. And also removed the use of price2ret.m (from financial toolbox).

03 May 2009

Changed the whole algorithm for a more intuitive structure. Also added more input options and outputs.

06 May 2009

Changed description

07 May 2009

Fixed the code for the cases where there is no need to trade when the position is already open

07 Nov 2009

Fixed bug for the cases where there was an ongoing trade and the respective pair had changed.

31 Jan 2010

Fixed the cases where pair of i is j and pair of j is i. When this happened, the trades were duplicate. See comment by Ahmet Seker.

10 Sep 2010

Implemented a better solution for the cases of duplicated trades (pair of asset i is j and pair of asset j is i).

Tag Activity for this File
Tag Applied By Date/Time
finance Marcelo Perlin 22 Oct 2008 08:50:41
modeling Marcelo Perlin 22 Oct 2008 08:50:42
analysis Marcelo Perlin 22 Oct 2008 08:50:42
financial modelling Marcelo Perlin 22 Oct 2008 08:50:42
quantitative strategy Marcelo Perlin 22 Oct 2008 08:50:42
pairs trading Marcelo Perlin 22 Oct 2008 08:50:42
quantitative strategy Alexander 21 Mar 2009 11:56:16
finance Peter 08 May 2009 03:07:31
analysis Daniel 29 Dec 2009 09:16:39
financial modelling Daniel 29 Dec 2009 09:16:46
pairs trading Imed 09 Sep 2010 15:24:25
quantitative strategy w z 09 Mar 2011 19:29:51
pairs trading JL Arenas 25 Apr 2011 15:08:19
pairs trading Junghoon 29 Aug 2011 05:33:18

Contact us at files@mathworks.com