Raw ATR Formula

Hi Everyone,
I recently try to replicate the ATR formula (ATR(14). with below code.

_SECTION_BEGIN("ATR");
YATRp = Param("ATR Period", 14, 2, 100, 1 );
YATR = ATR(YATRp);
YATRco = ParamColor("ATR color", colorLightOrange  );

Plot( YATR, StrFormat("ATR(%g)", YATRp), YATRco, styleLine,Null,Null,0,0,2 );

Calatr = Max(High,Ref(Close, -1))- Min(Low , Ref(Close, -1));

Yma2 = MA(Calatr, YATRp);


Plot(Yma2, StrFormat("ATR1(%g)", YATRp), YATRco, styleLine,Null,Null,0,0,2 );


_SECTION_END();

But did not get the success. Both the value is coming different.
Kindly provide the Raw atr formula.

Purpose of needed
Need to check the other indicator on ATR.
I know I can change the basic open, high , low, close of afl.
Like Close = RSI(14);
But that will show problem on my case because it will change all the built in function and my afl code also.
If I have raw formula I can modified it as needed. Kindly suggest.

https://www.amibroker.com/guide/afl/atr.html

AFL Function Reference - ATR

ATR

  • average true range Indicators
    (AFL 1.3)
SYNTAX atr( period )
RETURNS ARRAY
FUNCTION Calculates Average True Range indicator
EXAMPLE atr(7)
SEE ALSO

Comments:

Bob Jagow
bjagow@charterr.net
2003-02-06 23:37:50 For other MAs, use ATR(1) to get the True Range.

E.g., MA(ATR(1),period), WMA(ATR(1),period), etc.
Tomasz Janeczko
tj --at-- amibroker.com
2005-02-03 06:46:39 Note that original formulation of ATR (the one that AmiBroker implements)
uses WILDERS smoothing (not simple moving average)


So,

function cATR1( period ) {
    refC1 = Ref(C,-1);
    true_range = Max(H-L, Max(abs(H-refC1), abs(L-refC1)));
    return Wilders(true_range, period);
}

function cATR2( period ) {
    return Wilders(ATR(1), period);
}

period = 15;

Plot( cATR1( period ), "cATR1", colorRed, styleLine );
Plot( cATR2( period ), "cATR2", colorOrange, styleLine );
Plot( ATR( period ), "ATR in-built", colorGreen, styleLine ); 
5 Likes