I NEED HELP. MACROSS, ALERT.

 

Code:

bool ma4 = 0; // value 1 == Cross signal

if(ma1[1] < ma2[1]&& ma1[0] > ma2[0])

ma4 = 1;

Alert("Cross signal down");

 

Hi,

I want my code to print/alert only one time in the journal when ma4 is still in value 1. Right now my code is printing alot " cross signal down" when ma4 = 1.   

 

Any one can solve this problem?  

Thien   

 

You can use such function in your code:

bool flag()
  {
   static bool flag = true;
   if(!flag) return(false);
   flag = false;
   return(true);
  }

This function return true only once.

void OnTick()
  {
//---
   
   //...
   
   if(flag()) {
      // there should be some code which will be executed only once
     }
   
   //...

  }
 
Any other solutions! Thanks!
 

Another solution Have done it now in mql4  but why can't it this way in mql5

int start()
  {
   int    counted_bars=IndicatorCounted();
//----
if(ma1[1] < ma2[1]&& ma1[0] > ma2[0] && ma4 != 1)
   {
    ma4 = 1; 
    Alert("Cross signal down");
   } 
//----
   return(0);
  }

 

 
deVries:

Another solution Have done it now in mql4  but why can't it this way in mql5

 

Hi, 

 

this is a script. onstart(). I want it to work in the EA. But only one message in the journal. I want the program to jump over the print until the new signal is comming.  

 
thien_v:

Hi, 

 

this is a script. onstart(). I want it to work in the EA. But only one message in the journal. I want the program to jump over the print until the new signal is comming.  

You could use logic of my example to build your own logic. For solve your task you can use global or static variables.

If I correctly understand what you need there is solution:

void OnTick()
  {
//---
   
   //...
   
   bool ma4 = false; // value true == Cross signal 

   if(ma1[1]<ma2[1] && ma1[0]>ma2[0]) {
      if(flag()) {
         ma4 = true;
         Alert("Cross signal down");
        }
     }
   else {
      ma4 = false;
      flag(true); // reset
     }
   
   //...
   
  }


//################################################
//################################################
bool flag(bool reset=false)
  {
   static bool flag = true;
   if(reset) {
      flag = true;
      return(false);
     }
   if(!flag) return(false);
   flag = false;
   return(true);
  }
 
thien_v:

Hi, 

 

this is a script. onstart(). I want it to work in the EA. But only one message in the journal. I want the program to jump over the print until the new signal is comming.  

Now I don't understand 

 You want code to print/alert only one time in the journal when ma4 is still in value 1.  If you trie it out you can't have more then one alert it can only alert for more times    if the     bool ma4 = 0;   is turning again to zero after each cycle or there is something else in your EA making ma4 equal to zero

 

 

 

@WWer

Yes, it print one time, but I dont get the right Time in the journal. ex The time is 00:00:00 Cross down..

Test it so you will understand it.