Download MetaTrader 5

How to make the alert wait for 5 sec

To add comments, please log in or register
Saravanakumar D
360
Saravanakumar D  

Dear Friends. I got a code alert Horizontal line. but i could not make it as wait for 5 seconds after each and every alert..


Here is the code.

Kindly some one help me.. Thanks in advance


#property copyright "Metaquotes"
#property link      "mql4.com"

#property indicator_chart_window

extern double SoundWhenPriceGoesAbove = 0;
extern double SoundWhenPriceGoesBelow = 0;
//extern double SoundWhenPriceIsExactly = 0;
//extern bool SendEmail = false;

int init() 
{
   //if (SoundWhenPriceIsExactly > 0)
   //{
      //ObjectCreate("SoundWhenPriceIsExactly", OBJ_HLINE, 0, Time[0], SoundWhenPriceIsExactly);
      //ObjectSet("SoundWhenPriceIsExactly", OBJPROP_STYLE, STYLE_SOLID);
      //ObjectSet("SoundWhenPriceIsExactly", OBJPROP_COLOR, Yellow);
      //ObjectSet("SoundWhenPriceIsExactly", OBJPROP_WIDTH, 1);
   //}
   if (SoundWhenPriceGoesAbove > 0)
   {
      ObjectCreate("SoundWhenPriceGoesAbove", OBJ_HLINE, 0, Time[0], SoundWhenPriceGoesAbove);
      ObjectSet("SoundWhenPriceGoesAbove", OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet("SoundWhenPriceGoesAbove", OBJPROP_COLOR, Magenta);
      ObjectSet("SoundWhenPriceGoesAbove", OBJPROP_WIDTH, 1);
   }
   if (SoundWhenPriceGoesBelow > 0)
   {
      ObjectCreate("SoundWhenPriceGoesBelow", OBJ_HLINE, 0, Time[0], SoundWhenPriceGoesBelow);
      ObjectSet("SoundWhenPriceGoesBelow", OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet("SoundWhenPriceGoesBelow", OBJPROP_COLOR, Magenta);
      ObjectSet("SoundWhenPriceGoesBelow", OBJPROP_WIDTH, 1);
   }
   return(0);
}


int deinit()
{
   //ObjectDelete("SoundWhenPriceIsExactly");
   //ObjectDelete("PriceUpLevel");
   //ObjectDelete("PriceDownLevel");
   //ObjectDelete("PriceExactLevel");
   ObjectDelete("SoundWhenPriceGoesAbove");
   ObjectDelete("SoundWhenPriceGoesBelow");
   return(0);
}

int start()
{

   if (ObjectGet("SoundWhenPriceGoesAbove", 1) != SoundWhenPriceGoesAbove)
     SoundWhenPriceGoesAbove = ObjectGet("SoundWhenPriceGoesAbove", 1);
   if (ObjectGet("SoundWhenPriceGoesBelow", 1) != SoundWhenPriceGoesBelow)
     SoundWhenPriceGoesBelow = ObjectGet("SoundWhenPriceGoesBelow", 1);
   //if (ObjectGet("SoundWhenPriceIsExactly", 1) != SoundWhenPriceIsExactly)
     //SoundWhenPriceIsExactly = ObjectGet("SoundWhenPriceIsExactly", 1);


   if ((Bid > SoundWhenPriceGoesAbove) && (SoundWhenPriceGoesAbove > 0))
   {
      PlaySound("alert.wav");
      //Alert(Symbol(),Period(),"       Price above ",SoundWhenPriceGoesAbove);
      //ObjectCreate("PriceUpLevel", OBJ_HLINE, 0, Time[0], SoundWhenPriceGoesAbove);
      //ObjectSet("PriceUpLevel", OBJPROP_STYLE, STYLE_SOLID);
      //ObjectSet("PriceUpLevel", OBJPROP_COLOR, Magenta);
      //ObjectSet("PriceUpLevel", OBJPROP_WIDTH, 1);
      //SendMail("Price for " + Symbol() +  " above the alert level " + Bid, "Price for " + Symbol() +  " reached " + Bid + " level, which is above your alert level of " + SoundWhenPriceGoesAbove);
      //ObjectDelete("SoundWhenPriceGoesAbove");
      //SoundWhenPriceGoesAbove = 0;
   }
   if ((Bid < SoundWhenPriceGoesBelow) && (SoundWhenPriceGoesBelow > 0))
   {
      PlaySound("alert.wav");
      //Alert(Symbol(),Period(),"       Price Below ",SoundWhenPriceGoesBelow);
      //ObjectCreate("PriceDownLevel", OBJ_HLINE, 0, Time[0], SoundWhenPriceGoesBelow);
      //ObjectSet("PriceDownLevel", OBJPROP_STYLE, STYLE_SOLID);
      //ObjectSet("PriceDownLevel", OBJPROP_COLOR, Magenta);
      //ObjectSet("PriceDownLevel", OBJPROP_WIDTH, 1);
      //SendMail("Price for " + Symbol() +  " below the alert level " + Bid, "Price for " + Symbol() +  " reached " + Bid + " level, which is below your alert level of " + SoundWhenPriceGoesBelow);
      //ObjectDelete("SoundWhenPriceGoesBelow");
      //SoundWhenPriceGoesBelow = 0;
   }
   //if (Bid == SoundWhenPriceIsExactly)
   //{
      //Alert(Symbol(),Period(),"       Price is Exactly ",SoundWhenPriceIsExactly);
      //ObjectCreate("PriceExactLevel", OBJ_HLINE, 0, Time[0], SoundWhenPriceIsExactly);
      //ObjectSet("PriceExactLevel", OBJPROP_STYLE, STYLE_SOLID);
      //ObjectSet("PriceExactLevel", OBJPROP_COLOR, Yellow);
      //ObjectSet("PriceExactLevel", OBJPROP_WIDTH, 1);
      //SendMail("Price for " + Symbol() +  " exactly at the alert level " + Bid, "Price for " + Symbol() +  " reached " + Bid + "/" + Bid + " level, which is exactly your alert level of " + SoundWhenPriceIsExactly);
      //ObjectDelete("SoundWhenPriceIsExactly");
      //SoundWhenPriceIsExactly = 0;
   //}
   return(0);
}

qjol
3243
qjol  
honest_knave
Moderator
2336
honest_knave  

If you're using an indicator, sleep won't work.

Instead, you need to store the time the previous alert fired and then check how long has elapsed since then.

Frenchytrader
389
Frenchytrader  

Here an example:

datetime Alert_1;  
if ((Bid > SoundWhenPriceGoesAbove) && (SoundWhenPriceGoesAbove > 0 && TimeCurrent() > Alert_1))
   {
      PlaySound("alert.wav");
      //Alert(Symbol(),Period(),"       Price above ",SoundWhenPriceGoesAbove);
      //ObjectCreate("PriceUpLevel", OBJ_HLINE, 0, Time[0], SoundWhenPriceGoesAbove);
      //ObjectSet("PriceUpLevel", OBJPROP_STYLE, STYLE_SOLID);
      //ObjectSet("PriceUpLevel", OBJPROP_COLOR, Magenta);
      //ObjectSet("PriceUpLevel", OBJPROP_WIDTH, 1);
      //SendMail("Price for " + Symbol() +  " above the alert level " + Bid, "Price for " + Symbol() +  " reached " + Bid + " level, which is above your alert level of " + SoundWhenPriceGoesAbove);
      //ObjectDelete("SoundWhenPriceGoesAbove");
      //SoundWhenPriceGoesAbove = 0;

Alert_1 = TimeCurrent() + 5; // 5 = 5 seconds delayed

   }
whroeder1
15562
whroeder1  
d.saravana21: #property indicator_chart_window
qjol: Sleep
honest_knave: If you're using an indicator, sleep won't work.

Definitely an indicator.

  1. Use Frenchy's approach (for a repeating signal while market above level.) Or
  2. Don't check for levels:
    if ((Bid > SoundWhenPriceGoesAbove) && (SoundWhenPriceGoesAbove > 0))
    Check for a change in conditions.
    static bool isCondition = false; bool wasCondition = isCondition;
    isCondition = (Bid > SoundWhenPriceGoesAbove && SoundWhenPriceGoesAbove > 0);
    if (!wasCondition && isCondition) ...
    Will alert one time each time market moves up through level.
    Or disable the level after the alert
    if ((Bid > SoundWhenPriceGoesAbove) && (SoundWhenPriceGoesAbove > 0)){
       SoundWhenPriceGoesAbove = 0.0; // One Alert
    One alert only
To add comments, please log in or register