//+------------------------------------------------------------------+ //| G_Trend_Angle_v1.1.mq5 | //| G_Trend_Angle_v1.1 Copyright 2016, fxborg | //| http://fxborg-labo.hateblo.jp/ | //|This indicator drawing trend lines automatically using centroid of| //|convex hulls. | //|http://fxborg-labo.hateblo.jp/archive/category/Auto%20Trend%20Line| //+------------------------------------------------------------------+ #property copyright "Copyright 2016, fxborg" #property link "http://fxborg-labo.hateblo.jp/" #property version "1.00" #property indicator_separate_window #property indicator_buffers 16 #property indicator_plots 1 #property indicator_type1 DRAW_COLOR_HISTOGRAM #property indicator_color1 clrRed,clrDodgerBlue #property indicator_width2 6 input int InpConvexPeriod=40; // Polygon Period input int InpTrendPeriod=120; // Trend Period input int InpStep=2; // StepFactor input double InpX=0.05; // X int InpMaxBars=50000; // MaxBars int InpRegrPeriod=8; // Regression Period double HI[]; double LO[]; double CX1[]; double CY1[]; double LA1[]; double LB1[]; double TREND1[]; double TREND[]; double TRENDCLR[]; int min_rates_total=10; int Span1=InpTrendPeriod+int(InpConvexPeriod/2); //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { int i=0; SetIndexBuffer(i++,TREND1,INDICATOR_DATA);//--- SetIndexBuffer(i++,TRENDCLR,INDICATOR_DATA);//--- SetIndexBuffer(i++,CX1,INDICATOR_CALCULATIONS); SetIndexBuffer(i++,CY1,INDICATOR_CALCULATIONS); SetIndexBuffer(i++,LA1,INDICATOR_CALCULATIONS); SetIndexBuffer(i++,LB1,INDICATOR_CALCULATIONS); SetIndexBuffer(i++,HI,INDICATOR_CALCULATIONS); SetIndexBuffer(i++,LO,INDICATOR_CALCULATIONS); SetIndexBuffer(i++,TREND,INDICATOR_CALCULATIONS);//--- /// --- //--- digits // IndicatorSetInteger(INDICATOR_DIGITS,1); return(0); } //+------------------------------------------------------------------+ //| 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 &tick_volume[], const long &volume[], const int &spread[]) { //--- int i,first; if(rates_total<=min_rates_total) return(0); //--- int begin_pos=min_rates_total; //--- first=begin_pos; if(first+1TREND[i-1]) TREND[i]=TREND1[i]; else if((TREND1[i]+InpStep)TREND[i])TRENDCLR[i]=0; else TRENDCLR[i]=TRENDCLR[i-1]; } //--- return value of prev_calculated for next call return(rates_total); } //--- //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void convex_hull(double &upper[][2],double &lower[][2],const double &high[],const double &low[],const int i,const int len) { ArrayResize(upper,len,len); int k=0; for(int j=0;j=2 && (cross(upper[k-2][0],upper[k-2][1], upper[k-1][0],upper[k-1][1], i-j,high[i-j]))<=0) { k--; } upper[k][0]= i-j; upper[k][1]= high[i-j]; k++; } ArrayResize(upper,k,len); ArrayResize(lower,len,len); k=0; for(int j=0;j=2 && (cross(lower[k-2][0],lower[k-2][1], lower[k-1][0],lower[k-1][1], i-j,low[i-j]))>=0) { k--; } lower[k][0]= i-j; lower[k][1]= low[i-j]; k++; } ArrayResize(lower,k,len); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void calc_trend(double &alpha,int &x0,double &y0,int &x1,double &y1,double &y2 ,const double &CX[],const double &CY[],const double &LA[] ,const datetime &time[],const int i ,const int convex_period,const int period) { double sumx=0; double sumy=0; double a=0; int a_count=0; int ifrom=0; int cnt=0; int len=period; for(int j=0;j<=len;j++) { if(CX[i-j]!=EMPTY_VALUE && LA[i-j]!=EMPTY_VALUE) { a+=LA[i-j]; ifrom=i-j; a_count++; sumx+=CX[ifrom]; sumy+=CY[ifrom]; } } double ax=i-(sumx/a_count); double ay=sumy/a_count; int from_x=int(CX[ifrom]-convex_period*0.5); double aa=(a/a_count); double y=aa*ax+ay; double span=i-from_x; double from_y=y-aa*span; alpha=aa; x0=from_x; y0=from_y; x1=int((sumx/a_count)+0.5); y1=y-aa*(i-x1); y2=y; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void calc_vector(double &CX[],double &CY[],double &LA[],double &LB[], double &upper[][2],double &lower[][2],const int i) { if(CX[i]!=EMPTY_VALUE)return; int up_sz=int(ArraySize(upper)*0.5); int lo_sz=int(ArraySize(lower)*0.5); double mx,my; calc_centroid(mx,my,upper,lower); if(mx=0;j--) { vertices[n][0]=upper[j][0]; vertices[n][1]=upper[j][1]; n++; } for(int j=0;jfabs(y)) a=atan(y/x); else { a=atan(x/y); // pi/4 <= a <= pi/4 if(a<0.) a=-1.*M_PI_2-a; //a is negative, so we're adding else a=M_PI_2-a; } if(x<0.) { if(y<0.) a=a-M_PI; else a=a+M_PI; } return a; } //+------------------------------------------------------------------+ //| //+------------------------------------------------------------------+ double nd(const double x,const int n) { return(NormalizeDouble(x,n)); }