_SECTION_BEGIN("Pankaj Parimal - Pairs Trade");
//Time Frame to be used is 'Daily'.
// Enter the Variables Here
//Independent Scrip is the one with higher Volatility of the 2 scrips. But if one scrip is an Index, then the Index will become the Independent Scrip. Again, if both the scrips of the pair are Indices, then the one with the higher Volatility will be the Independent Scrip.
Mkt= ParamStr("High Priced Scrip", "Enter High Priced Scrip Here");//Enter the High Priced Scrip of the Pair as per the NSE Nomenclature or the nomenclature set by the Data Provider, by right clicking the mouse and selecting 'Parameters'.
Period=Param("Period - Correlation, Spread and Std Dev", 5, 2, 1000, 1);//Enter the required Period for the calculation of Correlation & Spread and also for Standard Deviation of Spread to calculate Z-Score by selecting 'Parameters'. Default CZPeriod=5
Observations=Param("Observations - Alpha", 50, 2, 1000, 1);//Enter the No. of Observations for the calculation of Alpha by selecting 'Parameters'. Default Observations=50
StdDev=Param("Standard Deviation", 0.02, 0.005, 0.1, 0.001);//Enter the required Standard Deviation by selecting 'Parameters'. Default StdDev=0.02;
//[Currently the Standard Deviation is taken as 2%, which will reduce the number of recommendations received as the distance between the Mean Spread AND Upper / Lower Spread will increase.
//With this method, one will notice that the 95% of Current Spread is enveloped by the Upper & Lower Spread.]
//Std=0.5;//Enter the required Standard Deviation
//***Base Market************/
P=Foreign(Mkt,"C",1);//Closing Price of High Priced Scrip (A) Entered Above
PH=Foreign(Mkt,"H",1);//High Price of High Priced Scrip (A) Entered Above
PL=Foreign(Mkt,"L",1);//Low Price of High Priced Scrip (A) Entered Above
//Description Calculation of R-Squared :
//R-Squared, which is an indicator for the validity of the Beta and Alpha measures.
//This is because the R-Squared value measures just how closely the Scrip tracks the Index with which it is being compared.
//An R-Squared value of 1.0 indicates a perfect match. In that case, one can trust that the Beta and Alpha measures are valid.
//But, the lower the R-Squared is, the less reliable the Beta and the Alpha measures are.
//The Beta and Alphas of a Scrip with an R-Squared value of below 0.50 are completely meaningless and hence should be ignored.
//Assuming the R-Squared is, say, 0.75 OR higher, one can move on to the Beta.
/***Spread****************/
AvgC=Sum(C,Period)/Period;//Average of Scrip B over a Set Period
AvgP=Sum(P,Period)/Period;//Average of Scrip A
Spr=P-C;//Spread of Scrip A & Scrip B (Difference of Closing Price of Scrip A & Scrip B)
Mspread=Sum(Spr,Period)/Period;//Average or Mean Spread of Scrip A & Scrip B
//Finding Max. & Min. Spread in the Last 10 Days, 20 Days & 30 Days
MaxSpr01=Max(Spr,Ref(Spr,-1));
MaxSpr23=Max(Ref(Spr,-2),Ref(Spr,-3));
MaxSpr45=Max(Ref(Spr,-4),Ref(Spr,-5));
MaxSpr67=Max(Ref(Spr,-6),Ref(Spr,-7));
MaxSpr89=Max(Ref(Spr,-8),Ref(Spr,-9));
MaxSpr10=Max(MaxSpr01,MaxSpr23);
MaxSpr20=Max(MaxSpr45,MaxSpr67);
MaxSpr30=Max(MaxSpr10,MaxSpr20);
MaxSpr10D=Max(MaxSpr30,MaxSpr89);//Max. Spread in the Last 10 Days
MaxSpr1011=Max(Ref(Spr,-10),Ref(Spr,-11));
MaxSpr1213=Max(Ref(Spr,-12),Ref(Spr,-13));
MaxSpr1415=Max(Ref(Spr,-14),Ref(Spr,-15));
MaxSpr1617=Max(Ref(Spr,-16),Ref(Spr,-17));
MaxSpr1819=Max(Ref(Spr,-18),Ref(Spr,-19));
MaxSpr11=Max(MaxSpr1011,MaxSpr1213);
MaxSpr21=Max(MaxSpr1415,MaxSpr1617);
MaxSpr31=Max(MaxSpr11,MaxSpr21);
MaxSpr41=Max(MaxSpr31,MaxSpr1819);
MaxSpr20D=Max(MaxSpr10D,MaxSpr41);//Max. Spread in the Last 20 Days
MaxSpr2021=Max(Ref(Spr,-20),Ref(Spr,-21));
MaxSpr2223=Max(Ref(Spr,-22),Ref(Spr,-23));
MaxSpr2425=Max(Ref(Spr,-24),Ref(Spr,-25));
MaxSpr2627=Max(Ref(Spr,-26),Ref(Spr,-27));
MaxSpr2829=Max(Ref(Spr,-28),Ref(Spr,-29));
MaxSpr12=Max(MaxSpr2021,MaxSpr2223);
MaxSpr22=Max(MaxSpr2425,MaxSpr2627);
MaxSpr32=Max(MaxSpr12,MaxSpr22);
MaxSpr42=Max(MaxSpr32,MaxSpr2829);
MaxSpr30D=Max(MaxSpr20D,MaxSpr42);//Max. Spread in the Last 30 Days
MinSpr01=Min(Spr,Ref(Spr,-1));
MinSpr23=Min(Ref(Spr,-2),Ref(Spr,-3));
MinSpr45=Min(Ref(Spr,-4),Ref(Spr,-5));
MinSpr67=Min(Ref(Spr,-6),Ref(Spr,-7));
MinSpr89=Min(Ref(Spr,-8),Ref(Spr,-9));
MinSpr1=Min(MinSpr01,MinSpr23);
MinSpr2=Min(MinSpr45,MinSpr67);
MinSpr3=Min(MinSpr1,MinSpr2);
MinSpr10D=Min(MinSpr3,MinSpr89);//Min. Spread in the Last 10 Days
MinSpr1011=Min(Ref(Spr,-10),Ref(Spr,-11));
MinSpr1213=Min(Ref(Spr,-12),Ref(Spr,-13));
MinSpr1415=Min(Ref(Spr,-14),Ref(Spr,-15));
MinSpr1617=Min(Ref(Spr,-16),Ref(Spr,-17));
MinSpr1819=Min(Ref(Spr,-18),Ref(Spr,-19));
MinSpr11=Min(MinSpr1011,MinSpr1213);
MinSpr21=Min(MinSpr1415,MinSpr1617);
MinSpr31=Min(MinSpr11,MinSpr21);
MinSpr41=Min(MinSpr31,MinSpr1819);
MinSpr20D=Min(MinSpr10D,MinSpr41);//Min. Spread in the Last 20 Days
MinSpr2021=Min(Ref(Spr,-20),Ref(Spr,-21));
MinSpr2223=Min(Ref(Spr,-22),Ref(Spr,-23));
MinSpr2425=Min(Ref(Spr,-24),Ref(Spr,-25));
MinSpr2627=Min(Ref(Spr,-26),Ref(Spr,-27));
MinSpr2829=Min(Ref(Spr,-28),Ref(Spr,-29));
MinSpr12=Min(MinSpr2021,MinSpr2223);
MinSpr22=Min(MinSpr2425,MinSpr2627);
MinSpr32=Min(MinSpr12,MinSpr22);
MinSpr42=Min(MinSpr32,MinSpr2829);
MinSpr30D=Min(MinSpr20D,MinSpr42);//Min. Spread in the Last 30 Days
RanC=H-L;//Range of Scrip B for the day
RangeC=Sum(RanC,Period)/Period;//Average Range of Scrip B over a Set Period
RanP=PH-PL;//Range of Scrip A for the day
RangeP=Sum(RanP,Period)/Period;//Average Range of Scrip A over a Set Period
//Deviation=(RangeC+RangeP)/2*Std;//Sum of Average range of Scrip A and Scrip B Divided by 2 and then Multiplied by the Standard Deviation
//UpperSpr=Mspread+Deviation;
//LowerSpr=Mspread-Deviation;
UpperSpr=IIf(Mspread>=0,(Mspread+(Mspread*StdDev)),(Mspread-(Mspread*StdDev)));
LowerSpr=IIf(Mspread>=0,(Mspread-(Mspread*StdDev)),(Mspread+(Mspread*StdDev)));
Cspread=P-C;//Current Spread using (positive or negative) Difference between Scrip A & Scrip B
Corr5=Correlation(Max(P,C),Min(P,C),5);//5 D Correlation
Corr3=Correlation(Max(P,C),Min(P,C),3);//3 D Correlation
Corr30=Correlation(Max(P,C),Min(P,C),30);//30 D Correlation
Corr1Y=Correlation(Max(P,C),Min(P,C),365);//1 Year Correlation
Corr2Y=Correlation(Max(P,C),Min(P,C),730);//2 Year Correlation
Corr3Y=Correlation(Max(P,C),Min(P,C),1095);//3 Year Correlation
/***R-Squared*************/
R2=Corr5^2;
blsh=Cspread>UpperSpr;
bhsl=Cspread<LowerSpr;
Buy=bhsl;
Sell=blsh;
Plot(Cspread,"Current Spread",ParamColor("Color - Current Spread",colorBlack),styleNoTitle | ParamStyle("Style - Current Spread"));
Plot(Mspread,"Mean Spread",colorBlue,styleDashed);
Plot(UpperSpr,"Upper Spread",colorGreen,styleLine);
Plot(LowerSpr,"Lower Spread",colorRed,styleLine);
//Plot(MaxSpr,"Max. Spread",colorViolet,styleLine);
//Plot(MinSpr,"Min. Spread",colorCustom12,styleLine);
Title=EncodeColor(colorCustom12)+
WriteIf(P>C,"High Priced Scrip is " +EncodeColor(colorBlue)+"'"+StrFormat(Mkt)+"'"+EncodeColor(colorBlack) +" AND "+
EncodeColor(colorCustom12)+"Low Priced Scrip is " +EncodeColor(colorBlue)+"'"+Name()+"'.",
"High Priced Scrip is " +EncodeColor(colorBlue)+Name()+EncodeColor(colorBlack) +" AND "+
EncodeColor(colorCustom12)+"Low Priced Scrip is " +EncodeColor(colorBlue)+"'"+StrFormat(Mkt)+"'.")
+" "+
EncodeColor(colorOrange)+"[Key in the 'High Priced' Scrip by right-clicking the mouse & Selecting 'Parameters'.]"+"\n"+
EncodeColor(colorBlue)+
WriteIf(P>C,StrFormat(Mkt)+" (LTP) : "+EncodeColor(colorGreen)+WriteVal(Max(P,C),format=1.2)+EncodeColor(colorBlack) +" | "+
EncodeColor(colorBrown)+Name()+" (LTP) : "+EncodeColor(colorRed)+WriteVal(Min(C,P),format=1.2),
Name()+" (LTP) : "+EncodeColor(colorGreen)+WriteVal(Max(P,C),format=1.2)+EncodeColor(colorBlack) +" | "+
EncodeColor(colorBrown)+StrFormat(Mkt)+" (LTP) : "+EncodeColor(colorRed)+WriteVal(Min(C,P),format=1.2))+"\n"+
EncodeColor(colorViolet) +"Correlation ["+int(Period)+" Day] : "+ WriteVal(Corr5,format=1.2)+EncodeColor(colorBlack) +" | "+EncodeColor(colorBlue) +"R-Squared ["+int(Period)+" Day] : "+WriteVal(R2,format=1.2)+"\n"+
EncodeColor(colorCustom12) +"Correlation [30 Day] : "+ WriteVal(Corr30,format=1.2)+EncodeColor(colorBlack) +" | "+EncodeColor(colorBlue) +"Correlation [1 Year] : "+ WriteVal(Corr1Y,format=1.2)+"\n"+
EncodeColor(colorBrown) +"Correlation [2 Year] : "+ WriteVal(Corr2Y,format=1.2)+EncodeColor(colorBlack) +" | "+EncodeColor(colorGreen) +"Correlation [3 Year] : "+ WriteVal(Corr3Y,format=1.2)+"\n"+"\n"+
EncodeColor(colorCustom12) +
WriteIf(Cspread>=0,
WriteIf(Cspread>UpperSpr,"Recommendation as per Spread : "+EncodeColor(colorRed) +"[CS > US] - "+EncodeColor(colorBlue) +"Buy "+Name()+" & Sell "+StrFormat(Mkt),
WriteIf(Cspread<LowerSpr,"Recommendation as per Spread : "+EncodeColor(colorGreen) +"[CS < LS] - "+EncodeColor(colorBlue) +"Buy "+StrFormat(Mkt)+" & Sell "+Name(),
"Recommendation as per Spread : "+EncodeColor(colorBlue) +"Wait - Do Not Trade")),
WriteIf(Cspread>UpperSpr,"Recommendation as per Spread : "+EncodeColor(colorRed) +"[CS > US] - "+EncodeColor(colorBlue) +"Buy "+StrFormat(Mkt)+" & Sell "+Name(),
WriteIf(Cspread<LowerSpr,"Recommendation as per Spread : "+EncodeColor(colorGreen) +"[CS < LS] - "+EncodeColor(colorBlue) +"Buy "+Name()+" & Sell "+StrFormat(Mkt),
"Recommendation as per Spread : "+EncodeColor(colorBlue) +"Wait - Do Not Trade")))+" "+"\n"+"\n"+
EncodeColor(colorBlack) +"Current Spread : "+WriteVal(Cspread,format=1.2)+EncodeColor(colorBlack) +" | "+ EncodeColor(colorBlue) +"Mean Spread : "+WriteVal(Mspread,format=1.2)+"\n"+
EncodeColor(colorGreen) +"Upper Spread : "+WriteVal(UpperSpr,format=1.2)+EncodeColor(colorBlack) +" | "+ EncodeColor(colorRed) +"Lower Spread : "+WriteVal(LowerSpr,format=1.2)+"\n"+
EncodeColor(colorViolet) +"Max. Spread [10 Day] : "+WriteVal(MaxSpr10D,format=1.2)+EncodeColor(colorBlack) +" | "+ EncodeColor(colorCustom12) +"Min. Spread [10 Day] : "+WriteVal(MinSpr10D,format=1.2)+"\n"+
EncodeColor(colorViolet) +"Max. Spread [20 Day] : "+WriteVal(MaxSpr20D,format=1.2)+EncodeColor(colorBlack) +" | "+ EncodeColor(colorCustom12) +"Min. Spread [20 Day] : "+WriteVal(MinSpr20D,format=1.2)+"\n"+
EncodeColor(colorViolet) +"Max. Spread [30 Day] : "+WriteVal(MaxSpr30D,format=1.2)+EncodeColor(colorBlack) +" | "+ EncodeColor(colorCustom12) +"Min. Spread [30 Day] : "+WriteVal(MinSpr30D,format=1.2);
Filter=1;//Corr>= 0.6018;
AddColumn( P, "Scrip A", 1.2 );
AddColumn( C, "Scrip B", 1.2 );
AddColumn( Corr5, "Correlation [5 Day]", 1.2 );
AddColumn( Corr3, "Correlation [3 Day]", 1.2 );
AddColumn( Corr30, "Correlation [30 Day]", 1.2 );
AddColumn( Corr1Y, "Correlation [1 Year]", 1.2 );
AddColumn( Corr2Y, "Correlation [2 Year]", 1.2 );
AddColumn( Corr3Y, "Correlation [3 Year]", 1.2 );
AddColumn( bhsl, "Buy High Sell Low"); //Buy High Priced Scrip and Sell Low Priced Scrip
AddColumn( blsh, "Buy Low Sell High"); // Buy Low Priced Scrip and Sell High Priced Scrip
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, LowerSpr, Offset=-100);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0, LowerSpr, Offset=-110);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0, LowerSpr, Offset=-105);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, UpperSpr, Offset=100);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0, UpperSpr, Offset=110);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0, UpperSpr, Offset=-105);
_SECTION_END();