//+------------------------------------------------------------------+ //| Robby DSS Bressert Colored.mq4 | //| Jaanus Jantson | //| http://fx.jantson.ee; jaanus@jantson.ee | //| Copyright © 2008, MetaQuotes Software Corp. | //| http://www.metaquotes.net/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, MetaQuotes Software Corp." #property link "http://www.metaquotes.net/" #property strict #property indicator_separate_window #property indicator_buffers 3 #property indicator_label1 "DSS" #property indicator_type1 DRAW_LINE #property indicator_color1 clrDimGray #property indicator_width1 3 #property indicator_label2 "DSS Up" #property indicator_type2 DRAW_ARROW #property indicator_color2 clrAqua #property indicator_width2 2 #property indicator_label3 "DSS Down" #property indicator_type3 DRAW_ARROW #property indicator_color3 clrRed #property indicator_width3 2 // // // enum enMaTypes { ma_sma, // Simple moving average ma_ema, // Exponential moving average ma_smma, // Smoothed MA ma_lwma, // Linear weighted MA ma_slwma, // Smoothed LWMA ma_dsema, // Double Smoothed Exponential average ma_tema, // Triple exponential moving average - TEMA ma_lsma // Linear regression value (lsma) }; input int StochasticLength = 55; // Stochastic length input int Smooth = 8; // Moving average smoothing period input enMaTypes SmoothMode = ma_ema; // Moving average smoothing method double dss[],dssUp[],dssDn[],valc[]; int len,smh; string names[] = {"SMA","EMA","SMMA","LWMA","SLWMA","DSEMA","TEMA","LSMA"}; //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // int OnInit() { IndicatorBuffers(4); SetIndexBuffer(0,dss, INDICATOR_DATA); SetIndexBuffer(1,dssUp,INDICATOR_DATA); SetIndexArrow(1,159); SetIndexBuffer(2,dssDn,INDICATOR_DATA); SetIndexArrow(2,159); SetIndexBuffer(3,valc, INDICATOR_CALCULATIONS); len = fmax(1,StochasticLength); smh = fmax(1,Smooth); IndicatorSetDouble(INDICATOR_MINIMUM,-5); IndicatorSetDouble(INDICATOR_MAXIMUM,105); IndicatorSetInteger(INDICATOR_LEVELS,4); IndicatorSetDouble( INDICATOR_LEVELVALUE,0,95); IndicatorSetInteger(INDICATOR_LEVELSTYLE,0,STYLE_DOT); IndicatorSetInteger(INDICATOR_LEVELCOLOR,0,clrMediumOrchid); IndicatorSetDouble( INDICATOR_LEVELVALUE,1,85); IndicatorSetInteger(INDICATOR_LEVELSTYLE,1,STYLE_DOT); IndicatorSetInteger(INDICATOR_LEVELCOLOR,1,clrMediumOrchid); IndicatorSetDouble( INDICATOR_LEVELVALUE,2,15); IndicatorSetInteger(INDICATOR_LEVELSTYLE,2,STYLE_DOT); IndicatorSetInteger(INDICATOR_LEVELCOLOR,2,clrMediumOrchid); IndicatorSetDouble( INDICATOR_LEVELVALUE,3,5); IndicatorSetInteger(INDICATOR_LEVELSTYLE,3,STYLE_DOT); IndicatorSetInteger(INDICATOR_LEVELCOLOR,3,clrMediumOrchid); IndicatorSetString(INDICATOR_SHORTNAME,"DSS smoothed with "+names[SmoothMode]+"("+(string)StochasticLength+","+(string)Smooth+")"); return(INIT_SUCCEEDED); } 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=fmin(rates_total-prev_calculated+1,rates_total-1); // // // // // for (i=limit; i>=0; i--) { dss[i] = iDss(close[i],high[i],low[i],len,smh,SmoothMode,i,rates_total); valc[i] = (idss[i+1]) ? 1 : (dss[i]=0; k++) { min = fmin(min,workDss[r-k][iNo+_pLow]); max = fmax(max,workDss[r-k][iNo+_pHigh]); } workDss[r][iNo+_st1] = (min!=max) ? 100*(close-min)/(max-min) : 0; workDss[r][iNo+_ss1] = iCustomMa(mode,workDss[r][iNo+_st1],smooth,i,Bars,maInstance+0); // // // // // min = workDss[r][iNo+_ss1]; max = workDss[r][iNo+_ss1]; for (int k=1; k=0; k++) { min = fmin(min,workDss[r-k][iNo+_ss1]); max = fmax(max,workDss[r-k][iNo+_ss1]); } double stoch = (min!=max) ? 100*(workDss[r][iNo+_ss1]-min)/(max-min) : 0; // // // // // workDss[r][iNo+_dss] = iCustomMa(mode,stoch,smooth,i,Bars,maInstance+1); return(workDss[r][iNo+_dss]); } //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // #define _maInstances 3 #define _maWorkBufferx1 1*_maInstances #define _maWorkBufferx2 2*_maInstances #define _maWorkBufferx3 3*_maInstances double iCustomMa(int mode, double price, double length, int r, int bars, int instanceNo=0) { r = bars-r-1; switch (mode) { case ma_sma : return(iSma(price,(int)length,r,bars,instanceNo)); case ma_ema : return(iEma(price,length,r,bars,instanceNo)); case ma_smma : return(iSmma(price,(int)length,r,bars,instanceNo)); case ma_lwma : return(iLwma(price,(int)length,r,bars,instanceNo)); case ma_slwma : return(iSlwma(price,(int)length,r,bars,instanceNo)); case ma_dsema : return(iDsema(price,length,r,bars,instanceNo)); case ma_tema : return(iTema(price,(int)length,r,bars,instanceNo)); case ma_lsma : return(iLinr(price,(int)length,r,bars,instanceNo)); default : return(price); } } // // // // // double workSma[][_maWorkBufferx1]; double iSma(double price, int period, int r, int _bars, int instanceNo=0) { if (ArrayRange(workSma,0)!= _bars) ArrayResize(workSma,_bars); workSma[r][instanceNo+0] = price; double avg = price; int k=1; for(; k=0; k++) avg += workSma[r-k][instanceNo+0]; return(avg/(double)k); } // // // // // double workEma[][_maWorkBufferx1]; double iEma(double price, double period, int r, int _bars, int instanceNo=0) { if (ArrayRange(workEma,0)!= _bars) ArrayResize(workEma,_bars); workEma[r][instanceNo] = price; if (r>0 && period>1) workEma[r][instanceNo] = workEma[r-1][instanceNo]+(2.0/(1.0+period))*(price-workEma[r-1][instanceNo]); return(workEma[r][instanceNo]); } // // // // // double workSmma[][_maWorkBufferx1]; double iSmma(double price, double period, int r, int _bars, int instanceNo=0) { if (ArrayRange(workSmma,0)!= _bars) ArrayResize(workSmma,_bars); workSmma[r][instanceNo] = price; if (r>1 && period>1) workSmma[r][instanceNo] = workSmma[r-1][instanceNo]+(price-workSmma[r-1][instanceNo])/period; return(workSmma[r][instanceNo]); } // // // // // double workLwma[][_maWorkBufferx1]; double iLwma(double price, double period, int r, int _bars, int instanceNo=0) { if (ArrayRange(workLwma,0)!= _bars) ArrayResize(workLwma,_bars); workLwma[r][instanceNo] = price; if (period<=1) return(price); double sumw = period; double sum = period*price; for(int k=1; k=0; k++) { double weight = period-k; sumw += weight; sum += weight*workLwma[r-k][instanceNo]; } return(sum/sumw); } // // // // // double workSlwma[][_maWorkBufferx2]; double iSlwma(double price, double period, int r, int _bars, int instanceNo=0) { if (ArrayRange(workSlwma,0)!= _bars) ArrayResize(workSlwma,_bars); // // // // // int SqrtPeriod = (int)floor(sqrt(period)); instanceNo *= 2; workSlwma[r][instanceNo] = price; // // // // // double sumw = period; double sum = period*price; for(int k=1; k=0; k++) { double weight = period-k; sumw += weight; sum += weight*workSlwma[r-k][instanceNo]; } workSlwma[r][instanceNo+1] = (sum/sumw); // // // // // sumw = SqrtPeriod; sum = SqrtPeriod*workSlwma[r][instanceNo+1]; for(int k=1; k=0; k++) { double weight = SqrtPeriod-k; sumw += weight; sum += weight*workSlwma[r-k][instanceNo+1]; } return(sum/sumw); } // // // // // double workDsema[][_maWorkBufferx2]; #define _ema1 0 #define _ema2 1 double iDsema(double price, double period, int r, int _bars, int instanceNo=0) { if (ArrayRange(workDsema,0)!= _bars) ArrayResize(workDsema,_bars); instanceNo*=2; // // // // // workDsema[r][_ema1+instanceNo] = price; workDsema[r][_ema2+instanceNo] = price; if (r>0 && period>1) { double alpha = 2.0 /(1.0+sqrt(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 workTema[][_maWorkBufferx3]; #define _tema1 0 #define _tema2 1 #define _tema3 2 double iTema(double price, double period, int r, int bars, int instanceNo=0) { if (ArrayRange(workTema,0)!= bars) ArrayResize(workTema,bars); instanceNo*=3; // // // // // workTema[r][_tema1+instanceNo] = price; workTema[r][_tema2+instanceNo] = price; workTema[r][_tema3+instanceNo] = price; if (r>0 && period>1) { 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 workLinr[][_maWorkBufferx1]; double iLinr(double price, int period, int r, int bars, int instanceNo=0) { if (ArrayRange(workLinr,0)!= bars) ArrayResize(workLinr,bars); // // // // // period = MathMax(period,1); workLinr[r][instanceNo] = price; if (r=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); }