//------------------------------------------------------------------ #property copyright "mladen" #property link "www.forex-tsd.com" //------------------------------------------------------------------ #property indicator_separate_window #property indicator_buffers 4 #property indicator_color1 LimeGreen #property indicator_color2 DarkOrange #property indicator_color3 DarkOrange #property indicator_color4 Yellow #property indicator_width1 2 #property indicator_width2 2 #property indicator_width3 2 #property indicator_style4 STYLE_DOT #property indicator_level1 0 // // // // // #define PRICE_HA 7 extern int FastPeriod = 24; extern int SlowPeriod = 52; extern int SignalPeriod = 9; extern int Price = PRICE_HA; extern int PricePreSmooth = 1; extern bool ColorOnSignalCross = true; double macd[]; double macdda[]; double macddb[]; double signal[]; double mcolor[]; //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // int init() { IndicatorBuffers(5); SetIndexBuffer(0,macd); SetIndexBuffer(1,macdda); SetIndexBuffer(2,macddb); SetIndexBuffer(3,signal); SetIndexBuffer(4,mcolor); IndicatorShortName("ZeroLag TEMA MACD("+FastPeriod+","+SlowPeriod+","+SignalPeriod+")"); return(0); } int deinit() { return(0); } //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // int start() { int counted_bars = IndicatorCounted(); if(counted_bars < 0) return(-1); if(counted_bars > 0) counted_bars--; int limit = MathMin(Bars-counted_bars,Bars-1); // // // // // double alpha = 2.0/(SignalPeriod+1); if (mcolor[limit]==-1) CleanPoint(limit,macdda,macddb); for (int i=limit; i>=0; i--) { double tema1 = ihTema(i,FastPeriod,Price,PricePreSmooth,0); double fast = tema1+(tema1-iTema(tema1,FastPeriod,i,0)); double tema2 = ihTema(i,SlowPeriod,Price,PricePreSmooth,1); double slow = tema2+(tema2-iTema(tema2,FastPeriod,i,1)); macd[i] = fast-slow; signal[i] = signal[i+1]+alpha*(macd[i]-signal[i+1]); macdda[i] = EMPTY_VALUE; macddb[i] = EMPTY_VALUE; mcolor[i] = mcolor[i+1]; if (ColorOnSignalCross) { if (macd[i]>signal[i]) mcolor[i] = 1; if (macd[i]macd[i+1]) mcolor[i] = 1; if (macd[i]=0; k++) price += (workhTema[r-k][index+4]+workhTema[r-k][index+5]+workhTema[r-k][index+6]+workhTema[r-k][index+7])/4.0; price /= preSmooth; } else price = iMA(NULL,0,preSmooth,0,MODE_SMA,usePrice,i); // // // // // double alpha = 2.0 / (1.0 + period); workhTema[r][index+3] = workhTema[r-1][index+3]+alpha*(price -workhTema[r-1][index+3]); workhTema[r][index+2] = workhTema[r-1][index+2]+alpha*(workhTema[r][index+3]-workhTema[r-1][index+2]); workhTema[r][index+1] = workhTema[r-1][index+1]+alpha*(workhTema[r][index+2]-workhTema[r-1][index+1]); workhTema[r][index+0] = 3*workhTema[r][index+3]-3*workhTema[r][index+2]+workhTema[r][index+1]; return(workhTema[r][index+0]); } //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // double workTema[][6]; #define _ema1 0 #define _ema2 1 #define _ema3 2 double iTema(double price, double period, int r, int instanceNo=0) { if (ArrayRange(workTema,0)!= Bars) ArrayResize(workTema,Bars); instanceNo*=3; r = Bars-r-1; // // // // // double alpha = 2.0 / (1.0+period); workTema[r][_ema1+instanceNo] = workTema[r-1][_ema1+instanceNo]+alpha*(price -workTema[r-1][_ema1+instanceNo]); workTema[r][_ema2+instanceNo] = workTema[r-1][_ema2+instanceNo]+alpha*(workTema[r][_ema1+instanceNo]-workTema[r-1][_ema2+instanceNo]); workTema[r][_ema3+instanceNo] = workTema[r-1][_ema3+instanceNo]+alpha*(workTema[r][_ema2+instanceNo]-workTema[r-1][_ema3+instanceNo]); return(workTema[r][_ema3+instanceNo]+3.0*(workTema[r][_ema1+instanceNo]-workTema[r][_ema2+instanceNo])); } //------------------------------------------------------------------- // //------------------------------------------------------------------- // // // // // void CleanPoint(int i,double& first[],double& second[]) { if ((second[i] != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE)) second[i+1] = EMPTY_VALUE; else if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE)) first[i+1] = EMPTY_VALUE; } // // // // // void PlotPoint(int i,double& first[],double& second[],double& from[]) { if (first[i+1] == EMPTY_VALUE) { if (first[i+2] == EMPTY_VALUE) { first[i] = from[i]; first[i+1] = from[i+1]; second[i] = EMPTY_VALUE; } else { second[i] = from[i]; second[i+1] = from[i+1]; first[i] = EMPTY_VALUE; } } else { first[i] = from[i]; second[i] = EMPTY_VALUE; } }