Flow control using char variable

 

Hi, i'm setting "alertswitch" to a number and using this to control the flow as in the example below. : but it is not repecting the alertswitch global variabl even when i printed it out it is clearly set to 3. The EA just bulldozes its way to the next line of code as if the alertswitch !=3 is not there!   what am i missing ?


if () {}...

else if (
        
         alertswitch != 3 && alertswitchPrevious != 3 
         
         && FramaCurrentPrice < botBB1  
         && BBConvDivergence == "NEUTRAL" || BBConvDivergence == "STRONG-LONG" 
         && TotalScore >=2
         )
         {
         alertswitch = 3; 
         alertcomment =  "Frama-Extreme";
         Check =1; //this goes to a switch statement with Check variable '1'.
         }  

else if ( alertswitch !=5) {do this instead ....Check = 5}


 
Custom Graphical Controls. Part 1: Creating a Simple Control
Custom Graphical Controls. Part 1: Creating a Simple Control
  • www.mql5.com
This article covers general principles of development of graphical controls. We are going to prepare tools for a quick and convenient work with graphical objects, analyze an example of creation of a simple control for entering text or numeric data as well as the ways of using it.
 

Hi and good morning,

you should use brackets around the  || expression:

if () {}...

else if (
        
         alertswitch != 3 && alertswitchPrevious != 3 
         
         && FramaCurrentPrice < botBB1  
         && (BBConvDivergence == "NEUTRAL" || BBConvDivergence == "STRONG-LONG")
         && TotalScore >=2
         )
         {
         alertswitch = 3; 
         alertcomment =  "Frama-Extreme";
         Check =1; //this goes to a switch statement with Check variable '1'.
         }  

else if ( alertswitch !=5) {do this instead ....Check = 5}

Best regards

 
JimSingadventure:

Hi, i'm setting "alertswitch" to a number and using this to control the flow as in the example below. : but it is not repecting the alertswitch global variabl even when i printed it out it is clearly set to 3. The EA just bulldozes its way to the next line of code as if the alertswitch !=3 is not there!   what am i missing ?


To be precise should it be set to three? Because the way you set it means "not equals three".
 
Werner Klehr #:

Hi and good morning,

you should use brackets around the  || expression:

Best regards

thanks Werner, i will try that. 

However,  I understand that if the first condition is false the second is not checked. So why will this matter since the first condition is false

alertswitch != 3 && alertswitchPrevious != 3

, it should not continue to evaluate this line, right? with or without brackets....i am confused.

&& (BBConvDivergence == "NEUTRAL" || BBConvDivergence == "STRONG-LONG")

 

 
pennyhunter #:
To be precise should it be set to three? Because the way you set it means "not equals three".

pennyhunter, if you read code further down, it is set to 3 below. So in a loop, i do not want this if statement to be evaluated the 2nd time if it has already been set to alertswitch=3 the first time..

 
JimSingadventure:

Hi, i'm setting "alertswitch" to a number and using this to control the flow as in the example below. : but it is not repecting the alertswitch global variabl even when i printed it out it is clearly set to 3. The EA just bulldozes its way to the next line of code as if the alertswitch !=3 is not there!   what am i missing ?


provide all the relevent code if you need help, it is not possible to provide answers when the code does not compile, we cannot see the declarations, values or the surrounding logic.

have you used the debugger to check the status of the variables just before the statement is processed?

 
Paul Anscombe #:

provide all the relevent code if you need help, it is not possible to provide answers when the code does not compile, we cannot see the declarations, values or the surrounding logic.

have you used the debugger to check the status of the variables just before the statement is processed?

thanks for your suggestion paul. i will try to learn/study how to use Debug.

it is not possible to send the code here because they are kindof too complicated with #include links to a few files....

 
JimSingadventure #:

thanks for your suggestion paul. i will try to learn/study how to use Debug.

it is not possible to send the code here because they are kindof too complicated with #include links to a few files....

then stub the code by setting the variables explicitly before your statement to test the logic....

the answer will be an error in your code or the variables having differnt values to what you think they have

 
Paul Anscombe #:

then stub the code by setting the variables explicitly before your statement to test the logic....

the answer will be an error in your code or the variables having differnt values to what you think they have

hi Paul,

my code is structured like this below. you can run this btw...what stumps me is why value of alertswitch magically becomes '1' when i use "if" (in the 2nd if statement) but if i change it to "else if", the logic seems to be running correctly.  by [char alertswitch;] , it should remain 0 all the way and trigger

"Hey, default Check=0 : "

right? The "if-only" statement is ignoring the logic of the conditions.

( my trading conditions are determined by a series of If, else-if controls which jumps to the SWITCH Case...now i'm confused about [if-only] for each control or to use "if" on the first, then "else-if" for all the rest. )

void OnTick()
  {
  char alertswitch = FnAdd (3, 2);
   }//end of OnTick
    
    
char FnAdd (char a, char b) 
      {
      char Check; //int type also works.
      char alertswitch;
      
      int c= a+b; 
  
  if (alertswitch ==5 &&  c !=3 )
      {
      //Alert(" Check =1  " + alertswitch);
      alertswitch =1;
      Check =1;      
      }
   
   //2nd if statement   
   if (alertswitch ==1 &&  c !=5 )
      {
      //Alert(" Hey Check =2 " + alertswitch);
      alertswitch =2;
      Check =2;  
      }
      
   if (alertswitch ==0 &&  c !=3  )
      {
      //Alert(" Hey this is ...3rd = " + alertswitch);
       alertswitch =3;
       Check =0;  
      //FnAdd( 2, 3, alertswitch);  
      }
  
//---
   switch(Check)
      {
      case 1:
         Alert("Check =1 :", Check," : ", alertswitch);
         break;

      case 2:
         Alert("Check=2 :", Check, " : ",alertswitch);
         break;

      default: 
         Alert("Hey, default Check=0 : ", Check, " : ", alertswitch);
         break;
 
      }//end of switch
      
      return (alertswitch);
    }//end of Fn
 
JimSingadventure #:

hi Paul,

my code is structured like this below. you can run this btw...what stumps me is why value of alertswitch magically becomes '1' when i use "if" (in the 2nd if statement) but if i change it to "else if", the logic seems to be running correctly.  by [char alertswitch;] , it should remain 0 all the way and trigger

right? The "if-only" statement is ignoring the logic of the conditions.

( my trading conditions are determined by a series of If, else-if controls which jumps to the SWITCH Case...now i'm confused about [if-only] for each control or to use "if" on the first, then "else-if" for all the rest. )

You have not initialised alertswitch in  FnAdd   therefore it could potentially have any value which will give random results.

If you had used 

#property strict

then you would have known this a long time ago :)

 
Paul Anscombe #:

You have not initialised alertswitch in  FnAdd   therefore it could potentially have any value which will give random results.

If you had used 

then you would have known this a long time ago :)

Thanks for quick reply Paul.

i do not understand your comments though because i have no background in IT and just self-taught in programming by reading and learning from guys like you. What do you mean by "i did not initialised alertswitch in FnAdd?" i thought by adding char in front of alertswitch at the top of the FnAdd, it is already initialized and can be 'seen' by all if statements since they are all within the FnAdd bracket? what else needs to be done? 

appreciate if you can go down to my level to explain...

separately, 

1. i added [#property strict] at the top of my code (both this sample above and my full working code)  and they both compiled the same withouth errors. 

2. there does not seem to be any documentation about #property strict in MQL5 : https://www.mql5.com/en/docs/basis/preprosessor/compilation

Documentation on MQL5: Language Basics / Preprocessor / Program Properties (#property)
Documentation on MQL5: Language Basics / Preprocessor / Program Properties (#property)
  • www.mql5.com
Program Properties (#property) - Preprocessor - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: