EA Randomly Pausing / Throttling Calculations

 

Hi there,


New to MQL5, have coded using MQL4 in the past.


I'm trying to create an EA that detects a specific pattern based off high's and lows. And then draw lines on the chart to visualize these patterns. The code goes through lots of iterations to calculate the end result, and when it gets there the result is correct. However, the speed is extremely slow.


I am aware that I am forcing it to do thousands of calculations, but I noticed that the EA seems to "pause" randomly. This is noticeable when watching the "comment" in the top left. It runs for a while processing the calcs quicky, and then pauses for about 20-30 seconds. And then continues.


I am trying to understand why this happens? Does MT5 "throttle" the EA if too many calculations are requested? Or what else could be causing this? Appreciate the help! Code pasted here:


//+------------------------------------------------------------------+
//|                                            Detect Pattern EA.mq5 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

bool done=false;

input int barstocount=500;//Bars to Count
int col=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   Comment("");
   done=false;
   ObjectsDeleteAll(0,"V");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
if(done)
      return;
   uint thestart=GetTickCount();
   done=true;
   MqlRates rates[];
   int bars=barstocount;//iBars(Symbol(),0)-1;

   ArraySetAsSeries(rates,true);

   int count=CopyRates(Symbol(),0,0,bars+1,rates);
   Alert("Please wait for analysis to complete...");
   for(int i=count-2;i>0;i--)
     {
      Comment("i = "+i+"\ni2 = 0\ni3 = 0\ni4 = 0\ni5 = 0\ni6 = 0");
      if(rates[i+1].high<rates[i].high && rates[i-1].high<rates[i].high)//Find First Peak H1
        {
         for(int i2=i-1;i2>0;i2--)
           {
            Comment("i = "+i+"\ni2 = "+i2+"\ni3 = 0\ni4 = 0\ni5 = 0\ni6 = 0");
            if(rates[i2].high>rates[i].high)//Cancel if it breaks First Peak High H1
               break;
            if(rates[i2+1].low>rates[i2].low && rates[i2-1].low>rates[i2].low && ((rates[i].high-rates[i2].low)>500*_Point) && rates[i2].low<=iLow(Symbol(),0,iLowest(Symbol(),0,MODE_LOW,(i-1)-i2,i2)))//Find L1 (Must be 500 points away)
              {
               for(int i3=i2-1;i3>0;i3--)
                 {
                  Comment("i = "+i+"\ni2 = "+i2+"\ni3 = "+i3+"\ni4 = 0\ni5 = 0\ni6 = 0");
                  if(rates[i3].low<rates[i2].low ||
                     rates[i3].high>rates[i].high)//Cancel if it breaks L1 Low OR H1 High
                     break;
                  if(rates[i3+1].high<rates[i3].high && rates[i3-1].high<rates[i3].high)//Find Next High H2
                    {
                     double mindistance=rates[i2].low+((rates[i].high-rates[i2].low)/2);
                     if(rates[i3].high>=mindistance && rates[i3].high>=iHigh(Symbol(),0,iHighest(Symbol(),0,MODE_HIGH,(i2-1)-i3,i3)))//Check H2 against 50% retrace
                       {
                        for(int i4=i3-1;i4>0;i4--)
                          {
                           Comment("i = "+i+"\ni2 = "+i2+"\ni3 = "+i3+"\ni4 = "+i4+"\ni5 = 0\ni6 = 0");
                           if(rates[i4].low<rates[i2].low ||
                              rates[i4].high>rates[i3].high)//Cancel if it breaks L1 Low OR H2 High
                              break;
                           if(rates[i4+1].low>rates[i4].low && rates[i4-1].low>rates[i4].low)//Next Low L2
                             {
                              double mindistance=rates[i3].high-((rates[i3].high-rates[i2].low)/2);
                              if(rates[i4].low<=mindistance && rates[i4].low<=iLow(Symbol(),0,iLowest(Symbol(),0,MODE_LOW,(i3-1)-i4,i4)))//Check L2 against 50% retrace
                                {
                                 for(int i5=i4-1;i5>0;i5--)
                                   {
                                    Comment("i = "+i+"\ni2 = "+i2+"\ni3 = "+i3+"\ni4 = "+i4+"\ni5 = "+i5+"0\ni6 = 0");
                                    if(rates[i5].low<rates[i4].low ||
                                       rates[i5].high>rates[i3].high)//Cancel if it breaks L2 Low OR H2 High
                                       break;
                                    if(rates[i5+1].high<rates[i5].high && rates[i5-1].high<rates[i5].high)//Next High H3
                                      {
                                       double mindistance=rates[i4].low+((rates[i3].high-rates[i4].low)/2);
                                       if(rates[i5].high>=mindistance && rates[i5].high>=iHigh(Symbol(),0,iHighest(Symbol(),0,MODE_HIGH,(i4-1)-i5,i5)))//Check H2 against 50% retrace
                                         {
                                          for(int i6=i5-1;i6>0;i6--)
                                            {
                                             Comment("i = "+i+"\ni2 = "+i2+"\ni3 = "+i3+"\ni4 = "+i4+"\ni5 = "+i5+"0\ni6 = "+i6);
                                             if(rates[i6].low<rates[i4].low ||
                                                rates[i6].high>rates[i5].high)//Cancel if it breaks L2 Low OR H2 High
                                                break;
                                             if(rates[i6+1].low>rates[i6].low && rates[i6-1].low>rates[i6].low)//Next Low L3
                                               {
                                                double mindistance=rates[i5].high-((rates[i5].high-rates[i4].low)/2);
                                                if(rates[i6].low<=mindistance && rates[i6].low<=iLow(Symbol(),0,iLowest(Symbol(),0,MODE_LOW,(i5-1)-i6,i6)))//Check H2 against 50% retrace
                                                  {
                                                   color clr;
                                                   switch(col)
                                                     {
                                                      case 0: clr=clrGreen;break;
                                                      case 1: clr=clrYellow;break;
                                                      case 2: clr=clrPink;break;
                                                      case 3: clr=clrCyan;break;
                                                      case 4: clr=clrRed;break;
                                                      case 5: clr=clrOrange;break;
                                                      case 6: clr=clrAzure;break;
                                                      case 7: clr=clrAntiqueWhite;break;
                                                      case 8: clr=clrCoral;break;
                                                      case 9: clr=clrLimeGreen;break;
                                                     }
                                                   col++;
                                                   if(col>9)
                                                      col=0;
                                                   //Print(i+" "+i2+" "+i3+" "+i4+" "+i5+" "+i6);
                                                   //Print(clr);
                                                   ObjectCreate(0,"V1"+iTime(Symbol(),0,i)+" "+iTime(Symbol(),0,i2),OBJ_TREND,0,iTime(Symbol(),0,i),rates[i].high,iTime(Symbol(),0,i2),rates[i2].low);
                                                   ObjectSetInteger(0,"V1"+iTime(Symbol(),0,i)+" "+iTime(Symbol(),0,i2),OBJPROP_COLOR,clr);
                                                   ObjectCreate(0,"V2"+iTime(Symbol(),0,i2)+" "+iTime(Symbol(),0,i3),OBJ_TREND,0,iTime(Symbol(),0,i2),rates[i2].low,iTime(Symbol(),0,i3),rates[i3].high);
                                                   ObjectSetInteger(0,"V2"+iTime(Symbol(),0,i2)+" "+iTime(Symbol(),0,i3),OBJPROP_COLOR,clr);
                                                   ObjectCreate(0,"V3"+iTime(Symbol(),0,i3)+" "+iTime(Symbol(),0,i4),OBJ_TREND,0,iTime(Symbol(),0,i3),rates[i3].high,iTime(Symbol(),0,i4),rates[i4].low);
                                                   ObjectSetInteger(0,"V3"+iTime(Symbol(),0,i3)+" "+iTime(Symbol(),0,i4),OBJPROP_COLOR,clr);
                                                   ObjectCreate(0,"V4"+iTime(Symbol(),0,i4)+" "+iTime(Symbol(),0,i5),OBJ_TREND,0,iTime(Symbol(),0,i4),rates[i4].low,iTime(Symbol(),0,i5),rates[i5].high);
                                                   ObjectSetInteger(0,"V4"+iTime(Symbol(),0,i4)+" "+iTime(Symbol(),0,i5),OBJPROP_COLOR,clr);
                                                   ObjectCreate(0,"V5"+iTime(Symbol(),0,i5)+" "+iTime(Symbol(),0,i6),OBJ_TREND,0,iTime(Symbol(),0,i5),rates[i5].high,iTime(Symbol(),0,i6),rates[i6].low);
                                                   ObjectSetInteger(0,"V5"+iTime(Symbol(),0,i5)+" "+iTime(Symbol(),0,i6),OBJPROP_COLOR,clr);
                                                  }
                                               }
                                            }
                                         }
                                      }
                                   }
                                }
                             }
                          }

                       }
                    }
                 }
              }
           }
        }
     }
   uint theend=GetTickCount();
   uint dur=(theend-thestart)/1000;
   Print("Action completed in "+dur+"s");
   Alert("Analysis Complete!");
  }
 
Richard Read:

Hi there,


New to MQL5, have coded using MQL4 in the past.


I'm trying to create an EA that detects a specific pattern based off high's and lows. And then draw lines on the chart to visualize these patterns. The code goes through lots of iterations to calculate the end result, and when it gets there the result is correct. However, the speed is extremely slow.


I am aware that I am forcing it to do thousands of calculations, but I noticed that the EA seems to "pause" randomly. This is noticeable when watching the "comment" in the top left. It runs for a while processing the calcs quicky, and then pauses for about 20-30 seconds. And then continues.


I am trying to understand why this happens? Does MT5 "throttle" the EA if too many calculations are requested? Or what else could be causing this? Appreciate the help! Code pasted here:


Create / modify objects outside the loop.

 
Enrique Dangeroux:

Create / modify objects outside the loop.

Thank you! 100x Better.

 
Richard Read:

Thank you! 100x Better.

If you need to optimize your code further use the code profiler (Debug -> Start Profiling...).

Reason: