CCI_MACD_Overlay

Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Indicators Used
Commodity channel indexMACD Histogram
0 Views
0 Downloads
0 Favorites
CCI_MACD_Overlay
ÿþ//+------------------------------------------------------------------+

//|                                             CCI_MACD_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 "CCI/MACD Overlay indicator"

#property indicator_chart_window

#property indicator_buffers 11

#property indicator_plots   4

//--- plot UP

#property indicator_label1  "Up"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot DN

#property indicator_label2  "Down"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrRed

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

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- input parameters

input uint     InpPeriodCCI   =  14;   // CCI period

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

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

input uint     InpPeriodSig   =  9;    // MACD Signal period

//--- indicator buffers

double         BufferUP[];

double         BufferDN[];

double         BufferNL[];

double         BufferCandleO[];

double         BufferCandleH[];

double         BufferCandleL[];

double         BufferCandleC[];

double         BufferColors[];

double         BufferCCI[];

double         BufferMACD[];

double         BufferSig[];

//--- global variables

int            period_cci;

int            period_fast;

int            period_slow;

int            period_sig;

int            period_max;

int            handle_cci;

int            handle_macd;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   period_cci=int(InpPeriodCCI<1 ? 1 : InpPeriodCCI);

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

   period_max=fmax(period_cci,fmax(period_sig,fmax(period_fast,period_slow)));

//--- 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,BufferCCI,INDICATOR_CALCULATIONS);

   SetIndexBuffer(9,BufferMACD,INDICATOR_CALCULATIONS);

   SetIndexBuffer(10,BufferSig,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);

   PlotIndexSetInteger(2,PLOT_ARROW,158);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"CCI/MACD Overlay("+(string)period_cci+","+(string)period_fast+","+(string)period_slow+","+(string)period_sig+")");

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

   ArraySetAsSeries(BufferMACD,true);

   ArraySetAsSeries(BufferSig,true);

//--- create handles

   ResetLastError();

   handle_cci=iCCI(NULL,PERIOD_CURRENT,period_cci,PRICE_TYPICAL);

   if(handle_cci==INVALID_HANDLE)

     {

      Print("The iCCI(",(string)period_cci,") 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(",(string)period_fast,",",(string)period_slow,",",(string)period_sig,") 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_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(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,2);

      ArrayInitialize(BufferCCI,0);

      ArrayInitialize(BufferMACD,0);

      ArrayInitialize(BufferSig,0);

     }

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

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

   copied=CopyBuffer(handle_cci,MAIN_LINE,0,count,BufferCCI);

   if(copied!=count) return 0;

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

   if(copied!=count) return 0;

   copied=CopyBuffer(handle_macd,SIGNAL_LINE,0,count,BufferSig);

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

      int status=CheckStatus(i);

      BufferColors[i]=status;

      if(status==0)

         BufferUP[i]=open[i];

      else if(status==1)

         BufferDN[i]=open[i];

      else if(status==2)

         BufferNL[i]=open[i];

     }

   

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

     (

      BufferCCI[shift]>0 && BufferMACD[shift]>BufferSig[shift] ? 0 :

      BufferCCI[shift]<0 && BufferMACD[shift]<BufferSig[shift] ? 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 ---