MA_Position_Overlay

Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
MA_Position_Overlay
ÿþ//+------------------------------------------------------------------+

//|                                          MA_Position_Overlay.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 "MA Position Overlay indicator"

#property description "In this variation of the MA Position, the candles in the current chart"

#property description "will be colored according to the following conditions:"

#property description "Green: MA1 > MA2 and MA2 > MA3"

#property description "Red: MA1 < MA2 and MA2 < MA3"

#property indicator_chart_window

#property indicator_buffers 10

#property indicator_plots   3

//--- plot SigUP

#property indicator_label1  "Up direction"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot SigDN

#property indicator_label2  "Down direction"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrRed

#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  clrLimeGreen,clrDarkSeaGreen,clrOrangeRed,clrBurlyWood,clrDarkGray

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- input parameters

input uint                 InpPeriodMA1         =  5;             // First MA period

input ENUM_MA_METHOD       InpMethodMA1         =  MODE_EMA;      // First MA method

input ENUM_APPLIED_PRICE   InpAppliedPriceMA1   =  PRICE_CLOSE;   // First MA applied price

input uint                 InpPeriodMA2         =  10;            // Second MA period

input ENUM_MA_METHOD       InpMethodMA2         =  MODE_EMA;      // Second MA method

input ENUM_APPLIED_PRICE   InpAppliedPriceMA2   =  PRICE_CLOSE;   // Second MA applied price

input uint                 InpPeriodMA3         =  20;            // Third MA period

input ENUM_MA_METHOD       InpMethodMA3         =  MODE_EMA;      // Third MA method

input ENUM_APPLIED_PRICE   InpAppliedPriceMA3   =  PRICE_CLOSE;   // Third MA applied price

//--- indicator buffers

double         BufferSigUP[];

double         BufferSigDN[];

double         BufferCandleO[];

double         BufferCandleH[];

double         BufferCandleL[];

double         BufferCandleC[];

double         BufferColors[];

double         BufferMA1[];

double         BufferMA2[];

double         BufferMA3[];

//--- 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,BufferSigUP,INDICATOR_DATA);

   SetIndexBuffer(1,BufferSigDN,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,BufferMA1,INDICATOR_CALCULATIONS);

   SetIndexBuffer(8,BufferMA2,INDICATOR_CALCULATIONS);

   SetIndexBuffer(9,BufferMA3,INDICATOR_CALCULATIONS);

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

   PlotIndexSetInteger(0,PLOT_ARROW,158);

   PlotIndexSetInteger(1,PLOT_ARROW,158);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"MA Position Overlay indicator");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting plot buffer parameters

   PlotIndexSetInteger(2,PLOT_SHOW_DATA,false);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferSigUP,true);

   ArraySetAsSeries(BufferSigDN,true);

   ArraySetAsSeries(BufferCandleO,true);

   ArraySetAsSeries(BufferCandleH,true);

   ArraySetAsSeries(BufferCandleL,true);

   ArraySetAsSeries(BufferCandleC,true);

   ArraySetAsSeries(BufferColors,true);

   ArraySetAsSeries(BufferMA1,true);

   ArraySetAsSeries(BufferMA2,true);

   ArraySetAsSeries(BufferMA3,true);

//--- create MA handles

   ResetLastError();

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

   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,InpAppliedPriceMA2);

   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,InpAppliedPriceMA3);

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

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(close,true);

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

   if(rates_total<period_max) return 0;

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

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-1;

      ArrayInitialize(BufferSigUP,EMPTY_VALUE);

      ArrayInitialize(BufferSigDN,EMPTY_VALUE);

      ArrayInitialize(BufferCandleO,EMPTY_VALUE);

      ArrayInitialize(BufferCandleH,EMPTY_VALUE);

      ArrayInitialize(BufferCandleL,EMPTY_VALUE);

      ArrayInitialize(BufferCandleC,EMPTY_VALUE);

      ArrayInitialize(BufferColors,4);

      ArrayInitialize(BufferMA1,0);

      ArrayInitialize(BufferMA2,0);

      ArrayInitialize(BufferMA3,0);

     }

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

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

   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

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

     {

      if(CheckPosition(i)>0)

        {

         BufferSigUP[i]=open[i];

         BufferCandleO[i]=open[i];

         BufferCandleH[i]=high[i];

         BufferCandleL[i]=low[i];

         BufferCandleC[i]=close[i];

         BufferColors[i]=(open[i]<close[i] ? 0 : open[i]>close[i] ? 1 : 4);

        }

      else if(CheckPosition(i)<0)

        {

         BufferSigDN[i]=open[i];

         BufferCandleO[i]=open[i];

         BufferCandleH[i]=high[i];

         BufferCandleL[i]=low[i];

         BufferCandleC[i]=close[i];

         BufferColors[i]=(open[i]>close[i] ? 2 : open[i]<close[i] ? 3 : 4);

        }

      else

        {

         BufferSigUP[i]=EMPTY_VALUE;

         BufferSigDN[i]=EMPTY_VALUE;

         BufferCandleO[i]=open[i];

         BufferCandleH[i]=high[i];

         BufferCandleL[i]=low[i];

         BufferCandleC[i]=close[i];

         BufferColors[i]=4;

        }

     }



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

   return(rates_total);

  }

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

//| >72@0I05B A>>B=>H5=85 ;8=89                                   |

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

int CheckPosition(const int shift)

  {

   return

     (

      BufferMA1[shift]>BufferMA2[shift] && BufferMA2[shift]>BufferMA3[shift] ? 1 :

      BufferMA1[shift]<BufferMA2[shift] && BufferMA2[shift]<BufferMA3[shift] ? -1 : 0

     );

  }

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

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