Question about a multi-timeframe matrix

 

Hello guys,

I made this test-indicator which checks all of my selected pairs and timeframes for correct history data so I can work with them.

This indicator should only check the timeframe when it's necessary. So it doesn't make sense to check the H4 chart for a new bar when it's 7:40 or so.

Most of the time it works fine but sometimes the larger timeframes are checked although they should not be checked at this time.

It's a bit difficult to explain, so I attached my code. I hope someone can tell me where the bug is.

#property indicator_chart_window
#property indicator_plots 0


string symbols[]={"AUDCAD.a","AUDCHF.a","AUDJPY.a","AUDNZD.a","AUDUSD.a","CADCHF.a","CADJPY.a","CHFJPY.a",
                  "EURAUD.a","EURCAD.a","EURCHF.a","EURGBP.a","EURJPY.a","EURNZD.a","EURUSD.a",
                  "GBPAUD.a","GBPCAD.a","GBPCHF.a","GBPJPY.a","GBPNZD.a","GBPUSD.a",
                  "NZDCAD.a","NZDCHF.a","NZDJPY.a","NZDUSD.a",
                  "USDCAD.a","USDCHF.a","USDJPY.a","BTCUSD.a","XAUUSD.a","US500.a"};
                  
ENUM_TIMEFRAMES timeframes[]={PERIOD_H4,PERIOD_H1,PERIOD_M15,PERIOD_M5};
ENUM_TIMEFRAMES tf;

string sym,strTF;
datetime timeMatrix[31][4];
datetime time0=0;
string error="";
string commentMatrix;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
   if (_Period!=PERIOD_M5) Alert("Change timeframe to M5");
   ChartSetInteger(0,CHART_COLOR_CHART_LINE,(color)ChartGetInteger(0,CHART_COLOR_BACKGROUND));
   ChartSetInteger(0,CHART_MODE,CHART_LINE);
   ChartSetInteger(0,CHART_SHOW_TICKER,false);
   
//---
   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[])
  {
//---

   if (time0==iTime(_Symbol,PERIOD_CURRENT,0)) return(rates_total);
   time0=iTime(_Symbol,PERIOD_CURRENT,0);
   
   error="NO ERRORS";

   commentMatrix="                      "+
                  StringSubstr(EnumToString(timeframes[0]),7)+"          "+
                  StringSubstr(EnumToString(timeframes[1]),7)+"            "+
                  StringSubstr(EnumToString(timeframes[2]),7)+"           "+
                  StringSubstr(EnumToString(timeframes[3]),7);
                  
   for (int s=0;s<ArraySize(symbols);s++) {
      sym=symbols[s];
      commentMatrix=commentMatrix+"\n"+sym;
      for (int t=0;t<ArraySize(timeframes);t++) {
         tf=timeframes[t];
         strTF=StringSubstr(EnumToString(tf),7);
         if (TimeCurrent()<timeMatrix[s][t]+PeriodSeconds(tf)) {
            commentMatrix=commentMatrix+"    "+TimeToString(timeMatrix[s][t],TIME_MINUTES)+"    ";
            continue;
         }
         int bars=Bars(sym,tf);
         Print("Checking "+sym+" "+strTF);
         commentMatrix=commentMatrix+"    "+TimeToString(TimeCurrent(),TIME_MINUTES)+"    ";
         if (bars>0) {
            timeMatrix[s][t]=iTime(sym,tf,0);
         }
         else {
            timeMatrix[s][t]=0;
            error=sym+" "+strTF+" NO HISTORY";
         }        
      }
      ChartSetString(0,CHART_COMMENT,commentMatrix);
   }
   Print(error);
   if (error!="NO ERRORS") time0=iTime(_Symbol,PERIOD_CURRENT,1);
   return(rates_total);
  }
//+------------------------------------------------------------------+


'

 
Marbo:

Hello guys,

I made this test-indicator which checks all of my selected pairs and timeframes for correct history data so I can work with them.

This indicator should only check the timeframe when it's necessary. So it doesn't make sense to check the H4 chart for a new bar when it's 7:40 or so.

Most of the time it works fine but sometimes the larger timeframes are checked although they should not be checked at this time.

It's a bit difficult to explain, so I attached my code. I hope someone can tell me where the bug is.


'

Here you see what I mean. The M15 timeframe should not be checked at 19:50. The M15 chart should only be checked every 15 minutes and not in between.

This happens pretty rare but logically it should not even happen once. I don't see any logical error in my code...

Files:
Unbenannt.png  13 kb
 
            timeMatrix[s][t]=iTime(sym,tf,0);

On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 (2019)

On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
          Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 (2020)
          Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum (2019)
          Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
          Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 (2018)
          SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum (2019)

 
William Roeder #:

On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 (2019)

On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
          Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 (2020)
          Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum (2019)
          Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
          Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 (2018)
          SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum (2019)

Hello,

I thought using Bars(sym,tf) will do the job to synchronize. It works pretty good most of the time. How do you synchronize? In the past I used the CheckHistory function which is shown in one of your links but it didn’t work for me. Additionally the sleep function is used in the CheckHistory code which can’t be used in indicators. I always thought that Bars() is a good way to start the synchronization.

 
Marbo #: I thought using Bars(sym,tf) will do the job to synchronize. It works pretty good most of the time. How do you synchronize?
  1. Bars(…) probably does (I don't know MT5). But Bars or iTime, you don't check the results and wait for any required update.
  2. It's your money; is EA “working most of the time” good enough for you?
  3. I gave you links; why don't you read them?
Reason: