# auto robo trading formula for Amibroker (AFL)

#### amahalder

##### New Member
:clap: All credit goes to the owner. I'm pasting the same for testing purpose only:

// ========== Dedicated to Amibroker 4 BEI ========= //

_SECTION_BEGIN("R-Squared+LinReg+Slope+Exploration");
SetChartOptions(0,chartShowArrows|chartShowDates);
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
Plot( Volume,", Volume",colorBlack,styleHidden);

MaxGraph=5;

Periods = Optimize("Periods",Param("Periods",126,2,300,1),2,500,1);
shift = Param("Look back period",0,0,500,1);
P = ParamField("Price field",3);

x = Cum(1);
lastx = LastValue( x ) - shift;
aa = LastValue( Ref(LinRegIntercept(P, Periods), -shift) );
bb = LastValue( Ref(LinRegSlope( P, Periods), -shift) );
y = Aa + bb * ( x - (Lastx - Periods+1 ) );

R2=(Correlation(P,y,Periods)^2);
R2Style=ParamStyle("R2 Style");
R2Color=ParamColor("R2 Color",colorBlack);
Rdm = 1-R2;

LRColor = ParamColor("LR Color", colorCycle );
LRStyle = ParamStyle("LR Style");
LRLine = IIf( x > (lastx - Periods) AND BarIndex() < Lastx, y, Null );
Plot( LRLine , "LinReg", LRCOLOR, LRSTYLE);

// =========================== LinReg1 =========================== //

SDP = Param("Standard Deviation", 1.0, 0, 6, 0.1);
SD = SDP/2;

width = LastValue( Ref(SD*StDev(p, Periods),-shift) );SDU = IIf( x > (lastx - Periods) AND BarIndex() < Lastx, y+width , Null ) ;
SDL = IIf( x > (lastx - Periods) AND BarIndex() < Lastx, y-width , Null ) ;

SDColor = ParamColor("SD Color", colorCycle );
SDStyle = ParamStyle("SD Style");

Plot( SDU , "Upper Lin Reg", SDColor,SDStyle );
Plot( SDL , "Lower Lin Reg", SDColor,SDStyle );

// =========================== LinReg2 =========================== //

SDP2 = Param("2d Standard Deviation", 2.0, 0, 6, 0.1);
SD2 = SDP2/2;

width2 = LastValue( Ref(SD2*StDev(p, Periods),-shift) );SDU2 = IIf( x > (lastx - Periods) AND BarIndex() < Lastx, y+width2 , Null ) ;
SDL2 = IIf( x > (lastx - Periods) AND BarIndex() < Lastx, y-width2 , Null ) ;

SDColor2 = ParamColor("2 SD Color", colorCycle );
SDStyle2 = ParamStyle("2 SD Style");

Plot( SDU2 , "Upper Lin Reg", SDColor2,SDStyle2 );
Plot( SDL2 , "Lower Lin Reg", SDColor2,SDStyle2 );

slope = LinRegSlope(P,Periods);

// === BarCount ===//

Start = ParamDate("Start","Default");

YY = int(Start / 10000) + 1900;
M = int((Start-((YY-1900)*10000))/100);
DD = (Start-((YY-1900)*10000))-(M*100);

// =========================== Exploration =========================== //
Degree = LinRegSlope(MA(P,Periods),2);

Bu1 = Degree > 0.05 AND Degree <= 0.25;
Bu2 = Degree > 0.25 AND Degree <= 0.5;
Bu3 = Degree > 0.5 AND Degree <= 0.75;
Bu4 = Degree > 0.75;

Si1 = Degree > 0.0175 AND Degree <= 0.05;
Si2 = Degree >= -0.0175 AND Degree <= 0.0175;
Si3 = Degree < -0.0175 AND Degree >= -0.05;

Be1 = Degree < -0.05 AND Degree >= -0.25;
Be2 = Degree < -0.25 AND Degree >= -0.5;
Be3 = Degree < -0.5 AND Degree >= -0.75;
Be4 = Degree < -0.75;

Result =
WriteIf( Bu1 , "Weak Bullish" ,
WriteIf( Bu2 , "Medium Bullish" ,
WriteIf( Bu3 , "Strong Bullish" ,
WriteIf( Bu4 , "Extreme Bullish" ,
WriteIf( Si1 , "Sideways-Bullish" ,
WriteIf( Si2 , "Sideways" ,
WriteIf( Si3 , "Sideways-Bearish" ,
WriteIf( Be1 , "Weak Bearish" ,
WriteIf( Be2 , "Medium Bearish" ,
WriteIf( Be3 , "Strong Bearish" ,
WriteIf( Be4 , "Extreme Bearish" , "")))))))))));

FGCol =
IIf(Bu1 , colorBlack ,
IIf(Bu2 , colorBlack ,
IIf(Bu3 , colorLightYellow ,
IIf(Bu4 , colorLightYellow ,
IIf(Si1 , colorBlack ,
IIf(Si2 , colorBlack ,
IIf(Si3 , colorBlack ,
IIf(Be1 , colorBlack ,
IIf(Be2 , colorBlack ,
IIf(Be3 , colorLightYellow ,
IIf(Be4 , colorLightYellow , colorDefault)))))))))));

BGCol =
IIf(Bu1 , colorBrightGreen ,
IIf(Bu2 , colorLime ,
IIf(Bu3 , colorGreen ,
IIf(Bu4 , colorDarkGreen ,
IIf(Si1 , colorSkyblue ,
IIf(Si2 , colorYellow ,
IIf(Si3 , colorRose ,
IIf(Be1 , colorPink ,
IIf(Be2 , ColorRGB(255,100,100) ,
IIf(Be3 , colorRed ,
IIf(Be4 , colorDarkRed , colorDefault)))))))))));

R2FGCol =
IIf(R2 >= 0 AND R2 < 0.25 , colorBlack ,
IIf(R2 >= 0.25 AND R2 < 0.5 , colorBlack ,
IIf(R2 >= 0.5 AND R2 < 0.75 , colorBlack ,
IIf(R2 >= 0.75 AND R2 < 0.85 , colorLightYellow ,
IIf(R2 >= 0.85 , colorLightYellow , colorDefault)))));

R2BGCol =
IIf(R2 >= 0 AND R2 < 0.25 , colorRose ,
IIf(R2 >= 0.25 AND R2 < 0.5 , colorLime ,
IIf(R2 >= 0.5 AND R2 < 0.75 , colorBrightGreen ,
IIf(R2 >= 0.75 AND R2 < 0.85 , colorGreen ,
IIf(R2 >= 0.85 , colorDarkGreen , colorDefault)))));

Bullish = Bu1 OR Bu2 OR Bu3 OR Bu4;
Bullish1 = Bu1 OR Bu2;
Bullish2 = Bu3 OR Bu4;
Sideways = Si1 OR Si2 OR Si3;
Bearish = Be1 OR Be2 OR Be3 OR Be4;
Bearish1 = Be1 OR Be2;
Bearish2 = Be3 OR Be4;
All = Bu1 OR Bu2 OR Bu3 OR Bu4 OR Si1 OR Si2 OR Si3 OR Be1 OR Be2 OR Be3 OR Be4;

Q = ParamList("Trend","Bullish|Good Bullish|Mild Bullish|Sideways|Bearish|Mild Bearish|Bad Bearish|All");
QQ =
IIf( Q == "Bullish" , Bullish ,
IIf( Q == "Mild Bullish" , Bullish1 ,
IIf( Q == "Good Bullish" , Bullish2 ,
IIf( Q == "Sideways" , Sideways ,
IIf( Q == "Bearish" , Bearish ,
IIf( Q == "Mild Bearish" , Bearish1 ,
IIf( Q == "Bad Bearish" , Bearish2 ,
IIf( Q == "All" , All , 0))))))));

PriceFrom = Param("Price From:", 1, 50 , 1000000, 1);
PriceTo = Param("Price To:", 100000, 50, 1000000, 1);
Filter = (QQ) AND (C >= PriceFrom AND C <= PriceTo);
AddColumn(C,"Close" , 1.2 , colorDefault , colorDefault , 65);
AddTextColumn( Result , "Trend" , 1.0 , FGCol , BGCol , 96);
AddColumn(R2 , "R-Sq" , 1.3 , R2FGCol , R2BGCol);
AddColumn(SDU2 , "Upper LinReg2" , 1.2 , colorDefault , SDColor2 , 65);
AddColumn(SDU , "Upper LinReg" , 1.2 , colorLightYellow , SDColor , 65);
AddColumn(LRLine , "LinReg" , 1.2 , colorDefault , LRColor , 65);
AddColumn(SDL , "Lower LinReg" , 1.2 , colorLightYellow , SDColor , 65);
AddColumn(SDL2 , "Lower LinReg2" , 1.2 , colorDefault , SDColor2 , 65);

GfxSetOverlayMode(1);
GfxSelectFont("Kunstler Script", Status("pxheight")*6/100 );
GfxSetBkMode(0);
GfxSetTextColor(colorBlue);
GfxTextOut("Christopher Tahir",Status("pxwidth")/300, Status("pxheight")/2.1 );

Title = " • "+Name()+" • || "+Interval(2)+" || • "+Date()+" • Open "+Open+" • Hi "+ H+" • Lo "+L+" • Close "+Close+"("+WriteVal(ROC(C,1),1.1)+"%)"
+EncodeColor(colorRed)+"\n----------------------------------------------------------------------------"
+EncodeColor(R2Color)+"\n • R-Squared ("+Periods+") = "+WriteVal(R2,1.4)+" • Random ("+Periods+") = "+WriteVal(Rdm,1.4)+" • "
+EncodeColor(colorRed)+"\n----------------------------------------------------------------------------"
+EncodeColor(colorGreen)+"\n • Slope ("+Periods+") = "+WriteVal(slope,1.4)+" • "
+EncodeColor(colorRed)+"\n----------------------------------------------------"
+EncodeColor(colorBlue)+"\n • Since = "+M+"/"+DD+"/"+YY+EncodeColor(colorDarkGrey)+" • BarCount = "+X+" • "
+EncodeColor(colorRed)+"\n----------------------------------------------------"