MACD_Stochastic_Overlay

Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Indicators Used
Stochastic oscillatorMACD Histogram
0 Views
0 Downloads
0 Favorites
MACD_Stochastic_Overlay
ÿþ//+------------------------------------------------------------------+

//|                                 MACD_Slow_Stochastic_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 "MACD/Stochastic Overlay indicator"

#property indicator_chart_window

#property indicator_buffers 10

#property indicator_plots   4

//--- plot UP

#property indicator_label1  "Both Up"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrLimeGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot DN

#property indicator_label2  "Both Down"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrOrangeRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot NL

#property indicator_label3  "Neutral"

#property indicator_type3   DRAW_ARROW

#property indicator_color3  clrDarkGray

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot Candle

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

#property indicator_type4   DRAW_COLOR_CANDLES

#property indicator_color4  clrLimeGreen,clrDarkSeaGreen,clrOrangeRed,clrBurlyWood,clrLightGray

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- input parameters

input uint                 InpPeriodFast     =  12;            // MACD Fast EMA period

input uint                 InpPeriodSlow     =  26;            // MACD Slow EMA period

input uint                 InpPeriodSig      =  9;             // MACD SMA period

input ENUM_APPLIED_PRICE   InpAppliedPrice   =  PRICE_CLOSE;   // MACD Applied price

input uint                 InpPeriodK        =  5;             // Stochastic Period %K

input uint                 InpPeriodD        =  3;             // Stochastic Period %D

input uint                 InpSlowing        =  3;             // Stochastic Slowing

input ENUM_MA_METHOD       InpMethod         =  MODE_SMA;      // Stochastic Method

input ENUM_STO_PRICE       InpPriceField     =  STO_LOWHIGH;   // Stochastic Price field

//--- indicator buffers

double         BufferUP[];

double         BufferDN[];

double         BufferNL[];

double         BufferCandleO[];

double         BufferCandleH[];

double         BufferCandleL[];

double         BufferCandleC[];

double         BufferColors[];

double         BufferSTO[];

double         BufferMACD[];

//--- global variables

int            period_k;

int            period_d;

int            slowing;

int            period_fast;

int            period_slow;

int            period_sig;

int            handle_sto;

int            handle_macd;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   period_k=int(InpPeriodK<1 ? 1 : InpPeriodK);

   period_d=int(InpPeriodD<1 ? 1 : InpPeriodD);

   slowing=int(InpSlowing<1 ? 1 : InpSlowing);

   period_fast=int(InpPeriodFast<1 ? 1 : InpPeriodFast);

   period_slow=int(InpPeriodSlow==period_fast ? period_fast+1 : InpPeriodSlow<1 ? 1 : InpPeriodSlow);

   period_sig=int(InpPeriodSig<1 ? 1 : InpPeriodSig);

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferUP,INDICATOR_DATA);

   SetIndexBuffer(1,BufferDN,INDICATOR_DATA);

   SetIndexBuffer(2,BufferNL,INDICATOR_DATA);

   SetIndexBuffer(3,BufferCandleO,INDICATOR_DATA);

   SetIndexBuffer(4,BufferCandleH,INDICATOR_DATA);

   SetIndexBuffer(5,BufferCandleL,INDICATOR_DATA);

   SetIndexBuffer(6,BufferCandleC,INDICATOR_DATA);

   SetIndexBuffer(7,BufferColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(8,BufferSTO,INDICATOR_CALCULATIONS);

   SetIndexBuffer(9,BufferMACD,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);

   PlotIndexSetInteger(2,PLOT_ARROW,32);

//--- setting indicator parameters

   string macd_params=(string)period_fast+","+(string)period_slow+","+(string)period_sig;

   string sto_params=(string)period_k+","+(string)period_d+","+(string)slowing;

   IndicatorSetString(INDICATOR_SHORTNAME,"MACD("+macd_params+") Stochastic("+sto_params+") Overlay");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting plot buffer parameters

   PlotIndexSetInteger(3,PLOT_SHOW_DATA,false);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferUP,true);

   ArraySetAsSeries(BufferDN,true);

   ArraySetAsSeries(BufferNL,true);

   ArraySetAsSeries(BufferCandleO,true);

   ArraySetAsSeries(BufferCandleH,true);

   ArraySetAsSeries(BufferCandleL,true);

   ArraySetAsSeries(BufferCandleC,true);

   ArraySetAsSeries(BufferColors,true);

   ArraySetAsSeries(BufferSTO,true);

   ArraySetAsSeries(BufferMACD,true);

//--- create handles

   ResetLastError();

   handle_sto=iStochastic(NULL,PERIOD_CURRENT,period_k,period_d,slowing,InpMethod,InpPriceField);

   if(handle_sto==INVALID_HANDLE)

     {

      Print("The iStochastic(",sto_params,") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

   handle_macd=iMACD(NULL,PERIOD_CURRENT,period_fast,period_slow,period_sig,PRICE_CLOSE);

   if(handle_macd==INVALID_HANDLE)

     {

      Print("The iMACD(",macd_params,") 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<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(BufferUP,EMPTY_VALUE);

      ArrayInitialize(BufferDN,EMPTY_VALUE);

      ArrayInitialize(BufferNL,EMPTY_VALUE);

      ArrayInitialize(BufferCandleO,EMPTY_VALUE);

      ArrayInitialize(BufferCandleH,EMPTY_VALUE);

      ArrayInitialize(BufferCandleL,EMPTY_VALUE);

      ArrayInitialize(BufferCandleC,EMPTY_VALUE);

      ArrayInitialize(BufferColors,4);

      ArrayInitialize(BufferSTO,0);

      ArrayInitialize(BufferMACD,0);

     }

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

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

   copied=CopyBuffer(handle_sto,MAIN_LINE,0,count,BufferSTO);

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_macd,MAIN_LINE,0,count,BufferMACD);

   if(copied!=count) return 0;

   

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

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

     {

      BufferUP[i]=BufferDN[i]=BufferNL[i]=EMPTY_VALUE;

      BufferCandleO[i]=open[i];

      BufferCandleH[i]=high[i];

      BufferCandleL[i]=low[i];

      BufferCandleC[i]=close[i];

      BufferColors[i]=4;

      int status=CheckStatus(i);

      if(status==0)

        {

         BufferUP[i]=open[i];

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

        }

      else if(status==1)

        {

         BufferDN[i]=open[i];

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

        }

      else

        {

         BufferNL[i]=open[i];

         BufferColors[i]=4;

        }

     }

   

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

   return(rates_total);

  }

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

//| @>25@O5B A>>B=>H5=85 ;8=89                                      |

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

int CheckStatus(const int shift)

  {

   return

     (

      BufferSTO[shift]>BufferSTO[shift+1] && BufferMACD[shift]>BufferMACD[shift+1] ? 0 :

      BufferSTO[shift]<BufferSTO[shift+1] && BufferMACD[shift]<BufferMACD[shift+1] ? 1 : 2

     );

  }

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

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