Candle touches MA Alert

Author: Copyright © 2021, Vladimir Karputov
Indicators Used
Moving average indicator
Miscellaneous
It issuies visual alerts to the screenIt sends emailsIt plays sound alerts
0 Views
0 Downloads
0 Favorites
Candle touches MA Alert
ÿþ//+------------------------------------------------------------------+

//|                                      Candle touches MA 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.00"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

//--- input parameters

input group             "MA"

input int                  Inp_MA_ma_period     = 12;          // MA: averaging period

input int                  Inp_MA_ma_shift      = 0;           // MA: horizontal shift

input ENUM_MA_METHOD       Inp_MA_ma_method     = MODE_SMA;    // MA: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_applied_price = PRICE_CLOSE; // MA: type of price

input group             "Alert"

input bool                 InpAlert             = true;           // Alert

input bool                 InpEmail             = true;           // Use send email

input bool                 InpNotification      = true;           // Use Push notifications

input string               InpSound             = "expert.wav";   // Play Sound ("" -> off)

input bool                 InpExperts           = true;           // Tab Experts

input bool                 InpComment           = true;           // Comment on Chart

//---

int      handle_iMA;             // variable for storing the handle of the iMA indicator



string   short_name     = "";

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

  {

//--- create handle of the indicator iMA

   handle_iMA=iMA(Symbol(),Period(),Inp_MA_ma_period,Inp_MA_ma_shift,

                  Inp_MA_ma_method,Inp_MA_applied_price);

//--- if the handle is not created

   if(handle_iMA==INVALID_HANDLE)

     {

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

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

     }

//--- show the symbol/timeframe the Moving Average indicator is calculated for

   short_name=StringFormat("MA(%d, %d, %s, %s, %d)",

                           Inp_MA_ma_period, Inp_MA_ma_shift,EnumToString(Inp_MA_ma_method),

                           EnumToString(Inp_MA_applied_price));

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//---

   if(InpComment)

      Comment("");

  }

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

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

//---

   double ma[];

   ArraySetAsSeries(ma,true);

   int start_pos=0,count=3;

   if(!iGetArray(handle_iMA,0,start_pos,count,ma))

      return(0);

   if(high[rates_total-1]>ma[0] || low[rates_total-1]<ma[0])

     {

      if(m_prev_bars<time[rates_total-1])

        {

         m_prev_bars=time[rates_total-1];

         if(InpAlert)

            Alert("candle touches ",short_name);

         if(InpEmail)

            SendMail("candle touches",short_name);

         if(InpNotification)

            SendNotification("candle touches "+short_name);

         if(InpSound!="")

            PlaySound(InpSound);

         if(InpExperts)

            Print("candle touches ",short_name);

         if(InpComment)

            Comment("candle touches ",short_name);

        }

     }

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

   return(rates_total);

  }

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

//| Get value of buffers                                             |

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

bool iGetArray(const int handle,const int buffer,const int start_pos,

               const int count,double &arr_buffer[])

  {

   bool result=true;

   if(!ArrayIsDynamic(arr_buffer))

     {

      PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);

      return(false);

     }

   ArrayFree(arr_buffer);

//--- reset error code

   ResetLastError();

//--- fill a part of the iBands array with values from the indicator buffer

   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);

   if(copied!=count)

     {

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

      PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",

                  __FILE__,__FUNCTION__,count,copied,GetLastError());

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

      return(false);

     }

   return(result);

  }

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

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