BEST AFL in CHOPPY & TRANDING MARKET

#2
AFL for First part of the chart
-----------------------------------

//KBrain System Coded by Karthikmarar. Blog. www.stocktechnician.blogspot.com email. [email protected]
//System based on the BrainTrend system in Metatrader
// The Turquiose colored Dot indicates start of a Up Trend
// The Magenta DOt indicates end of an Up move and beginning of a downmove
// The system is a Stop and reverse system
_SECTION_BEGIN("KBrain");
//========================Initiation========================
bts=ParamToggle("BrainTrend1 signal","No|Yes" ,0);
btst=ParamToggle("BrainTrend1 stop","No|Yes" ,0);
btsl=ParamToggle("BrainTrend1 stop line","No|Yes" ,0);
period=Param("Period",14,6,20,1);
x1=53;
x2=47;
d=2.3;
f=7;
s=1.5;
range=ATR(f);
Range1 = ATR(f)/d;
Range2 = (ATR(f)*s)/4;
range3=ATR(10);
R = ((HHV(H,period) - C) /(HHV (H,period) -LLV (L,period))) *-100;
EMA1= EMA(R,Period);
EMA2= EMA(EMA1,5);
Difference= EMA1 - EMA2;
ZeroLagEMA= EMA1 + Difference;
value2=abs(ZeroLagEMA);


function PercentR( periods )
{
return -100 * ( HHV( H, periods ) - C )/( HHV( H, periods ) - LLV( L, periods ) );
}


for( i = period+10; i < BarCount; i++ )
{
C[0]=0;
Value2[0]=0;
p[0]=0;
Plot1[0]=0;
Plot2[0]=0;
Val1=0;
Val2=0;
temp[0]=0;
Value3[0]=0;
tm[0]=0;
p1=0;
bt1[0]=0;
bt2[0]=0;
r[0]=0;
bt1a[0]=0;
bt2a[0]=0;
istop[0]=Val1[0];
stop=Val1[0];
bstop[0]=0;
sstop=0;
//==================================Indicators============================
{
if (value2 < x2 AND abs(Close-Close[i-2]) > range1)
p = 1 ;
else
{
if (value2 > x1 AND abs(Close-Close[i-2]) > Range1)
p = 2 ;

else

p=0;
}
}

if ((value2 < x2 AND p == 1) OR (value2 < x2 AND p == 0))

{
if (abs(Close-Close[i-2]) > Range1)
{
Plot1=H;
Plot2=L;
}
else
{
Plot1=Plot1[i-1];
Plot2=Plot2[i-1];
}
}
else
{
if ((value2 > x1 AND p == 2) OR (value2 > x1 AND p == 0))

{
Plot1=L;
Plot2=H;
}
else
{
Plot1=Plot1[i-1];
Plot2=Plot2[i-1];
}
}
//==================KBrain Signal ====================================
{
if (value2 < x2 AND (abs(Close-Close[i-2]) > Range1))
{
if (p == 1 OR p == 0)
Value3=L-range3;
val1=Value3;
p=1;
temp=1;
}
else
{
temp=temp[i-1];
}
{
if (value2 > x1 AND (abs(Close-Close[i-2]) > Range1))
{
if (p == 2 OR p == 0)
Value3=H+range3;
val2=Value3;
p=2;
temp=2;;
}

}
}
{

if (temp==1 AND Plot1>0 AND tm != 1)
tm= 1;

if (temp==2 AND Plot2>0 AND tm != 2)
tm=2;

}
//===============================stop========================================
{
if (value2 < x2 AND (abs(Close-Close[i-2]) > Range1)AND p !=2)

{
value3=L-range3;
va1=Value3;
p1=2;
r=Va1;
bstop=Va1;
bt2=bt2[i-1];
}

if (value2 > x1 AND (abs(Close-Close[i-2]) > Range1)AND p !=1)

{
Value3=H+range3;
va2=Value3;
p1=1;

r=Va2;
sstop=Value3;
bt1=bt1[i-1];
}

}



if (val1 == 0 AND val2 == 0 AND p == 0 )
{
bstop=bstop[i-1];
sstop=sstop[i-1];
}


if (bstop<bstop[i-1] AND tm==1 AND tm[i-1]==1) bstop=bstop[i-1];
if (sstop>sstop[i-1] AND tm==2 AND tm[i-1]==2) sstop=sstop[i-1];

}
//=============================SYSTEM======================================
Mycolor=IIf(p==1,colorLime,IIf(p==2,colorRed,colorBlue));
PlotOHLC( Open, High, Low, Close, "", Mycolor, styleBar| styleThick );
PlotShapes( shapeCircle* (bts AND tm==1 AND Ref(tm,-1)==2),colorTurquoise, 0, bstop, 0 );
PlotShapes( shapeCircle* (bts AND tm==2 AND Ref(tm,-1)==1), colorCustom12, 0, sstop, 0 );
PlotShapes( IIf(btst AND p==1,shapeSmallCircle,Null), colorTurquoise,0,bstop,0);
Plot(IIf(btsl AND tm==1,bstop,Null),"",colorPaleBlue,1);
PlotShapes( IIf(btst AND p==2,shapeSmallCircle,Null), colorCustom12,0,sstop,0);
Plot(IIf(btsl AND tm==2,sstop,Null),"",colorLightYellow,1);
Buy=Cover=(bts AND tm==1 AND Ref(tm,-1)==2);
Sell=Short=(bts AND tm==2 AND Ref(tm,-1)==1);
SellPrice=ValueWhen(Sell,C,1);
BuyPrice=ValueWhen(Buy,C,1);
Long=Flip(Buy,Sell);
Shrt=Flip(Sell,Buy );
_SECTION_END();
//=================TITLE================================================================================================
_SECTION_BEGIN("Title");
if( Status("action") == actionIndicator )
(
Title = EncodeColor(colorWhite)+ "KBrain V 1.0. " + " - " + Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
" - " + Date() +" - "+"\n" +EncodeColor(colorYellow) +"Op-"+O+" "+"Hi-"+H+" "+"Lo-"+L+" "+
"Cl-"+C+" "+ "Vol= "+ WriteVal(V)+"\n"+
EncodeColor(colorLime)+
WriteIf (Buy , " GO LONG / Reverse Signal at "+C+" ","")+
WriteIf (Sell , " EXIT LONG / Reverse Signal at "+C+" ","")+"\n"+EncodeColor(colorWhite)+
WriteIf(Sell , "Total Profit/Loss for the Last Trade Rs."+(C-BuyPrice)+"","")+
WriteIf(Buy , "Total Profit/Loss for the Last trade Rs."+(SellPrice-C)+"","")+
WriteIf(Long AND NOT Buy, "Trade : Long - Entry price Rs."+(BuyPrice),"")+
WriteIf(shrt AND NOT Sell, "Trade : Short - Entry price Rs."+(SellPrice),"")+"\n"+
WriteIf(Long AND NOT Buy, "Current Profit/Loss Rs."+(C-BuyPrice)+"","")+
WriteIf(shrt AND NOT Sell, "Current Profit/Loss Rs."+(SellPrice-C)+"",""));

_SECTION_END();
 

rrrajguru

Well-Known Member
#6
@ vijayakishorebabu

This is not the correct afl, what we are seeing in the upper immage.

There is no entry and exit rules as shown in the upper afl.
 
#8
//KBrain System Coded by Karthikmarar. Blog. www.stocktechnician.blogspot.com email. [email protected]
//System based on the BrainTrend system in Metatrader
// The Turquiose colored Dot indicates start of a Up Trend
// The Magenta DOt indicates end of an Up move and beginning of a downmove
// The system is a Stop and reverse system
//Revision Index - V.1.1 resolves the scan issues
_SECTION_BEGIN("Heikin");
HaC =EMA((O+C+H+L)/4,3);
HaO = AMA( Ref( HaC, -1 ), 0.8 );
HaH = Max( H, Max( HaC, HaO ) );
HaL = Min( L, Min( HaC, HaO ) );

e1=EMA(C,21);e2=MA(C,26);
ehh=e1>e2 ; ell=e1<e2 ;
ri= (e1+e2)/2;Colri=IIf(ehh,colorBlue,colorRed);

SetBarFillColor(IIf(Hac>Hao,colorBlue,colorRed));
Colcan=IIf(Hac>ri,colorBlue,colorRed);
PlotOHLC( HaO, HaH, HaL, HaC, "" + Name(), colcan, styleCandle | styleNoLabel );
_SECTION_END();

_SECTION_BEGIN("KBrain");
//========================Initiation========================
bts=ParamToggle("BrainTrend1 signal","No|Yes" ,1);
btst=ParamToggle("BrainTrend1 stop","No|Yes" ,1);
btsl=ParamToggle("BrainTrend1 stop line","No|Yes" ,1);
period=Param("Period",14,6,20,1);
x1=53;
x2=47;
d=2.3;
f=7;
s=1.5;
range=ATR(f);
Range1 = ATR(f)/d;
Range2 = (ATR(f)*s)/4;
range3=ATR(10);
R = ((HHV(H,period) - C) /(HHV (H,period) -LLV (L,period))) *-100;
EMA1= EMA(R,Period);
EMA2= EMA(EMA1,5);
Difference= EMA1 - EMA2;
ZeroLagEMA= EMA1 + Difference;
value2=abs(ZeroLagEMA);


function PercentR( periods )
{
return -100 * ( HHV( H, periods ) - C )/( HHV( H, periods ) - LLV( L, periods ) );
}


for( i = 1; i < Period-1; i++ )
{


tm = 1;
bstop = 1;
sstop = 1;
p = 1;
}

for( i = period+10; i < BarCount; i++ )
{
C[0]=0;
Value2[0]=0;
p[0]=0;
Plot1[0]=0;
Plot2[0]=0;
Val1=0;
Val2=0;
temp[0]=0;
Value3[0]=0;
tm[0]=0;
p1=0;
bt1[0]=0;
bt2[0]=0;
r[0]=0;
bt1a[0]=0;
bt2a[0]=0;
istop[0]=Val1[0];
stop=Val1[0];
bstop[0]=0;
sstop=0;
//==================================Indicators============================
{
if (value2 < x2 AND abs(Close-Close[i-2]) > range1)
p = 1 ;
else
{
if (value2 > x1 AND abs(Close-Close[i-2]) > Range1)
p = 2 ;

else

p=0;
}
}

if ((value2 < x2 AND p == 1) OR (value2 < x2 AND p == 0))

{
if (abs(Close-Close[i-2]) > Range1)
{
Plot1=H;
Plot2=L;
}
else
{
Plot1=Plot1[i-1];
Plot2=Plot2[i-1];
}
}
else
{
if ((value2 > x1 AND p == 2) OR (value2 > x1 AND p == 0))

{
Plot1=L;
Plot2=H;
}
else
{
Plot1=Plot1[i-1];
Plot2=Plot2[i-1];
}
}
//==================KBrain Signal ====================================
{
if (value2 < x2 AND (abs(Close-Close[i-2]) > Range1))
{
if (p == 1 OR p == 0)
Value3=L-range3;
val1=Value3;
p=1;
temp=1;
}
else
{
temp=temp[i-1];
}
{
if (value2 > x1 AND (abs(Close-Close[i-2]) > Range1))
{
if (p == 2 OR p == 0)
Value3=H+range3;
val2=Value3;
p=2;
temp=2;;
}

}
}
{

if (temp==1 AND Plot1>0 AND tm != 1)
tm= 1;

if (temp==2 AND Plot2>0 AND tm != 2)
tm=2;

}
//===============================stop========================================
{
if (value2 < x2 AND (abs(Close-Close[i-2]) > Range1)AND p !=2)

{
value3=L-range3;
va1=Value3;
p1=2;
r=Va1;
bstop=Va1;
bt2=bt2[i-1];
}

if (value2 > x1 AND (abs(Close-Close[i-2]) > Range1)AND p !=1)

{
Value3=H+range3;
va2=Value3;
p1=1;

r=Va2;
sstop=Value3;
bt1=bt1[i-1];
}

}



if (val1 == 0 AND val2 == 0 AND p == 0 )
{
bstop=bstop[i-1];
sstop=sstop[i-1];
}


if (bstop<bstop[i-1] AND tm==1 AND tm[i-1]==1) bstop=bstop[i-1];
if (sstop>sstop[i-1] AND tm==2 AND tm[i-1]==2) sstop=sstop[i-1];

}
//=============================SYSTEM======================================
Mycolor=IIf(p==1,colorLime,IIf(p==2,colorRed,colorBlue));

PlotShapes( shapeCircle* (bts AND tm==1 AND Ref(tm,-1)==2),colorTurquoise, 0, bstop, 0 );
PlotShapes( shapeCircle* (bts AND tm==2 AND Ref(tm,-1)==1), colorCustom12, 0, sstop, 0 );
PlotShapes( IIf(btst AND p==1,shapeSmallCircle,Null), colorTurquoise,0,bstop,0);
Plot(IIf(btsl AND tm==1,bstop,Null),"",colorPaleBlue,1);
PlotShapes( IIf(btst AND p==2,shapeSmallCircle,Null), colorCustom12,0,sstop,0);
Plot(IIf(btsl AND tm==2,sstop,Null),"",colorLightYellow,1);
Buy=Cover=(bts AND tm==1 AND Ref(tm,-1)==2);
Sell=Short=(bts AND tm==2 AND Ref(tm,-1)==1);
SellPrice=ValueWhen(Sell,C,1);
BuyPrice=ValueWhen(Buy,C,1);
Long=Flip(Buy,Sell);
Shrt=Flip(Sell,Buy );
_SECTION_END();
//=================TITLE================================================================================================
_SECTION_BEGIN("Title");
if( Status("action") == actionIndicator )
(
Title = EncodeColor(colorWhite)+ "KBrain V 1.1. " + " - " + Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
" - " + Date() +" - "+"\n" +EncodeColor(colorYellow) +"Op-"+O+" "+"Hi-"+H+" "+"Lo-"+L+" "+
"Cl-"+C+" "+ "Vol= "+ WriteVal(V)+"\n"+
EncodeColor(colorLime)+
WriteIf (Buy , " GO LONG / Reverse Signal at "+C+" ","")+
WriteIf (Sell , " EXIT LONG / Reverse Signal at "+C+" ","")+"\n"+EncodeColor(colorWhite)+
WriteIf(Sell , "Total Profit/Loss for the Last Trade Rs."+(C-BuyPrice)+"","")+
WriteIf(Buy , "Total Profit/Loss for the Last trade Rs."+(SellPrice-C)+"","")+
WriteIf(Long AND NOT Buy, "Trade : Long - Entry price Rs."+(BuyPrice),"")+
WriteIf(shrt AND NOT Sell, "Trade : Short - Entry price Rs."+(SellPrice),"")+"\n"+
WriteIf(Long AND NOT Buy, "Current Profit/Loss Rs."+(C-BuyPrice)+"","")+
WriteIf(shrt AND NOT Sell, "Current Profit/Loss Rs."+(SellPrice-C)+"",""));

_SECTION_END();
BuyPrice=ValueWhen(Buy,C);
SellPrice=ValueWhen(Sell,C);
Filter=(Buy OR Sell );;
AddColumn(IIf(Buy,BuyPrice,Null)," Buy ", 6.2,1,colorGreen);
AddColumn(IIf(Sell,SellPrice,Null)," sell ",6.2,1,colorOrange);
 
#10
Hi,

Anyboday knows mentioned MACD settings above snapshot


_SECTION_BEGIN("MACDPrediction");
/*An MACD, Signal Cross is, in general, a bullish Signal.
Since it comes with a significant delay, it would be important to anticipate this Cross. Some days before the Cross, we often see an MACD turning point. It is interesting to investigate the related conditions AND use them before the end of the session.
The following AFL code will give the necessary next ROC for a higher MACD value.*/

//The probable Cross between MACD AND its Signal, by D. Tsokakis, Feb2005
SetBarsRequired(10000,0);
function EMAn ( Cnext , r3)
{
return ( 2 * Cnext + ( r3 - 1 ) * EMA ( C , r3 ) ) / ( r3 + 1 ) ;
}
function MACDn ( Cnext , r1 , r2 )
{
return EMAn ( Cnext ,r1 ) - EMAn ( Cnext , r2 ) ;
}
r1 = Param( "Fast avg", 12, 2, 200, 1 );
r2 = Param( "Slow avg", 26, 2, 200, 1 );
r3 = Param( "Signal avg", 9, 2, 200, 1 );

det = 500 ;
perc = 25/100 ;
Clast = SelectedValue ( C ) ;
Plot( ml = MACD(r1, r2), StrFormat(_SECTION_NAME()+"(%g,%g)", r1,r2), ParamColor("MACD color", colorRed ), ParamStyle("MACD style") );
Plot( sl = Signal(r1,r2,r3), "Signal" + _PARAM_VALUES(), ParamColor("Signal color", colorBlue ), ParamStyle("Signal style") );
Plot( ml-sl, "MACD Histogram", IIf(ml-sl > 0,colorBrightGreen,colorRed ), styleNoTitle | ParamStyle("Histogram style",styleHistogram | styleNoLabel, maskHistogram ));

det = 500 ;
perc = 25/100 ;
Clast = SelectedValue ( C ) ;


Plot( ml = MACD(r1, r2), StrFormat(_SECTION_NAME()+"(%g,%g)", r1,
r2), ParamColor("MACD color", colorRed ), ParamStyle("MACD style") );
Plot( sl = Signal(r1,r2,r3), "Signal" + _PARAM_VALUES(), ParamColor("Signal color", colorBlue ), ParamStyle("Signal style") );
Plot( ml-sl, "MACD Histogram", IIf(ml-sl > 0,colorBrightGreen,colorRed ), styleNoTitle | ParamStyle("Histogram style",styleHistogram | styleNoLabel, maskHistogram ));
MACDlast = SelectedValue(MACD());
Buy= Cross(MACD(12,26),Signal(12,26,9));
Sell = Cross( Signal(12, 26, 9), MACD(12,26) );
PlotShapes(IIf(Buy,shapeUpArrow,shapeNone) ,colorBrightGreen);
PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed);


SIGNALlast = SelectedValue ( Signal ( ) );
Title = Name ( ) + ", " + Date ( ) + ", MACD=" + WriteVal ( MACDlast )+ " ,SIGNAL=" + WriteVal ( SignalLAST ) +"\nThe nextMACD will be higher if the next ROC will be > ";
Cnextmin = ( 1 - perc ) * Clast ;Cnextmax = ( 1 + perc ) * Clast ;
step = ( Cnextmax - Cnextmin ) / det ;
nextROC1 = -100 * perc ;
nextROC2 = -100 * perc ;
for ( Cnext = Cnextmin ; Cnext <= Cnextmax ; Cnext = Cnext + step )
{
X = MACDn ( Cnext , 12 , 26 ) ;
Y = EMA ( X , 9 ) ;
MACDnext = SelectedValue ( X );
SIGNALnext = SelectedValue ( Y );
NextROC = 100 * ( -1 + Cnext / Clast ) ;
NextROC0 = 100 * ( -1 + Cnext / Clast ) ;
if ( macdnext < SignalLAST )
{
NextROC1 = NextROC ;
}
if ( MACDnext < MACDlast )
{
nextROC2 = nextROC0;
}
}
Title = Title + WriteIf ( NextRoc2 >= 0.1 , "+" , "" ) + WriteVal ( NextROC2 , 1.2 ) + "%" + "\nThe nextMACD will be above the lastSIGNAL if the next ROC will be > " + WriteIf ( NextRoc1 >= 0.1 , "+" , "" ) + WriteVal ( nextROC1 , 1.2 )+"%" ;
Plot ( 0 ,"", colorBlack ,styleDashed ) ;
_SECTION_END();