//+------------------------------------------------------------------+
//|                                              opita_multipair.mq4 |
//|                               Copyright 2018, Kanny Technologies |
//|                                        https://www.kannytech.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, Kanny Technologies"
#property link      "https://www.kannytech.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

input string pairs = "AUDCAD AUDCHF AUDJPY AUDNZD AUDUSD CADCHF CADJPY CHFJPY EURAUD EURCAD EURCHF EURGBP EURJPY EURNZD EURUSD GBPAUD GBPCAD GBPCHF GBPJPY GBPNZD GBPUSD NZDCAD NZDCHF NZDJPY NZDUSD USDCAD USDCHF USDJPY US30 USTEC XAUUSD XAGUSD"; //Pairs
input int x_distance = 30; //X Distance in Points
input double atr_multiplier=2.5;//ATR multiplier to reset alert
input bool time_filter = false; //Use Time Filter
input string starttime="09:00"; //Start Time HH:MM
input string endtime="14:00"; //End Time Time HH:MM
input string moving_average_settings = "Indicator Settings"; //Moving Average Settings
input ENUM_TIMEFRAMES ma_timeframe = PERIOD_M30; //MA TimeFrame
input int ma_period=96; //MA Averaging Period 
int ma_shift=0; //MA Shift
input ENUM_MA_METHOD ma_method=MODE_EMA; //Averaging Method 
input ENUM_APPLIED_PRICE ma_applied_price=PRICE_CLOSE; //Applied Price 
input int atr_period=16;

string currency[];
bool g_alert_enabled[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
      int ret_code = StringSplit(pairs,' ',currency);
      if(ret_code == 0)
      {
          Print("Pairs is Empty");
      }
      if(ret_code < 0)
      {
          Print("Error in Pairs: "+IntegerToString(GetLastError()));
      }
      ArrayResize(g_alert_enabled,ArraySize(currency));
      ArrayInitialize(g_alert_enabled,false);
      
//---
   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[])
  {
//---
      MqlDateTime time1;
      TimeCurrent(time1);
      time1.hour = StrToInteger(StringSubstr(starttime,0,2));
      time1.min = StrToInteger(StringSubstr(starttime,3,2));
      time1.sec = 0;
      
      MqlDateTime time2;
      TimeCurrent(time2);
      time2.hour = StrToInteger(StringSubstr(endtime,0,2));
      time2.min = StrToInteger(StringSubstr(endtime,3,2));
      time2.sec = 0;
      
      if((time_filter == false) ||(TimeCurrent() > StructToTime(time1) && TimeCurrent() < StructToTime(time2)))
      {
         for(int i=0;i<ArraySize(currency);i++)
         {
            double ma_value = iMA(currency[i],ma_timeframe,ma_period,ma_shift,ma_method,ma_applied_price,0);
            //Print(currency[i]+" MA Value "+EnumToString(ma_timeframe)+" :"+DoubleToStr(ma_value,(int)SymbolInfoInteger(currency[i],SYMBOL_DIGITS)));
            //Print(currency[i]+" Bid: "+DoubleToStr(SymbolInfoDouble(currency[i],SYMBOL_BID),(int)SymbolInfoInteger(currency[i],SYMBOL_DIGITS)));
            //Print("Difference: "+IntegerToString((int)MathAbs((SymbolInfoDouble(currency[i],SYMBOL_BID) - ma_value)/SymbolInfoDouble(currency[i],SYMBOL_POINT))));
            double atr=atr_multiplier*iATR(currency[i],ma_timeframe,atr_period,0);
            if(!g_alert_enabled[i])
            {
               if(SymbolInfoDouble(currency[i],SYMBOL_BID)>=(ma_value+atr)) g_alert_enabled[i]=true;
               if(SymbolInfoDouble(currency[i],SYMBOL_BID)<=(ma_value-atr)) g_alert_enabled[i]=true;
               //if(g_alert_enabled[i]) Print("Alert enabled for "+currency[i]);
            }
            
            if(MathAbs((SymbolInfoDouble(currency[i],SYMBOL_BID) - ma_value)/SymbolInfoDouble(currency[i],SYMBOL_POINT)) <= x_distance)
            {
               if(g_alert_enabled[i])
               {
                  Alert(TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS)+": "+currency[i] +" Price Touching MA @ "+DoubleToString(SymbolInfoDouble(currency[i],SYMBOL_BID),(int)SymbolInfoInteger(currency[i],SYMBOL_DIGITS)));
                  SendNotification(TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS)+": "+currency[i] +" Price Touching MA @ "+DoubleToString(SymbolInfoDouble(currency[i],SYMBOL_BID),(int)SymbolInfoInteger(currency[i],SYMBOL_DIGITS)));
                  g_alert_enabled[i]=false;
               }
            }
         }
      }
      
//--- 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)
  {
//---
   
  }
//+------------------------------------------------------------------+
