//------------------------------------------------------------------
//                                                NRTR_ATR_STOP.mq4 
//------------------------------------------------------------------
#property copyright ""
#property link      ""

#property indicator_chart_window
#property indicator_buffers  2
#property indicator_color1 LimeGreen
#property indicator_color2 DarkOrange
#property indicator_width1 2
#property indicator_width2 2

extern string TimeFrame  = "Current time frame";
extern int    ATR        = 20;
extern double Coeficient = 2;

//
//
//
//
//

double Up[], Dn[], mode[];

string indicatorFileName;
bool   returnBars;
bool   calculateValue;
int    timeFrame;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
int init()
  {
   IndicatorBuffers(3);
   SetIndexBuffer(0, Up); SetIndexLabel (0, "Up");   SetIndexStyle (0,DRAW_LINE); 
   SetIndexBuffer(1, Dn); SetIndexLabel (1, "Dn");   SetIndexStyle (1,DRAW_LINE); 
   SetIndexBuffer(2, mode);
      indicatorFileName = WindowExpertName();
      returnBars        = TimeFrame == "returnBars";     if (returnBars)     return(0);
      calculateValue    = TimeFrame == "calculateValue"; if (calculateValue) return(0);
      timeFrame         = stringToTimeFrame(TimeFrame);
   return(0);
}
int deinit() 
{ 
   return(0); 
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
           int limit=MathMin(Bars-counted_bars,Bars-1);
           if (returnBars) { Up[0] = limit+1; return(0); }

   //
   //
   //
   //
   //
   
   if (calculateValue || timeFrame == Period())
   {
      for(int i = limit - 1; i >= 0; i--)
      {
       Dn[i]   = EMPTY_VALUE; 
       Up[i]   = EMPTY_VALUE;
       mode[i] = mode[i+1];
       double REZ = Coeficient*iATR(NULL, 0, ATR, i);

       if((mode[i] == -1 || mode[i]==EMPTY_VALUE) &&  Low[i+1] > Dn[i+1]) 
       { 
           Up[i+1] = Low[i+1] - REZ; 
           mode[i] = 1; 
       }
       if((mode[i]==1 || mode[i]==EMPTY_VALUE) && High[i+1] < Up[i+1]) 
       { 
           Dn[i+1] = High[i+1] + REZ; 
           mode[i] = -1; 
       }
       
       //
       //
       //
       //
       //
       
       if(mode[i]==1)
         {
           if(Low[i+1] > Up[i+1] + REZ) 
             { 
               Up[i] = Low[i+1] - REZ; 
               Dn[i] = EMPTY_VALUE; 
             }
		         else 
		           { 
		             Up[i] = Up[i+1]; 
                   Dn[i] = EMPTY_VALUE; 
		           }
		       }
       if(mode[i]==-1)
         {
     	     if(High[i+1] < Dn[i+1] - REZ) 
     	       { 
     	         Dn[i] = High[i+1] + REZ; 
     	         Up[i] = EMPTY_VALUE; 
     	       }
	          else 
	            { 
	              Dn[i] = Dn[i+1]; 
	              Up[i] = EMPTY_VALUE; 
	            }
	        }
      }
      return(0);
   }
   
   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   for (i=limit; i>=0; i--)
   {
       int y = iBarShift(NULL,timeFrame,Time[i]);               
          Up[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",ATR,Coeficient,0,y);
          Dn[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",ATR,Coeficient,1,y);
   }
   return(0);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   StringToUpper(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
