EMA Alert Cross on multi currency

 

Hi all, I'm trying to build a simple cross alert ea, but I would like to make it for every pair in the same code.

It is bugged because sometimes it alert me for crosses that doesn't exist, and also spam a lot..

I'm working with mql5


#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

struct Pair {
   string name;
   datetime time_incrocio;
      Pair(){
         time_incrocio = TimeCurrent()-60*tf*2;
         }
};

Pair pairs[];

ENUM_TIMEFRAMES tf = PERIOD_M15;

int OnInit()
  {
//---

    int symbols = SymbolsTotal(false); 
    
    for(int i=0; i<symbols; i++){
      int a = i+1; 
      ArrayResize(pairs,a);
      string name = SymbolName(i,false);
      SymbolSelect(name,true); //aggiungi i pair nel MarketWatch, altrimenti non trova il symbolname
      pairs[i].name = name;
      Print(name,": ",iClose(name,tf,222)); //this work
      if(name == "BTCUSD") break;
    }   
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

int input ema_fast = 50;
int input ema_slow = 200;

void OnTick()
  {
//---
   int size = ArraySize(pairs);
   for(int i = 0; i < size; i++){
      checkCross(i);
   }
  }
//+------------------------------------------------------------------+

void checkCross(int i){
   int a = incrocio(i);
   if(a == 1){ //incrocio a rialzo
      Alert("Incrocio a rialzo su", pairs[i].name);
   }
   if(a == 2){ //incrocio a ribasso
      Alert("Incrocio a ribasso su", pairs[i].name);
      
   }
}

int incrocio(int i){
   if(getShift(pairs[i].time_incrocio)>=1){
      string pair_name = pairs[i].name;
      double ema_fast_0 = getEMA(ema_fast,0,pair_name);
      double ema_fast_1 = getEMA(ema_fast,1,pair_name); 
      double ema_fast_2 = getEMA(ema_fast,2,pair_name);
      
      double ema_slow_0 = getEMA(ema_slow,0,pair_name);
      double ema_slow_1 = getEMA(ema_slow,1,pair_name);
      double ema_slow_2 = getEMA(ema_slow,2,pair_name);
      
      if(ema_fast_0 == 0.0) Print("No data");
      
      if(ema_fast_2 < ema_slow_2 && ema_fast_1 > ema_slow_1){
         Print(pairs[i].name,": ",ema_fast_0," slow: ",ema_slow_0); //invalid values sometimes
         return 1;
      }
      if(ema_fast_2 > ema_slow_2 && ema_fast_1 < ema_slow_1){
         Print(pairs[i].name,": ",ema_fast_2," slow:",ema_slow_0);
         return 2;
      }
      pairs[i].time_incrocio = TimeCurrent();
   }
   return 0;
}

double getEMA(int period, int shift, string sym){
   int ExtHandle = iMA(sym,tf,period,0,MODE_EMA,PRICE_CLOSE);
   if(ExtHandle==INVALID_HANDLE){
      printf("Error creating MA indicator");
      return(-1);
   }
   double ema[1];
   if(CopyBuffer(ExtHandle,0,shift,1,ema)!=1){
      Print("CopyBuffer from iMA failed, no data for ",sym);
      return -1;
   }
   return ema[0];
}

double getShift(datetime TimE){
   return iBarShift(Symbol(),tf,TimE);
}
Reason: