Download MetaTrader 5

I NEED HELP. MACROSS, ALERT.

To add comments, please log in or register
thien_v
91
thien_v 2011.10.27 18:20 

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   

Nazariy Stapyak
1744
Nazariy Stapyak 2011.10.27 21:44  

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
     }
   
   //...

  }
thien_v
91
thien_v 2011.10.28 00:44  
Any other solutions! Thanks!
Tjipke de Vries
6324
Tjipke de Vries 2011.10.28 03:34  

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);
  }

 

thien_v
91
thien_v 2011.10.28 12:43  
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.  

Nazariy Stapyak
1744
Nazariy Stapyak 2011.10.28 14:25  
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);
  }
Tjipke de Vries
6324
Tjipke de Vries 2011.10.28 14:44  
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

 

 

thien_v
91
thien_v 2011.10.28 15:49  

@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.    

To add comments, please log in or register