Australian (ASX) Stock Market Forum

Need some help understanding some Amibroker code

Joined
8 April 2015
Posts
2
Reactions
0
Hi Guys,

I am a bit of a newbie in Amibroker.
I found this code which dates back 10 years BUT it gives me signals that are not too bad on the assets I track.

Could anyone help me make sense of what is happening inside please?
I tried to contact the programmer but the email bounced (well... ti was written 10y ago! ;) )

Thanks for your help!


// Trend Detection
// Graham Kavanagh 11 Jan 05
// I am using version 4.66.2, but believe this will work with last offical
//version.

function Rise( Pd, perd, Pl, perl )
{
MAD = DEMA(Pd,perd);
MAL = LinearReg(Pl,perl);
CondR = ROC(MAD,1)>0 AND ROC(MAL,1)>0;
CondF = ROC(MAD,1)<0 AND ROC(MAL,1)<0;
R[0] = C[0]>(H[0]+L[0])/2;

for(i=1;i<BarCount;i++)
{
if( CondR )
{
R = 1;
}
else
{
if( CondF )
{
R = 0;
}
else
{
R = R[i-1];
}
}
}
return R;
}

PrD = C;
PrL = (H+L)/2;
PrdD = PrdL = PrdM = Param("Prd",12,2,40,1);

permax = Max(prdd,prdl);

Rs = IIf( BarIndex()<permax, 0, Rise(PrD, PrdD, PrL, PrdL) );
Fs = IIf( BarIndex()<permax, 0, 1-Rs );

Confirm = MA(C,prdm);

function DirBar( dr, df )
{
B[0] = 0;

for(i=1;i<BarCount;i++)
{
if( dr[i-1] && df )
{
B = 1;
}
else
{
if( df[i-1] && dr )
{
B = 1;
}
else
{
B = B[i-1] + 1;
}
}
}
return B;
}

Bs = DirBar( Rs, Fs );
Direction = ROC(Confirm,1) > 0 AND ROC(Confirm,5) > 0;
Downward = ROC(Confirm,1) < 0 AND ROC(Confirm,5) < 0;

Select = Rs AND Ref(Fs,-1);
Caution = Fs AND Ref(Rs,-1);

Change = IIf( Rs, H/ValueWhen(Fs,L)*100, L/ValueWhen(Rs,H)*100 );

Plot( C, "close", IIf( Rs, colorGreen, IIf( Fs, colorRed, colorBlack )),
styleBar);

PlotShapes( shapeSmallCircle* select, colorDarkGreen, 0, H, 5 );
PlotShapes( shapeSmallCircle* Caution, colorDarkRed, 0, L, -5 );

GraphXSpace=10;
_N( Title = "{{NAME}} - {{INTERVAL}} {{DATE}} Trend Plot - "+prdd+" Day" );


Filter = Select OR Caution;
AddColumn( Select, "UpTurn", 1 );
AddColumn( Caution, "DownTurn", 1 );

// ---indicator end---
"Rise = " + Rs;
"Fall = " + Fs;
"Current Trend Bars = " + Bs;
"Trend Move = " + Change + " %";
 
Look at the blue/red Linear Regression Line and the candle colour changes. The fact is the price moves and the line bends after it. :D One of the better manipulations of price averages. This simple linear regression line code shows the simplicity.

Code:
LinReg = LinearReg(C, Param("Lin Reg Period", 80, 1, 300, 1));
DirectionChange = IIf(LinReg > Ref(LinReg, -1), colorBlue, colorRed);

Plot(LinReg, "Lin. Reg.", DirectionChange, styleLine);


Untitled.jpg
 
Hi Wysiwyg

thanks for taking time to respond! :)

your code seems very clear to me: a change in direction of the slope = change in direction of the trend.
Graham adds the ROC of the DEMA on 1 period at the beginning of the code.

I am not too sure about what these 2 blocks are for though.
Could you help me understand this? (sorry I cant buy you a beer... unless you swing by Singapore! :) )

Code:
permax = Max(prdd,prdl);

Rs = IIf( BarIndex()<permax, 0, Rise(PrD, PrdD, PrL, PrdL) );
Fs = IIf( BarIndex()<permax, 0, 1-Rs );

and

Code:
Bs = DirBar( Rs, Fs );
Direction = ROC(Confirm,1) > 0 AND ROC(Confirm,5) > 0;
Downward = ROC(Confirm,1) < 0 AND ROC(Confirm,5) < 0;

Select = Rs AND Ref(Fs,-1);
Caution = Fs AND Ref(Rs,-1);

Change = IIf( Rs, H/ValueWhen(Fs,L)*100, L/ValueWhen(Rs,H)*100 );


The rest seems more or less to make (some) sense ;)
 
Hi Wysiwyg

thanks for taking time to respond! :)

your code seems very clear to me: a change in direction of the slope = change in direction of the trend.
Graham adds the ROC of the DEMA on 1 period at the beginning of the code.

I am not too sure about what these 2 blocks are for though.
Could you help me understand this? (sorry I cant buy you a beer... unless you swing by Singapore! :) )
Sorry, I don't understand the loopy stuff that well but let's just say the same thing can be represented many ways. Functions are in the AFL library for greater education.
 
Top