String in a conditional operator

 
Good morning,
Today I woke up with a new question: 
In MQL5, is it possible to use a string in a conditional operator?

For example, in this case, is there any way to do it?

double A=2.5, B=4;
string cadena 
if(A<3) cadena="A==B && RSI>30";
else cadena="A<=B && RSI>30";

if(cadena)
{
  .......
}
 

Operator: ?:  https://www.mql5.com/en/docs/basis/operators

cadena = (A<3) ? "A==B && RSI>30" :  "A<=B && RSI>30";

But what do you expect with if(cadena)?

Does this meat your ideas(?):

double A=2.5, B=4;
bool cadena;
cadena = (A<3) ? A==B && RSI>30 :  A<=B && RSI>30;
if(cadena)
{
  .......
}
Documentation on MQL5: Language Basics / Operators
Documentation on MQL5: Language Basics / Operators
  • www.mql5.com
Operators - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
febrero59:
Good morning,
Today I woke up with a new question: 
In MQL5, is it possible to use a string in a conditional operator?

For example, in this case, is there any way to do it?


cadena is a "string" type, not a "bool" type ... if operator needs a "bool" expression, an expression that can take a true or false value, in parantheses to be able to make a choice,

You can use string variables in if expressions in the following way.

if (cadena == "123")
   {
    ...
   }
 
bool RSI, MA, MOM;
double mRSI, mMA, mMOM;
string cadena; 

if(RSI==true){
               if(MA=true){ if(MOM==true) cadena=".... && mRSI>30  && mMA>60 && mMOM>60);
                            else cadena=".... && mRSI>30 && mMA>60);
                          }
               else { if(MOM==true) cadena=".... && mRSI>30  && mMOM>60);
                      else cadena=".... && mRSI>30);
                    }
                          
             }
else {
         if(MA=true){ if(MOM==true) cadena=".... && mMA>60 && mMOM>60);
                      else cadena=".... && mRSI>30 && mMA>60);
                    }
         else { if(MOM==true) cadena=".... && mMOM>60);
                else cadena=".... );
              } 
      }


if(cadena)
{
  .......
}
I intended to predefine a pre-selectable conditional package (with several selectable alternative Boolean variables), to run a single conditional analysis.

Did I make myself clear? Just in case this example:


 
febrero59 #: I intended to predefine a pre-selectable conditional package (with several selectable alternative Boolean variables), to run a single conditional analysis.
input bool useRSI=true;
input bool useMOM=false;
⋮
double mRsi = …;
⋮
bool condition1 = !useRSI || mRsi > 30;
bool condition2 = …
bool result = condition1 && condition2 …;

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.

 
febrero59:
Good morning,
Today I woke up with a new question: 
In MQL5, is it possible to use a string in a conditional operator?

For example, in this case, is there any way to do it?

Why do you want to make it a string ? Just make it a bool :

double A=2.5, B=4;
bool cadena=false;
if(A<3) cadena=A==B && RSI>30;
else cadena=A<=B && RSI>30;

if(cadena)
{
  .......
}
 
Guillermo Roeder # :

Nunca escribirías if( (2+2 == 4) == true) ¿ o sí? si (2+2 == 4) es suficiente. Así que no escribas if(bool == true) , solo usa if(bool) o if( ! bool) . El código se vuelve autodocumentado cuando usa nombres de variables significativas, como bool isLongEnabled , donde Long_Entry suena como un precio de activación o un número de boleto y "if long entry" es una oración incomple

Understood, that's what being self-taught is all about. thank you.

 
Alain Verleyen #:

Why do you want to make it a string ? Just make it a bool :

Understood, I will build with bool. thanks.

 
Alp Duman #:


cadena is a "string" type, not a "bool" type ... if operator needs a "bool" expression, an expression that can take a true or false value, in parantheses to be able to make a choice,

You can use string variables in if expressions in the following way.

ok, thanks

Reason: