Pair Trading - Exploring The Low Risk Statistical Arbitrage Trading Concepts

ncube

Well-Known Member
#21
How to analyze the stocks for Pair Trading:

Core Idea: Stocks in the pair move together but one of them will move more than the other in the same direction.

Activities to be done at end of day to find the stock pairs:
1. Update the stockdata.csv sheet with the closing values for each of the stocks that you want to track. This data can be downloaded from NSE site or from your regular data source. Ensure the data is cleaned for bonus & splits.
2. Identify the stock pairs that you are interested in based on your fundamental judgement..for example stocks from the same industry.
3. Find the co-integration pvalue for the stock pair. Note co-integration pvalue for stocks S1,S2 is not same as S2,S1. Hence you may want to check both the combination. A pValue of less than 0.05 is consider significant. Lower the better.
4. Once you find the stock pair S1,S2 which are significant, check what is their zScore, The first stock S1 is considered as Y stock & S2 is considered as X stock. If the zScore value is +ve, then the stock Y is considered as overvalued and higher probability of it to move down and if zScore value is -ve, the the stock Y is considered undervalued and has higher probability to move up in coming days. The further the zScore value from 0, the better.
5. If the zScore is as per our expectation, we need to identify the quantity of the stocks to trade. Since we want to keep the percentage of price movement same for both the stocks, we need to fix the rupee value for both the stocks same. For example if stock S1 closing price is 100, and our fix rupee value is 25000 for each stock then we can trade 25000/100 = 250 quantities of stock S1.

Summary at the end of day:
1. We have identified the 2 stocks S1,S2 for next day pair trading.
2. We have identified the direction for S1 & S2 to bet on.i.e which one to short and which one to long.
3. We have identified the quantities of each stocks that we will be trading.

In the next posts I will explain the 3 Pair trades that I executed on 24th July and the reasoning behind the trades.
 

ncube

Well-Known Member
#22
Pair-1: ALBK-ANDHRABANK - Traded on 24-07-2018:

1. Identified this pair on 23-07-2018 evening: S1=Y=ALBK, S2=X=ANDHRABANK
2. ALBK Closing price = 41.50, Quantity to trade = 25000/41.5 = 602
3. ANDHRABANK closing price = 31.3, Quantity to trade = 25000/31.3 = 799
4. zScore: +2.6
5. zScore is +ve, hence I will look to short the Y stock, i.e ALBK and Long the X stock, i,e ANDHRABANK

ALBK-ANDHRABANK.png


On 24-07-2018 wait for the 1st 30 min candle to be formed:
ALBK.png

ANDHRABANK.png

1. Place a SL Sell MIS order for ALBK below the low of 1st 30 min candle and place a SL Buy MIS order for ANDHRABANK above the high of 1st 30 min candle.
2. Our assumption is that both the stocks will move in same direction but one stock will move more than the other.
3. In the 2nd 30 min bar, ANDHRABANK buy is executed, but also the ALBK sell order is executed. Which means our assumption is invalidated. We can look to close these trades at breakeven or at a small loss.
4. However looking at the price action after the 2nd 30 min bar, it was clear that the trend is clearly up as the ANDHRABANK is not gone down much and ALBK has recovered, so instead of closing the trades, I decided to make it a directional trade [Not recommended for low risk traders] and placed a buy reverse order for ALBK at the top of the 2nd 30 min bar.
5. I closed both the trades at about 2:45 in the afternoon, by which time ALBK had recovered some of the initial loss and ANDHRABANK was in profit from the beginning.
 

ncube

Well-Known Member
#23
Pair-2: TATAMTRDVR-TATAMOTORS - Traded on 24-07-2018:

1. Identified this pair on 23-07-2018 evening: S1=Y=TATAMTRDVR, S2=X=TATAMOTORS
2. TATAMTRDVR Closing price = 141.75, Quantity to trade = 25000/141.75 = 176
3. TATAMOTORS closing price = 258.75, Quantity to trade = 25000/258.75 = 97
4. zScore: -3.2
5. zScore is -ve, hence I will look to long the Y stock, i.e TATAMTRDVR and short the X stock, i,e TATAMOTORS
TATAMTRDVR-TATAMOTORS.png

On 24-07-2018 wait for the 1st 30 min candle to be formed:
TATAMTRDVR.png
TATAMOTORS.png


1. When I checked these 2 pairs at around 9:45, the 1st 30 min candle was already large, and since we need to short the TATAMOTORS as per our analysis, it did not make sense keeping it at the low of such a large 30 min candle. So I shifted to 15 min candle chart.
2. Placed a SL Sell MIS order for TATAMOTORS at the low of the 2nd 15 min candle and Placed a SL Buy MIS order for TATAMTRDRV at the top of the 2nd 15 min candle.
3. As the trend was down only the TATAMOTORS short was executed and TATAMTRDRV trade was not executed.
4. I closed the trades at 2:45PM with TATAMOTORS making some profit.
 

ncube

Well-Known Member
#24
Pair-3: BANKBARODA-UNIONBANK - Traded on 24-07-2018:

1. Identified this pair on 23-07-2018 evening: S1=Y=BANKBARODA, S2=X=UNIONBANK
2. BANKBARODA Closing price = 126.30, Quantity to trade = 25000/126.30 = 198
3. UNIONBANK closing price = 78.95, Quantity to trade = 25000/78.95 = 317
4. zScore: +2.4
5. zScore is +ve, hence I will look to short the Y stock, i.e BANKBARODA and long the X stock, i,e UNIONBANK

BANKBARODA-UNIONBANK.png

On 24-07-2018 wait for the 1st 30 min candle to be formed:
BANKBARODA.png
UNIONBANK.png

1. This is a classic trade and went exactly as per analysis.
2. Placed a SL Sell MIS order for BANKBARODA at the low of 30 min candle and a SL Buy MIS order for UNIONBANK at the top of the 1st 30 min candle.
3. As the trend was up UNIONBANK buy order got executed and BANKBARODA did not get executed.
4. Closed this trade at 2:45pm with UNIONBANK trade in profit.
 
Last edited:

VJAY

Well-Known Member
#25
Dear ncube,
How you get pValue of pairs?
 

VJAY

Well-Known Member
#27
It's the just the co-integration significance value. There is built python library to get this detail it check the stationarity and ADF test and gives the pValue as output. I have included this function in the PairTrading jupyter notebook that I have shared in earlier post.
Dear ncube,
Thanks ...am still not done installation python after it stuck in initial face...am now away from home when I come back retry ...will ask if any difficulty happen...please help me....
Also share some info about positional trading too in pairtrading....atleast a basic touch....
Thanks a lot sharing everything with us
 

ncube

Well-Known Member
#28
Dear ncube,
Thanks ...am still not done installation python after it stuck in initial face...am now away from home when I come back retry ...will ask if any difficulty happen...please help me....
Also share some info about positional trading too in pairtrading....atleast a basic touch....
Thanks a lot sharing everything with us
Pair trading logic is same across all time frames, however when the trades are taken overnight one need do take few additional considerations as follows:
1. One can carry short position only in futures, hence the pair sizing would be based on lot sizes and traded in futures. If the lot sizes are not comparable for the 2 stocks then need to combine with cash buy.
2. The stocks need to be liquid enough and preferably pairs which are not commonly tracked by bigger players have better chances of success.
3. One would need to apply additional analysis methods to identify the stock pair based on fundamentals, data analytics, machine learning models etc
4. Have to use some regression analysis such as linear regression to identify the beta & intercept between the 2 pairs so that the buy & sell quantities are in proper ratios.
5. Some backtesting would be required for the pairs in order to identify the best time to trade the pairs and for what Zscores is ideal for each pairs to Identify the profit/loss targets and risk management rules.

Pair trading is a very simple form of statistical arbitrage, after one has gained some experience it can be easily extended to multiple non related stocks and make them co-integrate as a group. For example, Long Stock S1 & short 2 stocks S2,S3 in a ratio such that they together co-integrate. [Say Nifty index with few of its stock constituents]
 

superman

Well-Known Member
#29
Amazing thread ! Thank a lot ncube ! Looking forward for more such analysis
I am also trying out pair trading in Excel. Need to move towards ipynb soon

What you feel about more macro co-relation trades like crude oil v/s nifty , interest rate v/s nifty etc. I know its not exactly a pair trading but many say it gives a long term outlook , I didnt find any notable correlation between nifty and other asset ; Just wondering if you have done any research on this.
 

VJAY

Well-Known Member
#30
Ok this is becoz windows in not able to find python in the default path. While installing python please select the option Add python 3.7 to PATH.
Once installation is done, better to restart the pc once so that this information is updated in the registery.
View attachment 26890
Dear ncube,
I tryed your instructions also some other help which available in google but still am unable to do this pip install :( ...I think the problem is file location .many solutions available in google..I tryed almost all

Untitled.png
 
Last edited: