A control to not repeat the operation

 

Hello everyone. I wrote my first semi-ea. Meaning that it is discretional in that but i need to be advised whenever the conditions are met. 

 

My problem right now is that the EA keeps sending me signals every hour, even if IT HAS ALREADY SENT THE SAME SIGNAL. THis is what i would really like to eliminate. Meaning: if you give me the signal i don't want a second/third/fourth signal until the conditions present again.

Try to make an example (in there cond


// External variables

extern int MagicNumber = 23310;
static datetime lastbar;
bool SignAlreadySent=False;<<<<<<------------------------------


double var3=iRSI(Symbol(), PERIOD_H1, 10, PRICE_CLOSE,0 );
double var4=iRSI(Symbol(), PERIOD_H4, 10, PRICE_CLOSE,0 );
double var5=iRSI(Symbol(), PERIOD_D1, 10, PRICE_CLOSE,0 );
double var6=iRSI(Symbol(), PERIOD_W1, 10, PRICE_CLOSE,0 );

double OWHH = iCustom(Symbol(),PERIOD_D1,"Heiken Ashi",Red,White,Red,White,2,1); //Open WEEKLY
double CWHH = iCustom(Symbol(),PERIOD_D1,"Heiken Ashi",Red,White,Red,White,3,1); //Close WEEKLY
double ODHH = iCustom(Symbol(),PERIOD_D1,"Heiken Ashi",Red,White,Red,White,2,1); //Open daily
double CDHH = iCustom(Symbol(),PERIOD_D1,"Heiken Ashi",Red,White,Red,White,3,1); //Close daily
double O4HH = iCustom(Symbol(),PERIOD_H4,"Heiken Ashi",Red,White,Red,White,2,1); //Open4h
double C4HH = iCustom(Symbol(),PERIOD_H4,"Heiken Ashi",Red,White,Red,White,3,1); //Close 4h
double O1HH = iCustom(Symbol(),PERIOD_H1,"Heiken Ashi",Red,White,Red,White,2,1); //Open1H
double C1HH = iCustom(Symbol(),PERIOD_H1,"Heiken Ashi",Red,White,Red,White,3,1); //Close 1H


// Long
bool buy_condition_3 = iRSI(Symbol(), PERIOD_H1, 10, PRICE_CLOSE,0 )  >  50  ;
bool buy_condition_4 = iRSI(Symbol(), PERIOD_H4, 10, PRICE_CLOSE, 0)  >  50  ;
bool buy_condition_5 = iRSI(Symbol(), PERIOD_D1, 10, PRICE_CLOSE, 0)  >  50  ;
bool buy_condition_6 = iRSI(Symbol(), PERIOD_W1, 10, PRICE_CLOSE, 0)  >  50  ;


bool sell_condition_3 = iRSI(Symbol(), PERIOD_H1, 10, PRICE_CLOSE,0 )  <  50  ;
bool sell_condition_4 = iRSI(Symbol(), PERIOD_H4, 10, PRICE_CLOSE, 0)  <  50  ;
bool sell_condition_5 = iRSI(Symbol(), PERIOD_D1, 10, PRICE_CLOSE, 0)  <  50  ;
bool sell_condition_6 = iRSI(Symbol(), PERIOD_W1, 10, PRICE_CLOSE, 0)  <  50  ;

// Start function
void init()
{
lastbar=Time[1];
return(0);
}

int start()
        { 
        
        
//Variables    
if (IsNewBar() && SignAlreadySent==False)
{
                          
if ((buy_condition_3  &&  buy_condition_4  &&  buy_condition_5  &&  buy_condition_6 && CWHH>OWHH && CDHH>ODHH && C4HH>O4HH && C1HH>O1HH) ||
 (sell_condition_3 && sell_condition_4 && sell_condition_5 && sell_condition_6 && CWHH<OWHH && CDHH<ODHH && C4HH<O4HH && C1HH<O1HH)) {
                                 //MT4 SendMail function
           SignAlreadySent=True;<<<<<<----------------------------OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
      bool res = SendMail("MT4: OrderSent", +"Instrument: " + Symbol()                  + "\n"
                                            +"RSI H1: " + string(var3)                  + "\n"
                                            +"RSI H4: " + string(var4)                  + "\n"
                                            +"RSI D1: " + string(var5)                  + "\n"
                                            +"RSI W1: " + string(var6)                  + "\n"
                                            +"Heiken Ashi W1C: " + string(CWHH)         + "\n"
                                            +"Heiken Ashi W1O: " + string(OWHH)         + "\n"
                                            +"Heiken Ashi D1C: " + string(CDHH)         + "\n"
                                            +"Heiken Ashi D1O: " + string(ODHH)         + "\n"
                                            +"Heiken Ashi 4HC: " + string(C4HH)         + "\n"
                                            +"Heiken Ashi 4HO: " + string(O4HH)         + "\n"
                                            +"Heiken Ashi 1HC: " + string(C1HH)         + "\n"
                                            +"Heiken Ashi 1HO: " + string(O1HH)         + "\n"
                                            );
                                            
       }
       SignAlreadySent=False;<<<<<<<<<<<<<<<-----------------------OOOOOOOOOOOOOOOOO
}
           
return(0);
                
        }

bool IsNewBar()
{
datetime curbar= Time[0];

if(lastbar!=curbar)
{
lastbar=curbar;
return(true);
}
return(false);
}

      

itions are a little bit demanding).

Suppose my ea must send me and email when rsi daily >50 and rsi W> 50.

Currently it would send me every single hour the same signal. I'd rather prefer that the ea sends me a signal when both turned down <50 or if rsi daily first declined down to 49 (no signal) to step up to 51 (signal). Essentially, send signals only when there is a mutation (keeping that conditions are still met). Did i make my point?

I tryied to put a control, a sort of boolean but it didn't work out. (bool SignalSent=False; in variables ---- SignalSent=True; just below the conditions and SignalSent=False; after the end of the if cycle.

 

Thanks to everyone.

 

 


 

 

1)

static datetime lastbar;

global variables are always 'static'. Static is meaning full only within an function like start() or OnTick().


2) define a static or global variable which is set to OP_BUY or OP_SELL when your EA send an email and allow the next email only if the signal has changed - not not merge buy and saell the way you did:

if ((buy_condition_3  &&  buy_condition_4  &&  buy_condition_5  &&  buy_condition_6 && CWHH>OWHH && CDHH>ODHH && C4HH>O4HH && C1HH>O1HH) ||
 (sell_condition_3 && sell_condition_4 && sell_condition_5 && sell_condition_6 && CWHH<OWHH && CDHH<ODHH && C4HH<O4HH && C1HH<O1HH)) {

but

static int trend = -1; // not defined
if      (trend != OP_BUY  &&  buy_condition_3 &&  buy_condition_4 &&  buy_condition_5 &&  buy_condition_6 && CWHH>OWHH && CDHH>ODHH && C4HH>O4HH && C1HH>O1HH) { trend = OP_BUY; .. }
else if (trend != OP_SELL && sell_condition_3 && sell_condition_4 && sell_condition_5 && sell_condition_6 && CWHH<OWHH && CDHH<ODHH && C4HH<O4HH && C1HH<O1HH) { trend = OP_SELL...}
 

Thanks Gooly, but would you please provide an example of variable i can use to detect wheter the signal has changed?

 

Many thanks

 

bibi 

 

One way to check for a change in state:

static bool LastSignal=false;
bool Signal=/*signal logic*/;
if(Signal!=LastSignal) { /*do something on the change of signal*/ };
LastSignal=Signal;
 

Many thanks guys.

However i discovered that ea isn't working at all. It sends me emails every hour but values are Always the same.... 

Would move from int start to onTick all the code fix that up?

 

Here's the updated code

 

// External variables

extern int MagicNumber = 23310;
datetime lastbar;


double var3=iRSI(Symbol(), PERIOD_H1, 10, PRICE_CLOSE,0 );
double var4=iRSI(Symbol(), PERIOD_H4, 10, PRICE_CLOSE,0 );
double var5=iRSI(Symbol(), PERIOD_D1, 10, PRICE_CLOSE,0 );
double var6=iRSI(Symbol(), PERIOD_W1, 10, PRICE_CLOSE,0 );

double OWHH = iCustom(Symbol(),PERIOD_D1,"Heiken Ashi",Red,White,Red,White,2,1); //Open WEEKLY
double CWHH = iCustom(Symbol(),PERIOD_D1,"Heiken Ashi",Red,White,Red,White,3,1); //Close WEEKLY
double ODHH = iCustom(Symbol(),PERIOD_D1,"Heiken Ashi",Red,White,Red,White,2,1); //Open daily
double CDHH = iCustom(Symbol(),PERIOD_D1,"Heiken Ashi",Red,White,Red,White,3,1); //Close daily
double O4HH = iCustom(Symbol(),PERIOD_H4,"Heiken Ashi",Red,White,Red,White,2,1); //Open4h
double C4HH = iCustom(Symbol(),PERIOD_H4,"Heiken Ashi",Red,White,Red,White,3,1); //Close 4h
double O1HH = iCustom(Symbol(),PERIOD_H1,"Heiken Ashi",Red,White,Red,White,2,1); //Open1H
double C1HH = iCustom(Symbol(),PERIOD_H1,"Heiken Ashi",Red,White,Red,White,3,1); //Close 1H


// Long
bool buy_condition_3 = iRSI(Symbol(), PERIOD_H1, 10, PRICE_CLOSE,0 )  >  50  ;
bool buy_condition_4 = iRSI(Symbol(), PERIOD_H4, 10, PRICE_CLOSE, 0)  >  50  ;
bool buy_condition_5 = iRSI(Symbol(), PERIOD_D1, 10, PRICE_CLOSE, 0)  >  50  ;
bool buy_condition_6 = iRSI(Symbol(), PERIOD_W1, 10, PRICE_CLOSE, 0)  >  50  ;


bool sell_condition_3 = iRSI(Symbol(), PERIOD_H1, 10, PRICE_CLOSE,0 )  <  50  ;
bool sell_condition_4 = iRSI(Symbol(), PERIOD_H4, 10, PRICE_CLOSE, 0)  <  50  ;
bool sell_condition_5 = iRSI(Symbol(), PERIOD_D1, 10, PRICE_CLOSE, 0)  <  50  ;
bool sell_condition_6 = iRSI(Symbol(), PERIOD_W1, 10, PRICE_CLOSE, 0)  <  50  ;

// Start function
void init()
{
lastbar=Time[1];
return(0);
}

int start()
        { 

//Variables    
static bool LastSignal=false;
bool Signal;

if (IsNewBar())
{  if(Signal!=LastSignal){
                          
if ((buy_condition_3  &&  buy_condition_4  &&  buy_condition_5  &&  buy_condition_6 && CWHH>OWHH && CDHH>ODHH && C4HH>O4HH && C1HH>O1HH) 
   {
                                 //MT4 SendMail function   
      bool res = SendMail("MT4: OrderSent", +"Instrument: " + Symbol()                  + "\n"
                                            +"RSI H1: " + string(var3)                  + "\n"
                                            +"RSI H4: " + string(var4)                  + "\n"
                                            +"RSI D1: " + string(var5)                  + "\n"
                                            +"RSI W1: " + string(var6)                  + "\n"
                                            +"Heiken Ashi W1C: " + string(CWHH)         + "\n"
                                            +"Heiken Ashi W1O: " + string(OWHH)         + "\n"
                                            +"Heiken Ashi D1C: " + string(CDHH)         + "\n"
                                            +"Heiken Ashi D1O: " + string(ODHH)         + "\n"
                                            +"Heiken Ashi 4HC: " + string(C4HH)         + "\n"
                                            +"Heiken Ashi 4HO: " + string(O4HH)         + "\n"
                                            +"Heiken Ashi 1HC: " + string(C1HH)         + "\n"
                                            +"Heiken Ashi 1HO: " + string(O1HH)         + "\n"
                                            );
       LastSignal=Signal;   
 
     
                                
  
                             }
else if (sell_condition_3 && sell_condition_4 && sell_condition_5 && sell_condition_6 && CWHH<OWHH && CDHH<ODHH && C4HH<O4HH && C1HH<O1HH){

 //MT4 SendMail function
           
      bool res = SendMail("MT4: OrderSent", +"Instrument: " + Symbol()                  + "\n"
                                            +"RSI H1: " + string(var3)                  + "\n"
                                            +"RSI H4: " + string(var4)                  + "\n"
                                            +"RSI D1: " + string(var5)                  + "\n"
                                            +"RSI W1: " + string(var6)                  + "\n"
                                            +"Heiken Ashi W1C: " + string(CWHH)         + "\n"
                                            +"Heiken Ashi W1O: " + string(OWHH)         + "\n"
                                            +"Heiken Ashi D1C: " + string(CDHH)         + "\n"
                                            +"Heiken Ashi D1O: " + string(ODHH)         + "\n"
                                            +"Heiken Ashi 4HC: " + string(C4HH)         + "\n"
                                            +"Heiken Ashi 4HO: " + string(O4HH)         + "\n"
                                            +"Heiken Ashi 1HC: " + string(C1HH)         + "\n"
                                            +"Heiken Ashi 1HO: " + string(O1HH)         + "\n"
                                            );

          LastSignal=Signal;  

}
}
        }
           
return(0);
                
        }

bool IsNewBar()
{
datetime curbar= Time[0];

if(lastbar!=curbar)
{
lastbar=curbar;
return(true);
}
return(false);
}

      


 
lemming78:

Many thanks guys.

However i discovered that ea isn't working at all. It sends me emails every hour but values are Always the same.... 

Would move from int start to onTick all the code fix that up?

 

Here's the updated code

 

 

You've already been given the answer in this thread

https://forum.mql4.com/66514#1006464

Why ask for advice and then ignore it when it is given? 

 

Sorry man, i just didn't see it.

 

Btw gonna check the other thread. I hope to solve. Many Thanks.

 

ok man, i took the advice from the other post.

New code. I'm out now so i can't test it.

There is something i should change to make it work perfectly?

 

Thanks

 

// External variables

extern int MagicNumber = 23310;
datetime lastbar;

// Start function
void init()
{
lastbar=Time[1];
return(0);
}

int start()
        { 

//Variables    
static bool LastSignal=false;
bool Signal;

double Rsi1=iRSI(Symbol(), PERIOD_H1, 10, PRICE_CLOSE,0 );
double Rsi4=iRSI(Symbol(), PERIOD_H4, 10, PRICE_CLOSE,0 );
double RsiD=iRSI(Symbol(), PERIOD_D1, 10, PRICE_CLOSE,0 );
double RsiW=iRSI(Symbol(), PERIOD_W1, 10, PRICE_CLOSE,0 );

double OWHH = iCustom(Symbol(),PERIOD_D1,"Heiken Ashi",Red,White,Red,White,2,1); //Open WEEKLY
double CWHH = iCustom(Symbol(),PERIOD_D1,"Heiken Ashi",Red,White,Red,White,3,1); //Close WEEKLY
double ODHH = iCustom(Symbol(),PERIOD_D1,"Heiken Ashi",Red,White,Red,White,2,1); //Open daily
double CDHH = iCustom(Symbol(),PERIOD_D1,"Heiken Ashi",Red,White,Red,White,3,1); //Close daily
double O4HH = iCustom(Symbol(),PERIOD_H4,"Heiken Ashi",Red,White,Red,White,2,1); //Open4h
double C4HH = iCustom(Symbol(),PERIOD_H4,"Heiken Ashi",Red,White,Red,White,3,1); //Close 4h
double O1HH = iCustom(Symbol(),PERIOD_H1,"Heiken Ashi",Red,White,Red,White,2,1); //Open1H
double C1HH = iCustom(Symbol(),PERIOD_H1,"Heiken Ashi",Red,White,Red,White,3,1); //Close 1H

//Main Code

if (IsNewBar())   // NewBar yes or not
{  if(Signal!=LastSignal){  //if signal is different from the previous one
                          
if ((Rsi1>50 && Rsi4>50 && RsiD>50 && RsiW>50 && CWHH>OWHH && CDHH>ODHH && C4HH>O4HH && C1HH>O1HH) 
   {
                                 //MT4 SendMail function   
      bool res = SendMail("MT4: OrderSent", +"Instrument: " + Symbol()                  + "\n"
                                            +"RSI H1: " + string(var3)                  + "\n"
                                            +"RSI H4: " + string(var4)                  + "\n"
                                            +"RSI D1: " + string(var5)                  + "\n"
                                            +"RSI W1: " + string(var6)                  + "\n"
                                            +"Heiken Ashi W1C: " + string(CWHH)         + "\n"
                                            +"Heiken Ashi W1O: " + string(OWHH)         + "\n"
                                            +"Heiken Ashi D1C: " + string(CDHH)         + "\n"
                                            +"Heiken Ashi D1O: " + string(ODHH)         + "\n"
                                            +"Heiken Ashi 4HC: " + string(C4HH)         + "\n"
                                            +"Heiken Ashi 4HO: " + string(O4HH)         + "\n"
                                            +"Heiken Ashi 1HC: " + string(C1HH)         + "\n"
                                            +"Heiken Ashi 1HO: " + string(O1HH)         + "\n"
                                            );
       LastSignal=Signal;   
 
     
                                
  
                             }
else if (Rsi1<50 && Rsi4<50 && RsiD<50 && RsiW<50 && CWHH<OWHH && CDHH<ODHH && C4HH<O4HH && C1HH<O1HH){

 //MT4 SendMail function
           
      bool res = SendMail("MT4: OrderSent", +"Instrument: " + Symbol()                  + "\n"
                                            +"RSI H1: " + string(var3)                  + "\n"
                                            +"RSI H4: " + string(var4)                  + "\n"
                                            +"RSI D1: " + string(var5)                  + "\n"
                                            +"RSI W1: " + string(var6)                  + "\n"
                                            +"Heiken Ashi W1C: " + string(CWHH)         + "\n"
                                            +"Heiken Ashi W1O: " + string(OWHH)         + "\n"
                                            +"Heiken Ashi D1C: " + string(CDHH)         + "\n"
                                            +"Heiken Ashi D1O: " + string(ODHH)         + "\n"
                                            +"Heiken Ashi 4HC: " + string(C4HH)         + "\n"
                                            +"Heiken Ashi 4HO: " + string(O4HH)         + "\n"
                                            +"Heiken Ashi 1HC: " + string(C1HH)         + "\n"
                                            +"Heiken Ashi 1HO: " + string(O1HH)         + "\n"
                                            );

          LastSignal=Signal;  

}
}
        }
           
return(0);
                
        }

bool IsNewBar()
{
datetime curbar= Time[0];

if(lastbar!=curbar)
{
lastbar=curbar;
return(true);
}
return(false);
}
 
lemming78: i don't want a second/third/fourth signal until the conditions present again.
Then Don't check for just a level, check for a change in conditions. Like honest_knave's post or
static bool signalCurr=false; signalPrev = signalCurr;
signalCurr=/*signal logic*/;
if(!signalPrev && signalCurr) { /*new signal*/ };
 

To be honest, I can't work out what you are trying to do.

Why is Signal a bool ?

Where is Signal assigned a value?

You have bool res, but what do you do with it? 

 
Signal has no value.
static bool LastSignal=false;
bool Signal;
                          
if ((Rsi1>50 && Rsi4>50 && RsiD>50 && RsiW>50 && CWHH>OWHH && CDHH>ODHH && C4HH>O4HH && C1HH>O1HH) 
   {
        LastSignal=Signal;   
If you want to alternate
enum signals = {Buy, Sell, NA};
static signals LastSignal=NA;
                          
if ((Rsi1>50 && Rsi4>50 && RsiD>50 && RsiW>50 && CWHH>OWHH && CDHH>ODHH && C4HH>O4HH && C1HH>O1HH
&& LastSignal != Buy) 
   {
        LastSignal=Buy;
If you don't want to alternate, see my previous post. You'll need a static for each direction.
Reason: