Price touched the rectangle Alert

Author: Copyright © 2021, Vladimir Karputov
Miscellaneous
It plays sound alertsIt issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Price touched the rectangle Alert
ÿþ//+------------------------------------------------------------------+

//|                            Price touched the rectangle Alert.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

//--- input parameters

input string               InpRectangleName     = "Rectangle";    // Rectangle Name

input ENUM_APPLIED_PRICE   InpPrice             = PRICE_CLOSE;    // Type of price

input string               InpSoundUpwardsName  = "alert.wav";    // Upwards Sound Name

input string               InpSoundTopDownName  = "alert2.wav";   // Top down Sound Name

input uchar                InpSoundRepetitions  = 3;              // Repeats on the same bar

input uchar                InpSoundPause        = 3;              // Pause, in seconds

//---

datetime m_last_signal  =0;     // "0" -> D'1970.01.01 00:00';

datetime m_prev_bars    =0;     // "0" -> D'1970.01.01 00:00';

int      m_repeats      =0;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   Print(__FUNCTION__);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator iteration function                              |

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

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=rates_total-1;

   if(time[i]>m_prev_bars)

     {

      m_prev_bars    = time[i];

      m_last_signal  = 0;           // "0" -> D'1970.01.01 00:00';

      m_prev_bars    = time[i];     // "0" -> D'1970.01.01 00:00';

      m_repeats      = 0;

     }

//---

   long chart_id=ChartID();

   datetime time_1=(datetime)ObjectGetInteger(chart_id,InpRectangleName,OBJPROP_TIME,0);

   if(time_1==D'1970.01.01 00:00')

      return(0);

   datetime time_2=(datetime)ObjectGetInteger(chart_id,InpRectangleName,OBJPROP_TIME,1);

   if(time_2==D'1970.01.01 00:00')

      return(0);

   double price_1=ObjectGetDouble(chart_id,InpRectangleName,OBJPROP_PRICE,0);

   if(price_1==0.0)

      return(0);

   double price_2=ObjectGetDouble(chart_id,InpRectangleName,OBJPROP_PRICE,1);

   if(price_2==0.0)

      return(0);

   datetime time_left,time_right;

   double price_max,price_min;

//---

   double price=0.0;

   switch(InpPrice)

     {

      case PRICE_CLOSE:

         price = close[i];

         break;

      case PRICE_OPEN:

         price = open[i];

         break;

      case PRICE_HIGH:

         price = high[i];

         break;

      case PRICE_LOW:

         price = low[i];

         break;

      case PRICE_MEDIAN:

         price = (high[i]+low[i])/2.0;

         break;

      case PRICE_TYPICAL:

         price = (high[i]+low[i]+close[i])/3.0;

         break;

      case PRICE_WEIGHTED:

         price = (high[i]+low[i]+close[i]*2.0)/4.0;

         break;

     }

   if(price==0.0)

      return(0);

//---

   if(time_1<time_2)

     {

      time_left=time_1;

      time_right=time_2;

     }

   else

     {

      time_left=time_2;

      time_right=time_1;

     }

   if(price_1<price_2)

     {

      price_min=price_1;

      price_max=price_2;

     }

   else

     {

      price_min=price_2;

      price_max=price_1;

     }

//---

   datetime time_current=TimeCurrent();

   if(time_left<=time[i] && time[i]<=time_right) // upwards

     {

      if(low[i]<price_min && price>price_min)

        {

         if(time_current-m_last_signal>=InpSoundPause)

            if(m_repeats<InpSoundRepetitions)

              {

               PlaySound(InpSoundUpwardsName);

               Alert("Upwards");

               m_last_signal=time_current;

               m_repeats++;

              }

        }

      else

        {

         if(high[i]>price_max && price<price_max)

           {

            if(time_current-m_last_signal>=InpSoundPause)

               if(m_repeats<InpSoundRepetitions)

                 {

                  PlaySound(InpSoundTopDownName);

                  Alert("Top Down");

                  m_last_signal=time_current;

                  m_repeats++;

                 }

           }

        }

     }

//--- return value of prev_calculated for next call

   return(rates_total);

  }

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

Comments