//+------------------------------------------------------------------+ //| LR trend.mq4 | //| mladen | //+------------------------------------------------------------------+ #property copyright "mladen" #property link "mladenfx@gmail.com" #property indicator_separate_window #property indicator_buffers 6 #property indicator_color1 Green #property indicator_color2 Green #property indicator_color3 Red #property indicator_color4 Red #property indicator_color5 DimGray #property indicator_color6 DimGray #property indicator_width1 2 #property indicator_width3 2 #property indicator_width5 2 #property indicator_width6 2 // // // // // extern int Length = 34; extern int AppliedPrice = PRICE_CLOSE; extern string TimeFrame = "current time frame"; // // // // // double Buffer[]; double LRSBuffer[]; double LRSBufferUa[]; double LRSBufferUb[]; double LRSBufferDa[]; double LRSBufferDb[]; double RSQBuffer[]; // // // // // int timeFrame; double Trending; string IndicatorFileName; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // int init() { IndicatorDigits(5); IndicatorBuffers(7); SetIndexBuffer(0,LRSBufferUa); SetIndexLabel(0,"LR up"); SetIndexBuffer(1,LRSBufferUb); SetIndexLabel(1,"LR weak up"); SetIndexBuffer(2,LRSBufferDa); SetIndexLabel(2,"LR down"); SetIndexBuffer(3,LRSBufferDb); SetIndexLabel(3,"LR weak down"); SetIndexBuffer(4,Buffer); SetIndexLabel(4,"no trend"); SetIndexBuffer(5,LRSBuffer); SetIndexLabel(5,"LR regression slope"); SetIndexBuffer(6,RSQBuffer); for (int i = 0; i < 4; i++) SetIndexStyle(i,DRAW_HISTOGRAM); SetIndexStyle(4,DRAW_ARROW); SetIndexArrow(4,158); // // // // // timeFrame = stringToTimeFrame(TimeFrame); IndicatorShortName("LR trend "+TimeFrameToString(timeFrame)+" ("+Length+")"); IndicatorFileName = WindowExpertName(); Trending = 3.858889/Length; return(0); } // // // // // int deinit() { return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // int start() { int counted_bars=IndicatorCounted(); int i,limit; if (counted_bars<0) return(-1); if (counted_bars>0) counted_bars--; limit=Bars-counted_bars; // // // // // if (timeFrame != Period()) { datetime TimeArray[]; limit = MathMax(limit,timeFrame/Period()); ArrayCopySeries(TimeArray ,MODE_TIME ,NULL,timeFrame); // // // // // for(i=0,int y=0; i<limit; i++) { if(Time[i]<TimeArray[y]) y++; LRSBufferUa[i] = iCustom(NULL,timeFrame,IndicatorFileName,Length,AppliedPrice,0,y); LRSBufferUb[i] = iCustom(NULL,timeFrame,IndicatorFileName,Length,AppliedPrice,1,y); LRSBufferDa[i] = iCustom(NULL,timeFrame,IndicatorFileName,Length,AppliedPrice,2,y); LRSBufferDb[i] = iCustom(NULL,timeFrame,IndicatorFileName,Length,AppliedPrice,3,y); Buffer[i] = iCustom(NULL,timeFrame,IndicatorFileName,Length,AppliedPrice,4,y); LRSBuffer[i] = iCustom(NULL,timeFrame,IndicatorFileName,Length,AppliedPrice,5,y); } return(0); } // // // // // for (i=limit;i>=0;i--) { LRSBuffer[i] = LinearRegressionSlope(Length,i); RSQBuffer[i] = RSquared(Length,i); // // // // // Buffer[i] = EMPTY_VALUE; LRSBufferUa[i] = EMPTY_VALUE; LRSBufferUb[i] = EMPTY_VALUE; LRSBufferDa[i] = EMPTY_VALUE; LRSBufferDb[i] = EMPTY_VALUE; if (RSQBuffer[i] < Trending) { Buffer[i] = 0.00; continue; } if (LRSBuffer[i] > 0) { if (LRSBuffer[i] > LRSBuffer[i+1]) { LRSBufferUa[i] = LRSBuffer[i]; LRSBufferUb[i] = EMPTY_VALUE; } if (LRSBuffer[i] < LRSBuffer[i+1]) { LRSBufferUb[i] = LRSBuffer[i]; LRSBufferUa[i] = EMPTY_VALUE; } } if (LRSBuffer[i] < 0) { if (LRSBuffer[i] < LRSBuffer[i+1]) { LRSBufferDa[i] = LRSBuffer[i]; LRSBufferDb[i] = EMPTY_VALUE; } if (LRSBuffer[i] > LRSBuffer[i+1]) { LRSBufferDb[i] = LRSBuffer[i]; LRSBufferDa[i] = EMPTY_VALUE; } } } // // // // // for(i=0;i<indicator_buffers;i++) SetIndexDrawBegin(i,Length); return(0); } //---------------------------------------------------------------------------------| // | //---------------------------------------------------------------------------------| // // // // // int lrs.period=EMPTY_VALUE; double lrs.sumX; double lrs.sumXSqr; double lrs.divisor; // // // // // double LinearRegressionSlope(int len,int shift) { double LinearRegSlope; double SumXY = 0; double SumY = 0; // // // // // if (lrs.period != len) { lrs.period = len; lrs.sumX = lrs.period * (lrs.period-1) / 2; lrs.sumXSqr = lrs.period * (lrs.period-1) * (2 * lrs.period - 1) / 6; lrs.divisor = MathPow(lrs.sumX,2) - lrs.period * lrs.sumXSqr; } // // // // // for (int i=0; i<lrs.period; i++) { double price = iMA(NULL,0,1,0,MODE_EMA,AppliedPrice,i+shift); SumXY += i*price; SumY += price; } if( lrs.divisor != 0 ) LinearRegSlope = (lrs.period * SumXY - lrs.sumX * SumY)/lrs.divisor; else LinearRegSlope = 0; return (LinearRegSlope); } //---------------------------------------------------------------------------------| // | //---------------------------------------------------------------------------------| // // // // // double rs.len = EMPTY_VALUE; double rs.SumX; double rs.SumY; double rs.SumX2; double rs.SumY2; double rs.SumXY; double rs.SumR; // // // // // double RSquared(int Len,int shift) { double RSquared; double price; double Q1; double Q2; double Q3; int i; if (rs.len != Len) { rs.len = Len; rs.SumX = 0; for(i=0;i<=Len-1;i++) rs.SumX += i+1; rs.SumX2 = 0; for(i=0;i<=Len-1;i++) rs.SumX2 += (i+1)*(i+1); } rs.SumY = 0; rs.SumY2 = 0; rs.SumXY = 0; // // // // // for(i=0;i<Len;i++) { price = iMA(NULL,0,1,0,MODE_EMA,AppliedPrice,i+shift); rs.SumY += price; rs.SumY2 += MathPow(price,2); rs.SumXY += (i+1)*price; } Q1 = rs.SumXY - rs.SumX*rs.SumY/Len; Q2 = rs.SumX2 - rs.SumX*rs.SumX/Len; Q3 = rs.SumY2 - rs.SumY*rs.SumY/Len; // // // // // if( Q2*Q3 != 0 ) RSquared = Q1*Q1/(Q2*Q3); else RSquared = 0; return(RSquared); } //+------------------------------------------------------------------+ //| //+------------------------------------------------------------------+ // // // // // int stringToTimeFrame(string tfs) { int tf=0; tfs = StringTrimLeft(StringTrimRight(StringUpperCase(tfs))); if (tfs=="M1" || tfs=="1") tf=PERIOD_M1; if (tfs=="M5" || tfs=="5") tf=PERIOD_M5; if (tfs=="M15"|| tfs=="15") tf=PERIOD_M15; if (tfs=="M30"|| tfs=="30") tf=PERIOD_M30; if (tfs=="H1" || tfs=="60") tf=PERIOD_H1; if (tfs=="H4" || tfs=="240") tf=PERIOD_H4; if (tfs=="D1" || tfs=="1440") tf=PERIOD_D1; if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1; if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1; if (tf<Period()) tf=Period(); return(tf); } string TimeFrameToString(int tf) { string tfs=""; if (tf!=Period()) switch(tf) { case PERIOD_M1: tfs="M1" ; break; case PERIOD_M5: tfs="M5" ; break; case PERIOD_M15: tfs="M15" ; break; case PERIOD_M30: tfs="M30" ; break; case PERIOD_H1: tfs="H1" ; break; case PERIOD_H4: tfs="H4" ; break; case PERIOD_D1: tfs="D1" ; break; case PERIOD_W1: tfs="W1" ; break; case PERIOD_MN1: tfs="MN1"; } return(tfs); } // // // // // string StringUpperCase(string str) { string s = str; int lenght = StringLen(str) - 1; int char; while(lenght >= 0) { char = StringGetChar(s, lenght); // // // // // if((char > 96 && char < 123) || (char > 223 && char < 256)) s = StringSetChar(s, lenght, char - 32); else if(char > -33 && char < 0) s = StringSetChar(s, lenght, char + 224); lenght--; } // // // // // return(s); }
Sample
Analysis
Market Information Used:
Series array that contains open time of each bar
Indicator Curves created:
Implements a curve of type DRAW_HISTOGRAM
Implements a curve of type DRAW_ARROW
Indicators Used:
Moving average indicator
Custom Indicators Used:
IndicatorFileName
Order Management characteristics:
Other Features: