Time: 2018-08-23 | Download file:Polarized_Fractal_Efficiency_-_Erkhardt_2.mq4
//------------------------------------------------------------------ #property copyright "mladen" #property link "mladenfx@gmail.com" //------------------------------------------------------------------ #property indicator_separate_window #property indicator_buffers 3 #property indicator_color1 LimeGreen #property indicator_color2 Orange #property indicator_color3 Orange #property indicator_width1 2 #property indicator_width2 2 #property indicator_width3 2 // // // // // 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 }; enum enMaTypes { ma_sma, // simple moving average - SMA ma_ema, // exponential moving average - EMA ma_dsema, // double smoothed exponential moving average - DSEMA ma_dema, // double exponential moving average - DEMA ma_tema, // tripple exponential moving average - TEMA ma_smma, // smoothed moving average - SMMA ma_lwma, // linear weighted moving average - LWMA ma_pwma, // parabolic weighted moving average - PWMA ma_alxma, // Alexander moving average - ALXMA ma_vwma, // volume weighted moving average - VWMA ma_hull, // Hull moving average ma_tma, // triangular moving average ma_sine, // sine weighted moving average ma_linr, // linear regression value ma_ie2, // IE/2 ma_nlma, // non lag moving average ma_zlma, // zero lag moving average ma_lead, // leader exponential moving average ma_ssm, // super smoother ma_smoo // smoother }; extern int PFEPeriod = 32; extern enPrices PFEPrice = pr_close; extern int Smooth = 10; extern enMaTypes SmoothMethod = ma_smoo; double buffer1[]; double buffer2[]; double pfeda[]; double pfedb[]; double slope[]; //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // int init() { IndicatorBuffers(4); SetIndexBuffer(0,buffer1); SetIndexBuffer(1,pfeda); SetIndexBuffer(2,pfedb); SetIndexBuffer(3,slope); IndicatorShortName("Polarized fractal efficiency Erkhardt ("+PFEPeriod+","+Smooth+","+getAverageName(SmoothMethod)+")"); return(0); } //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // double mom[]; double div[]; double inp[]; int start() { int i,r,counted_bars=IndicatorCounted(); if(counted_bars<0) return(-1); if(counted_bars>0) counted_bars--; int limit=MathMin(Bars-counted_bars,Bars-1); if (ArraySize(mom)!=Bars) ArrayResize(mom,Bars); if (ArraySize(div)!=Bars) ArrayResize(div,Bars); if (ArraySize(inp)!=Bars) ArrayResize(inp,Bars); // // // // // if (slope[limit]==-1) CleanPoint(limit,pfeda,pfedb); for(i = limit,r=Bars-i-1; i >= 0; i--,r++) { inp[r] = getPrice(PFEPrice,Open,Close,High,Low,i); if (ibuffer1[i+1]) slope[i] = 1; if (buffer1[i] =period) workSma[r][instanceNo+1] = workSma[r-1][instanceNo+1]+(workSma[r][instanceNo]-workSma[r-period][instanceNo])/period; else { workSma[r][instanceNo+1] = 0; for(int k=0; k =0; k++) workSma[r][instanceNo+1] += workSma[r-k][instanceNo]; workSma[r][instanceNo+1] /= k; } return(workSma[r][instanceNo+1]); } // // // // // double workEma[][_maWorkBufferx1]; double iEma(double price, double period, int r, int instanceNo=0) { if (ArrayRange(workEma,0)!= Bars) ArrayResize(workEma,Bars); // // // // // double alpha = 2.0 / (1.0+period); workEma[r][instanceNo] = workEma[r-1][instanceNo]+alpha*(price-workEma[r-1][instanceNo]); return(workEma[r][instanceNo]); } // // // // // double workDsema[][_maWorkBufferx2]; #define _ema1 0 #define _ema2 1 double iDsema(double price, double period, int r, int instanceNo=0) { if (ArrayRange(workDsema,0)!= Bars) ArrayResize(workDsema,Bars); instanceNo*=2; // // // // // double alpha = 2.0 /(1.0+MathSqrt(period)); workDsema[r][_ema1+instanceNo] = workDsema[r-1][_ema1+instanceNo]+alpha*(price -workDsema[r-1][_ema1+instanceNo]); workDsema[r][_ema2+instanceNo] = workDsema[r-1][_ema2+instanceNo]+alpha*(workDsema[r][_ema1+instanceNo]-workDsema[r-1][_ema2+instanceNo]); return(workDsema[r][_ema2+instanceNo]); } // // // // // double workDema[][_maWorkBufferx2]; #define _dema1 0 #define _dema2 1 double iDema(double price, double period, int r, int instanceNo=0) { if (ArrayRange(workDema,0)!= Bars) ArrayResize(workDema,Bars); instanceNo*=2; // // // // // double alpha = 2.0 / (1.0+period); workDema[r][_dema1+instanceNo] = workDema[r-1][_dema1+instanceNo]+alpha*(price -workDema[r-1][_dema1+instanceNo]); workDema[r][_dema2+instanceNo] = workDema[r-1][_dema2+instanceNo]+alpha*(workDema[r][_dema1+instanceNo]-workDema[r-1][_dema2+instanceNo]); return(workDema[r][_dema1+instanceNo]*2.0-workDema[r][_dema2+instanceNo]); } // // // // // double workTema[][_maWorkBufferx3]; #define _tema1 0 #define _tema2 1 #define _tema3 2 double iTema(double price, double period, int r, int instanceNo=0) { if (ArrayRange(workTema,0)!= Bars) ArrayResize(workTema,Bars); instanceNo*=3; // // // // // double alpha = 2.0 / (1.0+period); workTema[r][_tema1+instanceNo] = workTema[r-1][_tema1+instanceNo]+alpha*(price -workTema[r-1][_tema1+instanceNo]); workTema[r][_tema2+instanceNo] = workTema[r-1][_tema2+instanceNo]+alpha*(workTema[r][_tema1+instanceNo]-workTema[r-1][_tema2+instanceNo]); workTema[r][_tema3+instanceNo] = workTema[r-1][_tema3+instanceNo]+alpha*(workTema[r][_tema2+instanceNo]-workTema[r-1][_tema3+instanceNo]); return(workTema[r][_tema3+instanceNo]+3.0*(workTema[r][_tema1+instanceNo]-workTema[r][_tema2+instanceNo])); } // // // // // double workSmma[][_maWorkBufferx1]; double iSmma(double price, double period, int r, int instanceNo=0) { if (ArrayRange(workSmma,0)!= Bars) ArrayResize(workSmma,Bars); // // // // // if (r =0; k++) { double weight = period-k; sumw += weight; sum += weight*workLwma[r-k][instanceNo]; } return(sum/sumw); } // // // // // double workLwmp[][_maWorkBufferx1]; double iLwmp(double price, double period, int r, int instanceNo=0) { if (ArrayRange(workLwmp,0)!= Bars) ArrayResize(workLwmp,Bars); // // // // // workLwmp[r][instanceNo] = price; double sumw = period*period; double sum = sumw*price; for(int k=1; k =0; k++) { double weight = (period-k)*(period-k); sumw += weight; sum += weight*workLwmp[r-k][instanceNo]; } return(sum/sumw); } // // // // // double workAlex[][_maWorkBufferx1]; double iAlex(double price, double period, int r, int instanceNo=0) { if (ArrayRange(workAlex,0)!= Bars) ArrayResize(workAlex,Bars); if (period<4) return(price); // // // // // workAlex[r][instanceNo] = price; double sumw = period-2; double sum = sumw*price; for(int k=1; k =0; k++) { double weight = period-k-2; sumw += weight; sum += weight*workAlex[r-k][instanceNo]; } return(sum/sumw); } // // // // // double workTma[][_maWorkBufferx1]; double iTma(double price, double period, int r, int instanceNo=0) { if (ArrayRange(workTma,0)!= Bars) ArrayResize(workTma,Bars); // // // // // workTma[r][instanceNo] = price; double half = (period+1.0)/2.0; double sum = price; double sumw = 1; for(int k=1; k =0; k++) { double weight = k+1; if (weight > half) weight = period-k; sumw += weight; sum += weight*workTma[r-k][instanceNo]; } return(sum/sumw); } // // // // // double workSineWMA[][_maWorkBufferx1]; #define Pi 3.14159265358979323846264338327950288 double iSineWMA(double price, int period, int r, int instanceNo=0) { if (period<1) return(price); if (ArrayRange(workSineWMA,0)!= Bars) ArrayResize(workSineWMA,Bars); // // // // // workSineWMA[r][instanceNo] = price; double sum = 0; double sumw = 0; for(int k=0; k =0; k++) { double weight = MathSin(Pi*(k+1.0)/(period+1.0)); sumw += weight; sum += weight*workSineWMA[r-k][instanceNo]; } return(sum/sumw); } // // // // // double workWwma[][_maWorkBufferx1]; double iWwma(double price, double period, int r, int instanceNo=0) { if (ArrayRange(workWwma,0)!= Bars) ArrayResize(workWwma,Bars); // // // // // workWwma[r][instanceNo] = price; int i = Bars-r-1; double sumw = Volume[i]; double sum = sumw*price; for(int k=1; k =0; k++) { double weight = Volume[i+k]; sumw += weight; sum += weight*workWwma[r-k][instanceNo]; } return(sum/sumw); } // // // // // double workHull[][_maWorkBufferx2]; double iHull(double price, double period, int r, int instanceNo=0) { if (ArrayRange(workHull,0)!= Bars) ArrayResize(workHull,Bars); // // // // // int HmaPeriod = MathMax(period,2); int HalfPeriod = MathFloor(HmaPeriod/2); int HullPeriod = MathFloor(MathSqrt(HmaPeriod)); double hma,hmw,weight; instanceNo *= 2; workHull[r][instanceNo] = price; // // // // // hmw = HalfPeriod; hma = hmw*price; for(int k=1; k =0; k++) { weight = HalfPeriod-k; hmw += weight; hma += weight*workHull[r-k][instanceNo]; } workHull[r][instanceNo+1] = 2.0*hma/hmw; hmw = HmaPeriod; hma = hmw*price; for(k=1; k =0; k++) { weight = HmaPeriod-k; hmw += weight; hma += weight*workHull[r-k][instanceNo]; } workHull[r][instanceNo+1] -= hma/hmw; // // // // // hmw = HullPeriod; hma = hmw*workHull[r][instanceNo+1]; for(k=1; k =0; k++) { weight = HullPeriod-k; hmw += weight; hma += weight*workHull[r-k][1+instanceNo]; } return(hma/hmw); } // // // // // double workLinr[][_maWorkBufferx1]; double iLinr(double price, double period, int r, int instanceNo=0) { if (ArrayRange(workLinr,0)!= Bars) ArrayResize(workLinr,Bars); // // // // // period = MathMax(period,1); workLinr[r][instanceNo] = price; double lwmw = period; double lwma = lwmw*price; double sma = price; for(int k=1; k =0; k++) { double weight = period-k; lwmw += weight; lwma += weight*workLinr[r-k][instanceNo]; sma += workLinr[r-k][instanceNo]; } return(3.0*lwma/lwmw-2.0*sma/period); } // // // // // double workIe2[][_maWorkBufferx1]; double iIe2(double price, double period, int r, int instanceNo=0) { if (ArrayRange(workIe2,0)!= Bars) ArrayResize(workIe2,Bars); // // // // // period = MathMax(period,1); workIe2[r][instanceNo] = price; double sumx=0, sumxx=0, sumxy=0, sumy=0; for (int k=0; k 0) { double sum = 0; for (k=0; k < nlmvalues[_len][instanceNo]; k++) sum += nlmalphas[k][instanceNo]*nlmprices[r-k][instanceNo]; return( sum / nlmvalues[_weight][instanceNo]); } else return(0); } //+------------------------------------------------------------------ //| //+------------------------------------------------------------------ // // // // // // double workHa[][4]; double getPrice(int price, const double& open[], const double& close[], const double& high[], const double& low[], int i, int instanceNo=0) { if (price>=pr_haclose && price<=pr_hatbiased) { if (ArrayRange(workHa,0)!= Bars) ArrayResize(workHa,Bars); int r = Bars-i-1; // // // // // double haOpen; if (r>0) haOpen = (workHa[r-1][instanceNo+2] + workHa[r-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); }