Simpler way to check rules condition - page 2

 
Luandre Ezra #:

ah I think I got the Idea how to use switch case. 

no 1 on above is referring to the sum of (B+C+D), so in this case if there's 2 bullish and 1 bearish the equation  would be like this (1+1+2). The sum of this equation would be the case no like this,

is this right?

I think you are getting confused - please read the documentation on switch https://www.mql5.com/en/docs/basis/operators/switch

You must understand how it works first, so I suggest you write a test script 

I will try to add some explanation here, but please do the above too

   if(A)
     {
      int sum = int(B + C + D);
      switch(sum)
        {
         case  1 :
           {
            //Bullish
            // (B + C + D) Only 1 value is true so sum == 1
            // Execute the code needed for 1 true value
            // if you need to know which one true you have to use some logic  within this code block
            break; 
           }

         case  2 :
           {
            //Bearish
            // (B + C + D) Only 2 values are true so sum == 2
            // Execute the code needed for 2 true values
            // if you need to know which 2 are true you have to use some logic within this code block

            break;
           }
         case  3 :
           {
            //Other
            // (B + C + D) All values are true so sum == 3
            // Execute the code needed for 3 true values
            break;
           }
        }
     }
Documentation on MQL5: Language Basics / Operators / Switch Operator
Documentation on MQL5: Language Basics / Operators / Switch Operator
  • www.mql5.com
Switch Operator - Operators - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
R4tna C #: You don't need the inner if statements - in fact they will never be true
case  1 :
            if(sum == 3 || 4) 

here you see sum == 1 so the if statement will always be false.

Actually, the if statement will always be true. False is zero, and true is non-zero. if(false || 4) equals if(4) equals if(true)

if( 3 < 2 < 1 )
if( false < 1 )
if(     0 < 1 )
if(     true  )
if( 3 > 2 > 1 )
iftrue > 1 )
if(     1 > 1 )
if(     false )

 
William Roeder #:

Actually, the if statement will always be true. False is zero, and true is non-zero. if(false || 4) equals if(4) equals if(true)

Ah yes good point - I was thinking of this scenario , which is how I usually explicitly write it:  

if((sum == 3) || (sum == 4))

I should have tested it be be sure.

Nevertheless, it makes me even more enthusiastic about wanting to bracket such statements!

 

I used switch case operator to determine a magic number on different timeframe like this,

   int timeframe = Period();
   switch(timeframe)
     {
      case 5:
         MagicNumber = MagicNumberM5;
         break;
      case 15:
         MagicNumber = MagicNumberM15;
         break;
      case 30:
         MagicNumber = MagicNumberM30;
         break;
      case 60:
         MagicNumber = MagicNumberH1;
         break;
      case 240:
         MagicNumber = MagicNumberH4;
         break;
      default:
         Alert("EA is not working in this timeframe");
         break;
     }

because timeframe returns int with number of 5,15,30,60,240 it so the case number would be those numbers.


Let me tells you what I understand with this problem,

if(A)
  {
   int sum = int(B + C + D);
   switch (sum):
      {
      //if all bullish then the equation would be (1+1+1) = 3, in this example I should write like this, btw I used int cast not boolean because of thread #3
      case 3:
      
      //if 2 condition bullish and 1 bearish then the equation would be (1+1+2) = 4, in this example I should write like this,
      case 4:

is this wrong?

Simpler way to check rules condition - How to check a trade condition without many or statement
Simpler way to check rules condition - How to check a trade condition without many or statement
  • 2022.08.26
  • www.mql5.com
From this 4 rules i need 3 including the entry rules to be true, the conventional way is to make it like this: is there a simpler way to check the condition without many or statement. Here you see sum == 1 so the if statement will always be false
 
Luandre Ezra #: I used switch case operator to determine a magic number on different timeframe like this,
      case 5:
         MagicNumber = MagicNumberM5;
         break;
      case 15:
  1. Don't hard code numbers
    case PERIOD_M5:
       MagicNumber = MagicNumberM5;
       break;
    case PERIOD_M15:

  2. Why not just embed the period?
    MagicNumber = extMagicNumber + _Period;
    ⋮
    ENUM_TIMEFRAMES period = OrderMagicNumber() - extMagicNumber;
              See also: Why are MT5 ENUM_TIMEFRAMES strange? - General - MQL5 programming forum - Page 2 #11 (2020)

Reason: