//+------------------------------------------------------------------+
//|                                           AA+ADX_Compression.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window

#property indicator_buffers 3
#property indicator_color1 clrRed
#property indicator_color2 clrBlue
#property indicator_color3 clrNONE

input string _comment_1 = "-- lower level --";
input double level_1 = 10.0;
input string _comment_2 = "-- upper level --";
input double level_2 = 20.0;
input color clrAbovelevelTwo = clrNONE;
input color clrBelowlevelTwo = clrBlue;
input color clrBelowLevelOne = clrRed;
input int maxBars = 3000;


#property indicator_minimum -200
#property indicator_maximum 10

//#property indicator_width1 1
//#property indicator_width2 1
//#property indicator_width3 1

// indicator buffers
double bAdx_1[];
double bAdx_2[];
double bAdx_3[];

string symb;
int tf;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

   symb = Symbol();
	tf = Period();
	int db = 14;
	int b = -1;
	// 0 - MODE_MAIN, 1 - MODE_PLUSDI, 2 - MODE_MINUSDI
	b++; SetIndexBuffer(b,bAdx_1);  SetIndexStyle(b, DRAW_HISTOGRAM, 0, 4, clrBelowLevelOne);	SetIndexDrawBegin(b, db);
	b++; SetIndexBuffer(b,bAdx_2);  SetIndexStyle(b, DRAW_HISTOGRAM, 0, 4, clrBelowlevelTwo);	SetIndexDrawBegin(b, db);
	b++; SetIndexBuffer(b,bAdx_3);  SetIndexStyle(b, DRAW_HISTOGRAM, 0, 1, clrAbovelevelTwo);	SetIndexDrawBegin(b, db);
	
  
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
//---

   int limit, initialLimit, shift;
   double value_1, value_2, value_3, value_4, value_5, max;
	
   limit = rates_total - prev_calculated;
	initialLimit = MathMin((int)maxBars, iBars(symb, tf) - 5);
	if (prev_calculated == 0) {
		limit = initialLimit;
	}

   for (shift=limit; shift>=0; shift--) {
		
		// calculate raw lines
		value_1 = iADX(symb, tf, 7, PRICE_CLOSE, MODE_MAIN, shift);
		value_2 = iADX(symb, tf, 21, PRICE_CLOSE, MODE_MAIN, shift);
		value_3 = iADX(symb, tf, 34, PRICE_CLOSE, MODE_MAIN, shift);
		value_4 = iADX(symb, tf, 89, PRICE_CLOSE, MODE_MAIN, shift);
		value_5 = iADX(symb, tf, 144, PRICE_CLOSE, MODE_MAIN, shift);

	   
	   bAdx_1[shift] = EMPTY_VALUE;
	   bAdx_2[shift] = EMPTY_VALUE;
	   bAdx_3[shift] = EMPTY_VALUE;
	   max = -100;
	   if (value_1 > max) { max = value_1;}
	   if (value_2 > max) { max = value_2;}
	   if (value_3 > max) { max = value_3;}
	   if (value_4 > max) { max = value_4;}
	   if (value_5 > max) { max = value_5;}
	   if (max > level_2) { bAdx_3[shift] = max;}
	   else if (max > level_1) { bAdx_2[shift] = max;}
	   else if (max < level_1) { bAdx_1[shift] = max;}

	}
   
//--- return value of prev_calculated for next call
   return(rates_total);
}
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
  }
//+------------------------------------------------------------------+
