Update / Refresh Problems in long loops

 
I have the fallowing code for a customized indicator. Basically, I found that it detects the end of micro-trends, before the price corrects itself. However, the only drawback is that since I have several loops, and I placed the indicator on a profile that has many charts on it, it takes a while to compute. The MetaTrader4 platform tends to freeze until the loop has completed. Now consider this, suppose that I mistakenly had an infinate loop. The MetaTrader4 platform will practically choke to death. Is there a way we can refresh / update the MetaTrader4 platform during the long iteration - counting of loops?

The RefreshRates function only refreshes incomming rates, not the entire charts on the software. Or does it?

Please take a look at my code:

//+------------------------------------------------------------------+
//| My Custom Indicator.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"


#property indicator_separate_window
#property indicator_color1 Blue
#property indicator_minimum 0

double buffer[];

extern int Length = 3;


//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,buffer);
SetIndexDrawBegin(0,0);



//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//----

for (int i = 0; i < Bars-1; i++) buffer[i] = EMPTY_VALUE;


for (i = 0; i < Bars - 1; i++)
{
int price_idx = i + 1;
int prev_price_idx = 0;
for (int j = 0; j < Length; j++)
{
if (j == 0)
{
// Find the first of highest highs
while (High[price_idx] > High[i] && price_idx < Bars - 1)
{
price_idx++;
}
}
else
{
// Find the lowest lows
while (Low[price_idx] > Low[prev_price_idx] && price_idx < Bars - 1)
{
price_idx++;
}
}
prev_price_idx = price_idx;
price_idx++;
}
buffer[i] = price_idx - i;
}

// At this point, when the indicator is displayed on the chart, there may be a segment that could have a
// range of 2000 - thereby hiding all the other values that could exist below (say) 100. We need a mechanism
// to shrink the results to scale so that we could view all values within the window.

double high = 0;
for (i = 0; i < Bars - 1; i++)
{
if (buffer[i] > high) high = buffer[i];
}

for (i = 0; i < Bars - 1; i++)
{
while (buffer[i] > 100) buffer[i] /= 2;
}


//----
return(0);
}
//+------------------------------------------------------------------+
 
don't know if this will help. goto tools-options-charts and change max number of bars in chart to 500.