Download MetaTrader 5

How to Stop repeatative alert pop-up

To add comments, please log in or register
rajtradingfx
22
rajtradingfx  

 Hello Everyone,

 I tried to modify Stochastic,  to pop-up alert  only one time once indicator reach to over-brought or Over-Sold Level. But again getting same error, 


//+------------------------------------------------------------------+
//|                                                   Stochastic.mq5 |
//|                   Copyright 2009-2017, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009-2017, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots   2
#property indicator_type1   DRAW_LINE
#property indicator_type2   DRAW_LINE
#property indicator_color1  LightSeaGreen
#property indicator_color2  Red
#property indicator_style2  STYLE_DOT
//--- input parameters
input int InpKPeriod=5;  // K period
input int InpDPeriod=3;  // D period
input int InpSlowing=3;  // Slowing
//--- indicator buffers


datetime Time[];
int count = 10;


double    ExtMainBuffer[];
double    ExtSignalBuffer[];
double    ExtHighesBuffer[];
double    ExtLowesBuffer[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
 
  //Auto Adjustment for broker digits
 
 
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtMainBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtSignalBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,ExtHighesBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,ExtLowesBuffer,INDICATOR_CALCULATIONS);
//--- set accuracy
   IndicatorSetInteger(INDICATOR_DIGITS,2);
//--- set levels
   IndicatorSetInteger(INDICATOR_LEVELS,2);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,20);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,80);
//--- set maximum and minimum for subwindow
   IndicatorSetDouble(INDICATOR_MINIMUM,0);
   IndicatorSetDouble(INDICATOR_MAXIMUM,100);
//--- name for DataWindow and indicator subwindow label
   IndicatorSetString(INDICATOR_SHORTNAME,"Stoch("+(string)InpKPeriod+","+(string)InpDPeriod+","+(string)InpSlowing+")");
   PlotIndexSetString(0,PLOT_LABEL,"Main");
   PlotIndexSetString(1,PLOT_LABEL,"Signal");
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpKPeriod+InpSlowing-2);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpKPeriod+InpDPeriod);
  
   ArraySetAsSeries(Time,true);
   CopyTime(_Symbol,_Period,0,count,Time);
  
  
  
//--- initialization done
  }
//+------------------------------------------------------------------+
//| Stochastic Oscillator                                            |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                 const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   int i,k,start;
//--- check for bars count
   if(rates_total<=InpKPeriod+InpDPeriod+InpSlowing)
      return(0);
//---
   start=InpKPeriod-1;
   if(start+1<prev_calculated) start=prev_calculated-2;
   else
     {
      for(i=0;i<start;i++)
        {
         ExtLowesBuffer[i]=0.0;
         ExtHighesBuffer[i]=0.0;
        }
     }
//--- calculate HighesBuffer[] and ExtHighesBuffer[]
   for(i=start;i<rates_total && !IsStopped();i++)
     {
      double dmin=1000000.0;
      double dmax=-1000000.0;
      for(k=i-InpKPeriod+1;k<=i;k++)
        {
         if(dmin>low[k])  dmin=low[k];
         if(dmax<high[k]) dmax=high[k];
        }
      ExtLowesBuffer[i]=dmin;
      ExtHighesBuffer[i]=dmax;
     }
//--- %K
   start=InpKPeriod-1+InpSlowing-1;
   if(start+1<prev_calculated) start=prev_calculated-2;
   else
     {
      for(i=0;i<start;i++) ExtMainBuffer[i]=0.0;
     }
//--- main cycle
   for(i=start;i<rates_total && !IsStopped();i++)
     {
      double sumlow=0.0;
      double sumhigh=0.0;
      for(k=(i-InpSlowing+1);k<=i;k++)
        {
         sumlow +=(close[k]-ExtLowesBuffer[k]);
         sumhigh+=(ExtHighesBuffer[k]-ExtLowesBuffer[k]);
        }
      if(sumhigh==0.0) ExtMainBuffer[i]=100.0;
      else             ExtMainBuffer[i]=sumlow/sumhigh*100;
     
 
 
   {
     if( Time[0] != Time[1])
    
       
    {
   
     if(ExtMainBuffer[i] > 79.99 && ExtMainBuffer[i] < 81.00 )
      { Alert( _Symbol, "  "+ "Over-brought");
       CopyTime(_Symbol,_Period,0,count,Time);}
   
      else{
      if(ExtMainBuffer[i] < 21.00 && ExtMainBuffer[i] > 19.99 )
      { Alert(_Symbol,"  "+"Over-Sold");
        CopyTime(_Symbol,_Period,0,count,Time); }
      }
    }
   
     }
     }
//--- signal
   start=InpDPeriod-1;
   if(start+1<prev_calculated) start=prev_calculated-2;
   else
     {
      for(i=0;i<start;i++) ExtSignalBuffer[i]=0.0;
     }
   for(i=start;i<rates_total && !IsStopped();i++)
     {
      double sum=0.0;
      for(k=0;k<InpDPeriod;k++) sum+=ExtMainBuffer[i-k];
      ExtSignalBuffer[i]=sum/InpDPeriod;
           
     }
//--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
  }
//+------------------------------------------------------------------+
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
Ask questions on technical analysis, discuss trading systems and improve your MQL5 programming skills to develop your own trading strategies. Communicate and share your experience with traders from anywhere in the world, answer questions and help beginners — MQL5.community is developing along with you. USD/JPY Analysis Well, the asset moves...
Eleni Anna Branou
Moderator
5658
Eleni Anna Branou  

Please use the </> button to insert your code.


Artur Zas
5881
Artur Zas  
rajtradingfx:

 Hello Everyone,

 I tried to modify Stochastic to pop-up only alert one time at over-brought or Over-Sold Level. But again getting same error, 

Hi,

   My suggestion is to move the alert code into a separate function. Something like:

void AlertMe(datetime barOpenTime, string msg)
{
   static datetime prevAlertTime = TimeCurrent();
   
   if(barOpenTime <= prevAlertTime)
      return;
   
   prevAlertTime = barOpenTime;
   Alert(_Symbol,"  "+msg);
}


Next, substitute the  "main cycle" code block with the following:

//--- main cycle
   for(i=start;i<rates_total && !IsStopped();i++)
   {
      double sumlow=0.0;
      double sumhigh=0.0;

      for(k=(i-InpSlowing+1);k<=i;k++)
      {
         sumlow +=(close[k]-ExtLowesBuffer[k]);
         sumhigh+=(ExtHighesBuffer[k]-ExtLowesBuffer[k]);
      }

      if(sumhigh==0.0) ExtMainBuffer[i]=100.0;
      else             ExtMainBuffer[i]=sumlow/sumhigh*100;    
   
      //
      // Alert code
      // 
      
      if(ExtMainBuffer[i] > 79.99 && ExtMainBuffer[i] < 81.00 )
         AlertMe(time[i],"overbought");
      else if(ExtMainBuffer[i] < 21.00 && ExtMainBuffer[i] > 19.99 )
         AlertMe(time[i],"oversold");   
   }
rajtradingfx
22
rajtradingfx  
Eleni Anna Branou:

Please use the </> button to insert your code.


Thank You,  I had updated Like that.
rajtradingfx
22
rajtradingfx  
Artur Zas:

Hi,

   My suggestion is to move the alert code into a separate function. Something like:


Next, substitute the  "main cycle" code block with the following:

Hii Mr. Artur Zas

Thank You so much for your reply I will check once,

rajtradingfx
22
rajtradingfx  
rajtradingfx:

Hii Mr. Artur Zas

Thank You so much for your reply I will check once,

Artur Zas:

Hi,

   My suggestion is to move the alert code into a separate function. Something like:


Next, substitute the  "main cycle" code block with the following:

     Hii Mr. Artur Zas,

    I wonder still It is not working, I had did the same

Files:
Artur Zas
5881
Artur Zas  
rajtradingfx:

     Hii Mr. Artur Zas,

    I wonder still It is not working, I had did the same

Please check the attached version. It seems to work fine on my end.

Files:
ttttttt.mq5 6 kb
rajtradingfx
22
rajtradingfx  
Artur Zas:

Please check the attached version. It seems to work fine on my end.

It"s working Mr. Artur Zas, Thank you so much
To add comments, please log in or register