AFBSR_BreakoutAlert

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

//|                                          AFBSR_BreakoutAlert.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 "Advanced Fractals Become Support/Resistance Breakout Alerts indicator"

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_plots   4

//--- plot Buy

#property indicator_label1  "Buy"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Sell

#property indicator_label2  "Sell"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Supp

#property indicator_label3  "Support"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrRed

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot Resist

#property indicator_label4  "Resistance"

#property indicator_type4   DRAW_LINE

#property indicator_color4  clrBlue

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- enums

enum ENUM_INPUT_YES_NO

  {

   INPUT_YES   =  1,    // Yes

   INPUT_NO    =  0     // No

  };

//---

enum ENUM_SIDE_ALLOWED

  {

   SIDE_ALLOWED_BOTH,   // Buy and Sell

   SIDE_ALLOWED_BUY,    // Buy only

   SIDE_ALLOWED_SELL    // Sell only

  };

//--- input parameters

input uint              InpFrames      =  5;                   // Frames

input ENUM_INPUT_YES_NO InpShowLines   =  INPUT_NO;            // Show support/resistance

input ENUM_SIDE_ALLOWED InpAllowSide   =  SIDE_ALLOWED_BOTH;   // Allowed sides

input ENUM_INPUT_YES_NO InpAlertON     =  INPUT_YES;           // Alerts enabled

input ENUM_INPUT_YES_NO InpUseAlert    =  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         BufferBuy[];

double         BufferSell[];

double         BufferSupprt[];

double         BufferResist[];

//--- global variables

int            frames;

int            count;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   frames=int(InpFrames<3 ? 3 : InpFrames);

   int hof=frames;

   for(int i=1; i<=frames;++i)

     {

      if(hof>1)

        {

         hof-=2;

         count+=1;

        }

      else

        {

         count-=1;

         break;

        }

     }

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferBuy,INDICATOR_DATA);

   SetIndexBuffer(1,BufferSell,INDICATOR_DATA);

   SetIndexBuffer(2,BufferSupprt,INDICATOR_DATA);

   SetIndexBuffer(3,BufferResist,INDICATOR_DATA);

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

   PlotIndexSetInteger(0,PLOT_ARROW,159);

   PlotIndexSetInteger(1,PLOT_ARROW,159);

//--- setting plot buffer parameters

   PlotIndexSetInteger(2,PLOT_DRAW_TYPE,InpShowLines);

   PlotIndexSetInteger(3,PLOT_DRAW_TYPE,InpShowLines);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferBuy,true);

   ArraySetAsSeries(BufferSell,true);

   ArraySetAsSeries(BufferSupprt,true);

   ArraySetAsSeries(BufferResist,true);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"AFBSR Breakout Alerts");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//---

   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(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(close,true);

   ArraySetAsSeries(time,true);

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

   if(rates_total<4) return 0;

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

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-count*2-3;

      ArrayInitialize(BufferBuy,EMPTY_VALUE);

      ArrayInitialize(BufferSell,EMPTY_VALUE);

      ArrayInitialize(BufferSupprt,EMPTY_VALUE);

      ArrayInitialize(BufferResist,EMPTY_VALUE);

     }



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

   static datetime last_time=0;

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

     {

      double curr=high[i+count];

      if(IsHighest(i,high))

         BufferResist[i+2]=BufferResist[i+1]=BufferResist[i]=curr;

      else if(BufferResist[i+1]!=EMPTY_VALUE)

         BufferResist[i]=BufferResist[i+1];



      curr=low[i+count];

      if(IsLowest(i,low))

         BufferSupprt[i+2]=BufferSupprt[i+1]=BufferSupprt[i]=curr;

      else if(BufferSupprt[i+1]!=EMPTY_VALUE)

         BufferSupprt[i]=BufferSupprt[i+1];



      int direction=GetDirection(i,close);

      switch(direction)

        {

         case 1:

            BufferBuy[i]=low[i];

            BufferSell[i]=EMPTY_VALUE;

            break;

         case -1:

            BufferSell[i]=high[i];

            BufferBuy[i]=EMPTY_VALUE;

            break;

        }

      if(i==0 && last_time!=time[0] && direction!=0)

        {

         string mess=(direction==1 ? "The resistance line is broken up" : "The support line is broken down");

         Notifications(mess,InpUseAlert,InpSendPush,InpSendMail);

         last_time=time[0];

        }

     }

   

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

   return(rates_total);

  }

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

//|                                                                  |

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

int GetDirection(const int shift,const double &close[])

  {

   return

     (

      !InpAlertON ? 0 :

      close[shift]>BufferResist[shift] && close[shift+1]<=BufferResist[shift+1] && BufferResist[shift]==BufferResist[shift+1] && InpAllowSide!=SIDE_ALLOWED_SELL ?  1 :

      close[shift]<BufferSupprt[shift] && close[shift+1]>=BufferSupprt[shift+1] && BufferSupprt[shift]==BufferSupprt[shift+1] && InpAllowSide!=SIDE_ALLOWED_BUY  ? -1 : 0

     );

  }

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

//| Timeframe to string                                              |

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

string TimeframeToString(const ENUM_TIMEFRAMES timeframe)

  {

   return StringSubstr(EnumToString(timeframe),7);

  }

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

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

  }

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

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

  }

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

//|                                                                  |

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

bool IsLowest(const int shift,const double &low[])

  {

   int x=shift+count*2;

   double curr=low[shift+count];

   for(int i=x; i>=shift; --i)

     {

      if(curr>low[i])

        {

         return false;

        }

     }

   return true;

  }

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

//|                                                                  |

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

bool IsHighest(const int shift,const double &high[])

  {

   int x=shift+count*2;

   double curr=high[shift+count];

   for(int i=x; i>=shift; --i)

     {

      if(curr<high[i])

        {

         return false;

        }

     }

   return true;

  }

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

Comments