Macd(2lines)

Author: Copyright 2017,fxMeter.
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
Macd(2lines)
ÿþ//+------------------------------------------------------------------+

//|                                                  MACD(2lines).mq5|

//|                                         Copyright 2017, fxMeter. |

//|                            https://www.mql5.com/zh/users/fxmeter |

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

//2021-10-15 12:02:34 V3.0 ㉳QMT5(W10g14åeGS§~0RBuild 3080&^eg„v˜

//2021-10-14 18:27:15 V2.0 Updated

//2020-2-5 15:13:49 9ebMT5Hr,g

//MT5Hr,gÿhttps://www.mql5.com/zh/code/27838

//MT4Hr,gÿhttps://www.mql5.com/zh/code/27603

//2017-04-27 13:54:38 	cgqýV…Q¡€hyoöN¾áO-N„vMACDlQ_™Q

/*

SHORT=12,LONG=26,MID=9

DIF:EMA(CLOSE,SHORT)-EMA(CLOSE,LONG);

DEA:EMA(DIF,MID);

MACD:(DIF-DEA)*2,COLORSTICK;



ô‹f:

ýV…QŠb DIF-DEAëSMACD(;ubñgP[)

€ž[E–
NýVE–
NÿSìbMT4„v…QnlQ_-Ný/fŠbDIFëSMACD(;ubñgP[),ŠbDEAëSSignal,¡l	g(DIF-DEA)*2

œ˜r‚	cgq¾áOMCADM‘n



*/

#property copyright "Copyright 2017,fxMeter."

#property link      "https://www.mql5.com/zh/users/fxmeter"

#property version   "4.00"

#property strict

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_plots   4

//--- plot DIF

#property indicator_label1  "DIF"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrSilver

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot DEA

#property indicator_label2  "DEA"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrYellow

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot macd hist+

#property indicator_label3  "Macd+"

#property indicator_type3   DRAW_HISTOGRAM

#property indicator_color3  clrRed

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot macd hist-

#property indicator_label4  "Macd-"

#property indicator_type4   DRAW_HISTOGRAM

#property indicator_color4  clrAqua

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1





input int FastEMA = 12;

input int SlowEMA = 26;

input int MACDEMA = 9;





//--- indicator buffers

double         DIFBuffer[];

double         DEABuffer[];

double         MacdHistBuffer[];

double         MacdHistBuffer1[];

double w=0,w1=0;

int fastHandle=-1,slowHandle=-1;

double fast[1],slow[1];

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

//| Custom indicator initialization function                         |

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

int OnInit()

{

//--- indicator buffers mapping

   SetIndexBuffer(0,DIFBuffer);

   SetIndexBuffer(1,DEABuffer);

   SetIndexBuffer(2,MacdHistBuffer);

   SetIndexBuffer(3,MacdHistBuffer1);



   ArraySetAsSeries(DIFBuffer,true);

   ArraySetAsSeries(DEABuffer,true);

   ArraySetAsSeries(MacdHistBuffer,true);

   ArraySetAsSeries(MacdHistBuffer1,true);



   for(int i=0; i<4; i++)

   {

      PlotIndexSetDouble(i,PLOT_EMPTY_VALUE,0.0);

      PlotIndexSetInteger(i,PLOT_DRAW_BEGIN,3);

   }





   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);



   string name = "MACD("+(string)FastEMA+","+(string)SlowEMA+","+(string)MACDEMA+")";

   IndicatorSetString(INDICATOR_SHORTNAME,name);



   if(FastEMA<0 || SlowEMA<0 || MACDEMA<0)

      return(INIT_FAILED);



   w = 2.0/(MACDEMA + 1);

   w1= 1.0-w;



   fastHandle = iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE);

   slowHandle = iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE);

   if(fastHandle==-1||slowHandle==-1)

   {

      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[])

{

//---

   int i,limit=0;

   if(rates_total<=0)return(0);

   if(prev_calculated<=0)limit=rates_total-1;

   else limit=rates_total-prev_calculated+1;

   double hst=0.0;



   if(prev_calculated<=0)

   {

      ArrayInitialize(DIFBuffer,0.0);

      ArrayInitialize(DEABuffer,0.0);

      ArrayInitialize(MacdHistBuffer,0.0);

      ArrayInitialize(MacdHistBuffer1,0.0);

   }



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

   {

      if(i>=rates_total-1)

      {

         DIFBuffer[i] = DEABuffer[i] = MacdHistBuffer[i] = MacdHistBuffer1[i] = 0;

         continue;

      }

      if(CopyBuffer(fastHandle,0,i,1,fast)!=1)return(0);

      if(CopyBuffer(slowHandle,0,i,1,slow)!=1)return(0);

      DIFBuffer[i]=fast[0]-slow[0];

      DEABuffer[i]=w*DIFBuffer[i]+w1*DEABuffer[i+1];

      hst = 2.0*(DIFBuffer[i]-DEABuffer[i]);

      if(hst>=0)

      {

         MacdHistBuffer[i]=hst;

         MacdHistBuffer1[i]=0.0;

      }

      else

      {

         MacdHistBuffer1[i]=hst;

         MacdHistBuffer[i]=0.0;

      }



   }



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

   return(rates_total);

}

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



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