iFractals iBands Alert

Author: Copyright © 2022, Vladimir Karputov
Indicators Used
FractalsBollinger bands indicator
Miscellaneous
It plays sound alertsIt issuies visual alerts to the screenIt sends emails
0 Views
0 Downloads
0 Favorites
iFractals iBands Alert
ÿþ//+------------------------------------------------------------------+

//|                                       iFractals iBands Alert.mq5 |

//|                              Copyright © 2022, Vladimir Karputov |

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

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

#property copyright "Copyright © 2022, Vladimir Karputov"

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

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 5

#property indicator_plots   5

//--- plot Fractals Up

#property indicator_label1  "Fractals Up"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrDarkOrange

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Fractals Down

#property indicator_label2  "Fractals Down"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrDarkOrange

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Bands Upper

#property indicator_label3  "Bands Upper"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrOrchid

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot Bands Middle

#property indicator_label4  "Bands Middle"

#property indicator_type4   DRAW_LINE

#property indicator_color4  clrOrchid

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- plot Bands Lower

#property indicator_label5  "Bands Lower"

#property indicator_type5   DRAW_LINE

#property indicator_color5  clrOrchid

#property indicator_style5  STYLE_SOLID

#property indicator_width5  1

//--- input parameters

input group             "Bands"

input int                  Inp_Bands_bands_period        = 20;             // Bands: period for average line calculation

input int                  Inp_Bands_bands_shift         = 0;              // Bands: horizontal shift of the indicator

input double               Inp_Bands_deviation           = 2.0;            // Bands: number of standard deviations

input ENUM_APPLIED_PRICE   Inp_Bands_applied_price       = PRICE_CLOSE;    // Bands: type of price

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   UpBuffer[];

double   DownBuffer[];

double   UpperBuffer[];

double   MiddleBuffer[];

double   LowerBuffer[];

//---

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

int      handle_iBands;                // variable for storing the handle of the iBands indicator

//---

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

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,UpBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,DownBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,UpperBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,MiddleBuffer,INDICATOR_DATA);

   SetIndexBuffer(4,LowerBuffer,INDICATOR_DATA);

//--- setting a code from the Wingdings charset as the property of PLOT_ARROW

   PlotIndexSetInteger(0,PLOT_ARROW,217);

   PlotIndexSetInteger(1,PLOT_ARROW,218);

//--- set as an empty value 0.0

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//--- 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);

     }

//--- create handle of the indicator iBands

   handle_iBands=iBands(Symbol(),Period(),Inp_Bands_bands_period,

                        Inp_Bands_bands_shift,Inp_Bands_deviation,Inp_Bands_applied_price);

//--- if the handle is not created

   if(handle_iBands==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the iBands 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);

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

   int values_to_copy;

//--- determine the number of values calculated in the indicator

   int calculated_fractals=BarsCalculated(handle_iFractals);

   if(calculated_fractals<=0)

     {

      PrintFormat("BarsCalculated(handle_iFractals) returned %d, error code %d",calculated_fractals,GetLastError());

      return(0);

     }

   int calculated_bands=BarsCalculated(handle_iBands);

   if(calculated_bands<=0)

     {

      PrintFormat("BarsCalculated(handle_iBands) returned %d, error code %d",calculated_bands,GetLastError());

      return(0);

     }

   if(calculated_fractals!=calculated_bands)

     {

      PrintFormat("BarsCalculated(handle_iFractals) returned %d, BarsCalculated(handle_iBands) returned %d",calculated_fractals,calculated_bands);

      return(0);

     }

   int calculated=calculated_fractals;

//--- if it is the first start of calculation of the indicator or if the number of values in the iFractals indicator changed

//---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history)

   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)

     {

      //--- if the UpBuffer array is greater than the number of values in the iFractals indicator for symbol/period, then we don't copy everything

      //--- otherwise, we copy less than the size of indicator buffers

      if(calculated>rates_total)

         values_to_copy=rates_total;

      else

         values_to_copy=calculated;

     }

   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

      values_to_copy=(rates_total-prev_calculated)+1;

     }

//--- fill the UpBuffer and DownBuffer arrays with values from the Fractals indicator

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

   if(!FillArraysFromBuffers(UpBuffer,DownBuffer,handle_iFractals,values_to_copy))

      return(0);

//--- fill the array with values of the Bollinger Bands indicator

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

   if(!FillArraysFromBuffers(MiddleBuffer,UpperBuffer,LowerBuffer,Inp_Bands_bands_shift,handle_iBands,values_to_copy))

      return(0);

//--- memorize the number of values in the Fractals indicator

   bars_calculated=calculated;

//--- main loop

//--- we work only at the time of the birth of new bar

   datetime time_0=time[rates_total-1];

   if(time_0!=m_prev_bars)

     {

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

      m_repetitions  = 0;  //

      m_text         = "";

      m_prev_bars    = time_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(UpBuffer[i-3]!=EMPTY_VALUE || DownBuffer[i-3]!=EMPTY_VALUE)

        {

         //--- Fractal Up Bands

         if(UpBuffer[i-3]!=EMPTY_VALUE && UpBuffer[i-3]>=UpperBuffer[i-3])

           {

            PlaySound(InpSoundName);

            m_text=Symbol()+","+StringSubstr(EnumToString(Period()),7,-1)+" Fractals Up Bands, "+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);

              }

           }

         //--- Fractal Down Bands

         if(DownBuffer[i-3]!=EMPTY_VALUE && DownBuffer[i-3]<=LowerBuffer[i-3])

           {

            PlaySound(InpSoundName);

            m_text=Symbol()+","+StringSubstr(EnumToString(Period()),7,-1)+" Fractals Down Bands, "+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 FillArraysFromBuffers(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 UpBuffer 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 UpBuffer 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 DownBuffer 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 DownBuffer 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);

  }

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

//| Filling indicator buffers from the iBands indicator              |

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

bool FillArraysFromBuffers(double &base_values[],     // indicator buffer of the middle line of Bollinger Bands

                           double &upper_values[],    // indicator buffer of the upper border

                           double &lower_values[],    // indicator buffer of the lower border

                           int shift,                 // shift

                           int ind_handle,            // handle_iBands of the iBands indicator

                           int amount                 // number of copied values

                          )

  {

//--- reset error code

   ResetLastError();

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

   if(CopyBuffer(ind_handle,0,-shift,amount,base_values)<0)

     {

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

      PrintFormat("Failed to copy data from the iBands indicator, 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 UpperBuffer array with values from the indicator buffer that has index 1

   if(CopyBuffer(ind_handle,1,-shift,amount,upper_values)<0)

     {

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

      PrintFormat("Failed to copy data from the iBands indicator, 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 LowerBuffer array with values from the indicator buffer that has index 2

   if(CopyBuffer(ind_handle,2,-shift,amount,lower_values)<0)

     {

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

      PrintFormat("Failed to copy data from the iBands indicator, 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);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   if(handle_iFractals!=INVALID_HANDLE)

      IndicatorRelease(handle_iFractals);

   if(handle_iBands!=INVALID_HANDLE)

      IndicatorRelease(handle_iBands);

  }

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

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 ---