AverageTrueRange

Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
AverageTrueRange
//+------------------------------------------------------------------+
//|                                             AverageTrueRange.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//---- input parameters
 extern int period = 14;
//---- buffer
 double Buffer[];
 double Range[100000]; 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 1 additional buffer used for counting.
   IndicatorBuffers( 1 );
 //  IndicatorDigits( Digits );
//---- indicator line
   SetIndexStyle( 0, DRAW_LINE );
   SetIndexBuffer( 0, Buffer );
//---- name for DataWindow and indicator subwindow label
   string short_name = "ATR("+period+")";
   IndicatorShortName( short_name );
   SetIndexLabel( 0, short_name );
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {

     double sum;
     int counted_bars = IndicatorCounted( );   
     //---- ïîñëåäíèé ïîñ÷èòàííûé áàð áóäåò ïåðåñ÷èòàí
     if ( counted_bars > 0 ) counted_bars--;
     int limit = Bars-counted_bars-1;
     //---- îñíîâíîé öèêë 
     
       for ( int i = 0; i <= limit; i++ )
       { 
           double high = High[i];
           double low = Low[i];          
           double prevclose = Close[i + 1]; 
           
              if ( high < prevclose )
              { 
               high = prevclose;
              }
              else
              {
              if ( low > prevclose )
              {
               low = prevclose;
              }
              }
              
                 Range[i] = high - low; 
                          
                    if ( i >= period )
                    { 
                     Buffer[i - period] = sum / period;
                     sum = sum + Range[i] - Range[i - period];                     
                    }
                    else
                    {
                    if ( i < period )
                    {
                     sum = sum + Range[i];
                    }
                    }        
        }     
    
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---