Download MetaTrader 5

please,can i put several OR "||" in one operator?

To add comments, please log in or register
itriad
162
itriad  
this is correct, or not???????
/--------------------------------------------------------------------------------------------------------------------------------------------------------
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
     int start()
//-------------------------------------------------------------------------------------------------------------------------------------------------------- 
     {
     
       
       double modebid = MarketInfo(Symbol(),MODE_BID);
       double modeask = MarketInfo(Symbol(),MODE_ASK);

        if(modebid>1261.00&&modeask<1262.00||modebid>1271.00&&modeask<1272.00||modebid>1241.00&&modeask<1242.00||modebid>1231.00&&modeask<1232.00)
           
         //{do someting}
or not??????
Keith Watford
Moderator
10637
Keith Watford  

I think that you have to add brackets to recognise combination conditions

if( (modebid>1261.00 && modeask<1262.00  ) || (modebid>1271.00 && modeask<1272.00) || (modebid>1241.00 && modeask<1242.00
|| (modebid>1231.00 && modeask<1232.00) )
Simon Gniadkowski
Moderator
18021
Simon Gniadkowski  
GumRai:

I think that you have to add brackets to recognise combination conditions

You don't have to, the precedence rules will govern what happens first and if it coincides with what is desired then it's OK . . . but in the new version of MT4 the precedence rules are changing . . . so it is of course good practice to make things unambiguous by using braces/brackets ( )
Keith Watford
Moderator
10637
Keith Watford  
RaptorUK:
You don't have to, the precedence rules will govern what happens first and if it coincides with what is desired then it's OK . . . but in the new version of MT4 the precedence rules are changing . . . so it is of course good practice to make things unambiguous by using braces/brackets ( )


To be honest, I wasn't sure, but it's the way I do it and as you say is good practice.

As is putting spaces either side of && and || to make it easier to read.

itriad
162
itriad  
ok,thank you
whroeder1
15715
whroeder1  
  1. What does it mean?
    if( (modebid>1261.00&&modeask<1262.00) || (modebid>1271.00&&modeask<1272.00) ||...
                (1261    -        1262)    or         (1271    -        1272) ...
    if(modebid>1261.00&& (modeask<1262.00||modebid>1271.00) && ...
         above 1261  but not      [1262   -        1271] ... 
    Always parenthesis when mixing ands and ors, so YOU can understand what you're writing.
     if( (modebid>1261.00&&modeask<1262.00)|| (modebid>1271.00&&modeask<1272.00)||...
    Make your code readable. Books aren't two foot wide, one column with no spaces between words for a reason. Neither should your code.
    if( (modebid > 1261.00 && modeask < 1262.00)
     || (modebid > 1271.00 && modeask < 1272.00)
     || ...
    Using bools with good names makes the code self documenting, and if it doesn't read right, it's not coded right.
    bool is1261 = modebid > 1261.00 && modeask < 1262.00,
         is1271 = modebid > 1271.00 && modeask < 1272.00,
         :
    if( is1261 || is1271 || ... )

  2. double modebid = MarketInfo(Symbol(),MODE_BID);
    double modeask = MarketInfo(Symbol(),MODE_ASK);
    Why are you using function calls for the current symbol when you can just use the predefined variables Bid and Ask?
itriad
162
itriad  
thank you WHRoeder
To add comments, please log in or register