MT4 freezes after installing custom indicators

 
Recently I made a support and resistance indicator, each support and resistance line has information about how many times these lines have been touched by a candle in the selected time frame, and will update automatically if the latest candle touches a support or resistance line.

Don't be half-hearted, so that I know how strong the support and resistance lines are, I entered historical data from 1993 to the latest in 2024, the result is that each line has its own value. some have been touched 390 times by the candle, some have even been touched 1000 times.

At first I really felt happy, because this way I could know  how strong support and resistance were by number.

but the problem I am facing is, when I installed the indicator, in the first one to 2 minutes, MT4 appeared to be running normally, but after that MT4 froze and I had to close it because it didn't want to respond. even though when compiling the code in the meta editor, there were no errors or warnings when compiling.

does anyone know what the cause is? and can you give me a solution?
 
Abdul Barrud Darovi:
Recently I made a support and resistance indicator, each support and resistance line has information about how many times these lines have been touched by a candle in the selected time frame, and will update automatically if the latest candle touches a support or resistance line.

Don't be half-hearted, so that I know how strong the support and resistance lines are, I entered historical data from 1993 to the latest in 2024, the result is that each line has its own value. some have been touched 390 times by the candle, some have even been touched 1000 times.

At first I really felt happy, because this way I could know  how strong support and resistance were by number.

but the problem I am facing is, when I installed the indicator, in the first one to 2 minutes, MT4 appeared to be running normally, but after that MT4 froze and I had to close it because it didn't want to respond. even though when compiling the code in the meta editor, there were no errors or warnings when compiling.

does anyone know what the cause is? and can you give me a solution?
You will get little help if you don’t post the code because that makes it a guessing game.

My guess is you are probably recalculating unnecessarily or putting to much history and objects on the chart.  Maybe both🤷
 
Paul Anscombe #:
You will get little help if you don’t post the code because that makes it a guessing game.

My guess is you are probably recalculating unnecessarily or putting to much history and objects on the chart.  Maybe both🤷

Oh right, here is the code

//+------------------------------------------------------------------+

//|                                                SNR V3.mq4|

//|                        Copyright 2024, MetaQuotes Software Corp.  |

//|                                       http://www.metaquotes.net   |

//+------------------------------------------------------------------+

#property strict

#property indicator_chart_window


input double SetPrice = 1236.69;

input double Percentage = 0.75;

input int NumberOfLines = 168;

input color LineColor = clrYellow;

input ENUM_LINE_STYLE LineStyle = STYLE_DOT;

input int LineWidth = 1;


// Array to hold the horizontal line handles

int lineHandles[];

// Array to hold the count of candle touches for each line

int lineTouchCounts[];

int currentTimeFrame = 0;


//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

    ArrayResize(lineHandles, NumberOfLines * 2);  // Twice the number of lines to account for above and below

    ArrayResize(lineTouchCounts, NumberOfLines * 2);

    currentTimeFrame = Period();

    DrawLines();

    EventSetTimer(60); // Set timer to update touch counts every 60 seconds

    return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator deinitialization function                       |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

    EventKillTimer();

  }

//+------------------------------------------------------------------+

//| 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 (currentTimeFrame != Period())

    {

        currentTimeFrame = Period();

        DrawLines();

    }

    UpdateTouchCounts(high, low, rates_total);

    return(rates_total);

  }

//+------------------------------------------------------------------+

//| Function to draw the support and resistance lines                |

//+------------------------------------------------------------------+

void DrawLines()

{

    DeleteLines();

    

    // Determine default values based on the symbol

    double defaultPrice;

    double defaultPercentage;

    

    if (Symbol() == "XAUUSD") { defaultPrice = 1236.69; defaultPercentage = 0.75; }

    else if (Symbol() == "GBPJPY") { defaultPrice = 184.005; defaultPercentage = 0.52; }

    else if (Symbol() == "EURJPY") { defaultPrice = 132.075; defaultPercentage = 0.48; }

    else if (Symbol() == "USDJPY") { defaultPrice = 113.765; defaultPercentage = 0.43; }

    else if (Symbol() == "GBPUSD") { defaultPrice = 1.57730; defaultPercentage = 0.43; }

    else if (Symbol() == "EURUSD") { defaultPrice = 1.27870; defaultPercentage = 0.42; }

    else if (Symbol() == "CHFJPY") { defaultPrice = 122.755; defaultPercentage = 0.45; }

    else if (Symbol() == "EURNZD") { defaultPrice = 1.98450; defaultPercentage = 0.48; }

    else if (Symbol() == "AUDJPY") { defaultPrice = 81.520; defaultPercentage = 0.52; }

    else if (Symbol() == "NZDJPY") { defaultPrice = 71.055; defaultPercentage = 0.59; }

    else if (Symbol() == "CADJPY") { defaultPrice = 96.990; defaultPercentage = 0.56; }

    else if (Symbol() == "AUDUSD") { defaultPrice = 0.82950; defaultPercentage = 0.53; }

    else if (Symbol() == "USDCHF") { defaultPrice = 1.06515; defaultPercentage = 0.43; }

    else if (Symbol() == "USDCAD") { defaultPrice = 1.24170; defaultPercentage = 0.40; }

    else if (Symbol() == "NZDUSD") { defaultPrice = 0.68710; defaultPercentage = 0.57; }

    else if (Symbol() == "NASDAQ") { defaultPrice = 8955.65; defaultPercentage = 0.89; }

    else { defaultPrice = SetPrice; defaultPercentage = Percentage; }


    double price = defaultPrice;

    double percentage = defaultPercentage / 100.0;

    double upperPrice = price;

    double lowerPrice = price;

    

    for (int i = 0; i < NumberOfLines; i++)

    {

        upperPrice = upperPrice * (1.0 + percentage);

        lowerPrice = lowerPrice * (1.0 - percentage);

        string upperLineName = "SRLineUpper_" + IntegerToString(i);

        string lowerLineName = "SRLineLower_" + IntegerToString(i);

        

        lineHandles[i] = ObjectCreate(0, upperLineName, OBJ_HLINE, 0, 0, upperPrice);

        ObjectSetInteger(0, upperLineName, OBJPROP_COLOR, LineColor);

        ObjectSetInteger(0, upperLineName, OBJPROP_STYLE, LineStyle);

        ObjectSetInteger(0, upperLineName, OBJPROP_WIDTH, LineWidth);

        

        lineHandles[i + NumberOfLines] = ObjectCreate(0, lowerLineName, OBJ_HLINE, 0, 0, lowerPrice);

        ObjectSetInteger(0, lowerLineName, OBJPROP_COLOR, LineColor);

        ObjectSetInteger(0, lowerLineName, OBJPROP_STYLE, LineStyle);

        ObjectSetInteger(0, lowerLineName, OBJPROP_WIDTH, LineWidth);

    }

}

//+------------------------------------------------------------------+

//| Function to delete all lines                                     |

//+------------------------------------------------------------------+

void DeleteLines()

{

    for (int i = 0; i < ArraySize(lineHandles); i++)

    {

        if (lineHandles[i] != 0)

        {

            ObjectDelete(0, "SRLineUpper_" + IntegerToString(i));

            ObjectDelete(0, "SRLineLower_" + IntegerToString(i - NumberOfLines));

            lineHandles[i] = 0;

        }

    }

}

//+------------------------------------------------------------------+

//| Function to update the touch counts for each line                |

//+------------------------------------------------------------------+

void UpdateTouchCounts(const double &high[], const double &low[], const int rates_total)

{

    ArrayFill(lineTouchCounts, 0, ArraySize(lineTouchCounts), 0);

    for (int i = 0; i < NumberOfLines; i++)

    {

        double upperPrice = ObjectGetDouble(0, "SRLineUpper_" + IntegerToString(i), OBJPROP_PRICE1);

        double lowerPrice = ObjectGetDouble(0, "SRLineLower_" + IntegerToString(i), OBJPROP_PRICE1);

        

        for (int j = 0; j < rates_total; j++)

        {

            if (high[j] >= upperPrice && low[j] <= upperPrice)

            {

                lineTouchCounts[i]++;

            }

            if (high[j] >= lowerPrice && low[j] <= lowerPrice)

            {

                lineTouchCounts[i + NumberOfLines]++;

            }

        }

        

        string upperLineName = "SRLineUpper_" + IntegerToString(i);

        string lowerLineName = "SRLineLower_" + IntegerToString(i);

        

        ObjectSetString(0, upperLineName, OBJPROP_TEXT, "Touched " + IntegerToString(lineTouchCounts[i]) + " times");

        ObjectSetString(0, lowerLineName, OBJPROP_TEXT, "Touched " + IntegerToString(lineTouchCounts[i + NumberOfLines]) + " times");

    }

}

//+------------------------------------------------------------------+
MetaQuotes — разработчик торговых платформ для брокеров, банков, бирж и хедж-фондов
MetaQuotes — разработчик торговых платформ для брокеров, банков, бирж и хедж-фондов
  • www.metaquotes.net
Торговая платформа MetaTrader 5 - это бесплатный инструмент трейдера, позволяющий торговать на форексе и фондовых биржах
 
Paul Anscombe #:
You will get little help if you don’t post the code because that makes it a guessing game.

My guess is you are probably recalculating unnecessarily or putting to much history and objects on the chart.  Maybe both🤷

i think its need to calculate all the candle sir, thats why i put so much history, what do you think? i already post the code, is that any flaw?  

 
Abdul Barrud Darovi #:

Oh right, here is the code

When you post code please use the CODE button (Alt-S)!

Use the CODE button

 
Sergey Golubev #:

When you post code please use the CODE button (Alt-S)!

sorry, i dont know, ill do it next time when i have trouble

 
Abdul Barrud Darovi #:

i think its need to calculate all the candle sir, thats why i put so much history, what do you think? i already post the code, is that any flaw?  

You are calculating every candle of chart every single tick.  So you have quite a lot of code to change
Start by looking at examples of how to use the rates total and prev total in an indicator to only calculate the latest bar
 
Paul Anscombe #:
You are calculating every candle of chart every single tick.  So you have quite a lot of code to change
Start by looking at examples of how to use the rates total and prev total in an indicator to only calculate the latest bar

simply put, to much data can cause freze in application?

can you tell me how much at least i should use? 20 years history, should be fine right?

 
Abdul Barrud Darovi #:

simply put, to much data can cause freze in application?

can you tell me how much at least i should use? 20 years history, should be fine right?

its not about that, you are recalculating EVERYTHING every tick....  you should calculate everything once, then only calculate the delta since last calculation. and for what you are doing probably only need to calculate at start of a new bar as well.  Do this and your processing would come down 99%

 
Paul Anscombe #:
you should calculate everything once, then only calculate the delta since last calculation. and for what you are doing probably only need to calculate at start of a new bar as well.  Do this and your processing would come down 99%
"you should calculate everything once, then only calculate the delta since last calculation. and for what you are doing probably only need to calculate at start of a new bar as well.  Do this and your processing would come down 99%"


so thats the key, thank mr paul, ill try...