any way to store expression into a variable or array?

 

Hi gurus,

I want to be able to test my conditions (expressions) and know which one of them triggers the trade in an if statement by printing out the trigger expression when true.

so i tried assigning the expression to x and then print x if it is true. However, since x is a bool, the final output is only "true" and not outputting the expression itself. 

appreciate any pointers! thanks in advance!

bool x = 0;

 if( alertswitchPrevious !=-8 //&& alertswitchPrevious !=67 && alertswitchPrevious !=65
            && (  x= (gCurrentPrice >= topScalpPrice)
               || (x= (Scalp_StdDev_top2 >= 100.05 ) )
               || (x= (gLivePnLLong >8 ) )
               || (x= (gnewbarcount >=8 && giHigh < gPbarHigh && gTailScore ==-1) )
               || (x= (noOfLadders >15 ) )
               
               || (x= (gcutLossP >0 && gLivePnLLong <= gcutLossP )) //17.Aug added
               )
        )
        {  
            
            Alert ( x + "  triggered");
        }
 
JimSingadventure:

Hi gurus,

I want to be able to test my conditions (expressions) and know which one of them triggers the trade in an if statement by printing out the trigger expression when true.

First of all get out of the habit of assigning a numeral to a bool variable. It is either true or false.

instead of using || you could use a series of if/else and print when it hits a true.

 
Keith Watford #:

First of all get out of the habit of assigning a numeral to a bool variable. It is either true or false.

instead of using || you could use a series of if/else and print when it hits a true.

Thanks Keith for quick reply.

'understand problem with bool. my || expressions can go to 100s of lines so that when i use the if/else statement for each line, the code will look "unclean" and repetitive and each line of literal expressions i have to manually hard-code again for printing. 

Is there a simpler, more elegant solution along the lines of "print expression if found true" without using if/else + hardcoding the print expressions on each and every line? I'm kindof also fuzzy how incorporating this if/else into a for loop might be able to print the expression without too much repetition...

One way i can think of is if there is a way to assign to a variable or into an array that is not a "bool" but an "expression" instead, perhaps a class or structure (but i'm not yet at that level) .   can you share a quick example? thanks again!

 
JimSingadventure #: One way i can think of is if there is a way to assign to a variable or into an array that is not a "bool" but an "expression" instead, ….   can you share a quick example? thanks again!

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.

Looking for a cross:

double aPrev = …(i+1), aCurr = …(i),
       bPrev = …(i+1), bCurr = …(i);
bool   wasUp = aPrev > bPrev,
        isUp = aCurr > bCurr,
     isCross = isUp != wasUp;
 
William Roeder #:

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.

Looking for a cross:

thanks william, i am not understanding how your example relate to my question. or it could be that seems you're misunderstanding my issue. the only reason i equated the expression to x (bool) is so that by doing so, i can print x ( i know it is wrong and illogical but just showing something as a sample)  which i was hoping will print out the entire expression assignment, instead of a true/false output. 

And so my question is, how do i print out the entire expression assigned to x?

 
JimSingadventure: I want to be able to test my conditions (expressions) and know which one of them triggers the trade in an if statement by printing out the trigger expression when true. so i tried assigning the expression to x and then print x if it is true. However, since x is a bool, the final output is only "true" and not outputting the expression itself.  appreciate any pointers! thanks in advance!

If you want to track multiple conditions, use a boolean array ...

bool condition[6];
condition[0] = gCurrentPrice >= topScalpPrice;
condition[1] = Scalp_StdDev_top2 >= 100.05;
condition[2] = gLivePnLLong >8;
condition[3] = gnewbarcount >=8 && giHigh < gPbarHigh && gTailScore == -1
condition[4] = noOfLadders >15;
condition[5] = gcutLossP >0 && gLivePnLLong <= gcutLossP;

Then scan the array to see which conditions were met ...

size = ArraySize( condition );
for( int i = 0; i < size; i++ )
{
   if( condition[i] ) PrintFormat( "Condition %d triggered", i );
};
Remember that the conditions will not update automatically, so you will have to reassign them again later.
 
Fernando Carreiro #:

If you want to track multiple conditions, use a boolean array ...

Then scan the array to see which conditions were met ...

Remember that the conditions will not update automatically, so you will have to reassign them again later.

Thanks very much for the example, Fernando. That is exactly what i was hoping and it solves my issue. I was hoping further it can print the entire expression itself instead of just the array position (pointer to the expression), but if it is not possible, then this is good enough!

btw, what do you mean by "conditions will not update automatically and i have to reassign"?  is there anything in the code which changed the conditions thereby requiring an update?

 
JimSingadventure #: Thanks very much for the example, Fernando. That is exactly what i was hoping and it solves my issue. I was hoping further it can print the entire expression itself instead of just the array position (pointer to the expression), but if it is not possible, then this is good enough! btw, what do you mean by "conditions will not update automatically and i have to reassign"?  is there anything in the code which changed the conditions thereby requiring an update?

MQL is a compiled language, so you cannot print the expression. Usually only interpreted languages can do that.

By update, I mean that if the conditions change at a later time, the boolean results in the array will not change until you run that part of the code again.

 
JimSingadventure #:

Thanks very much for the example, Fernando. That is exactly what i was hoping and it solves my issue. I was hoping further it can print the entire expression itself instead of just the array position (pointer to the expression), but if it is not possible, then this is good enough!

btw, what do you mean by "conditions will not update automatically and i have to reassign"?  is there anything in the code which changed the conditions thereby requiring an update?

The array solution is neat - kudos Fernando.

Perhaps if you make it a 2 dimensional string array you can store a representation of the expression, and the result.

You have to little work to translate the string version of true/false back to boolean values

   string condition[][2] = {};
   ArrayResize(condition, 6);
   ZeroMemory(condition);

   condition[0][0] = "gCurrentPrice >= topScalpPrice";
   condition[0][1] = string(gCurrentPrice >= topScalpPrice);

   condition[1][0] = "Scalp_StdDev_top2 >= 100.05";
   condition[1][1] = string(Scalp_StdDev_top2 >= 100.05);
   //and so forth for the other conditions
 
R4tna C #: The array solution is neat - kudos Fernando. Perhaps if you make it a 2 dimensional string array you can store a representation of the expression, and the result. You have to little work to translate the string version of true/false back to boolean values.

No, saving the boolean results as a string will make your code very inefficient. There are two options (at least), either use a structure or use use two arrays, one boolean for the condition and another for the expression string for printing.

string expression[] = {
   "gCurrentPrice >= topScalpPrice",
   "Scalp_StdDev_top2 >= 100.05",
   "gLivePnLLong >8",
   "gnewbarcount >=8 && giHigh < gPbarHigh && gTailScore == -1",
   "noOfLadders >15",
   "gcutLossP >0 && gLivePnLLong <= gcutLossP" };

void CheckCondition() {
   bool condition[6];
   condition[0] = gCurrentPrice >= topScalpPrice;
   condition[1] = Scalp_StdDev_top2 >= 100.05;
   condition[2] = gLivePnLLong >8;
   condition[3] = gnewbarcount >=8 && giHigh < gPbarHigh && gTailScore == -1;
   condition[4] = noOfLadders >15;
   condition[5] = gcutLossP >0 && gLivePnLLong <= gcutLossP;

   int size = ArraySize( condition );
   for( int i = 0; i < size; i++ )
      if( condition[i] ) PrintFormat( "Condition %d:[%s] triggered", i, expression[i] );
};

The second option is using an array of a structure.

struct STrigger {
   bool   condition;
   string expression; };

STrigger trigger[] = {
   { false, "gCurrentPrice >= topScalpPrice" },
   { false, "Scalp_StdDev_top2 >= 100.05" },
   { false, "gLivePnLLong >8" },
   { false, "gnewbarcount >=8 && giHigh < gPbarHigh && gTailScore == -1" },
   { false, "noOfLadders >15" },
   { false, "gcutLossP >0 && gLivePnLLong <= gcutLossP" } };

void CheckCondition() {
   trigger[0].condition = gCurrentPrice >= topScalpPrice;
   trigger[1].condition = Scalp_StdDev_top2 >= 100.05;
   trigger[2].condition = gLivePnLLong >8;
   trigger[3].condition = gnewbarcount >=8 && giHigh < gPbarHigh && gTailScore == -1;
   trigger[4].condition = noOfLadders >15;
   trigger[5].condition = gcutLossP >0 && gLivePnLLong <= gcutLossP;

   int size = ArraySize( trigger );
   for( int i = 0; i < size; i++ )
      if( trigger[i].condition ) PrintFormat( "Condition %d:[%s] triggered", i, trigger[i].expression );
};

Warning! The above code was just typed out in the post. It was not compiled or tested. There may be typos, that need to be checked.

 

There is also a way of using macros to "stringify" the expression, so that you don't have to type the expression twice. This will help keep the code consistent when you update the expressions.

However, this is a more power programmer coding skill that needs careful planing of the code logic, but you can research the "#" Stringification functionality of the standard C/C++ preprocessor which also works on MQL.

Reason: