1. You should never use iClose (or any iXXXX functions) on MT5, there are slow and provided only for compatibility with MT4.
2. Even using it, or any other functions that gets data. You need to ALWAYS check for error or success. It's essential when dealing with symbols/timeframes different from the chart.
Hi fellows, I wrote the following code, which I use to monitor 35 symbols by comand (each time I want to see the number this function gives for each symbol I press a designated key)
It happens that the first time I press the key to call this function MT5 takes a while (almost 1 minute) to return the value. The second time is much faster (2 seconds), third time is fast too... etc.
The problem is for when I change the timeframe, lets say I called the function in timeframe PERIOD_M1 and I got the results very fast (2 seconds). Now I change the timeframe to PERIOD_H1 and MT5 takes almost 60 seconds to complete the function for all 35 symols this time.
Does anyone know why that happens? And What I could do to reduce this time of almost 60 seconds ?
The performance issue you're experiencing, especially after changing the timeframe, could be due to multiple calls to iClose() within your loop. Each call to iClose() involves fetching historical price data, which can be relatively slow, especially if it needs to fetch data from a different timeframe, try to refrain from using iClose or any i functions in MQL5
To improve the performance of your function, you should aim to minimize the number of calls to iClose() within the loop. Here i wrote a small version of your function as example that reduces the number of calls to iClose() :
int contaPrecosDoMesmoLado(string simbolo, ENUM_TIMEFRAMES timeframe, int janela) { double closePrices[]; ArraySetAsSeries(closePrices, true); // Set the array as series int contadorPositivo = 0; int contadorNegativo = 0; int shift = 0; // Shift for current bar // Load historical close prices into array if (CopyClose(simbolo, timeframe, shift, janela, closePrices) != janela) { Print("Failed to copy close prices for ", simbolo); return 0; } double precoAtual = closePrices[0]; // Current close price // Loop through historical close prices for (int i = 1; i < janela; i++) { if (closePrices[i] <= precoAtual) contadorPositivo++; else contadorNegativo++; } if (contadorPositivo > contadorNegativo) return contadorPositivo; else if (contadorPositivo < contadorNegativo) return -contadorNegativo; return 0; }
Just check the syntax and functions
int contaPrecosDoMesmoLado(string simbolo, ENUM_TIMEFRAMES timeframe, int janela) { int contadorPositivo=0; int contadorNegativo=0; int contador=0; double precoAtual=iClose(simbolo,timeframe,0); int sinal=0; for(int i=0;i<janela;i++) { if(iClose(simbolo,timeframe,i)<=precoAtual) contadorPositivo++; if(iClose(simbolo,timeframe,i)>precoAtual) contadorNegativo++; } if(contadorPositivo>contadorNegativo) {contador=contadorPositivo; return contador;} if(contadorPositivo<contadorNegativo) {contador=-contadorNegativo; return contador;} return contador; }
Only a suggested code:
int contaPrecosDoMesmoLado(string simbolo, ENUM_TIMEFRAMES timeframe, int janela) { int contadorPositivo = 0; int contadorNegativo = 0; double precoAtual = Close[0]; for (int i = 0; i < janela; i++) { double preco = Close[i]; if (preco <= precoAtual) contadorPositivo++; else contadorNegativo++; } return (contadorPositivo > contadorNegativo) ? contadorPositivo : -contadorNegativo; }
Thank you gentlemen. But I make a provocation, the goal of this function is to give me a sentiment for any given symbol: if this function returns a high counter it means the current price is above most historic prices (bull sentiment) in the specifiec window and vice versa.
Other function that I made considers another parameter, just the index of the highest candle inside the specified window of candles (lets say 200 candles). If it returns a low index I interpret it as bull sentiment and vice versa.
double getSetupDeAlta(string symbol, ENUM_TIMEFRAMES timeframe, int janela) { double score=0; score=iHighest(symbol,timeframe,MODE_HIGH,janela); return score; }
How come this second function is never slow (always return in 3 seconds max, even for 35 symbols that I monitor) while the function tha uses iClose(symbol, timeframe, index) takes much more time? How does MT5 calculates and returns the index iHighest much faster? I ask this because in my opinion for MT5 to know which index is the index of the highest it has to compare all candles in the window (lets say 200 candles), right ?

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi fellows, I wrote the following code, which I use to monitor 35 symbols by comand (each time I want to see the number this function gives for each symbol I press a designated key)
It happens that the first time I press the key to call this function MT5 takes a while (almost 1 minute) to return the value. The second time is much faster (2 seconds), third time is fast too... etc.
The problem is for when I change the timeframe, lets say I called the function in timeframe PERIOD_M1 and I got the results very fast (2 seconds). Now I change the timeframe to PERIOD_H1 and MT5 takes almost 60 seconds to complete the function for all 35 symols this time.
Does anyone know why that happens? And What I could do to reduce this time of almost 60 seconds ?