Multi time frame indicator issue MT5.

 
Hi every one,
I have created a multi time frame indicator in which it gives signal on Lower time frame based on RSI values on higher time frame.
Let's say the higher time frame is H1 and I load it on M5.

When I put the indicator on M5 time frame, if H1 chart is open and RSI is opened on H1, it perfectly shows the signal on M5, but if H1 chart is not open or if RSI is not open on the H1 chart, it cannot show signal on M5. looks like it cannot get RSI values from H1 chart if it is not open.
I've search forum, I could not find any relevant subject,
can anyone help me? How can I have signal on M5 without opening chart on H1?


The code is simple.
//+------------------------------------------------------------------+
//|                                                 RSI HTF Test.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2

input group                "Settings"
input int                   BarCount=1000;               //Previous Bar
input group                "Higher Time Frame"
input string        IndHT1="<<=== RSI HTF ===>>";
input ENUM_TIMEFRAMES       RSIHTF=PERIOD_H1 ;            //RSI HTF
input int                   RSIPeriodHTF=14 ;             //RSI Period
input ENUM_APPLIED_PRICE    RSIPriceHTF=PRICE_CLOSE;      //RSI Applied Price
input double                OverboughtHTF=70;             //Overbought Limit
input double                OversoldHTF=30;               //Oversold Limit 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int RSI_HTF_Handle=0;
//--- indicator buffers
double    BuyArrowHTF[];
double    SellArrowHTF[];
datetime  TimeHTF[];
int     y=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,BuyArrowHTF,INDICATOR_DATA);
   SetIndexBuffer(1,SellArrowHTF,INDICATOR_DATA);
//---
   ArraySetAsSeries(BuyArrowHTF,true);    
   ArraySetAsSeries(SellArrowHTF,true);   
   ArraySetAsSeries(TimeHTF,true); 
//---
//--- draw begin settings
   PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_ARROW);  
   PlotIndexSetInteger(0,PLOT_ARROW,233); 
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,clrBlue); 
   PlotIndexSetInteger(1,PLOT_DRAW_TYPE,DRAW_ARROW);  
   PlotIndexSetInteger(1,PLOT_ARROW,234); 
   PlotIndexSetInteger(1,PLOT_LINE_COLOR,clrRed);    
//---
   string short_name="RSI HTF Signal";//StringFormat("MDX");//(%d,%d,%d)",InpKPeriod,InpDPeriod,InpSlowing);
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
   PlotIndexSetString(0,PLOT_LABEL,"Buy"); 
   PlotIndexSetString(1,PLOT_LABEL,"Sell");     
//--- indicator buffers mapping
    RSI_HTF_Handle=iRSI(Symbol(),RSIHTF,RSIPeriodHTF,RSIPriceHTF);
   if(RSI_HTF_Handle<0)
     {
      Print("The creation of RSI has failed:",INVALID_HANDLE,"Error",GetLastError());
      return(-1);
     }   
//---
   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[])
  {
//---
//---
   ArraySetAsSeries(open,true);  
   ArraySetAsSeries(high,true);  
   ArraySetAsSeries(low,true);  
   ArraySetAsSeries(close,true);
   ArraySetAsSeries(time,true);
//---
   int Limit=rates_total-prev_calculated;
//---
  if(Limit>1)  //Previous Bar Loading
   {
   int timeCopy=CopyTime(_Symbol,RSIHTF,0,iBars(Symbol(),RSIHTF),TimeHTF);   
   if(timeCopy==-1)  {Print("Cannot copy HTF, Try Again, Error: ",GetLastError()); return 0;}   
//---
   for(int i=0;i<ArraySize(TimeHTF);i++)
     {
      if(time[BarCount]>TimeHTF[i]) {y=i; break; }       
     }
//---
          
   for(int i=BarCount;i>=0;i--)
     {
   //  Print("i: ",i," y: ",y," RSI_Cross_HTF(y): ",RSI_Cross_HTF(y)," time[i]: ",time[i]," TimeHTF[y]: ",TimeHTF[y]);
//---Count HTF
    if(y-1>=0) 
     if(time[i]>=TimeHTF[y-1]) y--;

//---HTF Signal
    if(RSI_Cross_HTF(y)=="Buy")
      {
      BuyArrowHTF[i]=low[i]-100*Point();
      SellArrowHTF[i]=EMPTY_VALUE;
      }   
    else if(RSI_Cross_HTF(y)=="Sell")  
      {
      SellArrowHTF[i]=high[i]+100*Point();
      BuyArrowHTF[i]=EMPTY_VALUE;
      }
    else
      {
      BuyArrowHTF[i]=EMPTY_VALUE;
      SellArrowHTF[i]=EMPTY_VALUE;
      }          
 
     }  
   }     
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string RSI_Cross_HTF(int Candle)
  {
//---Update SMA Signal
   double RSI[];
   Copy_Buffer_AsSeries(RSI_HTF_Handle,0,Candle,3,true,RSI);     //RSI
   //Print("Candle: ",candle," RSI[1]: ",RSI[1]," RSI[2]: ",RSI[2]);
   if(RSI[1]<OversoldHTF && RSI[2]>=OversoldHTF)
      {    
     // Vertical_Line(0,"Vline"+(string)iTime(_Symbol,RSILTF,Candle),0,iTime(_Symbol,RSILTF,Candle),clrDodgerBlue);
      return "Buy";
      }
   else if(RSI[1]>OverboughtHTF && RSI[2]<=OverboughtHTF)
      {
     // Print("Candle: ",Candle," Time: ",iTime(_Symbol,RSIHTF,Candle)," RSI[1]: ",RSI[1]," RSI[2]: ",RSI[2]);
      //Vertical_Line(0,"Vline"+(string)iTime(_Symbol,RSILTF,Candle),0,iTime(_Symbol,RSILTF,Candle),clrRed);
      return "Sell";
      }

   return "No Signal";
  } 
//+------------------------------------------------------------------+
//| Filling Indicators Buffer                                        |
//+------------------------------------------------------------------+
bool Copy_Buffer_AsSeries(
   int handle,      // indicator's handle
   int buffer,       //buffer index
   int start,       // start index
   int number,      // number of elements to copy
   bool asSeries,   // if it's true, the elements will be indexed as series
   double &M[]      // target array
)
  {
//--- filling the array M with current values of the indicator
   if(CopyBuffer(handle,buffer,start,number,M)<=0)
      return(false);
//--- the elements will be indexed as follows:
//--- if asSeries=true, it will be indexed as timeseries
//--- if asSeries=false, it will be indexed as default
   ArraySetAsSeries(M,asSeries);
//---
   return(true);
  }      
     
 
if(BarsCalculated(rsi_htf_handle)<0) return 0;
Add this line at the start of onCalculate function.
 
return 0;
When it's week-end, this will not update your indicator as there is no new tick.
 
Yashar Seyyedin #:
Add this line at the start of onCalculate function.

There is more to debug here.

 
Yashar Seyyedin #:
Add this line at the start of onCalculate function.

Thank you very much.