//+------------------------------------------------------------------+
//|                                             check_conditions.mq4 |
//|                                                                  |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "nightlight4"
#property link "http://www.mql5.com"
#property version "1.0.3"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
double di_plus[], di_minus[], adx[], adxr[];
extern int M = 2;
extern int adx_period = 14;
extern string check_symbols = "EURUSD GBPUSD GBPEUR USDJPY AUDUSD";
extern bool check_M5 = true;
extern bool check_M15 = true;
extern bool check_M30 = true;
extern bool check_H1 = true;
extern bool check_H4 = true;
extern bool check_esike = true;
extern bool check_mas = true;
extern bool check_ma_200_proximity = true;
extern int minimum_time_between_alerts_in_sec = 60;



ENUM_TIMEFRAMES check_timeframes[5];
int no_timeframes = 0;

datetime now;

int init()
{
  
  ArrayResize(di_plus, 4000);
  ArrayResize(di_minus, 4000);
  ArrayResize(adx, 4000);
  ArrayResize(adxr, 4000);
  now = TimeCurrent()-minimum_time_between_alerts_in_sec;
  if (check_M5)
  {
    check_timeframes[no_timeframes] = PERIOD_M5;
    no_timeframes++;
  } 
  
  if (check_M15)
  {
    check_timeframes[no_timeframes] = PERIOD_M15;
    no_timeframes++;
  } 
  
  if (check_M30)
  { 
    check_timeframes[no_timeframes] = PERIOD_M30;
    no_timeframes++;
  } 
  
  if (check_H1)
  { 
    check_timeframes[no_timeframes] = PERIOD_H1;
    no_timeframes++;
  } 
  
  if (check_H4)
  { 
    check_timeframes[no_timeframes] = PERIOD_H4;
    no_timeframes++;
  } 
  
  
  
  return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
string prev_text;
int no;
int limit = 20;
void start()
{
  if (TimeCurrent()-now>= minimum_time_between_alerts_in_sec)
  {
     now = TimeCurrent();
  }
  else
    return;
  
  
  int i;
  
  string text;
  string compare_text;
  
  no = 5;
  string symbols[];
  StringSplit(check_symbols, StringGetCharacter(" ", 0), symbols);
  
  
  
  for (int s = 0; s < ArraySize(symbols); s++)
  {
//Print(symbols[s]);
    bool reported200 = false;
    for (int t = 0; t < no_timeframes; t++)
    {
      
      ENUM_TIMEFRAMES tf = check_timeframes[t];
      string tfs = EnumToString(tf);
      
      double ma72 = iMA(symbols[s],tf,72,0,MODE_SMMA,PRICE_CLOSE,0);
      double ma24 = iMA(symbols[s],tf,24,0,MODE_SMMA,PRICE_CLOSE,0);
      
      
      double ma = iMA(symbols[s],tf,200,0,MODE_SMA,PRICE_CLOSE,0);
      
      if (check_ma_200_proximity && !reported200 && ma*0.95<iClose(symbols[s], tf,0) && ma*1.05>iClose(symbols[s], tf,0))
      {
         text += symbols[s] + " " + tfs + " 200 ma is close within 5%\n";
         compare_text += symbols[s] + " " + tfs + " 200 ma is close winthin 5%\n";
         reported200 = true;
      }
      
      int direction = 0;
      
      if (!check_mas)
      {
      
      }
      else if (ma24< iClose(symbols[s], tf,0) && ma72< iClose(symbols[s], tf,0))
      {
        direction = 1;
      }
      else if (ma24> iClose(symbols[s], tf,0) && ma72> iClose(symbols[s], tf,0))
      {
       direction = -1;
      }
      else
       continue;
      
      
      
      
      for (int i = no - 1;i >= 0;i--)
      {
        di_plus[i] = iADX(symbols[s], tf, adx_period, PRICE_CLOSE, MODE_PLUSDI, i);
        di_minus[i] = iADX(symbols[s], tf, adx_period, PRICE_CLOSE, MODE_MINUSDI, i);
        adx[i] = iADX(symbols[s], tf, adx_period, PRICE_CLOSE, MODE_MAIN, i);
      }
      
      for (i = no - 1 - M;i >= 0;i--)
      {
        adxr[i] = (adx[i] + adx[i + M]) / 2;
      }
      
      if (check_esike)
      if (adxr[0] >= limit && ( (direction>=0 && di_plus[0] >= limit) || direction<=0 && di_minus[0] >= limit ) 
               && (adx[0] > di_plus[0] 
               && adx[0] < di_minus[0] || adx[0] < di_plus[0] && adx[0] > di_minus[0]))
      {
        text += symbols[s] + " " + tfs + " :time to check Esike adxr:" + NormalizeDouble(adxr[0], 1) + " adx:" + NormalizeDouble(adx[0], 1) + " di+:" + NormalizeDouble(di_plus[0], 1) + " di-:" + NormalizeDouble(di_minus[0], 1) + "\n" ;
        compare_text += symbols[s] + " " + tfs + " :time to check Esike adxr:";
      } 
      
      
      if (check_esike)
      if (adxr[0] >= limit && adx[0] > di_minus[0] && adx[0] > di_plus[0] && adx[0] > adxr[0])
      {
        text += symbols[s] + " " + tfs + " :time to check advanced Esike adxr:" + NormalizeDouble(adxr[0], 1) + " adx:" + NormalizeDouble(adx[0], 1) + " di+:" + NormalizeDouble(di_plus[0], 1) + " di-:" + NormalizeDouble(di_minus[0], 1) + "\n" ;;
        compare_text += symbols[s] + " " + tfs + " :time to check advanced Esike";
      }
      
    }
    
  }
  
  
  
  if (prev_text != compare_text)
  {
   // Print(text);
    string lines[];
    StringSplit(text, StringGetCharacter("\n", 0), lines);
    for (int i=0; i < ArraySize(lines)-1; i++)
    {
      Alert(lines[i]);
    }
  }
  
  prev_text = compare_text;
  
}
//+------------------------------------------------------------------+
