OBOS_Overlay_with_Alert

Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Indicators Used
Moving average indicatorStandard Deviation indicator
Miscellaneous
It issuies visual alerts to the screenIt sends emails
0 Views
0 Downloads
0 Favorites
OBOS_Overlay_with_Alert
ÿþ//+------------------------------------------------------------------+

//|                                      OBOS_Overlay_with_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 "OBOS Overlay with Alerts indicator"

#property description "OBOS: https://www.mql5.com/ru/code/22186"

#property indicator_chart_window

#property indicator_buffers 14

#property indicator_plots   3

//--- plot OB

#property indicator_label1  "OBOS Down"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot OS

#property indicator_label2  "OBOS Up"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrGreen

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Candles

#property indicator_label3  "Open;High;Low;Close"

#property indicator_type3   DRAW_COLOR_CANDLES

#property indicator_color3  clrGreen,clrRed,clrBlue,clrDarkGray

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- enums

enum ENUM_INPUT_YES_NO

  {

   INPUT_YES   =  1,    // Yes

   INPUT_NO    =  0     // No

  };

//--- input parameters

input uint              InpPeriod      =  9;             // OBOS Period

input ENUM_INPUT_YES_NO InpUseAlerts   =  INPUT_YES;     // Use 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         BufferDirDN[];

double         BufferDirUP[];

double         BufferCandleO[];

double         BufferCandleH[];

double         BufferCandleL[];

double         BufferCandleC[];

double         BufferColors[];

//--- OBOS buffers

double         BufferUP[];

double         BufferDN[];

double         BufferRAW[];

double         BufferAvgRAW[];

double         BufferPrice[];

double         BufferMA[];

double         BufferDev[];

//--- global variables

double         overbought;

double         oversold;

int            period_ma;

int            handle_ma;

int            handle_dev;

//--- includes

#include <MovingAverages.mqh>

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   period_ma=int(InpPeriod<1 ? 1 : InpPeriod);

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferDirDN,INDICATOR_DATA);

   SetIndexBuffer(1,BufferDirUP,INDICATOR_DATA);

   SetIndexBuffer(2,BufferCandleO,INDICATOR_DATA);

   SetIndexBuffer(3,BufferCandleH,INDICATOR_DATA);

   SetIndexBuffer(4,BufferCandleL,INDICATOR_DATA);

   SetIndexBuffer(5,BufferCandleC,INDICATOR_DATA);

   SetIndexBuffer(6,BufferColors,INDICATOR_COLOR_INDEX);

   //---

   SetIndexBuffer(7,BufferUP,INDICATOR_CALCULATIONS);

   SetIndexBuffer(8,BufferDN,INDICATOR_CALCULATIONS);

   SetIndexBuffer(9,BufferRAW,INDICATOR_CALCULATIONS);

   SetIndexBuffer(10,BufferAvgRAW,INDICATOR_CALCULATIONS);

   SetIndexBuffer(11,BufferPrice,INDICATOR_CALCULATIONS);

   SetIndexBuffer(12,BufferMA,INDICATOR_CALCULATIONS);

   SetIndexBuffer(13,BufferDev,INDICATOR_CALCULATIONS);

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

   PlotIndexSetInteger(0,PLOT_ARROW,32);

   PlotIndexSetInteger(1,PLOT_ARROW,32);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"OBOS Overlay with Alerts ("+(string)period_ma+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting plot buffer parameters

   PlotIndexSetInteger(2,PLOT_SHOW_DATA,false);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferDirDN,true);

   ArraySetAsSeries(BufferDirUP,true);

   ArraySetAsSeries(BufferCandleO,true);

   ArraySetAsSeries(BufferCandleH,true);

   ArraySetAsSeries(BufferCandleL,true);

   ArraySetAsSeries(BufferCandleC,true);

   ArraySetAsSeries(BufferColors,true);

   //---

   ArraySetAsSeries(BufferUP,true);

   ArraySetAsSeries(BufferDN,true);

   ArraySetAsSeries(BufferRAW,true);

   ArraySetAsSeries(BufferAvgRAW,true);

   ArraySetAsSeries(BufferPrice,true);

   ArraySetAsSeries(BufferMA,true);

   ArraySetAsSeries(BufferDev,true);

//--- create handles

   ResetLastError();

   handle_ma=iMA(NULL,PERIOD_CURRENT,period_ma,0,MODE_EMA,PRICE_WEIGHTED);

   if(handle_ma==INVALID_HANDLE)

     {

      Print("The iMA(",(string)period_ma,") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

   handle_dev=iStdDev(NULL,PERIOD_CURRENT,period_ma,0,MODE_EMA,PRICE_WEIGHTED);

   if(handle_dev==INVALID_HANDLE)

     {

      Print("The iStdDev(",(string)period_ma,") 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(open,true);

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(close,true);

   ArraySetAsSeries(time,true);

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

   if(rates_total<fmax(period_ma,4)) 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(BufferDirDN,EMPTY_VALUE);

      ArrayInitialize(BufferDirUP,EMPTY_VALUE);

      ArrayInitialize(BufferCandleO,EMPTY_VALUE);

      ArrayInitialize(BufferCandleH,EMPTY_VALUE);

      ArrayInitialize(BufferCandleL,EMPTY_VALUE);

      ArrayInitialize(BufferCandleC,EMPTY_VALUE);

      ArrayInitialize(BufferColors,3);

      ArrayInitialize(BufferUP,0);

      ArrayInitialize(BufferDN,0);

      ArrayInitialize(BufferRAW,0);

      ArrayInitialize(BufferAvgRAW,0);

      ArrayInitialize(BufferPrice,0);

      ArrayInitialize(BufferMA,0);

      ArrayInitialize(BufferDev,0);

     }

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

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

   copied=CopyBuffer(handle_ma,0,0,count,BufferMA);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_dev,0,0,count,BufferDev);

   if(copied!=count) return 0;



   for(int i=limit; i>=0 && !IsStopped(); i--)

     {

      BufferPrice[i]=(high[i]+low[i]+close[i]+close[i])/4.0;

      BufferRAW[i]=(BufferDev[i]!=0 ? (BufferPrice[i]-BufferMA[i])*100.0/BufferDev[i] : 0);

     }

   if(ExponentialMAOnBuffer(rates_total,prev_calculated,0,period_ma,BufferRAW,BufferAvgRAW)==0)

      return 0;

   if(ExponentialMAOnBuffer(rates_total,prev_calculated,0,period_ma,BufferAvgRAW,BufferUP)==0)

      return 0;

   if(ExponentialMAOnBuffer(rates_total,prev_calculated,0,period_ma,BufferUP,BufferDN)==0)

      return 0;



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

   static int last_direction=0;

   for(int i=limit; i>=0 && !IsStopped(); i--)

     {

      BufferCandleO[i]=BufferCandleH[i]=BufferCandleL[i]=BufferCandleC[i]=EMPTY_VALUE;

      BufferColors[i]=7;

      if(BufferUP[i+1]<BufferUP[i] && BufferDN[i+1]<BufferDN[i])

        {

         BufferCandleO[i]=open[i];

         BufferCandleH[i]=high[i];

         BufferCandleL[i]=low[i];

         BufferCandleC[i]=close[i];

         BufferColors[i]=0;

         BufferDirUP[i]=open[i];

         if(i==0 && last_direction!=1)

           {

            string text="OBOS on "+Symbol()+" "+TimeframeToString(Period())+": Up direction";

            Notifications(text,InpUseAlerts,InpSendPush,InpSendMail);

            last_direction=1;

           }

        }

      else if(BufferUP[i+1]>BufferUP[i] && BufferDN[i+1]>BufferDN[i])

        {

         BufferCandleO[i]=open[i];

         BufferCandleH[i]=high[i];

         BufferCandleL[i]=low[i];

         BufferCandleC[i]=close[i];

         BufferColors[i]=1;

         BufferDirDN[i]=open[i];

         if(i==0 && last_direction!=-1)

           {

            string text="OBOS on "+Symbol()+" "+TimeframeToString(Period())+": Down direction";

            Notifications(text,InpUseAlerts,InpSendPush,InpSendMail);

            last_direction=-1;

           }

        }

      else

        {

         BufferCandleO[i]=open[i];

         BufferCandleH[i]=high[i];

         BufferCandleL[i]=low[i];

         BufferCandleC[i]=close[i];

         BufferColors[i]=2;

         last_direction=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)+" "+ProgramType()+" Signal",message);

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

  }

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

//| >72@0I05B =08<5=>20=85 B8?0 ?@>3@0<<K                           |

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

string ProgramType(void)

  {

   ENUM_PROGRAM_TYPE type=(ENUM_PROGRAM_TYPE)MQLInfoInteger(MQL_PROGRAM_TYPE);

   return(type==PROGRAM_INDICATOR ? "indicator" : type==PROGRAM_EXPERT ? "expert" : "script");

  }

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

//| Timeframe to string                                              |

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

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