Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Indicators Used
MACD Histogram
Miscellaneous
It issuies visual alerts to the screenIt sends emails
0 Views
0 Downloads
0 Favorites
OsMA_Alert
ÿþ//+------------------------------------------------------------------+

//|                                                   OsMA_Alert.mq5 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                                                 https://mql5.com |

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

#property copyright "Copyright 2018, MetaQuotes Software Corp."

#property link      "https://mql5.com"

#property version   "1.00"

#property description "OsMA Alert indicator"

#property description "Indicator will alert (sound and/or email, or push-notification)"

#property description "whenever the moving average of oscillator is above a user defined level,"

#property description "when entering and leaving an overbought or oversold level."

#property indicator_separate_window

#property indicator_buffers 6

#property indicator_plots   3

//--- plot OS

#property indicator_label1  "OS"

#property indicator_type1   DRAW_COLOR_HISTOGRAM

#property indicator_color1  clrLimeGreen,clrDarkSeaGreen,clrOrangeRed,clrBurlyWood,clrDarkGray

#property indicator_style1  STYLE_SOLID

#property indicator_width1  8

//--- plot UP

#property indicator_label2  "Up"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot DN

#property indicator_label3  "Down"

#property indicator_type3   DRAW_ARROW

#property indicator_color3  clrRed

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- enums

enum ENUM_INPUT_YES_NO

  {

   INPUT_YES   =  1,       // Yes

   INPUT_NO    =  0        // No

  };

//--- 

enum ENUM_MODE_INTERSECTION

  {

   INTERSECTION_NONE,

   INTERSECTION_UP_OB,

   INTERSECTION_DN_OB,

   INTERSECTION_DN_OS,

   INTERSECTION_UP_OS

  };

//--- input parameters

input uint              InpPeriodFast  =  12;         // Fast EMA period

input uint              InpPeriodSlow  =  26;         // Slow EMA period

input uint              InpPeriodSig   =  9;          // Signal period

input double            InpOverbought  =  0.0005;     // Upper threshold level

input double            InpOversold    = -0.0005;     // Lower threshold level

input ENUM_INPUT_YES_NO InpUseAlert    =  INPUT_YES;  // Show alerts

input ENUM_INPUT_YES_NO InpSendMail    =  INPUT_NO;   // Send mail

input ENUM_INPUT_YES_NO InpSendPush    =  INPUT_YES;  // Send push-notifications

//--- indicator buffers

double         BufferOS[];

double         BufferColors[];

double         BufferUP[];

double         BufferDN[];

double         BufferMACD[];

double         BufferSignal[];

//--- global variables

double         overbought;

double         oversold;

int            period_max;

int            period_fast;

int            period_slow;

int            period_sig;

int            handle_macd;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   period_fast=int(InpPeriodFast<1 ? 1 : InpPeriodFast);

   period_slow=int(InpPeriodSlow<1 ? 1 : InpPeriodSlow);

   period_sig=int(InpPeriodSig<1 ? 1 : InpPeriodSig);

   period_max=fmax(period_sig,fmax(period_fast,period_slow));

   overbought=(fabs(InpOverbought)<0.000001 ? 0.000001 : InpOverbought);

   oversold=(-fabs(InpOversold)>-0.000001 ? -0.000001 : -fabs(InpOversold));

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferOS,INDICATOR_DATA);

   SetIndexBuffer(1,BufferColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(2,BufferUP,INDICATOR_DATA);

   SetIndexBuffer(3,BufferDN,INDICATOR_DATA);

   SetIndexBuffer(4,BufferMACD,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,BufferSignal,INDICATOR_CALCULATIONS);

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

   PlotIndexSetInteger(1,PLOT_ARROW,159);

   PlotIndexSetInteger(2,PLOT_ARROW,159);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"OsMA Alert");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

   IndicatorSetInteger(INDICATOR_LEVELS,2);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,overbought);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,oversold);

   IndicatorSetString(INDICATOR_LEVELTEXT,0,"Upper threshold");

   IndicatorSetString(INDICATOR_LEVELTEXT,1,"Lower threshold");

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferOS,true);

   ArraySetAsSeries(BufferColors,true);

   ArraySetAsSeries(BufferUP,true);

   ArraySetAsSeries(BufferDN,true);

   ArraySetAsSeries(BufferMACD,true);

   ArraySetAsSeries(BufferSignal,true);

//--- create MACD handle

   ResetLastError();

   handle_macd=iMACD(NULL,PERIOD_CURRENT,period_fast,period_slow,period_sig,PRICE_CLOSE);

   if(handle_macd==INVALID_HANDLE)

     {

      Print("The iMACD(",(string)period_fast,",",(string)period_slow,",",(string)period_sig,") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

//---

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

  {

//--- #AB0=>2:0 <0AA82>2 1CD5@>2 :0: B09<A5@89

   ArraySetAsSeries(time,true);

//--- @>25@:0 :>;8G5AB20 4>ABC?=KE 10@>2

   if(rates_total<period_max+3) return 0;

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-2;

      ArrayInitialize(BufferOS,EMPTY_VALUE);

      ArrayInitialize(BufferColors,4);

      ArrayInitialize(BufferUP,EMPTY_VALUE);

      ArrayInitialize(BufferDN,EMPTY_VALUE);

      ArrayInitialize(BufferMACD,0);

      ArrayInitialize(BufferSignal,0);

     }

//--- >43>B>2:0 40==KE

   int copied=0,count=(limit==0 ? 1 : rates_total);

   copied=CopyBuffer(handle_macd,MAIN_LINE,0,count,BufferMACD);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_macd,SIGNAL_LINE,0,count,BufferSignal);

   if(copied!=count) return 0;

   

//---  0AGQB 8=48:0B>@0

   static datetime last_time=0;

   for(int i=limit; i>=0; i--)

     {

      ENUM_MODE_INTERSECTION intersection=INTERSECTION_NONE;

      BufferOS[i]=BufferMACD[i]-BufferSignal[i];

      BufferColors[i]=(BufferOS[i]>0 ? (BufferOS[i]>BufferOS[i+1] ? 0 : 1) : BufferOS[i]<0 ? (BufferOS[i]<BufferOS[i+1] ? 2 : 3) : 4);

      BufferUP[i]=BufferDN[i]=EMPTY_VALUE;

      if(BufferOS[i]>overbought && BufferOS[i+1]<=overbought)

        {

         BufferUP[i]=BufferOS[i];

         intersection=INTERSECTION_UP_OB;

        }

      else if(BufferOS[i]<overbought && BufferOS[i+1]>=overbought)

        {

         BufferDN[i]=BufferOS[i];

         intersection=INTERSECTION_DN_OB;

        }

      if(BufferOS[i]<oversold && BufferOS[i+1]>=oversold)

        {

         BufferDN[i]=BufferOS[i];

         intersection=INTERSECTION_DN_OS;

        }

      else if(BufferOS[i]>oversold && BufferOS[i+1]<=oversold)

        {

         BufferUP[i]=BufferOS[i];

         intersection=INTERSECTION_UP_OS;

        }

      //--- ;5@BK

      if(i==0 && intersection!=INTERSECTION_NONE)

        {

         if(last_time!=time[0])

           {

            Notifications(IntersectionDescr(intersection),InpUseAlert,InpSendPush,InpSendMail);

            last_time=time[0];

           }

        }

     }



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

   return(rates_total);

  }

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

//| B?@02;O5B A>>1I5=8O                                             |

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

void Notifications(const string mess,bool use_alert=true,bool send_push=true,bool send_mail=false)

  {

   string message=Symbol()+", "+TimeframeToString(Period())+": "+mess;

   if(use_alert) Alert(message);

   if(send_mail  && TerminalInfoInteger(TERMINAL_EMAIL_ENABLED)) SendMail(MQLInfoString(MQL_PROGRAM_NAME)+" indicator Signal",message);

   if(send_push && TerminalInfoInteger(TERMINAL_NOTIFICATIONS_ENABLED)) SendNotification(MQLInfoString(MQL_PROGRAM_NAME)+" indicator Signal: "+message);

  }

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

//| >72@0I05B >?8A0=85 ?5@5A5G5=8O                                  |

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

string IntersectionDescr(ENUM_MODE_INTERSECTION type)

  {

   return

     (

      type==INTERSECTION_UP_OB ? "Upper threshold level was crossed up." : type==INTERSECTION_DN_OB ? "Upper threshold level was crossed down." :

      type==INTERSECTION_UP_OS ? "Lower threshold level was crossed up." : type==INTERSECTION_DN_OS ? "Lower threshold level was crossed down." : "No intersections"

     );

  }

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

//| >72@0I05B =08<5=>20=85 B09<D@59<0                               |

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

string TimeframeToString(const ENUM_TIMEFRAMES timeframe)

  {

   return StringSubstr(EnumToString(timeframe),7);

  }

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

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