// PolyMA.mq4 (H+L/2 Version) //Enhanced by Big Be /Brandon D'Rion Oct. Nov. 2011, including new settings and TickInterval code //Enhanced by MrPip /Robert Hill Nov. 2011, including functions moved to Init, Past plotting (non repaint) //---- #property copyright "Copyright © 2012, Robert Hill and Brandon D'Rion" #property link "http://www.MrPipForex.com/" #property indicator_chart_window #property indicator_buffers 2 #property indicator_color1 Yellow #property indicator_width1 2 #property indicator_color2 Red #property indicator_width2 2 string note1 = "Maximum Degree of 8"; // 9 string note2 = "Max Length about 300"; extern int Degree = 3; extern int Length = 40; extern double Width = 1.0; extern bool Speedup = True; extern string Speedup_Note = "M1, M5? use false"; extern int TickInterval = 20; //----- double fx[], fx2[]; double ai[10,10],b[10],x[10],sx[20]; double dMathPow[320,20]; double sum; //int n,f; double qq,mm,tt; int ii,jj,kk,ll,nn; int maxDegree = 8; datetime dtBarTime; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { IndicatorBuffers(2); IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)); SetIndexBuffer(0, fx); SetIndexStyle(0, DRAW_LINE); SetIndexBuffer(1, fx2); SetIndexStyle(1, DRAW_LINE); string short_name="PolyMA("+Length+","+Degree+")"; IndicatorShortName(short_name); SetIndexLabel(0,short_name); SetIndexLabel(1,short_name + "_smoothed"); SetIndexEmptyValue(0, 0.0); SetIndexDrawBegin(0, Length); nn = Degree+1; init_sx(); init_mathpow(); return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //clear(); return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { if (Bars < Length) return(0); //===================================================// bool RunGame = false; if(dtBarTime != Time[0] || MathMod(Volume[0],TickInterval) == 0) // Runs every n ticks { RunGame = True; } //==================================================// if(RunGame == True) { int mi, n; int limit; int counted_bars=IndicatorCounted(); counted_bars--; if(counted_bars <=Length) limit = Bars - Length; // if(counted_bars==0) limit = Bars - Length; // counted_bars--; // if(counted_bars >0) limit = Bars - counted_bars; if(counted_bars >Length) limit = Bars - counted_bars; // Comment("limit ",limit); for (int pos=limit; pos>=0; pos--) { //----------------------syx----------- for(mi =1; mi <=nn; mi++) { sum =0.00000; for(n=0; n <=Length; n++) { if(mi ==1) sum +=(High[pos + n] + Low[pos + n]) / 2.0; else sum +=(High[pos + n] + Low[pos + n]) / 2.0 * dMathPow[n,mi-1]; } b[mi] =sum; } //===============Matrix======================================================================================================= for(jj =1; jj <=nn; jj++) { for(ii=1; ii <=nn; ii++) { kk =ii+jj-1; ai[ii,jj] =sx[kk]; } } //===============Gauss======================================================================================================== for(kk =1; kk <=nn-1; kk++) { ll =0; mm =0; for(ii =kk; ii <=nn; ii++) { if(MathAbs(ai[ii,kk])>mm) { mm =MathAbs(ai[ii,kk]); ll =ii; } } if(ll ==0) return(0); if (ll !=kk) { for(jj =1; jj <=nn; jj++) { tt =ai[kk,jj]; ai[kk,jj] =ai[ll,jj]; ai[ll,jj] =tt; } tt =b[kk]; b[kk] =b[ll]; b[ll] =tt; } for(ii =kk+1;ii <=nn; ii++) { qq =ai[ii,kk]/ai[kk,kk]; for(jj =1; jj <=nn; jj++) { if(jj ==kk) ai[ii,jj] =0; else ai[ii,jj] =ai[ii,jj]-qq*ai[kk,jj]; } b[ii] =b[ii]-qq*b[kk]; } } // for(kk=1; (Gauss) x[nn] =b[nn]/ai[nn,nn]; for(ii =nn-1; ii>=1; ii--) { tt =0; for(jj =1; jj <=nn-ii; jj++) { tt =tt+ai[ii,ii+jj]*x[ii+jj]; x[ii] =(1/ai[ii,ii])*(b[ii]-tt); } } //=========================================================================================================================== fx[pos] = x[1]; dtBarTime = Time[0]; } // for (int pos=limit } // RunGame return(0); } // Start // Only needs to be done once - called by Init //----------------------sx------------------------------------------------------------------- void init_sx() { sx[1] =Length+1; // nn = Degree+1; for(int mi =1; mi <=nn*2-2; mi++) { sum =0; for(int n =0; n <=Length; n++) { sum +=MathPow(n,mi); } sx[mi+1] =sum; } } // Initialize power array for modest speed improvement void init_mathpow() { for(int n =0; n <=Length; n++) { for(int mi =0; mi <=maxDegree*2-2; mi++) { dMathPow[n,mi] =MathPow(n,mi); } } } //+------------------------------------------------------------------+