Pair Trading - Exploring The Low Risk Statistical Arbitrage Trading Concepts

ncube

Well-Known Member
Dear ncube ,
I think you missed my 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...:)
 

VJAY

Well-Known Member
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...:)
Dear ncube,
I using 10 pairs in my note book ....so how to apply it ..is this code identifies the pair which I need to check?

1535627149762.png
 

VJAY

Well-Known Member
you need to set SS1,SS2 to the stocks like you have done before calling plot_pairs() function:

SS1 = df['TATAMTRDRV']
SS2 = df['TATAMOTORS']
getnew_zScore(SS1,SS2,275,2175)
Thanks a lot :)
 

VJAY

Well-Known Member
BPCL-HINDPETRO
pVAlue:0.00129
Zscore:2.475



RELCAPITAL-RELINFRA
pVAlue:0.00128
Zscore: (-2.166)
 
BPCL-HINDPETRO
pVAlue:0.00129
Zscore:2.475



RELCAPITAL-RELINFRA
pVAlue:0.00128
Zscore: (-2.166)
HI @VJAY,
I am not getting the same P-value from my data, you mentioned it few days ago. I am using same code given by @ncube. I have checked the data in "stock_data.csv" file. Seems ok like no double entries for same day or not missed a day . Can you please check your data?

Today, for BPCL-HINDPETRO P-value I am getting is 0.00211
for RELCAPITAL -RELINFRA it is .00110

Thanks.
 

VJAY

Well-Known Member
HI @VJAY,
I am not getting the same P-value from my data, you mentioned it few days ago. I am using same code given by @ncube. I have checked the data in "stock_data.csv" file. Seems ok like no double entries for same day or not missed a day . Can you please check your data?

Today, for BPCL-HINDPETRO P-value I am getting is 0.00211
for RELCAPITAL -RELINFRA it is .00110

Thanks.
Dear Vevensa,
I last weekend completely removed old data and downloaded new data of one year and using it now....I checked last few days data which updated after that its looks good :confusedd::confusedd:..How can we check its correctness?also I use code for updation of file ....not doing manually...
 

ncube

Well-Known Member
Dear Vevensa,
I last weekend completely removed old data and downloaded new data of one year and using it now....I checked last few days data which updated after that its looks good :confusedd::confusedd:..How can we check its correctness?also I use code for updation of file ....not doing manually...
We can check the data is correct by running the following in a notebook cell:

lst = ['ICICIBANK','SBIN','TCS','WIPRO'] #Add the stock names to the list that you want to check
df[lst][-20:] #Output the last 20 days data for each of the stocks
 
Dear Vevensa,
I last weekend completely removed old data and downloaded new data of one year and using it now....I checked last few days data which updated after that its looks good :confusedd::confusedd:..How can we check its correctness?also I use code for updation of file ....not doing manually...
@VJAY,
There was data missing for 06-Jun-18 in "stockdata.csv". I have included that, now p-values are matching with yours.
Earlier I checked for past 20 days only. Now checked for longer period.
Thank you.
 

VJAY

Well-Known Member
BPCL-HINDPETRO
pVAlue:0.00129
Zscore:2.475



RELCAPITAL-RELINFRA
pVAlue:0.00128
Zscore: (-2.166)
1535710933101.png


Dear ncube,
Its possible trades of today ....exits @ 3.15 pm....Looks big loss in RELINFRA...please give your thoughts ...