Pair Trading - Exploring The Low Risk Statistical Arbitrage Trading Concepts

ncube

Well-Known Member
Dear ncube,
If we trade pair trading as positional then it must be in Futures...so we use cash data to find the pairs and zscore then enter in futures am right?How you entry take at morning ?is it same way as intra using 1st 30 minuits or any other way?Also please share the steps to balance lots as lot qty is different....
1. For positional Pair Trades, we dont bother about the stock trend direction, hence I want to trade the prices as near to the previous day close price (Used for calculating the zScore) as possible.
2. I will place Limit BUY/SELL orders at previous day closing price and wait for either 1 of the trades to get executed, if only one gets executed at the limit price I modify the other to market and execute the trade. This will to some extent ensure we got the price at the zScore level.
3. Yes, we need to use Futures to take trades but use the cash segment closing prices for our zScore calculations and trading. Actually I use futures for short trade, and use cash for the long trade as it allows me to dynamically maintain the hedge ratio. However if the lot size of long trade is achieved by futures then I take futures long trade and the difference in cash.

How to measure the hedge ratio for pair trading:
1. The classic approach to find the hedge ratio is to run a linear regression on the 2 stocks for the period say 20 days. What actually the linear regression does is that it provides a mathematical formula in the below format which explains how the 2 stocks X & Y prices are related in the last 20 days.

Y(Stock1 price) = (Some constant) + Beta * X (Stock2 Price) + (some error value)

2. The important parameter in the above formula is Beta. So using Beta we can calulate what should be the size of each stock for proper hedging. Lets say the beta value it 0.5, the the formula can be simplified to:

Y = 0.5 * X (ignore constant & error)

This means if we buy 100 shares of X then we need 50 shares of Y for hedging.

In most of the books and articles that you read on pair trading, you may find that once the trade is entered one just waits for the Zscore to mean revert, however I find this not a correct approach as there would be lot of drawdown if the pairs do not start to mean revert immediately. Hence I prefer to manage the trade daily using the rolling beta approach. I.e I run the linear regression on the 2 stocks every day and measure the stock daily position size based on the new beta value for that day, I will then sell or buy more of the Long stock so that the hedge is maintained on daily basis.This helps in reducing the volatility in the portfolio and drawdown is reduced. And once the zScore mean reverts I will close both the trades.
 
Last edited:

VJAY

Well-Known Member
1. For positional Pair Trades, we dont bother about the stock trend direction, hence I want to trade the prices as near to the previous day close price (Used for calculating the zScore) as possible.
2. I will place Limit BUY/SELL orders at previous day closing price and wait for either 1 of the trades to get executed, if only one gets executed at the limit price I modify the other to market and execute the trade. This will to some extent ensure we got the price at the zScore level.
3. Yes, we need to use Futures to take trades but use the cash segment closing prices for our zScore calculations and trading. Actually I use futures for short trade, and use cash for the long trade as it allows me to dynamically maintain the hedge ratio. However if the lot size of long trade is achieved by futures then I take futures long trade and the difference in cash.

How to measure the hedge ratio for pair trading:
1. The classic approach to find the hedge ratio is to run a linear regression on the 2 stocks for the period say 20 days. What actually the linear regression does is that it provides a mathematical formula in the below format which explains how the 2 stocks X & Y prices are related in the last 20 days.

Y(Stock1 price) = (Some constant) + Beta * X (Stock2 Price) + (some error value)

2. The important parameter in the above formula is Beta. So using Beta we can calulate what should be the size of each stock for proper hedging. Lets say the beta value it 0.5, the the formula can be simplified to:

Y = 0.5 * X (ignore constant & error)

This means if we buy 100 shares of X then we need 50 shares of Y for hedging.

In most of the books and articles that you read on pair trading, you may find that once the trade is entered one just waits for the Zscore to mean revert, however I find this not a correct approach as there would be lot of drawdown if the pairs do not start to mean revert immediately. Hence I prefer to manage the trade daily using the rolling beta approach. I.e I run the linear regression on the 2 stocks every day and measure the stock daily position size based on the new beta value for that day, I will then sell or buy more of the Long stock so that the hedge is maintained on daily basis.This helps in reducing the volatility in the portfolio and drawdown is reduced. And once the zScore mean reverts I will close both the trades.
1. For positional Pair Trades, we dont bother about the stock trend direction, hence I want to trade the prices as near to the previous day close price (Used for calculating the zScore) as possible.
2. I will place Limit BUY/SELL orders at previous day closing price and wait for either 1 of the trades to get executed, if only one gets executed at the limit price I modify the other to market and execute the trade. This will to some extent ensure we got the price at the zScore level.
3. Yes, we need to use Futures to take trades but use the cash segment closing prices for our zScore calculations and trading. Actually I use futures for short trade, and use cash for the long trade as it allows me to dynamically maintain the hedge ratio. However if the lot size of long trade is achieved by futures then I take futures long trade and the difference in cash.

How to measure the hedge ratio for pair trading:
1. The classic approach to find the hedge ratio is to run a linear regression on the 2 stocks for the period say 20 days. What actually the linear regression does is that it provides a mathematical formula in the below format which explains how the 2 stocks X & Y prices are related in the last 20 days.

Y(Stock1 price) = (Some constant) + Beta * X (Stock2 Price) + (some error value)

2. The important parameter in the above formula is Beta. So using Beta we can calulate what should be the size of each stock for proper hedging. Lets say the beta value it 0.5, the the formula can be simplified to:

Y = 0.5 * X (ignore constant & error)

This means if we buy 100 shares of X then we need 50 shares of Y for hedging.

In most of the books and articles that you read on pair trading, you may find that once the trade is entered one just waits for the Zscore to mean revert, however I find this not a correct approach as there would be lot of drawdown if the pairs do not start to mean revert immediately. Hence I prefer to manage the trade daily using the rolling beta approach. I.e I run the linear regression on the 2 stocks every day and measure the stock daily position size based on the new beta value for that day, I will then sell or buy more of the Long stock so that the hedge is maintained on daily basis.This helps in reducing the volatility in the portfolio and drawdown is reduced. And once the zScore mean reverts I will close both the trades.
Thanks ncube for detailed information about pairtrading as positional...If possible please share one trade which you took recent days as example...also please give the link for code of linear regression...or share here
 

VJAY

Well-Known Member
@VJAY , please modify the update_eod function in the notebook to that given below, it should work (The bhavcopy downloaded using NSE EOD Data Downloader v3.3 tool, if the format is different then it may need changes as per the format you use, usecols=[0,1,5] means field0 is Symbol, field1 is date and field5 is close price),

def update_eod(masterfile,eodfile):
master = pd.read_csv(masterfile, index_col=[0])
eod = pd.read_csv(eodfile, header=None,index_col=[0],usecols=[0,1,5])
date = eod.iloc[0,0]
df = master.append(eod.T.drop([1]).reset_index(drop=True).rename(index={0:date})).dropna(axis=1)
df.to_csv('C://master/stockdata.csv')
return

View attachment 27693
Dear ncube,
sorry to trouble again ..am getting this error...unable to rectify myself :(

1534938160162.png
 

VJAY

Well-Known Member
You need to give tab (4 spaces) for each of the lines after the def update_eod() line...you can see my screenshot. i,e master = line should start with a tab first.
Yes I got it .....My eod data sample like this
1534938916949.png
 

ncube

Well-Known Member
Thanks ncube for detailed information about pairtrading as positional...If possible please share one trade which you took recent days as example...also please give the link for code of linear regression...or share here
I have not taken any recent pair trades, my last trade was HINDZINC-NMDC which I had taken at the begining of this month. I prefer to take pair trades when the market is sideways or in downtrend. These days most of my time is occupied in taking short term momentum trades as they give good results in uptrend markets. I will be traveling again from next week so I will not be able to trade much and will have limited time to access the forum...so utilize me as much as possible till this weekend...:)

Coming to python code for linear regression, you can add the following function to the function cell: (Note give 4 spaces before the lines in the function)
import statsmodels.api as sm
def get_beta(y,x,lb=20):
model_ols = sm.OLS(y[-lb:], x[-lb:]).fit()
return(model_ols.params[0])

This function can be accessed by calling it as follows:
beta = get_beta(SS1,SS2,lb=20)

1534939104783.png


Note positional trades require leveraging, and I dont encourage it for someone new to this strategy. Hence please ensure money management and risks are under control and trade only when you have gained enough experience in pair trading.
 

VJAY

Well-Known Member
I have not taken any recent pair trades, my last trade was HINDZINC-NMDC which I had taken at the begining of this month. I prefer to take pair trades when the market is sideways or in downtrend. These days most of my time is occupied in taking short term momentum trades as they give good results in uptrend markets. I will be traveling again from next week so I will not be able to trade much and will have limited time to access the forum...so utilize me as much as possible till this weekend...:)

Coming to python code for linear regression, you can add the following function to the function cell: (Note give 4 spaces before the lines in the function)
import statsmodels.api as sm
def get_beta(y,x,lb=20):
model_ols = sm.OLS(y[-lb:], x[-lb:]).fit()
return(model_ols.params[0])

This function can be accessed by calling it as follows:
beta = get_beta(SS1,SS2,lb=20)


View attachment 27705

Note positional trades require leveraging, and I dont encourage it for someone new to this strategy. Hence please ensure money management and risks are under control and trade only when you have gained enough experience in pair trading.
Dear ncube,
Allready you shared valuable things which we not get openly ...also shared insights and thought process on pair trading ..Am sure it will help many traders here ..its a great thing which you done in Traderji....
I think need to specify SS1 &SS2 as 2 scrips here like
SS1 = df['HINDZINC']
SS2 = df['NMDC'] ?
1534940365359.png
 

UberMachine

Well-Known Member
I have not taken any recent pair trades, my last trade was HINDZINC-NMDC which I had taken at the begining of this month. I prefer to take pair trades when the market is sideways or in downtrend. These days most of my time is occupied in taking short term momentum trades as they give good results in uptrend markets. I will be traveling again from next week so I will not be able to trade much and will have limited time to access the forum...so utilize me as much as possible till this weekend...:)

Coming to python code for linear regression, you can add the following function to the function cell: (Note give 4 spaces before the lines in the function)
import statsmodels.api as sm
def get_beta(y,x,lb=20):
model_ols = sm.OLS(y[-lb:], x[-lb:]).fit()
return(model_ols.params[0])

This function can be accessed by calling it as follows:
beta = get_beta(SS1,SS2,lb=20)

View attachment 27705

Note positional trades require leveraging, and I dont encourage it for someone new to this strategy. Hence please ensure money management and risks are under control and trade only when you have gained enough experience in pair trading.
Its better to refrain from futures since there is every chance you could have gotten a bad pair or you may just be plain unlucky. You could move into futures if you could have more than 3 consistent pairs (so that you can diversify risk) and your z-score is above 2.5 (minimum 2.2 preferred).

One thing I noticted during backtests, there seems to be less correlation between pairs in intraday compared to positionals. This is due to the fact that I am predicting that the pairs would revert within a single day (statistically, I am giving something similar to a point estimate while pair trading works on confidence intervals). But the upside is I can take more trades and diversify among different pairs to reduce volatility. The mean returns so far comes to 0.2% per trade with avg 2 trades per week on BANK NIFTY which looks nice when leveraged up. Working on it to turn it into a strategy.
 

ncube

Well-Known Member
Dear ncube,
Allready you shared valuable things which we not get openly ...also shared insights and thought process on pair trading ..Am sure it will help many traders here ..its a great thing which you done in Traderji....
I think need to specify SS1 &SS2 as 2 scrips here like
SS1 = df['HINDZINC']
SS2 = df['NMDC'] ?
View attachment 27707
Yes, SS1, SS2 need to be defined...before calling get_beta() function. The parameter lb=20 stands for the look back period default I have kept it to 20days. Unsually should be same as that of zScore lookback period.