Resistance and Support 2 Alert

Author: Copyright © 2017-2022, Vladimir Karputov
Price Data Components
Indicators Used
Fractals
Miscellaneous
It plays sound alertsIt issuies visual alerts to the screenIt sends emails
0 Views
0 Downloads
0 Favorites
Resistance and Support 2 Alert
ÿþ//+------------------------------------------------------------------+

//|                               Resistance and Support 2 Alert.mq5 |

//|                         Copyright © 2017-2022, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

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

#property copyright "Copyright © 2017-2022, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "2.5"

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_plots   2

//--- plot "Resistance"

#property indicator_label1  "Resistance"

#property indicator_type1   DRAW_ARROW

#property indicator_width1  1

//--- plot "Support"

#property indicator_label2  "Support"

#property indicator_type2   DRAW_ARROW

#property indicator_width2  1

//--- input parameters

input group             "Arrow"

input uchar                InpArrowCode            = 159;         // Arrow code (Wingdings font's symbol codes)

input color                InpResistanceColor      = clrLime;     // Resistance color

input color                InpSupportColor         = clrMagenta;  // Support color

input group             "Alerts"

input string               InpSoundName            = "alert.wav"; // Sound Name

input uchar                InpSoundRepetitions     = 3;           // Repetitions

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

input bool                 InpAlert                = true;        // Alert

input bool                 InpMail                 = true;        // Send mail

input bool                 InpNotification         = true;        // Send notification

//--- indicator buffers

double   ResistanceBuffer[];

double   SupportBuffer[];

double   FractalsUpBuffer[];

double   FractalsDownBuffer[];

//---

int      handle_iFractals;             // variable for storing the handle of the iFractals indicator

int      bars_calculated   = 0;        // we will keep the number of values in the Fractals indicator

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

uchar    m_repetitions     = 0;        //

string   m_text            = "";       //

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

bool     m_init_error      = false;    // error on InInit

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,ResistanceBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,SupportBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,FractalsUpBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,FractalsDownBuffer,INDICATOR_CALCULATIONS);

//--- Define the symbol code for drawing in PLOT_ARROW

   PlotIndexSetInteger(0,PLOT_ARROW,InpArrowCode);

   PlotIndexSetInteger(1,PLOT_ARROW,InpArrowCode);

//--- Set the color line

   PlotIndexSetInteger(0,PLOT_LINE_COLOR,InpResistanceColor);

   PlotIndexSetInteger(1,PLOT_LINE_COLOR,InpSupportColor);

//--- Set the vertical shift of arrows in pixels

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-5);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,5);

//--- Set as an empty value 0

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- create handle of the indicator iFractals

   handle_iFractals=iFractals(Symbol(),Period());

//--- if the handle is not created

   if(handle_iFractals==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code

      PrintFormat("Failed to create handle of the iFractals indicator for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//---

   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[])

  {

   if(m_init_error)

      return(0);

//--- check for minimum rates count; minimum rates count for Fractals: 5

   if(rates_total<5)

      return(0);

//---

   int calculated_Fractals=BarsCalculated(handle_iFractals);

   if(calculated_Fractals!=rates_total)

      return(0);

//--- number of values copied from the iFractals indicator

   int values_to_copy=-1;

//--- if it is the first start of calculation of the indicator

   if(prev_calculated==0)

      values_to_copy=rates_total;

   else

     {

      //--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate()

      //--- for calculation not more than one bar is added

      //--- value "+5" - because of the feature of drawing fractals

      values_to_copy=(rates_total-prev_calculated)+5;

     }

//--- fill the FractalsUpBuffer and FractalsDownBuffer arrays with values from the Fractals indicator

//--- if FillFractalsArraysFromBuffers returns false, it means the information is nor ready yet, quit operation

   if(!FillFractalsArraysFromBuffers(FractalsUpBuffer,FractalsDownBuffer,handle_iFractals,values_to_copy))

      return(0);

//---

   int limit=(prev_calculated==0)?0:(rates_total-prev_calculated)-5;

   limit=(limit<0)?0:limit;

   static double prev_fractals_up=EMPTY_VALUE;

   static double prev_fractals_down=EMPTY_VALUE;

   for(int i=limit; i<rates_total; i++)

     {

      if(FractalsUpBuffer[i]!=EMPTY_VALUE)

         prev_fractals_up=FractalsUpBuffer[i];

      ResistanceBuffer[i]=prev_fractals_up;

      //---

      if(FractalsDownBuffer[i]!=EMPTY_VALUE)

         prev_fractals_down=FractalsDownBuffer[i];

      SupportBuffer[i]=prev_fractals_down;

     }

//--- alert

   if(time[rates_total-1]>m_prev_bars)

     {

      m_last_sound=0;

      m_prev_bars=time[rates_total-1];

      m_repetitions=0;

     }

   if(m_repetitions>=InpSoundRepetitions)

      return(rates_total);

   datetime time_current=TimeCurrent();

   if(time_current-m_last_sound>InpSoundPause)

     {

      int i=rates_total-1;

      if(open[i]<ResistanceBuffer[i] && close[i]>ResistanceBuffer[i])

        {

         PlaySound(InpSoundName);

         m_text=Symbol()+","+StringSubstr(EnumToString(Period()),7,-1)+" Trend UP, "+TimeToString(time[i]);

         if(InpAlert)

            Alert(m_text);

         m_last_sound=time_current;

         m_repetitions++;

         //---

         if(InpMail)

           {

            SendMail(Symbol()+","+StringSubstr(EnumToString(Period()),7,-1),m_text);

           }

         if(InpNotification)

           {

            SendNotification(Symbol()+","+StringSubstr(EnumToString(Period()),7,-1)+" "+m_text);

           }

        }

      else

        {

         if(open[i]>SupportBuffer[i] && close[i]<SupportBuffer[i])

           {

            PlaySound(InpSoundName);

            m_text=Symbol()+","+StringSubstr(EnumToString(Period()),7,-1)+" Trend DOWN, "+TimeToString(time[i]);

            if(InpAlert)

               Alert(m_text);

            m_last_sound=time_current;

            m_repetitions++;

            //---

            if(InpMail)

              {

               SendMail(Symbol()+","+StringSubstr(EnumToString(Period()),7,-1),m_text);

              }

            if(InpNotification)

              {

               SendNotification(Symbol()+","+StringSubstr(EnumToString(Period()),7,-1)+" "+m_text);

              }

           }

        }

     }

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

   return(rates_total);

  }

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

//| Filling indicator buffers from the iFractals indicator           |

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

bool FillFractalsArraysFromBuffers(double &up_arrows[],// indicator buffer for up arrows

                                   double &down_arrows[],      // indicator buffer for down arrows

                                   int ind_handle,             // handle of the iFractals indicator

                                   int amount                  // number of copied values

                                  )

  {

//--- reset error code

   ResetLastError();

//--- fill a part of the FractalUpBuffer array with values from the indicator buffer that has 0 index

   if(CopyBuffer(ind_handle,0,0,amount,up_arrows)<0)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the iFractals indicator to the FractalUpBuffer array, error code %d",

                  GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(false);

     }

//--- fill a part of the FractalDownBuffer array with values from the indicator buffer that has index 1

   if(CopyBuffer(ind_handle,1,0,amount,down_arrows)<0)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the iFractals indicator to the FractalDownBuffer array, error code %d",

                  GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(false);

     }

//--- everything is fine

   return(true);

  }

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

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---