News Indicator

Giorgio
123

Hi guys, I'm creating an indicator to implement news volatility, the following code should permit the backtest of It, but It doesn't work (NewsEventUSD Isn't fill with values); any ideas?

//+------------------------------------------------------------------+
//|                                             NEWS_VOLATILITY2.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window

input string GeneralSettings="-----------    General Settings    -----------";  //-- General Settings --
input datetime StartPeriod=D'01.01.2020';          //Start Economic Calendar  (backtesting)
input datetime EndPeriod=D'01.01.2023';            //End Economic Calendar

MqlCalendarValue NewsEventUSD[];
MqlCalendarValue NewsEventEUR[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   if(CalendarValueHistory(NewsEventUSD,StartPeriod,EndPeriod,NULL,"US")==false)
     {
      printf("ERROR CalendarValueHistory: ", GetLastError());
      return(INIT_FAILED);
     }
   
   if(CalendarValueHistory(NewsEventEUR,StartPeriod,EndPeriod,NULL,"EU")==false)
     {
      printf("ERROR CalendarValueHistory: ", GetLastError());
      return(INIT_FAILED);
     }
   
   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[])
  {
   for (int i=0; i<ArraySize(NewsEventUSD); i++)
      if (NewsEventUSD[i].time==TimeCurrent())
        {
         Print("ciao");
         break;
        }
   Print(ArraySize(NewsEventUSD));
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+