Maintain values until new signal

 

Hi there,

 

I need some help. How can i put a value on a variable, and chage it when a new signal shows?

Let me explain a little bit more:

 I have this code... 

     if(iEnvelopes(NULL,0,14,MODE_SMA,0,PRICE_CLOSE,Deviation,MODE_LOWER,1)>Close[1]  ){
      Flag = 1;

     }
    

     and i want that Flag = 1 until happens this (and vise versa):

    if(iEnvelopes(NULL,0,14,MODE_SMA,0,PRICE_CLOSE,Deviation,MODE_UPPER,1)<Close[1]){
      Flag = 2;

 The problem is that it only put 1 or 2 if the last bar is bigger/slower than the Envelopes channels indi, and i need it to maintain the value until it pass to 2 (if Flag = 1) or vise versa.

 

How can i do that? 

 

Thanks in advance,

 

Regards and good trading 

 
Declare the variable as a static or global variable
 
Keith Watford:
Declare the variable as a static or global variable
Sorry, but do i do that?
 
 

 I already did that, but the issue maintains. That's why i ask how could i do that.... The issue is that if the next candle don't equals that function, returns 0 and not the value that it should have (1 or 2), until it reach the other function.

 

static int Flag;
     if(iEnvelopes(NULL,0,14,MODE_SMA,0,PRICE_CLOSE,Deviation,MODE_LOWER,1)>Close[1]  ){
      Flag = 1;

     }
    
     if (Flag == 1)
     {
        ....
     }
    
     if(iEnvelopes(NULL,0,14,MODE_SMA,0,PRICE_CLOSE,Deviation,MODE_UPPER,1)<Close[1]){
      Flag = 2;
     }
    
     if (Flag == 2 )
     {
       .....
     }
         return (Flag);
       Comment("Flag: "+Flag);


 

 

    static int Flag=0;
     if( iEnvelopes(NULL,0,14,MODE_SMA,0,PRICE_CLOSE,Deviation,MODE_LOWER,1)>Close[1]  )
     {
      if(Flag == 1)
         return(0);
      else
         {
         Flag=1;
         //Anything else that you may want to do when Flag is reset
         return(1);
         }
     }
   
   
     if(iEnvelopes(NULL,0,14,MODE_SMA,0,PRICE_CLOSE,Deviation,MODE_UPPER,1)<Close[1])
     {
     if (Flag == 2 )
         return(0);
      else
         {
         Flag = 2;
         //Anything else that you may want to do when Flag is reset
         return(2);
         }
     }
Not compiled or tested, just quickly thrown together.
 

Hi Keith,

 

I think that it worked. Thanks a lot! 

Reason: