Simpler way to check rules condition

 

Hi everyone, I have 4 rules (A-B-C-D) to open a trade. The entry would be determined by the rule A and the rest is a confirmation indicator. From this 4 rules I need 3 including the entry rules to be true, the conventional way is to make it like this:

if(A == true &&
   (B && C == true ||
    C && D == true ||
    D && B == true ||
    B && C && D == true))

//Open trade here

is there a simpler way to check the condition without many or statement?

 
Luandre Ezra:

Hi everyone, I have 4 rules (A-B-C-D) to open a trade. The entry would be determined by the rule A and the rest is a confirmation indicator. 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?

At first I thought all one could do is shorten the text:

   if(A)
     {
      if((B && C) || (C && D) || (D && B) || (B && C && D))
        {
         //Open trade here
        }
     }

But that made me see that it looks like you want any combination of B, C, D, minimum 2 must be true, so maybe this could work 

      if(A &&  (int(B+C+D) >= 2)  )
        {
         //Open trade here
        }


Or if you want a boolean cast

   if(A && bool((B + C + D)/2))
     {
      //Open trade here
     }


[Disclaimer - not tested, please do so]

 
if(A == true &&
   (B && C == true ||
    C && D == true ||
    D && B == true ||
    B && C && D == true))
  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. Basically, you want A and at least two of the others.

    False is zero, true is one
    if(A && (B + C + D) >= 2)
      {
       //Open trade here
      }
 

thanks everyone for the answer, appreciate it,

quick question here!.

If there's 2 conditions which indicate bullish trend and bearish trend, I don't think boolean would be useable. So I use int result that return 1 if bullish 2 if bearish and 0 if there's no trend. If I use >= when 2 logic is correct and the last one isn't the equation would be like this

if(A &&  (int(B+C+D) >= 
4 )  )
        {
         //Open trade here
        }

the problem is that the last logic could  be return 1 and resulting true for the equation above. Is there a way to solve this problem?

 
Luandre Ezra #:

thanks everyone for the answer, appreciate it,

quick question here!.

If there's 2 conditions which indicate bullish trend and bearish trend, I don't think boolean would be useable. So I use int result that return 1 if bullish 2 if bearish and 0 if there's no trend. If I use >= when 2 logic is correct and the last one isn't the equation would be like this

the problem is that the last logic could  be return 1 and resulting true for the equation above. Is there a way to solve this problem?

You have only 3 inputs (B + C + D), using >= 4 would mean it is always false. (Similarly >= 0 would always be true). Neither of these make sense.

From your explanation, you would need to detect 1 or 2 specifically

   if(A)
     {
      if(int(B + C + D) == 1)
        {
         //Bullish
        }
      else
         if(int(B + C + D) == 2)
           {
            //Bearish
           }
         else
           {
            //3 = something else
           }
     }
 
R4tna C #:

You have only 3 inputs (B + C + D), using >= 4 would mean it is always false. (Similarly >= 0 would always be true). Neither of these make sense.

From your explanation, you would need to detect 1 or 2 specifically

You are also approaching a situation when maybe a switch is neater

   if(A)
     {
      int sum = int(B + C + D);
      switch(sum)
        {
         case  1 :
            //Bullish
            break;

         case  2 :
            //Bearish
            break;

         case  3 :
            //Something else
            break;
        }
     }
 
Luandre Ezra #:

thanks everyone for the answer, appreciate it,

quick question here!.

If there's 2 conditions which indicate bullish trend and bearish trend, I don't think boolean would be useable. So I use int result that return 1 if bullish 2 if bearish and 0 if there's no trend. If I use >= when 2 logic is correct and the last one isn't the equation would be like this

the problem is that the last logic could  be return 1 and resulting true for the equation above. Is there a way to solve this problem?

There is another way to approach this.

If you use negative values for bearish, they would decrement your bullish evaluation. And vice versa.




 

Thanks R4tna C for your help,

If I use your code then the result would be like this,

  if(A)
     {
      int sum = int(B + C + D);
      switch(sum)
        {
         case  1 :
            if(sum == 3 || 4) //(1+1+2) --> 2 condition correct and 1 not
            //do this
            break;

         case  2 :
            if(sum == 5 || 6) //(2+2+1) --> 2 condition correct and 1 not
            //do this
            break;

         case  3 :
            if(sum == 1 ||2)
            //do nothing
            break;
        }
     }

is this correct?


Thanks for your answer Dominik, but I don't think using minus would be working. Let's say that with this example, if the D would be bearish then the equation would be like this (1 + 1 + (-1)) >=2, the equation would always be false if only 2 of the condition are correct .

 
Luandre Ezra #:

Thanks R4tna C for your help,

If I use your code then the result would be like this,

is this correct?


Thanks for your answer Dominik, but I don't think using minus would be working. Let's say that with this example, if the D would be bearish then the equation would be like this (1 + 1 + (-1)) >=2, the equation would always be false if only 2 of the condition are correct .

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.

 
Luandre Ezra #:

Thanks R4tna C for your help,

If I use your code then the result would be like this,

is this correct?


Thanks for your answer Dominik, but I don't think using minus would be working. Let's say that with this example, if the D would be bearish then the equation would be like this (1 + 1 + (-1)) >=2, the equation would always be false if only 2 of the condition are correct .

I see... - It was jsut an idea on how to "balance" your conditions based on the direction.

 
R4tna C #:

You don't need the inner if statements - in fact they will never be true

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

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

case  1 :

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,

case 4:
    //2 bullish 1 bearish = rules in line
    open trade here

is this right?