Welcome Features News Download Registration Support FAQ Wish list Links
Advanced stock charting and analysis program

AFL Library

This is read-only version of AFL library entry. Ability to add user formulas and comment is only available from members-only area.

Details:

Formula name: Trend Detection
Author/Uploader: Graham Kavanagh - gkavanagh [at] e-wire.net.au
Date/Time added: 2005-01-10 18:55:44
Origin:
Keywords: Trends
Level: medium
Flags: indicator

DISCLAIMER: Most formulas present in AFL on-line library are submitted by the users and are provided here on an "as is" and "as available" basis. AmiBroker.com makes no representations or warranties of any kind to the contents or the operation of material presented here. We do not maintain nor provide technical support for 3rd party formulas.
Description:

Plots coloured bars for direction of current trend, green for rising, red for falling. Exploration included to find first bar of new direction.
Param included to vary the number of periods for detecting small to long term trends.
I am using beta version 4.66.2, but believe this will work with last offical version.

Formula:

// 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[i] )
  {
   R[i] = 1;
  }
  else
  {
   if( CondF[i] )
   {
    R[i] = 0;
   }
   else
   {
    R[i] = R[i-1];
   }
  }
 }
 return R;
} 

PrD = C;
PrL = H/2+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[i]  )
 {
  B[i] = 1;
 }
 else
 {
  if( df[i-1] && dr[i] )
  {
   B[i] = 1;
  }
  else
  {
   B[i] = 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 + " %";

Comments:

Tommy Beard
tab321 [at] yahoo.com
2005-01-14 14:42:01
Graham

This is a nice piece of work - thanks for sharing with us and thanks for putting the adjustable parameters in the code.

TBeard
Al_hobino
al_hobino
2005-01-15 13:25:46
As always, nice work Graham! Thanks.
tm

2005-01-22 13:52:27
thanks-why 12?
Graham Kavanagh
gkavanagh [at] e-wire.net.au
2005-01-22 18:04:47
If you mean the period default in param, just a number
I personally find 21 and 2 best visually for medium and short trends
Willem Jan
w.j.a.struyck [at] hccnet.nl
2005-07-29 00:47:57
Don't understand these lines:
"PrdD = PrdL = PrdM = Param("Prd",12,2,40,1);

permax = Max(prdd,prdl);"

If PrdD=PrdL Max has nothing to choose from.

Any explanation please.
Graham Kavanagh
gkavanagh [at] e-wire.net.au
2005-07-29 22:12:17
This published code comes from a more complex arranbgement that I personally use. Some lines I did not bother changing due to having to change the same variables through the code.
In the original PrdD and PrdL were separate values from when I was developing the system.
permax just stopped the results including anythin prior to the largest period so that my backtesting doid not include the first up signal, if the backtest include the chart beginning, which was just when the indicators kicked in with values.
If nothing else it gives you the arrangement to fiddle more easily with the code.
Ed Ramirez
rprs530 [at] yahoo.com
2005-09-18 03:00:37
That Looks nice.What Code can I use if I would Like to see a line trend instead of the barchart output Method? Thanks.
Graham Kavanagh
gkavanagh [at] e-wire.net.au
2005-09-18 22:11:16
change stylebar to styleline
Felix
felixthang [at] gmail.com
2006-04-08 00:05:32
I am using 4.79 I got errors and not able to display the indicator. please help me.
Graham Kavanagh
gkavanagh [at] e-wire.net.au
2006-04-08 02:47:01
check wordwrapping after you paste into editor
clicking Verify Syntax button should show which word is in wrong place
Mashael
shafarjeddah [at] hotmail.com
2006-12-04 04:29:09
Thanks Graham,good work,but can you make the to work with the outomatic analysis please
Regards
AO
hulksweider [at] verizon.net
2007-02-10 01:42:04
get the following error - have seen this in a few other files I have downloaded -

Unknown identifier at line 5, column 13:


function Rise
------------^

any help would be appreciated -
Dmitrii Zuzen
mitrizze [at] d3z.com [at] d3z.com
2007-04-15 06:07:47
Ne nasovsem a navsegda
TP

2008-01-18 13:38:12
Funny, the turnig points almost coincide with Parabloc SAR signals.
Another Brian
blog.tipster.ca
2008-03-05 11:49:27
When you use BUY / Sell, and plot the signals, the period is stuck at twelve. The buy / Sell signals do not occur at the same bar (or even close) to the red / green displayed on the screen.


About | Privacy | Terms of Use | Contact information
Copyright © 2001 AMIBROKER.COM