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

//|                                                         GRAB.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 "GRAB indicator"

#property indicator_chart_window

#property indicator_buffers 9

#property indicator_plots   5

//--- plot Signal

#property indicator_label1  "MA/Price Signal"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot MAH

#property indicator_label2  "High MA"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot MAC

#property indicator_label3  "Close MA"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrGray

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot MAL

#property indicator_label4  "Low MA"

#property indicator_type4   DRAW_LINE

#property indicator_color4  clrGreen

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- plot Candle

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

#property indicator_type5   DRAW_COLOR_CANDLES

#property indicator_color5  clrLimeGreen,clrLightBlue,clrGreen,clrOrangeRed,clrBurlyWood,clrFireBrick,clrSilver

#property indicator_style5  STYLE_SOLID

#property indicator_width5  1

//--- enums

enum ENUM_CANDLE_TYPE

  {

   CANDLE_TYPE_BULLISH,

   CANDLE_TYPE_BEARISH,

   CANDLE_TYPE_NEUTRAL

  };

//---

enum ENUM_CANDLE_POSITION_TYPE

  {

   CANDLE_ABOVE_BULLISH    =  10,

   CANDLE_ABOVE_BEARISH    =  5,

   CANDLE_BELOW_BEARISH    = -10,

   CANDLE_BELOW_BULLISH    = -5,

   CANDLE_INSIDE_BULLISH   =  1,

   CANDLE_INSIDE_BEARISH   = -1

  };

//---

enum ENUM_INPUT_YES_NO

  {

   INPUT_YES   =  1,    // Yes

   INPUT_NO    =  0     // No

  };

//--- input parameters

input uint              InpPeriod   =  34;         // MA period

input ENUM_MA_METHOD    InpMethod   =  MODE_EMA;   // MA method

input ENUM_INPUT_YES_NO InpShowMA   =  INPUT_YES;  // Show MA lines

//--- indicator buffers

double         BufferSignal[];

double         BufferMAH[];

double         BufferMAC[];

double         BufferMAL[];

double         BufferCandleO[];

double         BufferCandleH[];

double         BufferCandleL[];

double         BufferCandleC[];

double         BufferColors[];

//--- global variables

int            period_ma;

int            handle_maH;

int            handle_maC;

int            handle_maL;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

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

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferSignal,INDICATOR_DATA);

   SetIndexBuffer(1,BufferMAH,INDICATOR_DATA);

   SetIndexBuffer(2,BufferMAC,INDICATOR_DATA);

   SetIndexBuffer(3,BufferMAL,INDICATOR_DATA);

   SetIndexBuffer(4,BufferCandleO,INDICATOR_DATA);

   SetIndexBuffer(5,BufferCandleH,INDICATOR_DATA);

   SetIndexBuffer(6,BufferCandleL,INDICATOR_DATA);

   SetIndexBuffer(7,BufferCandleC,INDICATOR_DATA);

   SetIndexBuffer(8,BufferColors,INDICATOR_COLOR_INDEX);

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

   PlotIndexSetInteger(0,PLOT_ARROW,32);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"GRAB ("+(string)period_ma+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting plot buffer parameters

   PlotIndexSetInteger(1,PLOT_DRAW_TYPE,InpShowMA);

   PlotIndexSetInteger(2,PLOT_DRAW_TYPE,InpShowMA);

   PlotIndexSetInteger(3,PLOT_DRAW_TYPE,InpShowMA);

   PlotIndexSetInteger(4,PLOT_SHOW_DATA,false);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferSignal,true);

   ArraySetAsSeries(BufferMAH,true);

   ArraySetAsSeries(BufferMAC,true);

   ArraySetAsSeries(BufferMAL,true);

   ArraySetAsSeries(BufferCandleO,true);

   ArraySetAsSeries(BufferCandleH,true);

   ArraySetAsSeries(BufferCandleL,true);

   ArraySetAsSeries(BufferCandleC,true);

   ArraySetAsSeries(BufferColors,true);

//--- create MA's handles

   ResetLastError();

   handle_maH=iMA(NULL,PERIOD_CURRENT,period_ma,0,InpMethod,PRICE_HIGH);

   if(handle_maH==INVALID_HANDLE)

     {

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

      return INIT_FAILED;

     }

   ResetLastError();

   handle_maC=iMA(NULL,PERIOD_CURRENT,period_ma,0,InpMethod,PRICE_CLOSE);

   if(handle_maC==INVALID_HANDLE)

     {

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

      return INIT_FAILED;

     }

   ResetLastError();

   handle_maL=iMA(NULL,PERIOD_CURRENT,period_ma,0,InpMethod,PRICE_LOW);

   if(handle_maL==INVALID_HANDLE)

     {

      Print("The iMA(",(string)period_ma,") by PRICE_LOW 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<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-1;

      ArrayInitialize(BufferSignal,EMPTY_VALUE);

      ArrayInitialize(BufferMAH,EMPTY_VALUE);

      ArrayInitialize(BufferMAC,EMPTY_VALUE);

      ArrayInitialize(BufferMAL,EMPTY_VALUE);

      ArrayInitialize(BufferCandleO,EMPTY_VALUE);

      ArrayInitialize(BufferCandleH,EMPTY_VALUE);

      ArrayInitialize(BufferCandleL,EMPTY_VALUE);

      ArrayInitialize(BufferCandleC,EMPTY_VALUE);

      ArrayInitialize(BufferColors,6);

     }

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

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

   copied=CopyBuffer(handle_maH,0,0,count,BufferMAH);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_maC,0,0,count,BufferMAC);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_maL,0,0,count,BufferMAL);

   if(copied!=count) return 0;



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

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

     {

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

      SetCandle(i,open,high,low,close);

      ENUM_CANDLE_TYPE candle=CandleType(open[i],close[i]);

      

      if(close[i]>BufferMAH[i])

        {

         if(candle==CANDLE_TYPE_BULLISH)

           {

            BufferSignal[i]=CANDLE_ABOVE_BULLISH;

            BufferColors[i]=0;

           }

         if(candle==CANDLE_TYPE_BEARISH)

           {

            BufferSignal[i]=CANDLE_ABOVE_BEARISH;

            BufferColors[i]=1;

           }

        }

      else if(close[i]<BufferMAL[i])

        {

         if(candle==CANDLE_TYPE_BEARISH)

           {

            BufferSignal[i]=CANDLE_BELOW_BEARISH;

            BufferColors[i]=3;

           }

         if(candle==CANDLE_TYPE_BULLISH)

           {

            BufferSignal[i]=CANDLE_BELOW_BULLISH;

            BufferColors[i]=4;

           }

        }

      else if(close[i]<BufferMAH[i] && close[i]>BufferMAL[i])

        {

         if(candle==CANDLE_TYPE_BULLISH)

           {

            BufferSignal[i]=CANDLE_INSIDE_BULLISH;

            BufferColors[i]=2;

           }

         if(candle==CANDLE_TYPE_BEARISH)

           {

            BufferSignal[i]=CANDLE_INSIDE_BEARISH;

            BufferColors[i]=5;

           }

        }

     }

   

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

   return(rates_total);

  }

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

//| >72@0I05B B8? A25G8                                             |

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

ENUM_CANDLE_TYPE CandleType(const double open,const double close)

  {

   return(open<close ? CANDLE_TYPE_BULLISH : open>close ? CANDLE_TYPE_BEARISH : CANDLE_TYPE_NEUTRAL);

  }

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

//| #AB0=02;8205B 1CD5@ A25G8                                        |

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

void SetCandle(const int shift,const double &open[],const double &high[],const double &low[],const double &close[])

  {

   BufferCandleO[shift]=open[shift];

   BufferCandleH[shift]=high[shift];

   BufferCandleL[shift]=low[shift];

   BufferCandleC[shift]=close[shift];

  }

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

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