Download MetaTrader 5

"Tree optimization error" error

To add comments, please log in or register
Mihai Ionescu
1406
Mihai Ionescu  

I don't know if it's my indicator or not, but the behavior is really strange, so I presume it's a bug.

Here is the meaningful code:

string TButtonLName, TButtonHName;

TButtonLName = menuObjectName("T","ButtonL");
TButtonHName = menuObjectName("T","ButtonH");

//...

string menuObjectName(string menu, string obj) {
   return (StringFormat("%s%s%s%s",objectPrefix,"Menu",menu,obj));
}

 If I comment one of the two calls of menuObjectName, compiler says ok, everything works.

The error appear only when both calls are uncommented, valid, visible to the compiler. So I cannot have both.

This is the first time I encounter such error in my entire programming life.

Is there something wrong with what I wrote, or is it a bug?

Get in touch with developers using Service Desk!
  • www.mql5.com
We therefore attach great importance to all user reports about issues in our programs and try to answer each one of them.
Mihai Ionescu
1406
Mihai Ionescu  

After playing a little with the function menuObjectName, I saw that the following code works:

string menuObjectName(string menu, string obj) {
   string s = StringFormat("%s%s%s%s",objectPrefix,"Menu",menu,obj);
   printf("s=%s",s);
   return (s);
}

So the problem seems to be with return statement in the original version. 

StringFormat returns string, right? Why return cannot handle it? 

Mihai Ionescu
1406
Mihai Ionescu  

Today, I bumped again in "tree optimization error", in another situation.

This time involves arrays of strings. Maybe I did something wrong, and I guess it maybe from allocation of memory. I don't really know.

Here is the significant code (and again, if I let both hideObjects and showObjects to be visible to mql5 compiler, compiler would not compile, it will throw this error:

void hideObjects(string objNames, string separator = ","){
   string s[];
   splitS(objNames,separator,s);
   int slen = ArraySize(s);
   for (int i=0; i< slen; i++) {
      ObjectSetInteger(0,s[i],OBJPROP_TIMEFRAMES,OBJ_NO_PERIODS);   
   } 
}
   
void showObjects(string objNames, string separator = ","){
   string s[];
   splitS(objNames,separator,s);
   int slen = ArraySize(s);
   for (int i=0; i< slen; i++) {
      ObjectSetInteger(0,s[i],OBJPROP_TIMEFRAMES,OBJ_ALL_PERIODS);   
   } 
}


void splitS(string s, string separator, string& res[]){
   int currentChar = 0, commaIndex = 0; 
   string a[100];
   int slen = StringLen(s);
   bool canIRun = (slen >= 1);
   int reslen = 0;
   while (canIRun) { 
         commaIndex = StringFind(s, separator, currentChar);
         if (commaIndex == -1) {//daca nam gasit ;
            a[reslen] = StringSubstr(s,currentChar);
            canIRun = false;
         }      
         else {
            a[reslen] = StringSubstr(s,currentChar,commaIndex-currentChar);
            currentChar = commaIndex+1;
         }   
         reslen++;
   }
   ArrayResize(res,reslen);
   ArrayCopy(res,a,0,0,reslen);
}

 

PS.

Reasoning that compiler wants only one function to be present (either hideObjects or showObjects),

I combined hideObjects and showObjects into a single function, 

and it worked.

So who has issues? Me, mql5 compiler or both?:) 

 

Mihai Ionescu
1406
Mihai Ionescu  

And, third time, even weirder behavior:)

In the following code, the only way compiler compiles is when every line (inside if statement) is commented out:)

         if (menu == "C") {
            //string prefix=objectPrefix + "MenuC";
            //int x=4,y=20,xs=30,wx=35,wy=20;
            //color labelColor = Green, editColor=Blue;
            /*
            switch (action) {
               case 1:  //case MENU_SHOW
                  objectsVisibility(true,StringFormat("%s,%s,%s,%s",prefix+"LabelPrice",prefix+"LabelDate",prefix+"EditPrice",prefix+"EditDate"));
                  break;
               case 2: //case MENU_HIDE:
                  objectsVisibility(false,StringFormat("%s,%s,%s,%s",prefix+"LabelPrice",prefix+"LabelDate",prefix+"EditPrice",prefix+"EditDate"));
                  break;
               case 0: //case MENU_INIT:
                  createLabel(prefix + "LabelPrice",x,y+2*wy,xs,"Price",OBJ_NO_PERIODS);
                  createLabel(prefix + "LabelDate",x,y+wy,xs,"Date",OBJ_NO_PERIODS);
                  
                  createEdit(prefix + "EditPrice",x+wx,y,xs,"-1",OBJ_NO_PERIODS);
                  tempString = TimeToString(TimeCurrent(),TIME_DATE|TIME_MINUTES);
                  createEdit(prefix + "EditDate",x+wx,y+wy,2*xs,tempString,OBJ_NO_PERIODS);
                break;  
               case 4: //case MENU_DELETE:
                  break;
            }
            */
         } //end Menu C 

 Even if I uncomment one simple declaration, "tree optimization error" is thrown.

Now is there something about last update? or I'm doing something wrong?

because until now, it worked. 

 

Alexey Da
Moderator
6655
Alexey Da  

Which build number of client terminal do you use? It would be better if you sent us full source code.

I tried to reproduce in 296 build but I couldn't. 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Client Terminal Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Environment State / Client Terminal Properties - Documentation on MQL5
Mihai Ionescu
1406
Mihai Ionescu  
alexvd:

Which build number of client terminal do you use? It would be better if you sent us full source code.

I tried to reproduce in 296 build but I couldn't. 

About page says 294 (17 jul). 

First I want to update to 296.

But shouldn't it update itself automatically through live? It usually asks for update.

Anyway, it seems to be related to declaring variables inside decision statements like if and switch

I've created a thread about variables and switch statement here: https://www.mql5.com/en/forum/1039 

Tree optimization error disappeared when I declared variables outside if statements. Like this:

string prefix;
int x=4,y=20,xs=30,wx=35,wy=20;
color labelColor = Green, editColor=Blue;
            
   while (canIContinue) { 
         commaIndex1 = StringFind(menus, ",", currentChar1);
         if (commaIndex1 == -1) {//daca nam gasit ;
            menu = StringSubstr(menus,currentChar1);
            canIContinue = false;
         }      
         else {
            menu = StringSubstr(menus,currentChar1,commaIndex1-currentChar1);
            currentChar1 = commaIndex1+1;
         }
         
         if (menu == "L") {
            //string prefix=objectPrefix + "MenuL";
            //int x=4,y=20,xs=30,wx=35,wy=20;
            //color labelColor = Green, editColor=Blue;
            prefix=objectPrefix + "MenuL";

            //...code...
            
         } //end menu L
         
         if (menu == "T") {
            //string prefix=objectPrefix + "MenuT";
            //int x=4,y=20,xs=30,wx=35,wy=20;
            //color labelColor = Green, editColor=Blue;
            prefix=objectPrefix + "MenuT";

            //...code...
            
         } //end Menu T      
         
         if (menu == "C") {
            //string prefix=objectPrefix + "MenuC";
            //int x=4,y=20,xs=30,wx=35,wy=20;
            //color labelColor = Green, editColor=Blue;
            prefix=objectPrefix + "MenuC";
            
            //...code...
            
         } //end Menu C   
         
   }//end While
compilation error when declaring a variable inside a switch statement
  • www.mql5.com
Creating a new custom indicator and having this switch statement:.
Alexey Da
Moderator
6655
Alexey Da  
ifmihai:

About page says 294 (17 jul). 

First I want to update to 296.

But shouldn't it update itself automatically through live? It usually asks for update.

Anyway, it seems to be related to declaring variables inside decision statements like if and switch

I've created a thread about variables and switch statement here: https://www.mql5.com/en/forum/1039 

Tree optimization error disappeared when I declared variables outside if statements. Like this:

 

 

 

1. Why don't you send full source code to us? You can send it using private message.

2. Please give us more info about your problem with live update. Which server do you connect? Could you send logs of your terminal?

Mihai Ionescu
1406
Mihai Ionescu  
alexvd:

1. Why don't you send full source code to us? You can send it using private message.

2. Please give us more info about your problem with live update. Which server do you connect? Could you send logs of your terminal?

1. The code it's kind of private. I don't know if I can give it away. At least not yet until it's decided.

2. It seems there's no pb with live update. You said you tried build 296. But 296 wasn't available from what I know. With 298 terminal announced me immediately the update was done. So update system is ok if 296 was never released.

I don't know what log file to send, if it's still required. 

Alexey Da
Moderator
6655
Alexey Da  
ifmihai:

1. The code it's kind of private. I don't know if I can give it away. At least not yet until it's decided.

2. It seems there's no pb with live update. You said you tried build 296. But 296 wasn't available from what I know. With 298 terminal announced me immediately the update was done. So update system is ok if 296 was never released.

I don't know what log file to send, if it's still required. 

If your terminal was updated successfully, you wouldn't need to send any log.

Are you able to reproduce error "Tree optimization error" in 298 build?

Mihai Ionescu
1406
Mihai Ionescu  
alexvd:

If your terminal was updated successfully, you wouldn't need to send any log.

Are you able to reproduce error "Tree optimization error" in 298 build?

Yes, I was able to reproduce it. All needed was to declare again those variables inside if statements.

I got one new error playing around deleting here and there in search of cause: OnCalculate event is too complex. Or something like this.  

I didn't find, yet, the cause, but it's around declaring variables, if statements, switch statements.

Inside switch's or if's  I use variables from some custom headers. Maybe this can be the cause too.

I'm still searching. 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Chart Constants / Types of Chart Events
  • www.mql5.com
Standard Constants, Enumerations and Structures / Chart Constants / Types of Chart Events - Documentation on MQL5
Mihai Ionescu
1406
Mihai Ionescu  
alexvd:

If your terminal was updated successfully, you wouldn't need to send any log.

Are you able to reproduce error "Tree optimization error" in 298 build?

Today I've declared all variables outside switch statement,just before it. Tree optimization error disappeared again. I did this for safety.

Basically this was (still is, but without variables declared inside case statements) the basic principal structure of code:

void OnChartEvent(const int id,         // Event identifier  
                  const long& lparam,   // Event parameter of long type
                  const double& dparam, // Event parameter of double type
                  const string& sparam  // Event parameter of string type
                  ) { 
   if(id==(int)CHARTEVENT_KEYDOWN) {
      commandWasPressed = true;
      string menuToShow = "";
      switch (lparam) {
         case KEY_ESC:////daca eram in vreun mod, vreo unealta, se anihileaza contoarele
            lastCommandKey = 0;
            consecutiveClick = 0;
            //commandWasPressed = false;
            break;
         case KEY_MBUTTON:
            crosshair = true;
            break;   
         case KEY_H: case KEY_L:
            menuToShow = "L";
            break;
         case KEY_T:
            menuToShow = "T";
            if (lastMenuShowed != menuToShow) consecutiveClick = 0;
            consecutiveClick = 0;
            break;    
         case KEY_R:
            if (lastMenuShowed != menuToShow) consecutiveClick = 0;
            break;
         case KEY_D: 
            menuToShow = lastMenuShowed;
            break;
         case KEY_I:  case KEY_S:
            //nothing, decat ca sa fie aware ca sa apasat o comanda (commandWasPressed = true adica)
            break;      
         default:
            commandWasPressed = false;      
            Print("Some meaningless key has been pressed:",lparam);
      }
      if (commandWasPressed) {
         manageMenuForm(MENU_HIDE,"ALL");//hide all
         manageMenuForm(MENU_SHOW,menuToShow);
         lastCommandKey = lparam;
         lastMenuShowed = menuToShow;
      }   
   }
   //--- the key has been pressed
      
   //declarare variabile anti switch case tree optimization error
   double barHL2,bar1HL2,bar2HL2,yPrice;
   double tDiff,pDiff,f1,f2,TRatio,Tp[],Ta[],price,pricePmn,LPrice,LRatioX,LRatioY,;
   int f,index,Tplen,Talen,direction,p,v,h,seconds,LSquaresX,LSquaresY,LPos1,LPos2,angle;
   MqlRates bar,r,candle,candle0;
   
   if ( id==(int)CHARTEVENT_CLICK && StringFind(sparam,objectPrefix+"Menu",0) < 0 || id==(int)CHARTEVENT_OBJECT_CLICK) {
      //Print("The coordinates of the mouse click on the chart are: x=",lparam,"  y=",dparam);
      
      string tempString = "";
      //printf("lastPressedKey = %d",lastCommandKey) ;          
      switch (lastCommandKey) {
         case KEY_S: //KEY_S nu merge
            Comment("Key S");   
            break;
         case KEY_I: { // I module = information about candlestick; 
             //variables declarations       
             //code     
            } 
            break;
         case KEY_R: { //R module = range info
             //variables declarations    
            //code
            }
            break;
         case KEY_T: { 
             //variables declarations  
            //code
            }
            break;      
         case KEY_H: { 
            //variables declarations  
            //code
            }
            break;      
         case KEY_L: { 
            //variables declarations  
            //code
            }
            break;      
         case KEY_D: { 
            //variables declarations  
            //code
            }
            break;      
         default: {
            //variables declarations  
            //code
         }   
            
      } //endSwitch
   }//end click
   
//--- the mouse has been clicked on the graphic object
   if(id==(int)CHARTEVENT_OBJECT_CLICK) {
      //code
     }

//--- the object has been created
   if(id==(int)CHARTEVENT_OBJECT_CREATE)     {
      //Print("The object with name",sparam,"has been created");
      //if (StringSubstr(sparam,0)
   }
//--- the object has been moved or its anchor point coordinates has been changed
//   if(id==(int)CHARTEVENT_OBJECT_DRAG)     {
//      Print("The anchor point coordinates of the object with name",sparam,"has been changed");
//   }
//--- the text in the Edit of object has been changed
//   if(id==(int)CHARTEVENT_OBJECT_ENDEDIT)     {
//      Print("The text in the Edit field of the object with name",sparam,"has been changed");
//   }
   
   ChartRedraw();
}

 

 

 

To add comments, please log in or register