Download MetaTrader 5

Why this always returns false?

To add comments, please log in or register
Morteza Khorasani
131
Morteza Khorasani  

Hello

I wrote a boolean function .
This code always returns false. I wrote some print order to check the program.

I have the value of the deviation and the function prints the calculated value of Parameter2. Most of times it meets the conditions (Parameter2<=-deviation)  or (Parameter2>=deviation)

but it still returns false.

The end section:

   Print("5Not Bigger Not Smaller");
               return(0);

works fine. When the two other conditions don't meet programs reach two this point and print order executes and then return 0.

but for the if and else if statement it doesn't work.

bool BoolFunction()
{//BoolFunction
Parameter1=Function1();
double Average=Function2();
double Parameter2=Parameter1-Average;

Print("Value of Parameter2:",DoubleToStr(Parameter2,4),"deviation:",deviation);  //deviation set as a constant in input 

  if(Parameter2<=-deviation) {
                       if(FirstOrderType==0){
                                 return(1); 
                                 Print("1Parameter2<=-deviation.. and returned 1");
                                            }
                       else if(FirstOrderType==1)
                                            {
                                 return(0); 
                                 Print("2Parameter2<=-deviation..and returned 0");
                                            }
                             }

   else if(Parameter2>=deviation) 
                             {
                    if(FirstOrderType==1)
                                            {
                                return(1); 
                                Print("3Parameter2>=deviation..returned 1");
                                            }
                      else if(FirstOrderType==0)
                                           {
                                return(0); 
                                Print("4Parameter2>=deviation..returned 0");
                                            }
                              }

               Print("5Not Bigger Not Smaller");
               return(0);
}//BoolFunction
Carl Schreiber
Moderator
7813
Carl Schreiber  
Have you gone through your function with the debugger watching the relevant values?
Morteza Khorasani
131
Morteza Khorasani  
Carl Schreiber:
Have you gone through your function with the debugger watching the relevant values?

Not really...

I thought that the print orders works fine to find the problem.

whroeder1
16764
whroeder1  
  1. You have no idea what your values are. Use the debugger or print out your variables, and find out why.

  2. if(FirstOrderType==1)
    else if(FirstOrderType==0)
    Don't hard code constants, use the proper enumerations.
               Order Properties - Trade Constants - Standard Constants, Enumerations and Structures - MQL4 Reference

  3.                                 return(1); 
                                    return(0); 
    
    Your function returns a bool, not an int/double. Return true/false.

  4. An if/else means you do A or B and then C
    if(condition){
       A
    } else {
       B
    }
    C
    When the last part of A is a return/break/continue there is no C. Simplify your code.
    if(condition){
       return;
    }
    B
    C

Morteza Khorasani
131
Morteza Khorasani  
whroeder1:
  1. You have no idea what your values are. Use the debugger or print out your variables, and find out why.

  2. Don't hard code constants, use the proper enumerations.
               Order Properties - Trade Constants - Standard Constants, Enumerations and Structures - MQL4 Reference

  3. Your function returns a bool, not an int/double. Return true/false.

  4. An if/else means you do A or B and then C
    When the last part of A is a return/break/continue there is no C. Simplify your code.


I have the idea..

i have the parameter A. Imagine it can be any real number.

If it is bigger than 2, the function check another condition say condition2. if it is true the function returns true and if the condition2 is false the function returns false.

if it is smaller than -2 the function also check for condition2. here if it is true the function returns false and if the condition2 is false the function returns true.

for values between -2 and 2 it returns false.

constant double d;
bool function()
             {//beginning of function
double a=it is generate here inside the function;
   if(a<-d){
             if(condition2)  return(true);
             if(!condition2) return(false);
            {
    if(a>d){
             if(condition2)  return(false);
             if(!condition2) return(true);
            {    

return(false);



             }//End of function
whroeder1
16764
whroeder1  
if( condition2) return(true);
if(!condition2) return(false);
Simplify your code. Increase Order after stoploss - MQL4 and MetaTrader 4 - MQL4 programming forum
To add comments, please log in or register