Different results with slightly different code???

 

Hello all,

I'm curious to know why would I get different results in my testing and optimization when I make a few minor tweeks in my code that "technically" should bring the same result... see example below.

int total=OrdersTotal();
   if(total>0){
   for(int pos=0;pos<total;pos++) {
    if(OrderSelect(pos,SELECT_BY_POS)==true && OrderSymbol()==Symbol()){
      nearbyLong=true;
      break;
     } else {
       nearbyLong=false;
     }
    }
   } 
     if(nearbyLong==false || total==0){
 ......... this here gives me better profit
     }
int total=OrdersTotal();
   if(total>0){
   for(int pos=0;pos<total;pos++) {
    if(OrderSelect(pos,SELECT_BY_POS)==true && OrderSymbol()==Symbol()){
      nearbyLong=true;
      break;
     } else {
       nearbyLong=false;
     }
    }
   } else {
       nearbyShort=false;
   }
     if(nearbyLong==false){
 ......... this here gives me lower profit
     }

Anyone???

 
agu2a:

Hello all,

I'm curious to know why would I get different results in my testing and optimization when I make a few minor tweeks in my code that "technically" should bring the same result... see example below.

int total=OrdersTotal();
   if(total>0){
   for(int pos=0;pos<total;pos++) {
    if(OrderSelect(pos,SELECT_BY_POS)==true && OrderSymbol()==Symbol()){
      nearbyLong=true;
      break;
     } else {
       nearbyLong=false;
     }
    }
   } 
     if(nearbyLong==false || total==0){
 ......... this here gives me better profit
     }
int total=OrdersTotal();
   if(total>0){
   for(int pos=0;pos<total;pos++) {
    if(OrderSelect(pos,SELECT_BY_POS)==true && OrderSymbol()==Symbol()){
      nearbyLong=true;
      break;
     } else {
       nearbyLong=false;
     }
    }
   } else {	//ONLY	when	the	OrdersTotal	=	0	<<<
       nearbyShort=false;
   }
     if(nearbyLong==false){
 ......... this here gives me lower profit
     }

Anyone???


the "else" is the key answer

in the 1st code: it was doing what in the code with out else. so, it will see it always.

in the 2nd code: it won't see the code always.

 
  1. You don't show the context of where nearbyLong is declared. If it is static or global and there are no open orders, it keeps it's previous value in the top version. It is always set in the bottom.
  2. Simplify your code. The first if/else is unnecessary.
           nearbyLong=false;
    int total=OrdersTotal();
       for(int pos=0;pos<total;pos++) {
        if(OrderSelect(pos,SELECT_BY_POS) && OrderSymbol()==Symbol()){
          nearbyLong=true;
          break;
         }
        }
  3. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

  4. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
 

If-Else Conditional Operator

The IF - ELSE operator is used when a choice must be made. Formally, the syntax is as follows:

if (expression)
     operator1
else
     operator2

If the expression is true, operator1 is executed and control is given to the operator that follows operator2 (operator2 is not executed). If the expression is false, operator2 is executed.

The else part of the if operator can be omitted. Thus, a divergence may appear in nested if operators with omitted else part. In this case, else addresses to the nearest previous if operator in the same block that has no else part.

Examples:

//--- The else part refers to the second if operator:
if(x>1)
   if(y==2) z=5;
else     z=6;
//--- The else part refers to the first if operator:
if(x>l)
  {
   if(y==2) z=5;
  }
else        z=6;
//--- Nested operators
if(x=='a')
  {
   y=1;
  }
else if(x=='b')
  {
   y=2;
   z=3;
  }
else if(x=='c')
  {   
   y=4;
  }
else Print("ERROR");

 
whroeder1:
  1. You don't show the context of where nearbyLong is declared. If it is static or global and there are no open orders, it keeps it's previous value in the top version. It is always set in the bottom.
  2. Simplify your code. The first if/else is unnecessary.
  3. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

  4. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

Yes

you are right

also, nearbyShort



However, the full answer is below:

https://book.mql4.com/operators/if

Conditional Operator 'if - else' - Operators - MQL4 Tutorial
Conditional Operator 'if - else' - Operators - MQL4 Tutorial
  • book.mql4.com
As a rule, if you write an application program, you need to code several solutions in one program. To solve these tasks, you can use the conditional operator 'if-else' in your code. Format of the Operator 'if-else' Full Format The full-format operator 'if-else' contains a heading that includes a condition, body 1, the key word 'else', and body...
 
Mohammad Soubra:

the "else" is the key answer

in the 1st code: it was doing what in the code with out else. so, it will see it always.

in the 2nd code: it won't see the code always.


Thank you for your input Mohammad

 
whroeder1:
  1. You don't show the context of where nearbyLong is declared. If it is static or global and there are no open orders, it keeps it's previous value in the top version. It is always set in the bottom.
  2. Simplify your code. The first if/else is unnecessary.
  3. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

  4. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

Thanks for the reply whroeder1

1.nearbyLong is declared globally and set as false, so if the function finds a nearby order it will set it to true.

2. If I remove that if(total>0) statement, wouldn't the EA go and try start the for loop?

3. & 4. Noted, I will work on simplifying my code.

 

I followed your suggestions and did some cleaning on my code and now it runs smoothly, light weight and with expected results.  I did find a few parts where I wasn't declaring variables properly so I had fixed it now. Thx again for your input.

 
agu2a:
1.nearbyLong is declared globally and set as false, so if the function finds a nearby order it will set it to true.

2. If I remove that if(total>0) statement, wouldn't the EA go and try start the for loop?

  1. After that, how does it ever get set to false?
  2. for(int pos=0;pos<total;pos++) {
    Yes it would "try to start the for loop." How many times would it execute the loop? Remember pos is zero.
 
whroeder1:
  1. After that, how does it ever get set to false?
  2. Yes it would "try to start the for loop." How many times would it execute the loop? Remember pos is zero.

1. nearbyLong is actually set to false OnTick(), so it's always false unless the for loop finds an open position nearby the current price which in that case will set it to true

2. Makes sense...

Thanks again!

 
agu2a:

1. nearbyLong is actually set to false OnTick(), so it's always false unless the for loop finds an open position nearby the current price which in that case will set it to true

My question asked was "how does it ever get set to false?" Why did you not answer it? It is not "it's always false." Once you set it to true, it will always be true.

Reason: