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.
- 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.
- 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; } }
-
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 - 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) |
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: |
- 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.
- Simplify your code. The first if/else is unnecessary.
-
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 - 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:
- book.mql4.com
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
- 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.
- Simplify your code. The first if/else is unnecessary.
-
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 - 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.
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?
- After that, how does it ever get set to false?
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.
- After that, how does it ever get set to false?
- 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!
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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.
Anyone???