MT5 Chart Freezes after running for sometime (Multi Timeframes & Multi Symhols)

 

HI all!

I'm developing a "Helicopter View" of all symbols and all timeframes.

My approach is using sufficient handles for the built-in indicators.

Thus, creating & freeing the handles intensively as the following

#include <Telegram\Comment.mqh>
#include <Indicators\Trend.mqh>

CiBands BB20;
CComment MsgBoard;
CiMA MA50,LWMA5,LWMA10;

int OnInit()
  {
   MsgBoard.Create("CalPanel",200,0);
   MsgBoard.SetColor(clrDimGray,clrBlack,220);
   MsgBoard.SetFont("Lucida Console",12,false,1);
   MsgBoard.SetText(0,StringFormat("%s V%s",INAME,VERSION),clrWhite);
   MsgBoard.SetText(0,StringFormat("©%s V%s (Expires %s)",INAME,VERSION,TimeToString(ExpDate,TIME_DATE)),clrWhite);
   MsgBoard.SetText(1,"M1 │M5 │M15│M30│H1 │H4 │D1 │W1 │MN1│SYMBOL",clrMagenta);

   EventSetMillisecondTimer(1000);

   return(INIT_SUCCEEDED);
  }
  
void OnTimer(){
   for(int i=0;i<SymbolsTotal(true);i++){
      string data=FindZZL(SymbolName(i,true))+SymbolName(i,true);
      MsgBoard.SetText(i+2,data,StringFindCount(data,"▲")>StringFindCount(data,"▼")?clrLimeGreen:StringFindCount(data,"▼")>StringFindCount(data,"▲")?clrOrangeRed:clrGoldenrod);
   }
   MsgBoard.Show();
}
  
//+------------------------------------------------------------------+
//| 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(prev_calculated!=rates_total)
//   ChartRedraw();
   return(rates_total);
  }
//+------------------------------------------------------------------+
string FindZZL(string symbol){
  string result="",cell;
  double BarPrc;
  ENUM_TIMEFRAMES tf[]={PERIOD_M1,PERIOD_M5,PERIOD_M15,PERIOD_M30,PERIOD_H1,PERIOD_H4,PERIOD_D1,PERIOD_W1,PERIOD_MN1};
  for(int i=0;i<ArraySize(tf);i++){
     cell="   ";
     if(BB20.Create(symbol,tf[i],20,0,2,PRICE_CLOSE) &&
        MA50.Create(symbol,tf[i],50,0,MODE_EMA,PRICE_CLOSE)){
        MA50.Refresh();BB20.Refresh();
        cell="   ";
        if(BB20.Base(0)>MA50.Main(0)){ //MidBB > EMA50
           LWMA5.Create(symbol,tf[i],5,0,MODE_LWMA,PRICE_LOW);
           LWMA10.Create(symbol,tf[i],10,0,MODE_LWMA,PRICE_LOW);
           LWMA5.Refresh();LWMA10.Refresh();
           BarPrc=MathMin(iOpen(symbol,tf[i],0),iClose(symbol,tf[i],0));
           if(LWMA5.Main(0)>BB20.Base(0) && LWMA10.Main(0)>BB20.Base(0)){   
              if(BarPrc>LWMA5.Main(0) && BarPrc>LWMA10.Main(0)){  // ZZL Up!
                 cell=" ▲ ";
                 BarPrc=MathMax(iOpen(symbol,tf[i],0),iClose(symbol,tf[i],0));
                 if(BarPrc>BB20.Upper(0)) cell="▲M▲";
              }   
              if(iClose(symbol,tf[i],0)>MathMin(LWMA5.Main(0),LWMA10.Main(0)) && 
                 iClose(symbol,tf[i],0)<MathMax(LWMA5.Main(0),LWMA10.Main(0))) cell="▲R▲";  
           }   
        }   
        if(BB20.Base(0)<MA50.Main(0)){ //MidBB < EMA50
           LWMA5.Create(symbol,tf[i],5,0,MODE_LWMA,PRICE_HIGH);
           LWMA10.Create(symbol,tf[i],10,0,MODE_LWMA,PRICE_HIGH);
           LWMA5.Refresh();LWMA10.Refresh();
           BarPrc=MathMax(iOpen(symbol,tf[i],0),iClose(symbol,tf[i],0));
           if(LWMA5.Main(0)<BB20.Base(0) && LWMA10.Main(0)<BB20.Base(0)){
              if(BarPrc<LWMA5.Main(0) && BarPrc<LWMA10.Main(0)){  // ZZL Dn!
                 cell=" ▼ ";  // ZZL Up!
                 BarPrc=MathMin(iOpen(symbol,tf[i],0),iClose(symbol,tf[i],0));
                 if(BarPrc<BB20.Lower(0)) cell="▼M▼";
              }   
              if(iClose(symbol,tf[i],0)>MathMin(LWMA5.Main(0),LWMA10.Main(0)) && 
                 iClose(symbol,tf[i],0)<MathMax(LWMA5.Main(0),LWMA10.Main(0))) cell="▼R▼"; 
           }   
        }   
        LWMA5.FullRelease();
        LWMA10.FullRelease();
     }      
     BB20.FullRelease();
     MA50.FullRelease();
     result+=cell+"│";
  }   
  return(result);
}

It runs well for sometime and freezes.

What the code does is simply loop through the symbols and timeframes, creating the needed handles for the indicators and then freeing all up in each iteration using FullRelease() method.

I even put ChartRedraw() int OnCalculate() but it didn't help.

Changing timeframe would of course reinitialize everything and it starts working temporarily and freezes after a period of time....

I made sure that every 'Create()' ends with 'FullRelease()' to avoid memory leaks... Am I even allowed to do that in MQL5????

Any helps is highly appreciated.

Brgds,


Herman

 
Herman Makmur:

HI all!

I'm developing a "Helicopter View" of all symbols and all timeframes.

My approach is using sufficient handles for the built-in indicators.

Thus, creating & freeing the handles intensively as the following

It runs well for sometime and freezes.

What the code does is simply loop through the symbols and timeframes, creating the needed handles for the indicators and then freeing all up in each iteration using FullRelease() method.

I even put ChartRedraw() int OnCalculate() but it didn't help.

Changing timeframe would of course reinitialize everything and it starts working temporarily and freezes after a period of time....

I made sure that every 'Create()' ends with 'FullRelease()' to avoid memory leaks... Am I even allowed to do that in MQL5????

Any helps is highly appreciated.

Brgds,


Herman

Every second you are opening and closing a lot of indicators, it won’t keep up it will just overflow the events queue 
You need to rethink your approach 
 
Paul Anscombe #:
Every second you are opening and closing a lot of indicators, it won’t keep up it will just overflow the events queue 
You need to rethink your approach 

Hi Paul, thank you for your fast reply....

So, what I need to do is just adding the delay time? To 5 seconds maybe? Do you think it would help?

I'll try to set the timer to 5000 msecs and see what happens....

It's just mind boggling to me how slow MQL5 is then... 1 second is generally an eternity in the computer world....


Thanks & regards


Herman

 
Herman Makmur #:

Hi Paul, thank you for your fast reply....

So, what I need to do is just adding the delay time? To 5 seconds maybe? Do you thing it would help?

I'll try to set the timer to 5000 msecs and see what happens....

It's just mind boggling to me how slow MQL5 is then... 1 second is generally an eternity in the computer world....


Thanks & regards


Herman

A normal approach is to establish your indicators in oninit and close them in ondeinit . Not to keep opening and closing it causes a lot of work 
 
Paul Anscombe #:
A normal approach is to establish your indicators in oninit and close them in ondeinit . Not to keep opening and closing it causes a lot of work 

That's what I was planning to do next....


But imagine I have 30 symbols & 10 timeframes....

I would end up with 300 handles to work with.... And it would eat up the memory don't you think?

That's not a good approach from memory saving's point of view....

But I thank you for telling me how slow the processes are in MQL5...


Appreciate it....


Rgds,


Herman

 
Herman Makmur #:

That's what I was planning to do next....


But imagine I have 30 symbols & 10 timeframes....

I would end up with 300 handles to work with.... And it would eat up the memory don't you think?

That's not a good approach from memory saving's point of view....

But I thank you for telling me how slow the processes are in MQL5...


Appreciate it....


Rgds,


Herman

That is the correct way of doing it and the most efficient  I use 28 symbols and 24 indicators/timeframes
 
what is the Create function doing? have you abstracted some things like CopyBuffer?
In my view, MT5 may not have been built to handle multi timeframe. There are plenty of topics about the 4806 error, and it is difficult to get a multitimeframe indicator to run smoothly in mql5. But strangely enough, not very difficult in mql4.
In mql5 I believe it's trying to calculate all bars needed on the multiple periods simultaneously, but it tries to do it too quickly. The result is that an error can occur very easily and then it will stop trying to calculate the bars on one of the timeframes. 
You can see that with conditions using BarsCalculated on the handles. My only solution was to limit the total amount of bars it can process on every timeframe
 
Paul Anscombe #:
That is the correct way of doing it and the most efficient  I use 28 symbols and 24 indicators/timeframes

Owh OK....

I set the timer to 3000 ms, and I think it works fine now....

Thanks again for your help....

Herman

 
Conor Mcnamara #:
what is the Create function doing? have you abstracted some things like CopyBuffer?
In my view, MT5 may not have been built to handle multi timeframe. There are plenty of topics about the 4806 error, and it is difficult to get a multitimeframe indicator to run smoothly in mql5. But strangely enough, not very difficult in mql4.
In mql5 I believe it's trying to calculate all bars needed on the multiple periods simultaneously, but it tries to do it too quickly. The result is that an error can occur very easily and then it will stop trying to calculate the bars on one of the timeframes. 
You can see that with conditions using BarsCalculated on the handles. My only solution was to limit the total amount of bars it can process on every timeframe

The Create() method is the wrapped version of IndicatorCreate() function derived from CIndicator class found in standard Trend.mqh MT5 include files.

In fact, I only need the BB, EMA & OHLC values of the current (index 0) bar...