/** * Classic Moving Averages with colors * * Simple moving average * Exponential moving average * Smoothed moving average * Linear weighted moving average * Smoothed moving average * * The indicator displays a colored moving average. * * It has three parameters: * * Period calculation period * * Method calculation method * * Price applied prise used for calculation * * This version is faster and code is more flexible and reusable */ //+------------------------------------------------------------------ #property copyright "" #property link "" #property version "1.0" #property indicator_chart_window #property indicator_chart_window #property indicator_buffers 2 #property indicator_plots 1 #property indicator_type1 DRAW_COLOR_LINE #property indicator_color1 clrLimeGreen,clrRed #property indicator_style1 STYLE_SOLID #property indicator_width1 2 enum enMATypes { _sma, // Simple _ema, // Exponential _smma, // Smoothed _lwma // Linear weighted }; input int MaPeriod = 9; // Period input enMATypes MaMethod = _sma; // Method input ENUM_APPLIED_PRICE Price = PRICE_CLOSE; // Price double MaBuffer[]; double ColorBuffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping Comment("MT5中文网:WWW.MT5USER.COM"); SetIndexBuffer(0,MaBuffer,INDICATOR_DATA); SetIndexBuffer(1,ColorBuffer,INDICATOR_COLOR_INDEX); IndicatorSetString(INDICATOR_SHORTNAME,shortName(MaMethod)); //--- 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 &TickVolume[], const long &Volume[], const int &Spread[]) { int i=(int)MathMax(prev_calculated-1,0);for (; i0) { ColorBuffer[i] = ColorBuffer[i-1]; if (MaBuffer[i]>MaBuffer[i-1]) {ColorBuffer[i]=0;} if (MaBuffer[i]=0; k++) avg += maArray[r-k]; return(avg/(double)k); } double iEMA(double price,double period,int bars, int r) { if (ArraySize(maArray)!=bars) ArrayResize(maArray,bars); maArray[r]=price; if(r>0 && period>1) maArray[r]=maArray[r-1]+(2.0/(1.0+period))*(price-maArray[r-1]); return(maArray[r]); } double iSMMA(double price,double period,int bars, int r) { if (ArraySize(maArray)!=bars) ArrayResize(maArray,bars); maArray[r]=price; if(r>1 && period>1) maArray[r]=maArray[r-1]+(price-maArray[r-1])/period; return(maArray[r]); } double iLWMA(double price,double period,int bars, int r) { if (ArraySize(maArray)!=bars) ArrayResize(maArray,bars); maArray[r] = 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*maArray[r-k]; } return(sum/sumw); } // double getPrice(ENUM_APPLIED_PRICE tprice,const double &open[],const double &close[],const double &high[],const double &low[],int i,int _bars,int instanceNo=0) { switch(tprice) { case PRICE_CLOSE: return(close[i]); case PRICE_OPEN: return(open[i]); case PRICE_HIGH: return(high[i]); case PRICE_LOW: return(low[i]); case PRICE_MEDIAN: return((high[i]+low[i])/2.0); case PRICE_TYPICAL: return((high[i]+low[i]+close[i])/3.0); case PRICE_WEIGHTED: return((high[i]+low[i]+close[i]+close[i])/4.0); } return(0); } //+---------------------------------------------------------