Setting up a moving averages crossover indicator

 

Hello guys, 

based on 2 moving averages (fast and slow), I want to create an indicator cv which is

  • cv[i] = 1, if a crossover happened at bar i
  • cv[i] = 0 else

When I add the indicator to the chart, cv[i] is always zero and checking the log, ExtFastMaBuffer[0] never changes the value besides being a Timeseries (set true in OnInit()). Strangely enough, the same code run in the Strategy Tester actually outputs sometimes cv[i] = 1. Any advice is appreciated.

//+------------------------------------------------------------------+
//|                                                         MACD.mq5 |
//|                   Copyright 2009-2020, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright   "2009-2020, MetaQuotes Software Corp."
#property link        "http://www.mql5.com"
#property description ""
//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   1

#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  Silver
#property indicator_width1  2
#property indicator_label1  "Cross over"

//--- input parameters
input int                InpFastMA=10;               // Fast EMA period
input int                InpSlowMA=30;               // Slow EMA period

//--- indicator buffers

double ExtCVBuffer[];     // 1: if crossover occured, 0:else
double ExtFastMaBuffer[];     // Fast MA
double ExtSlowMaBuffer[];     // Slow MA

int    ExtFastMaHandle;
int    ExtSlowMaHandle;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- indicator buffers mapping

   SetIndexBuffer(0,ExtCVBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtFastMaBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,ExtSlowMaBuffer,INDICATOR_CALCULATIONS);

//--- get MA handles
   ExtFastMaHandle=iMA(NULL,0,InpFastMA,0,MODE_SMA,PRICE_CLOSE);
   ExtSlowMaHandle=iMA(NULL,0,InpSlowMA,0,MODE_SMA,PRICE_CLOSE); 
   
   ArraySetAsSeries(ExtFastMaBuffer, true);
   ArraySetAsSeries(ExtSlowMaBuffer, true);
  }

 void setCV(int i)
   {
      double prev;
      double curr;
      
      ExtCVBuffer[i] = 0;
      prev = ExtFastMaBuffer[1]-ExtSlowMaBuffer[1];
      curr = ExtFastMaBuffer[0]-ExtSlowMaBuffer[0];
     
     Print ("i: ", i); 
     Print ("ExtFastMaBuffer[0]: ", ExtFastMaBuffer[0]);   
      
      if (prev >0 && curr <=0){
         ExtCVBuffer[i] = 1;
     }
      
      if (prev <0 && curr >=0){
         ExtCVBuffer[i] = 1;
      }
   }
  
//+------------------------------------------------------------------+
//|CV                    |
//+------------------------------------------------------------------+
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[])
  {
  
   if (rates_total < InpSlowMA - 1)
    return(0);

   CopyBuffer(ExtFastMaHandle, 0,0,2,ExtFastMaBuffer);
   CopyBuffer(ExtSlowMaHandle, 0,0,2,ExtSlowMaBuffer);
   
   int start;
   if (prev_calculated == 0)   // checking for the first call of the OnCalculate function
     {
      start = InpSlowMA - 1;  // starting index for calculating all of the bars 
     }
     else 
     {
       start = prev_calculated -1;
     }
         
//+---------------------CALCULATIONS--------------------------------------+//
   for(int i=start; i<rates_total && !IsStopped(); i++)
   {
      setCV(i); 
    }
      
   return(rates_total);
  }

 
 
Thank you. Exactly what I was searching for.