bleh
[mqlkit.git] / indicators / ATR.mq4
blob12e6cdb8518227e3d467b453348eae9e896c266b
1 //+------------------------------------------------------------------+\r
2 //|                                                          ATR.mq4 |\r
3 //|                      Copyright © 2005, MetaQuotes Software Corp. |\r
4 //|                                       http://www.metaquotes.net/ |\r
5 //+------------------------------------------------------------------+\r
6 #property copyright "Copyright © 2005, MetaQuotes Software Corp."\r
7 #property link      "http://www.metaquotes.net/"\r
8 \r
9 #property indicator_separate_window\r
10 #property indicator_buffers 1\r
11 #property indicator_color1 DodgerBlue\r
12 //---- input parameters\r
13 extern int AtrPeriod=14;\r
14 //---- buffers\r
15 double AtrBuffer[];\r
16 double TempBuffer[];\r
17 //+------------------------------------------------------------------+\r
18 //| Custom indicator initialization function                         |\r
19 //+------------------------------------------------------------------+\r
20 int init()\r
21   {\r
22    string short_name;\r
23 //---- 1 additional buffer used for counting.\r
24    IndicatorBuffers(2);\r
25 //---- indicator line\r
26    SetIndexStyle(0,DRAW_LINE);\r
27    SetIndexBuffer(0,AtrBuffer);\r
28    SetIndexBuffer(1,TempBuffer);\r
29 //---- name for DataWindow and indicator subwindow label\r
30    short_name="ATR("+AtrPeriod+")";\r
31    IndicatorShortName(short_name);\r
32    SetIndexLabel(0,short_name);\r
33 //----\r
34    SetIndexDrawBegin(0,AtrPeriod);\r
35 //----\r
36    return(0);\r
37   }\r
38 //+------------------------------------------------------------------+\r
39 //| Average True Range                                               |\r
40 //+------------------------------------------------------------------+\r
41 int start()\r
42   {\r
43    int i,counted_bars=IndicatorCounted();\r
44 //----\r
45    if(Bars<=AtrPeriod) return(0);\r
46 //---- initial zero\r
47    if(counted_bars<1)\r
48       for(i=1;i<=AtrPeriod;i++) AtrBuffer[Bars-i]=0.0;\r
49 //----\r
50    i=Bars-counted_bars-1;\r
51    while(i>=0)\r
52      {\r
53       double high=High[i];\r
54       double low =Low[i];\r
55       if(i==Bars-1) TempBuffer[i]=high-low;\r
56       else\r
57         {\r
58          double prevclose=Close[i+1];\r
59          TempBuffer[i]=MathMax(high,prevclose)-MathMin(low,prevclose);\r
60         }\r
61       i--;\r
62      }\r
63 //----\r
64    if(counted_bars>0) counted_bars--;\r
65    int limit=Bars-counted_bars;\r
66    for(i=0; i<limit; i++)\r
67       AtrBuffer[i]=iMAOnArray(TempBuffer,Bars,AtrPeriod,0,MODE_SMA,i);\r
68 //----\r
69    return(0);\r
70   }\r
71 //+------------------------------------------------------------------+