Please help with bool

 

Good day all.


Is there someone that can assist me with a small piece of code. I know it is probably easy but I am new to mql4 and struggling.


I need a piece of code that does the following:


When a specific order, lets say "ordersell_1" is triggered, I need a "true" statement

when the specific order "ordersell_1" is not triggered, I need a "false" statement.


When it is "true" double x = 123

when it is "false" double x = 321


I know it is something to do with a bool. 


Please help.

 
if(ordersell_1 == true)
 {
  double x = 123;
 }
else
 {
  double x = 321;
  }
 

Ahmed Hussain:

if(ordersell_1 == true)
 {
  double x = 123;
 }
else
 {
  double x = 321;
  }


It is no good declaring the variable inside the braces as it will not exist outside of the braces.

 
Ahmed Hussain:

Thanks.


Ill try and figure it out from there. 

Needs to exist outside the brackets, but you gave me a start.

 
  1. if(ordersell_1 == true)

    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.

  2. Declaration outside, assignment inside.

  3. Or:
    double x = ordersell_1 ? 123 : 321; // Ternary

 
William Roeder:
  1. 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.

  2. Declaration outside, assignment inside.

  3. Or:

Thanks William.


Makes absolute sense. Thanks for taking the time to explain. Much appreciated.


Kind regards.