alert when price bar crossing specific price line

 

Hi!

i found a indicator script on the net which suit for me .

this indicator draw and alert when crossing specific above and below line.

But one lack of function is when break out line it alert but there is no limit bar .

i want to set 5 bars so if i set 5 bars i want to receive alert in 5 bars only if over 5 bar like 6 , 7 ...

i just want to skip and ignore this alert.

i checked this script and found Close[0] is ok when check current price crossing above or below line but if i limit to 5 bars then it make some difficult for me.

i thought to use iTime function .

for instance if i want 5 bar limits then i thought i can make it with

iTime(_Symbol, 0, 0) + timeoffset > LastTime

so if timeframe is 5min and 5 bars is 25min so i set timeoffset as 1500

but it not working.

i tested with different value but not working .

//+------------------------------------------------------------------+
//|                                        JC - Line Break Alert.mq4 |
//|                                         Juan Carlos Christensen. |
//|                                     http://www.eumefinancial.com |
//+------------------------------------------------------------------+
#property copyright "Juan Carlos Christensen."
#property link      "http://www.eumefinancial.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_width1 2
#property indicator_width2 2

//---- input parameters
extern double Price1 = 1.25000;
extern double Price2 = 1.24000;
extern string Modes = "0- Line; 1- Channel";
extern int Mode = 1;
extern bool UseSoundAlert = True;
extern string SoundFile = "alert.wav";
extern int PlaySoundNTimes = 5;
extern bool UseAlertPopUp = True;
extern bool UseTickMode = True;
extern bool UseSendMail = False;
extern bool DeactivateOnAlert = False;
string PairWav = "gbpusd.wav";
string PeriodWave = "5min.wav";
string DirectionWavUp = "breakingup.wav";
string DirectionWavDown = "breakingdown.wav";
extern int sleeptime = 4000;

double PRICE1[];
double PRICE2[];
bool DidBreakout = False;

extern int timeoffset = 60; // i added this 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  //---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,PRICE1);
   
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,PRICE2);
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectDelete("Price1_0");
   ObjectDelete("Price1_1");
   ObjectDelete("Price1_2");
   ObjectDelete("Price1_3");
   ObjectDelete("Price1_4");
   ObjectDelete("Price1_5");
   ObjectDelete("Price1_6");
   ObjectDelete("Price1_7");
   ObjectDelete("Price1_8");
   ObjectDelete("Price1_9");
   ObjectDelete("Price1_10");
   ObjectDelete("Price1_11");
   ObjectDelete("Price1_12");
   ObjectDelete("Price1_13");
                  
   ObjectDelete("PRICE1_0");
   ObjectDelete("PRICE1_1");
   ObjectDelete("PRICE1_2");
   ObjectDelete("PRICE1_3");      
   ObjectDelete("PRICE1_4");
   ObjectDelete("PRICE1_5");
   ObjectDelete("PRICE1_6");
   ObjectDelete("PRICE1_7");      
   ObjectDelete("PRICE1_8");
   ObjectDelete("PRICE1_9");
   ObjectDelete("PRICE1_10");
   ObjectDelete("PRICE1_11");      
   ObjectDelete("PRICE1_12");   
   ObjectDelete("PRICE1_13");
      
   ObjectDelete("Price2_0");
   ObjectDelete("Price2_1");   
   ObjectDelete("Price2_2");
   ObjectDelete("Price2_3");
   ObjectDelete("Price2_4");   
   ObjectDelete("Price2_5");
   ObjectDelete("Price2_6");
   ObjectDelete("Price2_7");
   ObjectDelete("Price2_8");   
   ObjectDelete("Price2_9");
   ObjectDelete("Price2_10");
   ObjectDelete("Price2_11");   
   ObjectDelete("Price2_12");
   ObjectDelete("Price2_13");   
                  
   ObjectDelete("PRICE2_0");
   ObjectDelete("PRICE2_1");   
   ObjectDelete("PRICE2_3");
   ObjectDelete("PRICE2_4");
   ObjectDelete("PRICE2_5");
   ObjectDelete("PRICE2_6");      
   ObjectDelete("PRICE2_7");
   ObjectDelete("PRICE2_8");   
   ObjectDelete("PRICE2_9");
   ObjectDelete("PRICE2_10");
   ObjectDelete("PRICE2_11");
   ObjectDelete("PRICE2_12");
   ObjectDelete("PRICE2_13");     
   
   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----

   switch(Mode)
   {
      case 0:
         CrossingPrice1Only();
         break;
      case 1:
         ChannelBreakOut();
         break;
      default:
         CrossingPrice1Only();
   }
   

//----
   return(0);
  }
//+------------------------------------------------------------------+

void CrossingPrice1Only()
{
   int counted_bars =IndicatorCounted();
   int pos =Bars-counted_bars;
   
   double LastPrice;
   double CurrentPrice;
   int x;
   
   while(pos>=0)
   {
      PRICE1[pos]= Price1;
      
      pos--;
   }
   
   if(UseTickMode)
   {
      LastPrice = iClose(NULL, 0, 1);
      CurrentPrice = iClose(NULL, 0, 0);
   }
   else if(UseTickMode == False)
   {
      LastPrice = iOpen(NULL, 0, 1);
      CurrentPrice = iOpen(NULL, 0, 0);
   }

   int counter = 0;
   
   while(!ObjectCreate("Price1_" + counter, OBJ_HLINE, 0, 0, Price1) && counter < 100)
   {
      counter++;
   }
   
   ObjectSet("Price1", OBJPROP_COLOR, Green);
   
   if((((LastPrice < Price1) && (CurrentPrice >= Price1)) || ((LastPrice > Price1) && (CurrentPrice <= Price1))) && !DidBreakout) 
   {
      if(DeactivateOnAlert)
      {
         DidBreakout = True;
      }
      
      if(UseSoundAlert)
      {
         for(x=0;x<=PlaySoundNTimes;x++)
         {
            PlaySound(PairWav);
            PlaySound(PeriodWave);
            PlaySound(DirectionWavUp);
         }
      }
      
      if(UseAlertPopUp)
      {
         Alert(Symbol()," ",Period(),"Price Crossing Alert Line at ", Price1);   //Symbol()," ",Period()," @ ",Bid
      }
      
      if(UseSendMail)
      {
         SendMail("Price Crossing Alert Line at " + Price1 + "!", "This is an email from the JC - Line Break Alert indicator. Price is crossing your alert line at " + Price1 + "!");
      }
   }
}

//+------------------------------------------------------------------+

void ChannelBreakOut()
{
   int counted_bars = IndicatorCounted();
   int pos = Bars-counted_bars;
   
   double LastPrice;
   double CurrentPrice;
   int x;
   
   while(pos>=0)
   {
      PRICE1[pos]= Price1;
      PRICE2[pos]= Price2;
      
      pos--;
   }
   
   if(UseTickMode)
   {
      LastPrice = iClose(NULL, 0, 1);
      CurrentPrice = iClose(NULL, 0, 0);
   }
   else if(UseTickMode == False)
   {
      LastPrice = iOpen(NULL, 0, 1);
      CurrentPrice = iOpen(NULL, 0, 0);
   }

   int counter = 0;
   
   while(!ObjectCreate("Price1_" + counter, OBJ_HLINE, 0, 0, Price1) && counter < 100)
   {
      counter++;
   }
   
   while((!ObjectCreate("Price2_" + counter, OBJ_HLINE, 0, 0, Price2)) && counter < 100)
   {
      counter++;
   }
   
   ObjectSet("Price1", OBJPROP_COLOR, Green);
   ObjectSet("Price2", OBJPROP_COLOR, Red);
   
   if((LastPrice < Price1) && (CurrentPrice >= Price1) && !DidBreakout) 
   {
      if(DeactivateOnAlert)
      {
         DidBreakout = True;
      }
      
      if(UseSoundAlert)
      {
         for(x=0;x<=PlaySoundNTimes;x++)
         {
            Sleep(sleeptime);
            PlaySound(PairWav);
            Sleep(sleeptime);
            PlaySound(PeriodWave);
            Sleep(sleeptime);
            PlaySound(DirectionWavUp);
         }
      }
      
      if(UseAlertPopUp)
      {
         Alert(Symbol()," ",Period()," Price Breaking Up at ", Price1);
      }
      
      if(UseSendMail)
      {
         SendMail("Channel Breaking Up at " + Price1 + "!", "This is an email from the JC - Line Break Alert indicator. Price broke up your channel alert line at " + Price1 + "!");
      }
   }
   
   if((LastPrice > Price2) && (CurrentPrice <= Price2) && !DidBreakout)
   {
      if(DeactivateOnAlert)
      {
      DidBreakout = True;
      }
      
      if(UseSoundAlert)
      {
         for(x=0;x<=PlaySoundNTimes;x++)
         {
            Sleep(sleeptime);
            PlaySound(PairWav);
            Sleep(sleeptime);
            PlaySound(PeriodWave);
            Sleep(sleeptime);
            PlaySound(DirectionWavDown);
         }
      }
      
      if(UseAlertPopUp)
      {
         Alert(Symbol()," ",Period()," Price Breaking Down at ", Price2);
      }
      
      if(UseSendMail)
      {
         SendMail("Channel Breaking Down at " + Price2 + "!", "This is an email from the JC - Line Break Alert indicator. Price broke down your channel alert line at " + Price2 + "!");
      }
   }
}
// ================ what i added code
bool NewBar()
{
   static datetime LastTime = 0;
   if (iTime(_Symbol, 0, 0) + timeoffset > LastTime)
   {
      LastTime = iTime(_Symbol, 0, 0) + timeoffset;
      return (true);
   }
   else
      return (false);
}
//+------------------------------------------------------------------+


if anyone enlighten me much appreciate!

Timeline for alert when price bar crossing specific price line in MQL4 indicator
Timeline for alert when price bar crossing specific price line in MQL4 indicator
  • stackoverflow.com
Stack Overflow | The World’s Largest Online Community for Developers
 
james272:

Hi!

i found a indicator script on the net which suit for me .

this indicator draw and alert when crossing specific above and below line.

But one lack of function is when break out line it alert but there is no limit bar .

i want to set 5 bars so if i set 5 bars i want to receive alert in 5 bars only if over 5 bar like 6 , 7 ...

i just want to skip and ignore this alert.

i checked this script and found Close[0] is ok when check current price crossing above or below line but if i limit to 5 bars then it make some difficult for me.

i thought to use iTime function .

for instance if i want 5 bar limits then i thought i can make it with

iTime(_Symbol, 0, 0) + timeoffset > LastTime

so if timeframe is 5min and 5 bars is 25min so i set timeoffset as 1500

but it not working.

i tested with different value but not working .


if anyone enlighten me much appreciate!

If I have understood correctly, this is the changes you want. See the attachment.

You have an input to limit the number of times an alert happens and then resets after some time (as input).

 
james272:
           

But one lack of function is when break out line it alert but there is no limit bar . i want to set 5 bars so if i set 5 bars i want to receive alert in 5 bars only if over 5 bar like 6 , 7 ... i just want to skip and ignore this alert. i checked this script and found Close[0] is ok when check current price crossing above or below line but if i limit to 5 bars then it make some difficult for me.

           

if you want alert after(?) 5 bars (your explanation is really not very clear) - can see the signature of iClose

double  iClose(
   string           symbol,          // _Symbol
   int              timeframe,       // _Period
   int              shift            // shift at certain bars quantity  
   );

? perhaps you need shift to adjust

LastPrice = iClose(NULL, 0, 6);   // 1+5 because when closed the bar is aleady closed & new bar opened, the closed bar is having index [1] + 5 = 6th bar from the right side - break-even occured