{ Downloaded From https://www.WiseStockTrader.com }
{ True, Reverse & MetaStock ATR v3.0 }
{ (c) Copyright 2004 Jose Silva }
{ http://www.metastocktools.com }

{ Reverse True Range is the the *smallest*
of the following for each period:
* The distance from today's High
to today's Low;
* The distance from yesterday's Close
to today's High;
* The distance from yesterday's Close
to today's Low.}

{ user input }
plot:=Input("[1]True ATR, [2]Reverse ATR, [3]Both, [4]MS-ATR",1,4,1);
pds:=Input("Average True Range periods",1,252,10);
pdsN:=Input("normalizing periods (1=none)",1,2520,1);
smooth:=Input("Sine-weighted smoothing? [1]Yes, [0]No",0,1,0);

{ define True Range }
x1:=ValueWhen(2,1,C);
TrueRange:=Max(H-L,Max(Abs(x1-H),Abs(x1-L)));
RevTrueRange:=Min(H-L,Min(Abs(x1-H),Abs(x1-L)));

{ average True Range }
ATRtrue:=Mov(TrueRange,pds,E);
ATRrev:=Mov(RevTrueRange,pds,E);
ATRmeta:=Mov(TrueRange,pds*2-1,E);

{ normalize ATR }
ATRraw:=If(plot=1,ATRtrue,If(plot=2,ATRrev,
If(plot=4,ATRmeta,ATRtrue)));
ATRnorm:=100*(ATRraw-LLV(ATRraw,pdsN))
/(HHV(ATRraw,pdsN)-LLV(ATRraw,pdsN)+.000001);
ATRplot:=If(pdsN<2,ATRraw,ATRnorm);
rATRnorm:=100*(ATRrev-LLV(ATRrev,pdsN))
/(HHV(ATRrev,pdsN)-LLV(ATRrev,pdsN)+.000001);
rATRplot:=If(pdsN<2,RevTrueRange,rATRnorm);

{ optional sine-weighted smoothing }
ATRplot:=If(smooth=1,(Sin(30)*ATRplot
+Sin(60)*Ref(ATRplot,-1)
+Sin(90)*Ref(ATRplot,-2)
+Sin(60)*Ref(ATRplot,-3)
+Sin(30)*Ref(ATRplot,-4))
/(Sin(30)*2+Sin(60)*2+Sin(90)),ATRplot);
rATRplot:=If(smooth=1,(Sin(30)*rATRplot
+Sin(60)*Ref(rATRplot,-1)
+Sin(90)*Ref(rATRplot,-2)
+Sin(60)*Ref(rATRplot,-3)
+Sin(30)*Ref(rATRplot,-4))
/(Sin(30)*2+Sin(60)*2+Sin(90)),rATRplot);

{ plot ATR }
If(plot=3,rATRplot,ATRplot);
ATRplot