.

Bull Bear Trend Reversal Detector - Amibroker AFL Code

Click Image To Enlarge. Please Rate And Comment.

Bull Bear Trend Reversal Detector

_SECTION_BEGIN("Chart Settings");
 SetChartOptions(0,chartShowArrows|chartShowDates); 
 SetChartBkColor(ParamColor("Outer Panel",colorPaleBlue));
 SetChartBkGradientFill(ParamColor("Upper Chart",colorBlack),ParamColor("Lower Chart",colorBlack));
 GraphXSpace=Param("GraphXSpace",20,0,100,1);
 dec = (Param("Decimals",2,0,7,1)/10)+1;
 bi = BarIndex();
 Lbi = LastValue(BarIndex());
 sbi = SelectedValue(bi);
 x1= BarCount-1;
Title = EncodeColor(55)+  Title = Name() + "     " + EncodeColor(32) + Date() +
"      " + EncodeColor(5) + "{{INTERVAL}}  " +
 EncodeColor(55)+ "     Open = "+ EncodeColor(52)+ WriteVal(O,dec) + 
 EncodeColor(55)+ "     High = "+ EncodeColor(5) + WriteVal(H,dec) +
 EncodeColor(55)+ "      Low = "+ EncodeColor(32)+ WriteVal(L,dec) + 
 EncodeColor(55)+ "    Close = "+ EncodeColor(52)+ WriteVal(C,dec)+
 EncodeColor(55)+ "    Volume = "+ EncodeColor(52)+ WriteVal(V,1.0);
_SECTION_END();

_SECTION_BEGIN("");
//========================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;
//R1 SELECT MA
Usema = Param("UsedMA EMA=1| DEMA=2| GSMA=3| T3MA=4",3,1,4,1);


PI = 3.1415926;
//A=C-Ref(C,-T1);
//a[0]=C[0];
function IIR2( input, f0, f1, f2 ) 
{ 
    result[ 0 ] = input[ 0 ]; 
    result[ 1 ] = input[ 1 ]; 

    for( i = 10; i < BarCount; i++ ) 
    { 
       result[ i ] = f0 * input[ i ] + 
                     f1 * result[ i - 1 ] + 
                     f2 * result[ i - 2 ]; 
    } 

   return result; 
} 

function GSMA( input, Period )
{
  N = 0;
  an = 2 * PI / Period;
  c0 = b0 = 1;
  c1 = b1 = b2 = a1 = a2 = gamma1 = 0;
  beta1 = 2.415 * ( 1- cos( an ) );
  alpha = -beta1 + sqrt( beta1 ^ 2 + 2 * beta1 );
  alpha1 = ( cos( an ) + sin( an ) - 1 )/cos( an );
   {
    fo = alpha ^ 2;
    f1 = 2 * ( 1- alpha ); f2 = -( 1 - alpha )*( 1 - alpha );
  }
  

  return IIR2( input, fo,f1,f2);
}
if(USema==3)
{

GSMA1= GSMA(R,Period);
GSMA2= GSMA(GSMA1,5);
Difference= GSMA1 - GSMA2;
ZeroLagEMA= GSMA1 + Difference;
value2=abs(ZeroLagEMA);
}



function T3(price,periods)
{
 s = 0.84;
 e1=EMA(price,periods);
 e2=EMA(e1,Periods);
 e3=EMA(e2,Periods);
 e4=EMA(e3,Periods);
 e5=EMA(e4,Periods);
 e6=EMA(e5,Periods);
 c1=-s*s*s;
 c2=3*s*s+3*s*s*s;
 c3=-6*s*s-3*s-3*s*s*s;
 c4=1+3*s+s*s*s+3*s*s;
 Ti3=c1*e6+c2*e5+c3*e4+c4*e3;
 return ti3;
}

if(USema == 4)
{

T3MA1= T3(R,Period);
T3MA2= T3(T3MA1,5);
Difference= T3MA1 - T3MA2;
ZeroLagEMA= T3MA1+ Difference;
value2=abs(ZeroLagEMA);

}

if(USema == 2)
{
DEMA1= DEMA(R,Period);
DEMA2= DEMA(DEMA1,5);
Difference= DEMA1 - DEMA2;
ZeroLagEMA= DEMA1 + Difference;
value2=abs(ZeroLagEMA);
}

if(USema == 1)
{
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[i]=0;
bt1[0]=0;
bt2[0]=0;
r[0]=0;
bt1a[0]=0;
bt2a[0]=0;
istop[0]=Val1[0];
stop[i]=Val1[0];
bstop[0]=0;
sstop[i]=0;
//==================================Indicators============================
{
if (value2[i] < x2 AND abs(Close[i]-Close[i-2]) > range1[i])
p[i] = 1 ;
else
{
if (value2[i] > x1 AND abs(Close[i]-Close[i-2]) > Range1[i])
p[i] = 2 ;

else

p[i]=0;
}
}

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

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

{
Plot1[i]=L[i];
Plot2[i]=H[i];
}
else
{
Plot1[i]=Plot1[i-1];
Plot2[i]=Plot2[i-1];
}
}

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

}
}
{

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

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

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

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

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

{
Value3[i]=H[i]+range3[i];
va2[i]=Value3[i];
p1[i]=1;

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

}



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


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

}
//=============================SYSTEM======================================
Mycolor=IIf(p==1,colorLime,IIf(p==2,colorRed,colorBlue));
PlotOHLC( Open, High, Low, Close, "", Mycolor, styleCandle| 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)+ " " + " - " + Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
" - " + Date() +" - "+"\n" +EncodeColor(colorYellow) +"Op ="+O+" "+"Hi ="+H+" "+"Lo ="+L+" "+
"Cl ="+C+" "+ "Vol = "+ WriteVal(V,1.0)+"\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);
Previous Post Next Post