//+------------------------------------------------------------------+ //| G_Band_v1.mq5 | //| G_Band_v1 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_chart_window #property indicator_buffers 9 #property indicator_plots 3 #property indicator_type1 DRAW_LINE #property indicator_color1 clrMediumSlateBlue #property indicator_width1 2 #property indicator_type2 DRAW_LINE #property indicator_color2 clrMediumSlateBlue #property indicator_width2 2 #property indicator_type3 DRAW_LINE #property indicator_color3 clrRed #property indicator_width3 2 input double InpBandsDeviations = 2.0; // Bands Deviations input int InpConvexPeriod=40; // Polygon Period input int InpRegrPeriod=8; // Regression Period input int Inp1stPeriod=60; // Trend Period input int InpShowLine=1; //Show Line (1:show ,0:hide) input int InpMaxBars=5000; // MaxBars double UP[]; double DN[]; double HI[]; double LO[]; double CX[]; double CY[]; double LA[]; double LB[]; double TREND[]; int WinNo=ChartWindowFind(); int min_rates_total=InpConvexPeriod+Inp1stPeriod; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { SetIndexBuffer(0,UP,INDICATOR_DATA);//--- SetIndexBuffer(1,DN,INDICATOR_DATA);//--- SetIndexBuffer(2,TREND,INDICATOR_DATA);//--- SetIndexBuffer(3,CX,INDICATOR_CALCULATIONS); SetIndexBuffer(4,CY,INDICATOR_CALCULATIONS); SetIndexBuffer(5,LA,INDICATOR_CALCULATIONS); SetIndexBuffer(6,LB,INDICATOR_CALCULATIONS); SetIndexBuffer(7,HI,INDICATOR_CALCULATIONS); SetIndexBuffer(8,LO,INDICATOR_CALCULATIONS); /// --- //--- digits IndicatorSetInteger(INDICATOR_DIGITS,2); 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+1=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 datetime &time[],const int i) { double sumx=0; double sumy=0; double a=0; int a_count=0; int ifrom=0; int cnt=0; int len=Inp1stPeriod; 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]-InpConvexPeriod*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 &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;j