I took a stab at it
Note the considerable similarities between MS and AB code.
//Dunn-Type1
//==========
TD1 = IIf( BarsSince( H > Ref( H, -1 ) AND L > Ref( L, -1 ) ) <
BarsSince( L < Ref( L, -1 ) AND H < Ref( H, -1 ) ), 1, -1 );
//Dunn-Type2
//==========
TD2 = IIf( BarsSince( ( H > Ref( H, -1 ) AND L > Ref( L, -1 ) ) AND
( Ref( H, -1 ) > Ref( H, -2 ) AND Ref( L, -1 ) > Ref( L, -2 ) ) ) <
BarsSince( ( L < Ref( L, -1 ) AND H < Ref( H, -1 ) ) AND
( Ref( L, -1 ) < Ref( L, -2 ) AND Ref( H, -1 ) < Ref( H, -2 ) ) ), 1, -1
);
// Ask to use 1 Day OR 2 Day Swing type}
St = Param( "Short Term Swing Type, 1 or 2 ?", 2, 1, 2, 1 );
//{Call Swing Type Formula}
Sd = IIf( St == 1, TD1, TD2 );
//{Number Of Periods Since Swing Started Up}
Hc = BarsSince( SD = -1 );
//{Number Of Periods Since Swing Started Down}
Lc = BarsSince( SD = 1 );
//{Find Highest Value Of Up Swing}
Hv = IIf( Hc > Lc AND H > Ref( H, -1 ), HighestSince( Hc = 1, H, 1 ) , 0 );
//{Find Lowest Value Of Down Swing}
Lv = IIf( Hc < Lc AND L < Ref( L, -1 ), LowestSince( Lc = 1, L, 1 ) , 0 );
//{Find The Low Of The Highest High}
Hlv = ValueWhen( H = Hv, L, 1 ) ;
//{Find The High Of The Lowest Low}
Lhv = ValueWhen( L = Lv, H, 1 ) ;
//{Calculate AND Plot Trend Direction,
//Note: 1= Uptrend,
//-1= Downtrend}
TD3 = IIf( Sd = 1 AND H > Lhv, 1, IIf( Sd = -1 AND L < Hlv, -1, 0 ) );
TD4 = ValueWhen( TD2 != 0,
TD2, 1 );
// All that remains is to plot whatever you want to see
Best Regards
Rick Osborn
From: Tim <timkthung@xxxxxxxxxxxx>
To: amibroker@xxxxxxxxxxxxxxx
Sent: Fri, November 27, 2009 3:29:34 AM
Subject: [amibroker] Dunnigan Trend
Hi,
I am a newbie to Amibroker, but I had
tried Metastock briefly. To me Amibroker is more user friendly and has many
value added applications such as free data feed from IB and yahoo
+ many others....
I need your help converting one my trend
following indicator to Amibroker format and create buy and sell signals for my
backtesting. While at the same time I will try to learn a little bit at a time
in doing so. Any useful reference for me would be appreciated.
Anyway thinks you all in advance.
Best regards,
Tim
Dunn-Type1
==========
TD1:=If(BarsSince( H>Ref(H,-1) AND L>Ref(L,-1))
<
BarsSince(L<Ref(L,-1) AND
H<Ref(H,-1)),
{then}1,
{else}-1);
TD1
Dunn-Type2
==========
TD1:=If(BarsSince( (H>Ref(H,-1) AND
L>Ref(L,-1))
AND (Ref(H,-1)>Ref(H,-2)
AND Ref(L,-1)>Ref(L,-2)))
<
BarsSince((L<Ref(L,-1) AND H<Ref(H,-1))
AND
(Ref(L,-1)<Ref(L,-2)
AND
Ref(H,-1)<Ref(H,-2))),
{then}1,
{else}-1);
TD1
Dunnigan Trend
============ ==
{Ask to use 1 day or 2 day Swing
type}
St:=Input("Short Term Swing Type, 1 or 2 ?",
1,2,2);
{Call Swing
Type Formula}
Sd:=If(Round( St)=1,
{then}
FmlVar("Dunn- Type1","TD1" ),
{else} FmlVar("Dunn- Type2","TD1" ));
{Number Of
Periods Since Swing Started Up}
Hc:=BarsSince( SD=-1);
{Number Of Periods
Since Swing Started Down}
Lc:=BarsSince( SD=1);
{Find Highest Value Of Up
Swing}
Hv:=If(Hc>Lc AND
H>Ref(H,-1),
{then}HighestSince( 1,Hc=1,H) ,
{else}0);
{Find Lowest
Value Of Down Swing}
Lv:=If(Hc<Lc AND
L<Ref(L,-1),
{then}LowestSince( 1,Lc=1,L) ,
{else}0);
{Find The Low Of
The Highest High}
Hlv:=ValueWhen( 1,H=Hv,L) ;
{Find The High Of The Lowest
Low}
Lhv:=ValueWhen( 1,L=Lv,H) ;
{Calculate And Plot Trend
Direction,
Note: 1= Uptrend,
-1= Downtrend}
TD2:=If(Sd=1 AND
H>Lhv,
{then}1,
{else}If(Sd= -1 AND
L<Hlv,
{then}-1,
{else}0));
TD3:=ValueWhen( 1,TD2<>0,TD2);
TD3