//+------------------------------------------------------------------+ //| MACD - original.mq5 | //+------------------------------------------------------------------+ #property strict #property description "Moving Average Convergence/Divergence" #property indicator_separate_window #property indicator_buffers 7 // // // // // enum enPrices { pr_close, // Close pr_open, // Open pr_high, // High pr_low, // Low pr_median, // Median pr_typical, // Typical pr_weighted, // Weighted pr_average, // Average (high+low+open+close)/4 pr_medianb, // Average median body (open+close)/2 pr_tbiased, // Trend biased price pr_haclose, // Heiken ashi close pr_haopen , // Heiken ashi open pr_hahigh, // Heiken ashi high pr_halow, // Heiken ashi low pr_hamedian, // Heiken ashi median pr_hatypical, // Heiken ashi typical pr_haweighted, // Heiken ashi weighted pr_haaverage, // Heiken ashi average pr_hamedianb, // Heiken ashi median body pr_hatbiased // Heiken ashi trend biased price }; input int InpFastEMA = 12; // Macd fast period input int InpSlowEMA = 26; // Macd slow period input int InpSignalEMA = 9; // Signal period input enPrices InpAppliedPrice = pr_close; // Price to use double ExtMacdBuffer[],ExtMacdBuffer2[]; double ExtMacdBufferl[]; double ExtSignalBuffer[]; double ExtFastMaBuffer[]; double ExtSlowMaBuffer[]; double Colors[]; double Colorsl[]; double Colorss[]; #property indicator_plots 3 #property indicator_type1 DRAW_COLOR_HISTOGRAM2 #property indicator_type2 DRAW_COLOR_LINE #property indicator_type3 DRAW_COLOR_LINE #property indicator_color1 clrDimGray, clrLimeGreen, clrGreen, clrRed, clrFireBrick #property indicator_color2 clrDimGray, clrLimeGreen, clrGreen, clrRed, clrFireBrick #property indicator_color3 clrLimeGreen, clrRed #property indicator_width1 2 #property indicator_width2 2 #property indicator_width3 2 #property indicator_label1 "MACD" #property indicator_label2 "MACD line" #property indicator_label3 "Signal" void OnInit() { SetIndexBuffer(0,ExtMacdBuffer, INDICATOR_DATA); SetIndexBuffer(1,ExtMacdBuffer2, INDICATOR_DATA); SetIndexBuffer(2,Colors ,INDICATOR_COLOR_INDEX); SetIndexBuffer(3,ExtMacdBufferl, INDICATOR_DATA); SetIndexBuffer(4,Colorsl ,INDICATOR_COLOR_INDEX); SetIndexBuffer(5,ExtSignalBuffer,INDICATOR_DATA); SetIndexBuffer(6,Colorss ,INDICATOR_COLOR_INDEX); PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,InpSignalEMA-1); IndicatorSetString(INDICATOR_SHORTNAME,"MACD original("+string(InpFastEMA)+","+string(InpSlowEMA)+","+string(InpSignalEMA)+")"); } 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 &TickVolume[], const long &Volume[], const int &Spread[]) { // // // // // for (int i=(int)MathMax(prev_calculated-1,0); i0) { if (ExtMacdBuffer[i]>0) { if (ExtMacdBuffer[i]>ExtMacdBuffer[i-1]) Colors[i] = 1; if (ExtMacdBuffer[i]ExtMacdBuffer[i-1]) Colors[i] = 4; } } Colorsl[i] = Colors[i]; if (ExtMacdBufferl[i]>ExtSignalBuffer[i]) Colorss[i] = 0; if (ExtMacdBufferl[i]0) workEma[r][instanceNo] = workEma[r-1][instanceNo]+alpha*(price-workEma[r-1][instanceNo]); return(workEma[r][instanceNo]); } double workHa[][4]; double getPrice(int price, const double& open[], const double& close[], const double& high[], const double& low[], int bars, int i, int instanceNo=0) { if (price>=pr_haclose) { if (ArrayRange(workHa,0)!= bars) ArrayResize(workHa,bars); double haOpen; if (i>0) haOpen = (workHa[i-1][instanceNo+2] + workHa[i-1][instanceNo+3])/2.0; else haOpen = (open[i]+close[i])/2; double haClose = (open[i] + high[i] + low[i] + close[i]) / 4.0; double haHigh = MathMax(high[i], MathMax(haOpen,haClose)); double haLow = MathMin(low[i] , MathMin(haOpen,haClose)); if(haOpen haOpen) return((haHigh+haClose)/2.0); else return((haLow+haClose)/2.0); } } switch (price) { case pr_close: return(close[i]); case pr_open: return(open[i]); case pr_high: return(high[i]); case pr_low: return(low[i]); case pr_median: return((high[i]+low[i])/2.0); case pr_medianb: return((open[i]+close[i])/2.0); case pr_typical: return((high[i]+low[i]+close[i])/3.0); case pr_weighted: return((high[i]+low[i]+close[i]+close[i])/4.0); case pr_average: return((high[i]+low[i]+close[i]+open[i])/4.0); case pr_tbiased: if (close[i]>open[i]) return((high[i]+close[i])/2.0); else return((low[i]+close[i])/2.0); } return(0); } int Set_IndexBuffer5(int Buf, double &array[], ENUM_INDEXBUFFER_TYPE type) { Buf+=1; SetIndexBuffer(Buf,array,type); return(Buf); } int Set_IndexBuffer5(int Buf, double &array[], ENUM_INDEXBUFFER_TYPE type, int drawtype, int width, string label, color clrplot0=clrNONE, color clrplot1=clrNONE, color clrplot2=clrNONE, color clrplot3=clrNONE, color clrplot4=clrNONE, color clrplot5=clrNONE) { Buf+=1; SetIndexBuffer(Buf,array,type); PlotIndexSetString(Buf,PLOT_LABEL,label); PlotIndexSetInteger(Buf,PLOT_LINE_WIDTH,width); if(type != INDICATOR_DATA) return(Buf); PlotIndexSetInteger(Buf,PLOT_DRAW_TYPE,drawtype); if(drawtype == DRAW_NONE || clrplot0 == clrNONE) return(Buf); int colorcount=0; if(clrplot0 !=clrNONE ) Set_PlotIndexColor(Buf, colorcount,clrplot0); if(clrplot1 !=clrNONE ) Set_PlotIndexColor(Buf, colorcount,clrplot1); if(clrplot2 !=clrNONE ) Set_PlotIndexColor(Buf, colorcount,clrplot2); if(clrplot3 !=clrNONE ) Set_PlotIndexColor(Buf, colorcount,clrplot3); if(clrplot4 !=clrNONE ) Set_PlotIndexColor(Buf, colorcount,clrplot4); if(clrplot4 !=clrNONE ) Set_PlotIndexColor(Buf, colorcount,clrplot5); PlotIndexSetInteger(Buf,PLOT_DRAW_BEGIN,0); return(Buf); } void Set_PlotIndexColor(int buf, int &colorcount, color clrcolor) { PlotIndexSetInteger(buf,PLOT_LINE_COLOR,colorcount,clrcolor); colorcount++; } int getIndexOfColor(int bar, double &array[]) { if(bar ==0) return(1); if(array[bar] < array[bar-1]) return(0); if(array[bar] > array[bar-1]) return(2); return(1); }