I'm trying to add an EMA filter to an indicator.

 

I'm a novice in MQL5, and I'm trying to add an EMA as a filter to an indicator. I want this indicator to only give buy alerts when the price closes above the EMA and sell alerts when the price is below the EMA.
To achieve this, I'm trying to merge the code from two different indicators. Here's my code:

int OnCalculate(const int rates_total,    // Función llamada en cada cálculo del indicador.
                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[])
{
    bool IsNewCandle = CheckIfNewCandle();       // Verifica si es una nueva vela.

    int counted_bars = 0;
    if (prev_calculated > 0) counted_bars = prev_calculated - 1;

    if (counted_bars < 0) return -1;
    if (counted_bars > 0) counted_bars--;
    int limit = rates_total - counted_bars;

    if (limit > BarsToScan)       // Limita la cantidad de barras a escanear.
    {
        limit = BarsToScan;
        if (rates_total < BarsToScan + RSIPeriod) limit = rates_total - RSIPeriod;
    }
    if (limit > rates_total - RSIPeriod) limit = rates_total - RSIPeriod;

    if (CopyBuffer(BufferMainHandle, 0, 0, limit, BufferMain) <= 0)      // Copia los datos al buffer principal.
    {
        Print("Failed to create the indicator! Error: ", GetLastErrorText(GetLastError()), " - ", GetLastError());
        return 0;
    }

    for (int i = limit - 1; (i >= 0) && (!IsStopped()); i--)       // Obtiene datos de las velas.
    {
        Open[i] = iOpen(Symbol(), PERIOD_CURRENT, i);
        Low[i] = iLow(Symbol(), PERIOD_CURRENT, i);
        High[i] = iHigh(Symbol(), PERIOD_CURRENT, i);
        Close[i] = iClose(Symbol(), PERIOD_CURRENT, i);
        Time[i] = iTime(Symbol(), PERIOD_CURRENT, i);
    }

    if ((IsNewCandle) || (prev_calculated == 0))       // Si es una vela nueva o la primera vez.
    {
        if (EnableDrawArrows) DrawArrows(limit);
        CleanUpOldArrows();
    }

    if (EnableDrawArrows) DrawArrow(0);       // Dibuja una flecha.

    if (EnableNotify) NotifyHit();      // Realiza notificaciones.
    
    
    // EMA
    if(prev_calculated==0)
     {
      EmaBuffer[0] = 0.;
     }

   for(int i=0;i<rates_total;i++)
     {
      if(rates_total<InpPeriod)
        {
         return 0;
        }

      if(i>0)
        {
         EmaBuffer[i] = ExponentialMA(i, InpPeriod, EmaBuffer[i-1], close);
        }
     }

    lastEMA = EmaBuffer[rates_total - 1];
    PrintFormat("Valor EMA: %f", lastEMA);
   /////

    return rates_total;
}

However, I'm getting an 'array out of range (150,17)' error, referring to the line: 'EmaBuffer[0] = 0.;'
The RSI line seems to go to infinity.  And I'm only getting incorrect buy signals

I've attached a screenshot of the indicator's behavior on the chart.

graph

Could someone please help me with this code? Thanks in advance.

Files:
RSI.zip  15 kb
 

Maybe this already exists: https://www.mql5.com/en/search#!keyword=rsi%20ema

There's virtually nothing that hasn't already been programmed for MT4/MT5.

Reason: