Pair Trading - Exploring The Low Risk Statistical Arbitrage Trading Concepts

VJAY

Well-Known Member
can you please give the code which we get pvalue with chart itself without placing pairnames again
 

ncube

Well-Known Member
can you please give the code which we get pvalue with chart itself without placing pairnames again
Code:
This function selects the recent 200 day data into returns dataframe:

def load_data(file):
    df = pd.read_csv(file, index_col=[0])
    returns = df.pct_change()[-200:]
    returns = returns.iloc[1:,:].dropna(axis=1)
    return returns,df
    
This function calculates the pvalue:
# Function to get the cointegration significance score, by default the value is set to 0.05,
# it can be changed while calling the function. This function returns the pvalue for the pair.
def find_coint_significance(S1,S2,significance=0.01):
    pvalue = coint(S1, S2, autolag=None)[1]
    if pvalue < significance:
        print("Pair is Significant, pValue= ", pvalue)
    else:
        print("Pair is Not Significant, pValue= ", pvalue)
 

VJAY

Well-Known Member
Code:
This function selects the recent 200 day data into returns dataframe:

def load_data(file):
    df = pd.read_csv(file, index_col=[0])
    returns = df.pct_change()[-200:]
    returns = returns.iloc[1:,:].dropna(axis=1)
    return returns,df
   
This function calculates the pvalue:
# Function to get the cointegration significance score, by default the value is set to 0.05,
# it can be changed while calling the function. This function returns the pvalue for the pair.
def find_coint_significance(S1,S2,significance=0.01):
    pvalue = coint(S1, S2, autolag=None)[1]
    if pvalue < significance:
        print("Pair is Significant, pValue= ", pvalue)
    else:
        print("Pair is Not Significant, pValue= ", pvalue)
Dear ncube,
I think these codes are present in my formula cells as your codes....though am getting these all values running cells seperately
I tryed to edit /add in same code which plots chart...here I getting zscore twice...also I need all result on above chart...its some output showing below chart...can you please help to solve this?

1538962875431.png
 

ncube

Well-Known Member
Dear ncube,
I think these codes are present in my formula cells as your codes....though am getting these all values running cells seperately
I tryed to edit /add in same code which plots chart...here I getting zscore twice...also I need all result on above chart...its some output showing below chart...can you please help to solve this?

View attachment 29249
Its simple just change the sequence of the functions in the cell as follows:
Code:
S1 = returns['HINDZINC']
S2 = returns['NMDC']
SS1 = df['HINDZINC']
SS2 = df['NMDC']

find_coint_significance()
get_beta()
plot_pairs()
In the function get_beta(), comment the print line print('Latest zScore : ',zscore.iloc[-1]) with a # prefix as follows:
Code:
def get_beta(y,x,lb=20,amt=10000):
    ratio = y[-lb:] / x[-lb:]
    ratio_mean = ratio.mean()
    std_dev = ratio.std()
    zscore = (ratio- ratio_mean)/std_dev
    #print('Latest zScore : ',zscore.iloc[-1])
    model_ols = sm.OLS(y[-lb:], x[-lb:]).fit()
    if zscore.iloc[-1] < 0 :
        yqty = int(amt/y.iloc[-1])
        xqty = int((amt/y.iloc[-1])*model_ols.params[0]) * -1
    else:
        yqty = int(amt/y.iloc[-1]) * -1
        xqty = int((amt/y.iloc[-1])*model_ols.params[0])
    print("%s Qty: " %y.name, yqty)
    print("%s Qty: " %x.name, xqty)
    return(model_ols.params)
 

VJAY

Well-Known Member
Its simple just change the sequence of the functions in the cell as follows:
Code:
S1 = returns['HINDZINC']
S2 = returns['NMDC']
SS1 = df['HINDZINC']
SS2 = df['NMDC']

find_coint_significance()
get_beta()
plot_pairs()
In the function get_beta(), comment the print line print('Latest zScore : ',zscore.iloc[-1]) with a # prefix as follows:
Code:
def get_beta(y,x,lb=20,amt=10000):
    ratio = y[-lb:] / x[-lb:]
    ratio_mean = ratio.mean()
    std_dev = ratio.std()
    zscore = (ratio- ratio_mean)/std_dev
    #print('Latest zScore : ',zscore.iloc[-1])
    model_ols = sm.OLS(y[-lb:], x[-lb:]).fit()
    if zscore.iloc[-1] < 0 :
        yqty = int(amt/y.iloc[-1])
        xqty = int((amt/y.iloc[-1])*model_ols.params[0]) * -1
    else:
        yqty = int(amt/y.iloc[-1]) * -1
        xqty = int((amt/y.iloc[-1])*model_ols.params[0])
    print("%s Qty: " %y.name, yqty)
    print("%s Qty: " %x.name, xqty)
    return(model_ols.params)
Dear ncube bro,
Thanks for the solution ...are we need to put pair scrip name 2 times here?!!!!is it possible to use once only?

1538979061352.png
 

ncube

Well-Known Member
Dear ncube bro,
Thanks for the solution ...are we need to put pair scrip name 2 times here?!!!!is it possible to use once only?

View attachment 29258
Yes, cointegration significance is calculated using the daily returns of each stocks, however if you want to run the whole set using a single statement, you enclose the functions in another function as follows:
Code:
def pair_trade(df,returns,ystock,xstock,xres=20,yres=10,amt=10000):
    SS1 = df[ystock]
    SS2 = df[xstock]
    S1 = returns[ystock]
    S2 = returns[xstock]
    find_coint_significance(S1,S2,significance=0.05)
    get_beta(SS1,SS2,20,amt)
    plot_pairs(df,SS1,SS2,20,xres,yres)
    
#You can then call this single function as follows:
pair_trade(df,returns,'HINDZINC','NMDC',20,10,10000)