ATR CountBack

Author: Altered by cja
ATR CountBack
Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each barSeries array that contains close prices for each bar
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
1 Views
0 Downloads
0 Favorites
ATR CountBack
//+------------------------------------------------------------------+
//|                                                ATR CountBack.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Altered by cja"
#property link      "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//---- input parameters
extern int AtrPeriod=14;
extern int ATR_TF = 60;
extern int CountBack = 0;

//---- buffers
double AtrBuffer[];
double TempBuffer[];

string TimeFrameStr;
extern int Maxbars   = 24;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
    
    switch(ATR_TF)
   {
      case 1 : TimeFrameStr="Period M1"; break;
      case 5 : TimeFrameStr="Period M5"; break;
      case 15 : TimeFrameStr="Period M15"; break;
      case 30 : TimeFrameStr="Period M30"; break;
      case 60 : TimeFrameStr="Period H1"; break;
      case 240 : TimeFrameStr="Period H4"; break;
      case 1440 : TimeFrameStr="Period D1"; break;
      case 10080 : TimeFrameStr="Period W1"; break;
      case 43200 : TimeFrameStr="Period MN1"; break;
      default : TimeFrameStr=" Current Timeframe "; ATR_TF=0;
   }
  
   string short_name;
//---- 1 additional buffer used for counting.
   IndicatorBuffers(2);
//---- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,AtrBuffer);
   SetIndexBuffer(1,TempBuffer);
//---- name for DataWindow and indicator subwindow label
   short_name="ATR( "+AtrPeriod+" )( "+TimeFrameStr+" )( "+CountBack+" )";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//----
   SetIndexDrawBegin(0,AtrPeriod);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Average True Range                                               |
//+------------------------------------------------------------------+
int start()
  {
   int i,counted_bars=IndicatorCounted();
   
    int limit;
   limit = MathMin(counted_bars,Maxbars);
   if (limit ==0)
      limit = Maxbars;
   
  //----
   if(Bars<=AtrPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=AtrPeriod;i++) AtrBuffer[Bars-i]=0.0;
//----
   i=Bars-counted_bars-1;
   while(i>=0)
     {
      double high=iHigh(NULL,ATR_TF,i+CountBack);
      double low =iLow(NULL,ATR_TF,i+CountBack);
      if(i==Bars-1) TempBuffer[i]=high-low;
      else
        {
         double prevclose=iClose(NULL,ATR_TF,i+1+CountBack);
         TempBuffer[i]=MathMax(high,prevclose)-MathMin(low,prevclose);
        }
      i--;
     }
//----
   if(counted_bars>0) counted_bars--;
   //int limit=Bars-counted_bars;
   for(i=0; i<limit; i++)
      AtrBuffer[i]=iMAOnArray(TempBuffer,Bars,AtrPeriod,0,MODE_SMA,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 ---