Huge mt4 bug?

 

I have the snippet below in a function. To detect if a closed bar lies completely above a trade for buy or below for sell. Problem is that the global variable gets set immediately the trade opens ignoring the set conditions/rules that must be present for it to be set. Is this a mt4 bug or am I missing something?

                string gvname1=(string)OrderTicket();
                           
               if(Low[1]>=OrderOpenPrice())
                 {

                
                 datetime setbuy=GlobalVariableSet(gvname1,Low[1]);
                  
                 }
 
Tonny Obare:

I have the snippet below in a function. To detect if a closed bar lies completely above a trade for buy or below for sell. Problem is that the global variable gets set immediately the trade opens ignoring the set conditions/rules that must be present for it to be set. Is this a mt4 bug or am I missing something?

It's not possible to answer from this little code snippet. If the GV is set, that means your condition is true, why you think it's false is not clear.

My bet is it's a bug in your code but I can't prove it without more information.

 
Alain Verleyen:

It's not possible to answer from this little code snippet. If the GV is set, that means your condition is true, why you think it's false is not clear.

My bet is it's a bug in your code but I can't prove it without more information.

GV gets set even when the open price is above previous bar low(Low[1]) for buy and vice versa for sell. Even adding more conditions to the if makes no change. I have provided the whole function below.

void GVsetter(int tsMagicNumber)
  {

   if(OrdersTotal()>0)
     {
      for(int tsi=1; tsi<=OrdersTotal(); tsi++)
        {          // Cycle searching in orders

         if(OrderSelect(tsi-1,SELECT_BY_POS)==true && OrderMagicNumber()==tsMagicNumber && OrderSymbol()==Symbol())
           { // If the next is available



            
            if(OrderType()==OP_BUY && OrderOpenTime()<Time[0])// for buy trades
              {
                           string gvname1=(string)OrderTicket();
                           
               if(Low[1]>=OrderOpenPrice() )
                 {


                 datetime setbuy=GlobalVariableSet(gvname1,Low[1]);
                }
                

              }

            if(OrderType()==OP_SELL && OrderOpenTime()<Time[0])// for sell trades
              {
            string gvname2=(string)OrderTicket();
               if(High[1]<=OrderOpenPrice() )
                 {


                  
                  datetime setsell=GlobalVariableSet(gvname2,High[1]);
                  
                 }

              }

            //--------Trailing stop-------------
           } // if available
        } // cycle
     }// orders total

  }
//------------------------------------------------------------------------------------
 
Tonny Obare:

GV gets set even when the open price is above previous bar low(Low[1]) for buy and vice versa for sell. Even adding more conditions to the if makes no change. I have provided the whole function below.

The code seems correct.

You need to debug it, add some print the check the values in the log :

datetime setbuy=GlobalVariableSet(gvname1,Low[1]);
printf("GV %s set, Low[1]=%f OOP=%f",gvname1,Low[1],OrderOpenPrice());
 
Alain Verleyen:

The code seems correct.

You need to debug it, add some print the check the values in the log :

If i set anything else to be executed in place of place of GlobalVariableSet() it works fine but with the GlobalVariableSet() the conditions get ignored.

 
Alain Verleyen:

The code seems correct.

You need to debug it, add some print the check the values in the log :

Just did that and nothing gets printed. Adding this comment below confirms the GV has been set because it shows some value as a comment immediately a trade opens even before the conditions are met.

Comment(GlobalVariableGet((string)OrderTicket()));
 

As in the image above at the top left corner you can see the value of the GV and as you can see visually the rules aren't met at all.

 
Tonny Obare:

As in the image above at the top left corner you can see the value of the GV and as you can see visually the rules aren't met at all.

Check your whole code, the snippet you posted can not lead to this behaviour.
 
Alain Verleyen:
Check your whole code, the snippet you posted can not lead to this behaviour.

Its the only place that GVs are set. Perhaps its mt4 bug? Because the Print, Printf, Alert and other functions don't work because the condition hasn't been met. Create a dummy ea with the function and see.

 
Tonny Obare:

Its the only place that GVs are set. Perhaps its mt4 bug? Because the Print, Printf, Alert and other functions don't work because the condition hasn't been met. Create a dummy ea with the function and see.

I have used GV in several projects and never had a problem. If you added the print just after or before the GlobalVariableSet() and nothing is printed, but the variable is set, that's just not possible, we are not living in a magical world. Things have a reason, search it. Unfortunately I can't help more.
 

Silly question, but did you press F3 in the terminal and check global variables before, during and after you run your EA?

Is it possible that another copy of the EA, running in another window, sets GV values?

Also, global variables are not destroyed after your EA exits. As it is stated in a documentation: "Global variables exist in the client terminal during 4 weeks since their last use, then they are automatically deleted."

If all above checks, than listen what Alain told you and debug your code.