Dealing with multiple objects on MQL5 efficiently

 
Hey, Please, i coded and indicator and everything is okey and fine but it creates multiple objects and in the code i try to loop through them and it works fine. But if i load with other indicatos that themselves also creates multiple objects, the number of objects increase on the chart and when trying to modify them with by looping through them with the standard ObjectSetInteger() etc functions which are asynchronous, the objects then take time to refresh despite using ChartRedraw so for this issue where we have a large number of objects, what would be the best approach to modify them? Been looking for this on the forum but didn't found anything. Maybe I am wording it wrongly. Example of code of me looping throught the objects to modify them.
for(int i = ObjectsTotal(0) - 1; i >= 0; i--){
      
         string nameObj = ObjectName(0, i);
         if(StringFind(nameObj, Symbol()) != -1 && (StringFind(nameObj, prefix+" BUYZONE") != -1 || StringFind(nameObj, prefix+" SELLZONE") != -1)){
            
            ObjectSetInteger(0, nameObj, OBJPROP_TIMEFRAMES, OBJ_ALL_PERIODS);ChartRedraw(0);
         
         }
         if(StringFind(nameObj, Symbol()) == -1 && (StringFind(nameObj, prefix+" BUYZONE") != -1 || StringFind(nameObj, prefix+" SELLZONE") != -1)){
            
            ObjectSetInteger(0, nameObj, OBJPROP_TIMEFRAMES, OBJ_NO_PERIODS);ChartRedraw(0);
         
         }
         
      }
 
Objects and efficiency? This is a contradiction in terms, as all objects have to be redrawn with each new bar. Use structures and make a button that, when you click on it, draws the last ones as objects on the chart - and then deletes them again.
 
Carl Schreiber #:
Objects and efficiency? This is a contradiction in terms, as all objects have to be redrawn with each new bar. Use structures and make a button that, when you click on it, draws the last ones as objects on the chart - and then deletes them again.
I am not sure i get you. The thing is on mt4, looping through objects and modifying them have immediate effects on chart with no lag but on MT5 seems it’s not the case and i read that hen using the standard ObjectGet… ObjectSet… functions on MQL5 which are asynchronous, the effects of these functions will have a lag so what I am asking is how to deal with multiple objects so that it refreshes as fast as with MQL4
 

It depends on how your indicator works. For example, does your indicator create a fixed amount of objects? If so, you don't need to loop through all of them. Just create as many string variables as needed and store their names.

If your indicator creates an unknown number of objects, you can create a array and store every object name/info in that array. Then you won't need to loop through every object in the chart, but just through every object created by that indicator, which will certainly save some time.

Also, if you need to redraw, you can call ChartRedraw after every loop, not inside the loop.

If you are using if statements and two of them contradict each other, use an if else. This avoids the checking of the second if. For example:


int i = 0;

if (i == 0)
{
        Print("i equals 0"); // if "i == 0", then the next if statement is not checked, due to the else keyword. this is good, because i cannot be 1 anymore
}
else if (i == 1)
{
        Print("i is not equal 0");
}

// ---------------------------------------------- // 

if (i == 0)
{
        Print("i equals 0"); // even if "i == 0", the next if statement will be checked, but it'll never be true. this makes your code, in theory, slower.
}

if (i == 1)
{
        Print("i is not equal 0");
}


There're other stuff that may make your code faster, but it would depend on how it is structured.

 
Bryan Djoufack Nguessong:
Hey, Please, i coded and indicator and everything is okey and fine but it creates multiple objects and in the code i try to loop through them and it works fine. But if i load with other indicatos that themselves also creates multiple objects, the number of objects increase on the chart and when trying to modify them with by looping through them with the standard ObjectSetInteger() etc functions which are asynchronous, the objects then take time to refresh despite using ChartRedraw so for this issue where we have a large number of objects, what would be the best approach to modify them? Been looking for this on the forum but didn't found anything. Maybe I am wording it wrongly. Example of code of me looping throught the objects to modify them.

Do NOT place a ChartRedraw() in a loop.

Add it ONCE, after the loop.

 
Emanuel Cavalcante Amorim Filho #:

It depends on how your indicator works. For example, does your indicator create a fixed amount of objects? If so, you don't need to loop through all of them. Just create as many string variables as needed and store their names.

If your indicator creates an unknown number of objects, you can create a array and store every object name/info in that array. Then you won't need to loop through every object in the chart, but just through every object created by that indicator, which will certainly save some time.

Also, if you need to redraw, you can call ChartRedraw after every loop, not inside the loop.

If you are using if statements and two of them contradict each other, use an if else. This avoids the checking of the second if. For example:



There're other stuff that may make your code faster, but it would depend on how it is structured.

thanks for your reply, added a kind of solution using this info and it's a bit faster

 
Alain Verleyen #:

Do NOT place a ChartRedraw() in a loop.

Add it ONCE, after the loop.

thanks for this reply, recoded following this and there's improvement