Please correct this afl

looser

Active Member
#1
Hi friends,

I have written this small piece of code but it is not giving any output, please check and rectify the code if possible.

Code:
_SECTION_BEGIN("Pairs");
minscale = Param("Scale min", 0, 0, 300, 1) ;
maxscale = Param("Scale max", 300, 0, 1500, 1) ;

Symbol1 = ParamStr("Ticker1", "DLF,ONGC");

C1 = Foreign( "Symbol1", "C" ); 
O1 = Foreign( "Symbol1", "O" ); 
H1 = Foreign( "Symbol1", "H" ); 
L1 = Foreign( "Symbol1", "L" ); 

RestorePriceArrays( True ); 

Symbol2 = ParamStr("Ticker2", "RELIANCE,LT");

C2 = Foreign( "Symbol2", "C" ); 
O2 = Foreign( "Symbol2", "O" ); 
H2 = Foreign( "Symbol2", "H" ); 
L2 = Foreign( "Symbol2", "L" ); 
RestorePriceArrays( True ); 


PaClose = C1+C2;
PaOpen =O1+O2;
PaHigh =H1+H2;
PaLow = L1+L2;

//PlotOHLC( O1, H1, L1, C1, "Pair",colorWhite, styleCandle );
PlotOHLC( PaOpen, PaHigh, PaLow, PaClose, "Pair",colorBlue, styleCandle|styleOwnScale, minscale, maxscale );
_SECTION_END();
 

looser

Active Member
#2
30 views and no reply :)
 
#3
hI FRIEND

Dont worry.
here is a code by pankaj parimal who put lots of effort into pairs code.
you can try and start moving from there
be haapy!
------------------------------------------------------
HTML:
/*
Waited for the assistance from the seniors / experts but I guess they missed this post. So finally I put on my thinking cap AND VOILA!!! I did it.

I successfully coded (actually modified) this AFL. I, by no means, want to take full credit for the same AND would like to mention that base AFL was posted by one Mr. Gopinathan C on another forum around 6 months back. I also went thru a few other AFLs available on the AmiBroker Forum.

for those interested in the same, I am posting the AFL. You may find it very raw (some of the coding could have been better though) but pls understand that I am no expert in this field...... Most importantly, this AFL fully meets my requirement. So here it goes :

************************************************** ******
*/

_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'.
Mkt= ParamStr("Independent Scrip (A)", "LT" );
//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),styleNoTitle | 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(colorBlue) +" R-Squared "+"("+WriteVal(R2,format=1.2)+");"+"\n"+
EncodeColor(colorTeal) +"Correlation [3 Day] "+ "("+WriteVal(Corr3,format=1.2)+");"+EncodeColor(colorCustom12) +" Correlation [30 Day] "+ "("+WriteVal(Corr30,format=1.2)+");"+"\n"+
EncodeColor(colorBlue) +"Correlation [1 Year] "+ "("+WriteVal(Corr1Y,format=1.2)+");"+EncodeColor(colorBrown) +" 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)+");"+EncodeColor(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();

//************************************************** *
//PROCEDURE
/*
The chart may not be visible as there may be some spaces in the AFL. I have pasted the AFL in a Notepad and it can be downloaded from :


Copy the AFL from the Notepad and then go to "Analysis" in Menu and paste the AFL by select "Formula Editor". Save it and open a "New Blank Chart" and double click on the AFL, which can be accessed from the Chart Window.
If you still get the error, do the following steps :
(i) Right Click the mouse and select "Edit Formula".
(ii) Go To : "Tools > Code Check & Profile"
(iii) This will identify where the error is and put the cursor at the error.....(just remove the spaces and save the AFL).
*/

_SECTION_BEGIN("Price1");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();
 

Similar threads