Complicated conditionals (selecting lines of a conditional using extern variables)

 

Is it possible to “selectively use” lines of a conditional, based upon whether certain external boolean variables are TRUE or FALSE?

(I’m not even sure what to call this problem, so it has hindered my attempts to find an answer by searching so far. Perhaps someone already knows whether it is even possible in MT4, or whether I’m barking up the wrong tree?)

This example is a simplified version of a function I’m trying to write. You pass into the function which indicators you want to use, and then only those relevant lines are used in the conditional.

int TEST_FUNCTION(bool UseRSI, bool UseStochastic, bool UseSMA, bool UseADX)
{   
   
int answer = 0;   
   
if (
      ( iRSI(etc....)         > 50  )    // Only include this line in this conditional if UseRSI == TRUE
   && ( iStochastic(etc....)  > 50  )    // Only include this line in this conditional if UseStochastic == TRUE
   && ( iMA(etc....)          > 1.5 )    // Only include this line in this conditional if UseMA == TRUE
   && ( iADX(etc....)         > 20  )    // Only include this line in this conditional if UseADX == TRUE
   )
   {answer = 1;}   
      
return(answer);   
}  

The way I am doing it at the moment is by manually going into the code and placing “//” before the lines which I want to exclude – but that obviously won’t work for the function here because that would make the inputs pointless.

There are at least 14 different combinations of the 4 lines in the conditional (as far I as can count them). I really don’t want to have to write 14 different functions. I also would like to avoid just having 14 separate conditionals in the function as well. I’ve never needed to use a “switch” before, but I think that would still require all the different possibilities to be written out in full as well. Using nested conditionals would be even more complicated than just writing out all the possibilities separately I think (because the use of one indicator is independent of the state of the others, so there would be too many “elses” to be efficient – and it would be a nightmare to update).

If I only had to write out 14 possibilities, I could stomach that, but my actual function already has around 60 conditions so writing out all the possibilities would be insane.

I thought perhaps it would be possible to use #define to define one value as “/*” and another as “*/”, and then insert them into the conditional to be activated when the relevant extern bool is set to FALSE, so that they blank out the relevant line (the same way as manually typing “//” at the start of that line would do) – but that was unsuccessful because there I can see no way to sneak them into the main conditional without breaking it.

I have no background in programming, and I have always tried to avoid this situation in the past. This must be a common problem. Is there any standard way to work this in MT4, or do I have to write them all out separately?

 
clerin6:

Is it possible to “selectively use” lines of a conditional, based upon whether certain external boolean variables are TRUE or FALSE?

You mean like this ?

int TEST_FUNCTION(bool UseRSI, bool UseStochastic, bool UseSMA, bool UseADX)
{   
   
int answer = 0;   
   
if (
      ( ( iRSI(etc....)         > 50 && UseRSI ) || !UseRSI )    // Only include this line in this conditional if UseRSI == TRUE
   && ( ( iStochastic(etc....)  > 50 && UseStochastic ) || !UseStochastic )    // Only include this line in this conditional if UseStochastic == TRUE
   && ( ( iMA(etc....)          > 1.5 && UseMA ) || !UseMA )   // Only include this line in this conditional if UseMA == TRUE
   && ( ( iADX(etc....)         > 20 && UseADX ) || !UseADX )    // Only include this line in this conditional if UseADX == TRUE
   )
   {answer = 1;}   
      
return(answer);   
}  
 

Yes. That would work perfectly, thank you :-)

It seems utterly obvious in retrospect. I never even thought of using ||. Duh.

... Armadillos!

 
clerin6:

Yes. That would work perfectly, thank you :-)

It seems utterly obvious in retrospect. I never even thought of using ||. Duh.

... Armadillos!

You are most welcome.