Saravanan Notes

SaravananKS

Well-Known Member
#22
The AFL From which Pankaj Seeking Improvement
_SECTION_BEGIN("Pankaj Parimal - Pairs Trade");

// Enter the Variables Here

Mkt= ParamStr("Independent Scrip (A)", "<'Enter Independent Scrip'>");//Enter the other Scrip (henceforth 'Scrip A') of the Pair as per the NSE Nomenclature by right clicking the mouse and selecting 'Parameters'.

//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.

//Time Frame to be used is 'Daily'.

Period=5;//Enter the required Period for the calculation of Correlation, Spread and Averages

Observations=58;//Enter the No. of Observations for the calculation of R-Squared, Beta & Alpha

Std=0.5;//Enter the required Standard Deviation


//***Base Market************/

P=Foreign(Mkt,"C",1);//Closing Price of Independent Scrip (A) Entered Above

PH=Foreign(Mkt,"H",1);//High Price of Independent Scrip (A) Entered Above

PL=Foreign(Mkt,"L",1);//Low Price of Independent 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;

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(Mspread,"Mean Spread",colorBlue,styleDashed);

Plot(UpperSpr,"Upper Spread",colorGreen,styleLine);

Plot(Cspread,"Current Spread",ParamColor("Color",colorBlack),styleNoTitl e | ParamStyle("Style"));

Plot(LowerSpr,"Lower Spread",colorRed,styleLine);

//Plot(MaxSpr,"Max. Spread",colorViolet,styleLine);

//Plot(MinSpr,"Min. Spread",colorCustom12,styleLine);

Title=EncodeColor(colorCustom12)+"Independent Scrip (A) is " +EncodeColor(colorBlue)+StrFormat(Mkt)+EncodeColor (colorBlack) +" AND "+
EncodeColor(colorCustom12)+"Dependent Scrip (B) is " +EncodeColor(colorBlue)+"'"+Name()+"'."+" "+
EncodeColor(colorOrange)+"[Key in the 'Independent' Scrip (A) by right-clicking the mouse & Selecting 'Parameters'.]"+"\n"+
EncodeColor(colorBlue)+"High Priced Scrip "+"("+WriteVal(Max(p,C),format=1.2)+");"+" "+
EncodeColor(colorBrown)+"Low Priced Scrip "+"("+WriteVal(Min(C,p),format=1.2)+");"+"\n"+
EncodeColor(colorViolet) +"Correlation [5 Day] "+ "("+WriteVal(Corr5,format=1.2)+");"+EncodeColor(co lorBlue) +" R-Squared "+"("+WriteVal(R2,format=1.2)+");"+"\n"+
EncodeColor(colorTeal) +"Correlation [3 Day] "+ "("+WriteVal(Corr3,format=1.2)+");"+EncodeColor(co lorCustom12) +" Correlation [30 Day] "+ "("+WriteVal(Corr30,format=1.2)+");"+"\n"+
EncodeColor(colorBlue) +"Correlation [1 Year] "+ "("+WriteVal(Corr1Y,format=1.2)+");"+EncodeColor(c olorBrown) +" Correlation [2 Year] "+ "("+WriteVal(Corr2Y,format=1.2)+");"+
EncodeColor(colorGreen) +" Correlation [3 Year] "+ "("+WriteVal(Corr3Y,format=1.2)+");"+"\n"+"\n" +
EncodeColor(colorCustom12) +WriteIf(Cspread>UpperSpr,"Recommendation : "+EncodeColor(colorRed) +"[CS > US] - "+EncodeColor(colorBlue) +"Buy Low Priced Scrip & Sell High Priced Scrip",
WriteIf(Cspread<LowerSpr,"Recommendation : "+EncodeColor(colorGreen) +"[CS < LS] - "+EncodeColor(colorBlue) +"Buy High Priced Scrip & Sell Low Priced Scrip",
"Recommendation : "+EncodeColor(colorBlue) +"Wait - Do Not Trade"))+" "+"\n"+"\n"+
EncodeColor(colorGreen) +"Upper Spread "+"("+WriteVal(UpperSpr,format=1.2)+");" + EncodeColor(colorBlue) +" Mean Spread "+"("+WriteVal(Mspread,format=1.2)+");"+
EncodeColor(colorRed) +" Lower Spread "+"("+WriteVal(LowerSpr,format=1.2)+");"+EncodeCol or(colorViolet) +" Current Spread "+"("+WriteVal(Cspread,format=1.2)+")"+"\n"+
EncodeColor(colorViolet) +"Max. Spread [10 Day] "+"("+WriteVal(MaxSpr10D,format=1.2)+");" + EncodeColor(colorCustom12) +" Min. Spread [10 Day] "+"("+WriteVal(MinSpr10D,format=1.2)+");"+"\n" +
EncodeColor(colorViolet) +"Max. Spread [20 Day] "+"("+WriteVal(MaxSpr20D,format=1.2)+");" + EncodeColor(colorCustom12) +" Min. Spread [20 Day] "+"("+WriteVal(MinSpr20D,format=1.2)+");"+"\n" +
EncodeColor(colorViolet) +"Max. Spread [30 Day] "+"("+WriteVal(MaxSpr30D,format=1.2)+");" + 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();

************************************************** *

Disclaimer (just as a precautionary measure) :
This AFL is for academic purpose only. Kindly do not use this AFL for trading purpose unless you are fully aware of how to trade pairs and its consequences. Pls note that Pairs Trading also involves Risk and if not traded properly can lead to huge losses. However, those of you who decide to use it for Trading, will do so at their own risk and the Author(s) of this AFL are in no way responsible for any losses they incur.

All the very best. Do reply if you have any suggestions.

Also, anyone interested in Pairs Trading can ask their queries. (Though I may not be able to provide much support if the queries are on the AFL). I will try my level best to answer all the queries with whatever little knowledge that I have. Seniors / Experts on this topic are welcome to the share their knowledge for the benefit of all.
 

SaravananKS

Well-Known Member
#26
Factors and econimic indicators affecting Silver Prices
1. US dollar stability is the top silver market price indicator. If the dollar is strong, the silver market price is low, and vice versa. The market price of gold is also in strong reverse correlation with the value of US dollar.

2. World silver market supply is another important factor in determining silver market price. If the supply is high, the silver market price is to stay low. However, its believed that worlds silver deposits are being diminished at a rate of 5% a year, so in the coming years we are going to face real silver deficit.

3. World silver market demand is also known to drive the market price of silver up. If businesses and individuals are actively buying silver bullion coins and bars, the silver market price is usually on the rise.

4. Extreme industrial demand on silver is caused by a plethora of new electronic devices entering the market in the recent years like smart phones, computers, plasma TVs and many more. Many of these smart devices require substantial silver components for proper functioning.

5. Unemployment rate is also another important economic indicator affecting the price of silver.

6. Hyperinflation is absolutely guaranteed to send silver market price sky high.

7. Government policies can have incredible effect on the market price of silver.

8. Investor fears about extremely volatile stock market and the dwindling value of the US dollar are pushing them into stocking up on silver and gold investments.

9. Worlds overall economic situation is really another important driving force for the spot price of silver that inevitably affects the price of tangible silver bullion products and even intangible silver futures.

10. The rising population in the worlds biggest silver consumers like China is destined to affect the price of silver.
 

SaravananKS

Well-Known Member
#27
Cycle Analysis AFL
SECTION_BEGIN("EhlersDominantPeriod");
SetBarsRequired(200, 0);

// Ehlers Dominant Cycle Period
// from Ehlers, John F. Cybernetic Analysis for Stocks and Futures. Wiley. 2004.
// Chapter 9, p. 107. Code on p. 111.

function CyclePeriod(array, alpha)
// Figure 9.4 on p. 111
{
smooth = (array + 2*Ref(array, -1) + 2*Ref(array, -2) + Ref(array, -3))/6;

// for(i = 0; i < 7; i++) cycle=array; // Initialize early values and as array

for(i = 0; i < 6; i++)
{
InstPeriod = 0; // Initialize early values and as array
DeltaPhase = 0;
cycle=0;
Period=0;
}

for(i = 6; i < BarCount; i++)
{
cycle = (1 - .5*alpha)*(1 - .5*alpha)*(smooth - 2*smooth[i-1] + smooth[i-2]) +
2*(1 - alpha)*cycle[i-1] - (1 - alpha)*(1 - alpha)*cycle[i-2];
Q1 = (.0962*cycle + .5769*cycle[i-2] -.5769*cycle[i-4] - .0962*cycle[i-6])*(.5 + .08*InstPeriod[i-1]);
I1 = cycle[i-3];

if(Q1 != 0 AND Q1[i-1] != 0)
DeltaPhase = (I1/Q1 - I1[i-1]/Q1[i-1])/(1 + I1*I1[i-1]/(Q1*Q1[i-1]));
if(DeltaPhase < 0.1) DeltaPhase = 0.1;
if(DeltaPhase > 1.1) DeltaPhase = 1.1;

//----- Speed up the median calculation by placing it inline

mlen = 5;
for(k = mlen - 1; k >= 0; k--) {temparray[k] = DeltaPhase[i + k - (mlen - 1)];}

temp=0;
for(k = mlen - 1; k > 0; k--)
{for (j = mlen - 1; j > 0; j--)
{if (temparray[j-1] > temparray[j])
{
temp = temparray[j-1];
temparray[j-1] = temparray[j];
temparray[j] = temp;
}
}
}
MedianDelta = temparray[mlen - 1 - (mlen / 2)];

//----- End median calculation

if(MedianDelta == 0) DC = 15;
else DC = 6.28318/MedianDelta + .5;

InstPeriod = .33*DC + .67*InstPeriod[i-1];
Period = .15*InstPeriod + .85*Period[i-1];
}
return Period;
}

Med = (H+L)/2;

// CyclePeriod
CP = CyclePeriod(Med, .07);
Plot(CP, "CyclePeriod", colorRed, styleLine);
_SECTION_END();
 

SaravananKS

Well-Known Member
#30
Way to Put Alert sound in AFL
AlertIf( Buy, "SOUND C:\\Windows\\Media\\Ding.wav", "Audio alert", 2 );
AlertIf( Sell, "SOUND C:\\Windows\\Media\\Ding.wav", "Audio alert", 2
 

Similar threads