Three_MA_Trend_Continuation

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

//|                                  Three_MA_Trend_Continuation.mq5 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                                                 https://mql5.com |

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

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

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

#property description "Three MA Trend Continuation indicator"

#property version   "1.00"

#property indicator_chart_window

#property indicator_buffers 8

#property indicator_plots   7

//--- plot MA1

#property indicator_label1  "MA 1"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrDarkOrange

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot MA2

#property indicator_label2  "MA 2"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrMediumSeaGreen

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot MA3

#property indicator_label3  "MA 3"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrDeepSkyBlue

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot Arr1

#property indicator_label4  "Entry long"

#property indicator_type4   DRAW_ARROW

#property indicator_color4  clrBlue

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- plot Arr2

#property indicator_label5  "Entry short"

#property indicator_type5   DRAW_ARROW

#property indicator_color5  clrRed

#property indicator_style5  STYLE_SOLID

#property indicator_width5  1

//--- plot Arr3

#property indicator_label6  "Exit long"

#property indicator_type6   DRAW_ARROW

#property indicator_color6  clrDodgerBlue

#property indicator_style6  STYLE_SOLID

#property indicator_width6  1

//--- plot Arr4

#property indicator_label7  "Exit short"

#property indicator_type7   DRAW_ARROW

#property indicator_color7  clrOrangeRed

#property indicator_style7  STYLE_SOLID

#property indicator_width7  1

//--- enums

enum ENUM_INPUT_YES_NO

  {

   INPUT_YES   =  1,    // Yes

   INPUT_NO    =  0     // No

  };

//--- input parameters

input uint              InpPeriodMA1=10;  // First MA period

input ENUM_MA_METHOD    InpMethodMA1=1;   // First MA method

input uint              InpPeriodMA2=15;  // Second MA period

input ENUM_MA_METHOD    InpMethodMA2=1;   // Second MA method

input uint              InpPeriodMA3=50;  // Third MA period

input ENUM_MA_METHOD    InpMethodMA3=0;   // Third MA method

input ENUM_INPUT_YES_NO InpUseAlert=1;    // Show alerts

input ENUM_INPUT_YES_NO InpSendMail=0;    // Send mail

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

//--- indicator buffers

double         BufferMA1[];

double         BufferMA2[];

double         BufferMA3[];

double         BufferEntryB[];

double         BufferEntryS[];

double         BufferExitB[];

double         BufferExitS[];

double         BufferSignal[];

//--- global variables

int            period_ma1;

int            period_ma2;

int            period_ma3;

int            period_max;

int            handle_ma1;

int            handle_ma2;

int            handle_ma3;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   period_ma1=int(InpPeriodMA1<1 ? 1 : InpPeriodMA1);

   period_ma2=int(InpPeriodMA2<1 ? 1 : InpPeriodMA2);

   period_ma3=int(InpPeriodMA3<1 ? 1 : InpPeriodMA3);

   period_max=fmax(period_ma1,fmax(period_ma2,period_ma3));

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferMA1,INDICATOR_DATA);

   SetIndexBuffer(1,BufferMA2,INDICATOR_DATA);

   SetIndexBuffer(2,BufferMA3,INDICATOR_DATA);

   SetIndexBuffer(3,BufferEntryB,INDICATOR_DATA);

   SetIndexBuffer(4,BufferEntryS,INDICATOR_DATA);

   SetIndexBuffer(5,BufferExitB,INDICATOR_DATA);

   SetIndexBuffer(6,BufferExitS,INDICATOR_DATA);

   SetIndexBuffer(7,BufferSignal,INDICATOR_CALCULATIONS);

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

   PlotIndexSetInteger(3,PLOT_ARROW,159);

   PlotIndexSetInteger(4,PLOT_ARROW,159);

   PlotIndexSetInteger(5,PLOT_ARROW,251);

   PlotIndexSetInteger(6,PLOT_ARROW,251);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferMA1,true);

   ArraySetAsSeries(BufferMA2,true);

   ArraySetAsSeries(BufferMA3,true);

   ArraySetAsSeries(BufferEntryB,true);

   ArraySetAsSeries(BufferEntryS,true);

   ArraySetAsSeries(BufferExitB,true);

   ArraySetAsSeries(BufferExitS,true);

   ArraySetAsSeries(BufferSignal,true);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Three MA Trend Continuation ("+(string)period_ma1+","+(string)period_ma2+","+(string)period_ma3+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- create handle

   ResetLastError();

   handle_ma1=iMA(NULL,PERIOD_CURRENT,period_ma1,0,InpMethodMA1,PRICE_CLOSE);

   if(handle_ma1==INVALID_HANDLE)

     {

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

      return INIT_FAILED;

     }

   handle_ma2=iMA(NULL,PERIOD_CURRENT,period_ma2,0,InpMethodMA2,PRICE_CLOSE);

   if(handle_ma2==INVALID_HANDLE)

     {

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

      return INIT_FAILED;

     }

   handle_ma3=iMA(NULL,PERIOD_CURRENT,period_ma3,0,InpMethodMA3,PRICE_CLOSE);

   if(handle_ma3==INVALID_HANDLE)

     {

      Print("The iMA(",(string)period_ma3,") 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(close,true);

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

   if(rates_total<fmax(period_max,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(BufferMA1,EMPTY_VALUE);

      ArrayInitialize(BufferMA2,EMPTY_VALUE);

      ArrayInitialize(BufferMA3,EMPTY_VALUE);

      ArrayInitialize(BufferEntryB,EMPTY_VALUE);

      ArrayInitialize(BufferEntryS,EMPTY_VALUE);

      ArrayInitialize(BufferExitB,EMPTY_VALUE);

      ArrayInitialize(BufferExitS,EMPTY_VALUE);

      ArrayInitialize(BufferSignal,0);

     }



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

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

   copied=CopyBuffer(handle_ma1,0,0,count,BufferMA1);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_ma2,0,0,count,BufferMA2);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_ma3,0,0,count,BufferMA3);

   if(copied!=count) return 0;



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

   static int last_sign=0;

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

     {

      BufferEntryB[i]=BufferEntryS[i]=BufferExitB[i]=BufferExitS[i]=EMPTY_VALUE;

      if(close[i]>BufferMA1[i] && close[i]>BufferMA2[i] && close[i]>BufferMA3[i])

         BufferSignal[i]=2;

      else if(close[i]<BufferMA1[i] && close[i]<BufferMA2[i] && close[i]<BufferMA3[i])

         BufferSignal[i]=-2;

      else if(close[i]<BufferMA1[i] && close[i]<BufferMA2[i] && close[i]>BufferMA3[i])

         BufferSignal[i]=1;

      else if(close[i]>BufferMA1[i] && close[i]>BufferMA2[i] && close[i]<BufferMA3[i])

         BufferSignal[i]=-1;

      else

         BufferSignal[i]=BufferSignal[i+1];



      if(BufferSignal[i]!=BufferSignal[i+1])

        {

         int signal=(int)BufferSignal[i];

         switch(signal)

           {

            case  2  : BufferEntryB[i]=fmax(BufferMA1[i],fmax(BufferMA2[i],BufferMA3[i]));  break;

            case  1  : BufferExitB[i]=fmin(BufferMA1[i],BufferMA2[i]);  break;

            case -1  : BufferExitS[i]=fmax(BufferMA1[i],BufferMA2[i]);  break;

            case -2  : BufferEntryS[i]=fmin(BufferMA1[i],fmin(BufferMA2[i],BufferMA3[i]));  break;

            default  : break;

           }

         if(i==0 && signal!=0 && signal!=last_sign)

           {

            string mess=(signal==1 ? "Exit from a long position" : signal==2 ? "Entering a long position" : signal==-1 ? "Exit from a short position" : signal==-2 ? "Entering a short position" : "");

            Notifications(mess,InpUseAlert,InpSendPush,InpSendMail);

            last_sign=signal;

           }

        }

     }

   

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