Pair Trading - Exploring The Low Risk Statistical Arbitrage Trading Concepts

VJAY

Well-Known Member
Why these pairs showing 0.0?

1536589605328.png
 
@VJAY @Vevensa_P, Add the following function "find_pair_coint()" in the function section and you can call it as follows, "find_pair_coint(returns,0.05)" if you want to find pairs with coint significance less than 0.01 then change the 2nd parameter to 0.01 instead of 0.05. Please ensure that you dont have many stocks in the excel sheet, else it will run all the combinations and output the pairs which will be in lakhs. Better limit it to max 25 pairs (50 stocks)

View attachment 28308

Code:
def find_pair_coint(data, significance=0.05):
    n = data.shape[1]
    keys = data.keys()
    for i in range(n):
        for j in range(i+1, n):
            S1 = data[keys[i]]
            S2 = data[keys[j]]
            result = coint(S1, S2)
            pvalue = result[1]
            if pvalue < significance:
                print((keys[i], keys[j]),round(pvalue,5))
Code:
find_pair_coint(returns,0.05)
Another update, I ran backtesting for the 30 min break out for all the pairs for last 5 years and the results are not that significant for intra-day (30min candle breakout and close the trade at eod). Its giving good result for positional trades only. For intraday it would require active monitoring and use discretionary price action methods to generate profits. Hence I would not encourage this strategy and trade this only if you can monitor the trade continuously.

On the other hand 15min breakout in the direction of prominent trend for both stocks and closing the trade at abt 0.03%-0.05% is giving better results, hence you can explore this method.

However I always believe the ultimate goal for every trader should be Positional trading as it is less stressful and objective should be to have a life outside trading and not spend the whole day in front of monitor.
@ncube,
Thanks for the code and the update on backtest for intraday trades.
 

VJAY

Well-Known Member
Beta value formula is:
HINDZINC-price = beta * NMDC-price

If beta is 2.69 and NMDC-Lot size is 6000, then Hindzinc-qty should be (NMDC-LOT Size)/2.69 = 6000/2.69 = 2230 , i.e NMDC quantity is 2.69 times of HINDZINC.

If you are testing positional trades, then this need to be calculated every day and the buy quantity need to be adjusted accordingly.
Dear ncube,
Here you mentioned calculate beta everyday means it eod after market?then adjustment done at market open next morning?Hope its not meant by near closing....if so how can we claculate beta in CMP?
 

ncube

Well-Known Member
Dear ncube,
Here you mentioned calculate beta everyday means it eod after market?then adjustment done at market open next morning?Hope its not meant by near closing....if so how can we claculate beta in CMP?
Yes, I make the adjustments based on eod prices not near closing as sometimes I will not be infront of monitor during trading hours. However if you want find the beta based on the latest value, I had shared a code snipped in one of my earlier posts, you can use it.
 

VJAY

Well-Known Member
Yes, I make the adjustments based on eod prices not near closing as sometimes I will not be infront of monitor during trading hours. However if you want find the beta based on the latest value, I had shared a code snipped in one of my earlier posts, you can use it.
SS1 = df['HINDZINC']
SS2 = df['NMDC']
get_beta (SS1,SS2,275,2175)

Is this code works which shared for zscore?
 

ncube

Well-Known Member
Refer to this post ..

Please add this new function in the function cell:

def getnew_zScore(SS1,SS2,SS1_price,SS2_price,lb=20):
S1=SS1.append(pd.Series([SS1_price]),ignore_index=True)
S2=SS2.append(pd.Series([SS2_price]),ignore_index=True)
spread = S1[-lb:] / S2[-lb:]
spread_mean = spread.mean()
std_dev = spread.std()
zscore = (spread - spread_mean)/std_dev
print('Latest zScore : ',zscore.iloc[-1])

You can call this function from a cell at the end of the notebook as follows:

getnew_zScore(SS1,SS2,275,2175)

where 275 is the latest price of SS1 & 2175 is the latest price of SS2. The output will be the latest zScore...:)