//+------------------------------------------------------------------+ //| 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); } //+------------------------------------------------------------------+