Download MetaTrader 5

Freezing code

To add comments, please log in or register
mima
375
mima  

Here is code of indicator that freez MT5 Aand computer...Tried on two different computers with different OS...same thing freez everything...As soon as  I remove indicator from the chart everything is back to normal again.

Indicator was converted from MT4 to MT5...MT4 version works perfectly good.... Must be something in code that cause freezing...

There are no other indicators that cause such problem.

Can someone help to resolve problem please?

 

#property copyright "xxxxxxxxxxxxx"
#property link      "xxxxxxxxxxxxx"
#property version   "1.00"
#property indicator_chart_window

#property indicator_buffers 3
#property indicator_plots 3
#property indicator_color1 clrRoyalBlue
#property indicator_type1 DRAW_ARROW
#property indicator_color2 clrGold
#property indicator_type2 DRAW_ARROW
#property indicator_color3 clrNONE
#property indicator_type3 DRAW_ARROW

input int     MAPeriod = 100;
input int     LookBack = 20;
input bool    AlertOnBarClimax = true;
input int     Offset = 5;

int NumberOfBars;
datetime alertonce = 0;
datetime alertonceTL = 0;
double D[], S[];
bool redVol;
bool whiteVol;
double iOffset;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
{
   iOffset = Offset * _Point;

   SetIndexBuffer(0, D);
   PlotIndexSetInteger(0, PLOT_ARROW, 233);
   PlotIndexSetString(0, PLOT_LABEL, "Demand");
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0);
   
   SetIndexBuffer(1, S);
   PlotIndexSetInteger(1, PLOT_ARROW, 234);
   PlotIndexSetString(1, PLOT_LABEL, "Supply");
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0);
   
   
   ArraySetAsSeries(D, true);
   ArraySetAsSeries(S, true);
  
}
  
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 &Volume[],
                const long &real_volume[],
                const int &spread[])
{
   ArraySetAsSeries(High, true);
   ArraySetAsSeries(Low, true);
   ArraySetAsSeries(Close, true);
   ArraySetAsSeries(Volume, true);
   ArraySetAsSeries(Time, true);
   
   //BetVol vars
   double Range, Value2, Value3, HiValue2, HiValue3, LoValue3, tempv2, tempv3, tempv;
   long VolLowest;
   int limit;
   int counted_bars = prev_calculated;
   //CDRW Vars
   double spd1, spd2;
   long v1, v2;
   
   //---- last counted bar will be recounted
   if (counted_bars > 0) counted_bars--;
   NumberOfBars = rates_total - counted_bars;
   limit = NumberOfBars; //Bars-counted_bars;   
   
   if (limit - rates_total < MathMax(MathMax(21, LookBack + 1), MAPeriod + 1)) limit = rates_total - MathMax(MathMax(21, LookBack + 1), MAPeriod + 1);
   
   for (int i = limit; i >= 0; i--)  
   {
      //---------- BEGIN Bet Vol Code   ---------------\\
      Value2 = 0; Value3 = 0; HiValue2 = 0; HiValue3 = 0; LoValue3 = 99999999; tempv2 = 0; tempv3 = 0; tempv = 0;
      VolLowest = Volume[ArrayMinimum(Volume, i + 1, 20)];
      
      Range = (High[i + 1] - Low[i + 1]);
      
      Value2 = Volume[i + 1] * Range;
      
      if (Range != 0) Value3 = Volume[i + 1] / Range;
      
      redVol = false;  
      whiteVol = false;              
      
      for (int n = i + 1; n < i + MAPeriod; n++)
      {
         tempv = Volume[n + 1] + tempv; 
      } 

      for (int n = i + 1; n < i + LookBack; n++)
      {
         tempv2 = Volume[n] * ((High[n] - Low[n])); 
         if (tempv2 >= HiValue2) HiValue2 = tempv2;
         if (Volume[n] * ((High[n] - Low[n])) != 0)
         {           
            tempv3 = Volume[n] / ((High[n] - Low[n]));
            if (tempv3 > HiValue3) HiValue3 = tempv3; 
            if (tempv3 < LoValue3) LoValue3 = tempv3;
         } 
      }
                                   
      if ((Value2 == HiValue2) && (Close[i + 1] > (High[i + 1] + Low[i +1 ]) / 2 ))
      {
         redVol = true;        
      }

      if ((Value2 == HiValue2) && (Close[i+1] <= (High[i + 1] + Low[i + 1]) / 2 ))
      {
         whiteVol = true;                    
      } 
      //---------- END Bet Vol Code   ---------------\\    
                  
      //---------- BEGIN CDRW Code   ---------------\\
      spd1 = High[i + 1] - Low[i + 1];
      spd2 = High[i + 2] - Low[i + 2];
      
      D[i] = 0; S[i] = 0;
      
      v1 = Volume[i + 1];
      v2 = Volume[i + 2];  
      if (spd1 > spd2)
      {
         if (v1 < v2)
         {
            if (High[i + 1] - Close[i + 1] < Close[i + 1] - Low[i + 1])
            {
               S[i + 1] = 0;
            }
            if (High[i + 1] - Close[i + 1] > Close[i + 1] - Low[i + 1])
            {
               D[i + 1] = 0;
            }
         }           
         if (v1 > v2)
         {
            if (High[i + 1] - Close[i + 1] < Close[i + 1] - Low[i + 1])
            {
               if (redVol)
               {
                  D[i + 1] = Low[i + 1] - iOffset;     
               }
            }
            if (High[i + 1] - Close[i + 1] > Close[i + 1] - Low[i + 1])
            {
               if (whiteVol)
               {
                  S[i + 1] = High[i + 1] + iOffset;
               } //end if white vol
           } //if impo bar   
         } //end if v1 v2      
      }
      //---------- END CDRW Code   ---------------\\
   }  //end for
   
   if (AlertOnBarClimax)
   {
      if ((redVol) && (D[1] != 0) && (alertonce != Time[0]))
         Alert("Demand !!! ", Symbol(), "  ", "M", Period());
      if ((whiteVol) && (S[1] != 0) && (alertonce != Time[0]))
         Alert("Supply !!!! ", Symbol(), "  ", "M", Period()); 
      alertonce = Time[0];     
   }  
  
   return(rates_total);
}
Alain Verleyen
Moderator
32410
Alain Verleyen  
mima:

Here is code of indicator that freez MT5 Aand computer...Tried on two different computers with different OS...same thing freez everything...As soon as  I remove indicator from the chart everything is back to normal again.

Indicator was converted from MT4 to MT5...MT4 version works perfectly good.... Must be something in code that cause freezing...

There are no other indicators that cause such problem.

Can someone help to resolve problem please?

 

It works for me. Which symbol/period and parameters are you using ?
mima
375
mima  

I put indicator on any pair...several of them (10+), same as  i use it for MT4 version...Sorry for repeating: (no other MT5 indicator make such problem and soon as I remove indicator from chart everything is back to normal, MT4 version is perfect no problem), unbelievable ...time period should not be issue at all...

I thought that code is looking  entire chart instead last two bars (testing:spd1, spd2, v1,v2)...so it overload CPU....I do not know.

This is indicator that I ask someone in forum to code (https://www.mql5.com/en/forum/23508) ...Now I have it and  i cannot use it...so sorry.

Angevoyageur thank you for reviewing code in my post.

Volume_Spread_Change
Volume_Spread_Change
  • www.mql5.com
Supposed to be up arrow only on red bar ( then gives alert "buy"), down arrow only on white bar ( then gives alert "sell"). - - Category: technical indicators
Alain Verleyen
Moderator
32410
Alain Verleyen  
mima:

I put indicator on any pair...several of them (10+), same as  i use it for MT4 version...Sorry for repeating: (no other MT5 indicator make such problem and soon as I remove indicator from chart everything is back to normal, MT4 version is perfect no problem), unbelievable ...time period should not be issue at all...

I thought that code is looking  entire chart instead last two bars (testing:spd1, spd2, v1,v2)...so it overload CPU....I do not know.

This is indicator that I ask someone in forum to code (https://www.mql5.com/en/forum/23508) ...Now I have it and  i cannot use it...so sorry.

Angevoyageur thank you for reviewing code in my post.

Can you please answer my questions is you need help ?

This code is working well, though it's not optimal. Try to place it only one time on a chart and see what happens.

Stanislav Korotky
20333
Stanislav Korotky  
If the PC is slow or other apps is running concurrently, it may take some time for the indicator to show up, up to a minute or more. How long did you observe the freezing?
mima
375
mima  
angevoyageur:

Can you please answer my questions is you need help ?

This code is working well, though it's not optimal. Try to place it only one time on a chart and see what happens.

I use same template for MT4 and MT5 ( meaning similar indicators just different versions) . ..Under same conditions (same computer, same platforms, same operating system)  there is no problem using MT4...and there is no problem for other MT5 indicators...Just this one.

I Use 16 charts in  one profile, time frame M30 ..or M5...It doesn't freeze on one chart but sometimes that particular indicator ( no other indicators on same chart) disappear and then appear again after a while...

It is used with this attached indicator.... Attached indicator doesn't make problems or any other MT5 indicator on chart...So if you want to try ...

 Hope I answered your question. 

mima
375
mima  
marketeer:
If the PC is slow or other apps is running concurrently, it may take some time for the indicator to show up, up to a minute or more. How long did you observe the freezing?

PC is good ...as I mentioned before ...It makes problem only with this particular inicator and shouldnt be...It just draw two arros checking last two bars on spread and volume...Other MT5 indicators doesn't make any problem...on same computer..MT5 indicators doesn't make any problem MT4 indicators doesn't make any problem

 Just this MT5 indicator.  I hope I answered to your comment.

Stanislav Korotky
20333
Stanislav Korotky  
mima:

PC is good ...as I mentioned before ...It makes problem only with this particular inicator and shouldnt be...It just draw two arros checking last two bars on spread and volume...Other MT5 indicators doesn't make any problem...on same computer..MT5 indicators doesn't make any problem MT4 indicators doesn't make any problem

 Just this MT5 indicator.  I hope I answered to your comment.

Not in a full degree. I asked how long did you try to wait for the indicator calculation completion. So?

You have been told that your code does work for angevoyageur, so the problem can be with, say, a number of bars you have in your chart. If it's ~100000, it can take a while to finish calculations.

mima
375
mima  
marketeer:

Not in a full degree. I asked how long did you try to wait for the indicator calculation completion. So?

You have been told that your code does work for angevoyageur, so the problem can be with, say, a number of bars you have in your chart. If it's ~100000, it can take a while to finish calculations.

It should test only last two bars (not entire chart) for spread and volume(spd1>spd2 and v1<v2) draw 1 arow and give alert. MT4 do it instantly ...I do not have to wait for anything...anyway I do not interrupt operation...I know very well how same indicator work on MT4...

Hope this answer your comment. 

Stanislav Korotky
20333
Stanislav Korotky  
mima:

It should test only last two bars (not entire chart) for spread and volume(spd1>spd2 and v1<v2) draw 1 arow and give alert. MT4 do it instantly ...I do not have to wait for anything...anyway I do not interrupt operation...I know very well how same indicator work on MT4...

Hope this answer your comment. 

It should calculate all bars (not 2 latest ones) when you place the indicator on a chart first time. You did not answer my question.
Alain Verleyen
Moderator
32410
Alain Verleyen  
marketeer:
It should calculate all bars (not 2 latest ones) when you place the indicator on a chart first time. You did not answer my question.
Seems an habit.
12
To add comments, please log in or register