Download MetaTrader 5

Alert Problems

To add comments, please log in or register
Stephen Reynolds
8710
Stephen Reynolds  

Why when I use this code to sound an alert it behaves okay and sounds only when the statement is true.

#property indicator_chart_window   

// External Input Variables 
extern string SECTION_A  = "*** Arrow Color Settings ***";
input string SoundFile = "alert.wav"; 
input bool playsound = false; // Play Sound
input bool alert = true;    // Show Alert Box
input bool sendmail = false; // Send eMail
input bool sendnotification = false;  // Send Mobile

//+------------------------------------------------------------------
// Start Event Handler                               
//+------------------------------------------------------------------

int start()
{     
   int counted_bars = IndicatorCounted(); 
   if(counted_bars>0) counted_bars--; 
   int limit=Bars-counted_bars-2;   
     
   // The Basic Code (Uses a comparison of current Datetime to a static Datetime. 
   static datetime TimeStamp;

   // Bar Count
   static int count = 0;
   static datetime timeCur; 
   datetime timePre = timeCur; 
   timeCur=Time[0];
   bool isNewBar = timeCur != timePre;
   
   if(isNewBar) count++; 
   
   // Main Statement
   if(count > 1 && TimeStamp != Time[0])
   {      
      if(playsound) PlaySound(SoundFile);
      if(alert) Alert(_Symbol + IntegerToString(_Period) + "Signal", "Signal", MB_OK);         
      if(sendmail) SendMail(_Symbol + IntegerToString(_Period) + "Signal", "Signal " + OrderSymbol());
      if(sendnotification) SendNotification(_Symbol + IntegerToString(_Period) + "Signal " + OrderSymbol());

      TimeStamp = Time[0];
      count = 0;
    
   }

   return(0);
}


But when I use this 2nd code it displays an alert on chart as soon as its placed onto chart?

#property strict
#property indicator_chart_window

#property indicator_separate_window  
#property indicator_buffers 6      
#property indicator_level1 0

#define arrowsDisplacement 0.00005  

//---- input parameters
extern string SECTION_A = "*** MACD Settings ***";  
input int FastMAPeriod = 12;                 
input int SlowMAPeriod = 26;
input int SignalMAPeriod = 9;

extern string SECTION_C  = "*** Arrow Color Settings ***";
input string SoundFile = "alert.wav"; 
input bool playsound = true; // Play Sound
input bool alert = false;    // Show Alert Box
input bool sendmail = false; // Send eMail
input bool sendnotification = false;  // Send Mobile

//---- buffers
double MACDLineBuffer[];
double MACDLineBufferHi[];
double MACDLineBufferLo[];
double SignalLineBuffer[];
double HistogramBuffer[];
double bearishDivergence[];

//---- variables
double alpha=0;
double alpha_1=0;

//+------------------------------------------------------------------+
// Indicator initialization function   
//+------------------------------------------------------------------+
int init() // Initialising all settings below on start up
  {
   IndicatorDigits(Digits+1); 

   SetIndexStyle(0,DRAW_NONE); 
   SetIndexBuffer(0,MACDLineBuffer); 
   SetIndexDrawBegin(0,SlowMAPeriod); 

   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,2,clrRed); 
   SetIndexBuffer(1,MACDLineBufferHi); 

   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,2,clrBlue); 
   SetIndexBuffer(2,MACDLineBufferLo); 

   SetIndexStyle(3,DRAW_NONE); 
   SetIndexBuffer(3,SignalLineBuffer); 
   SetIndexDrawBegin(3,SlowMAPeriod+SignalMAPeriod);

   SetIndexStyle(4,DRAW_NONE); 
   SetIndexBuffer(4,HistogramBuffer); 
   SetIndexDrawBegin(4,SlowMAPeriod+SignalMAPeriod); 
 
   SetIndexStyle(5,DRAW_ARROW,STYLE_SOLID,2,clrBlue);
   SetIndexArrow(5,234); 
   SetIndexBuffer(5,bearishDivergence); 
   
      
   alpha=2.0/(SignalMAPeriod+1.0); 
   alpha_1=1.0-alpha;  

//----
   return(0);
  }
  
//+------------------------------------------------------------------
// Start Event Handler                               
//+------------------------------------------------------------------

int start()
{  
   int counted_bars = IndicatorCounted(); 
   if(counted_bars>0) counted_bars--; 
   int limit=Bars-counted_bars-2;   
   if(counted_bars==0) limit-=1+5;   
   
   for(int i=limit; i>=1; i--)
   { 
      MACDLineBuffer[i]=iMA(NULL,0,FastMAPeriod,0,MODE_EMA,PRICE_CLOSE,i) -
                        iMA(NULL,0,SlowMAPeriod,0,MODE_EMA,PRICE_CLOSE,i);
      SignalLineBuffer[i]= alpha*MACDLineBuffer[i]+alpha_1*SignalLineBuffer[i+1];
      HistogramBuffer[i] = MACDLineBuffer[i]-SignalLineBuffer[i];

      if(MACDLineBuffer[i] > MACDLineBuffer[i+5])
        MACDLineBufferHi[i] = MACDLineBuffer[i];
      else
        MACDLineBufferLo[i] = MACDLineBuffer[i];
         
      CatchBearishDivergence(i);                
   }
         
   return(0);
}

void CatchBearishDivergence(int shift) 
{   
   if(IsIndicatorPeak(shift)==false)
      return; 
   int currentPeak=shift; 
   double arrdist = 15 * _Point;
   static datetime TimeStamp;
   
   if(MACDLineBuffer[currentPeak])
   {
      bearishDivergence[currentPeak]=MACDLineBuffer[currentPeak]+arrdist;
   }
       
   if(IsIndicatorPeak(shift) && TimeStamp != Time[0])   
   { 
      if(playsound) PlaySound(SoundFile);
      if(alert) Alert(_Symbol + IntegerToString(_Period) + "MACD Bear Convergence");        
      if(sendmail) SendMail(_Symbol + IntegerToString(_Period) + "MACD Bear Convergence", "MACD Bear Convergence " + OrderSymbol());
      if(sendnotification) SendNotification(_Symbol + IntegerToString(_Period) + "MACD Bear Convergence " + OrderSymbol());
      
      TimeStamp = Time[0];
       
   }  
     
} 

 //+------------------------------------------------------------------+
// Current Peak
//+------------------------------------------------------------------+
bool IsIndicatorPeak(int shift) 
{ 
   if(MACDLineBuffer[shift+1]>=MACDLineBuffer[shift+2] && MACDLineBuffer[shift]>MACDLineBuffer[shift+3]  
      && MACDLineBuffer[shift+1]>MACDLineBuffer[shift]) 
      return(true);
   else
      return(false);
} 
Naguisa Unada
118
Naguisa Unada  
 //if (IsIndicatorPeak(shift) && TimeStamp != Time [ 0 ])    
 if (shift==1 && IsIndicatorPeak(shift) && TimeStamp != Time [ 0 ])   
{ 
   .........
}

Since these alerts were issued in response to past data, I corrected it to sound only with the latest confirmed candle.

Stephen Reynolds
8710
Stephen Reynolds  
Naguisa Unada:

Since the alerts were issued in response to past data, I corrected it to sound only with the latest confirmed candle.


This works!!! thanks 😃

To add comments, please log in or register