//+------------------------------------------------------------------+ //| G_Trend_v1.1.mq5 | //| G Trend_v1.1 Copyright 2016, fxborg | //| http://fxborg-labo.hateblo.jp/ | //+------------------------------------------------------------------+ #property copyright "Copyright 2016, fxborg" #property link "http://fxborg-labo.hateblo.jp/" #property version "1.1" #property indicator_chart_window #property indicator_buffers 10 #property indicator_plots 0 input int InpConvexPeriod=40; // Convex Hull Period input int InpRegrPeriod=8; // Regression Period input int Inp1stPeriod=60; // Trend Period input color Inp1stColor=clrGold; // Color 1 input color Inp2ndColor=clrSeaGreen; // Color 2 double R1[]; double S1[]; double TREND[]; double CX[]; double CY[]; double LA[]; double LB[]; int WinNo=ChartWindowFind(); int min_rates_total=InpConvexPeriod+Inp1stPeriod; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { ObjectsDeleteAll(0,WinNo); //--- SetIndexBuffer(0,CX,INDICATOR_CALCULATIONS); SetIndexBuffer(1,CY,INDICATOR_CALCULATIONS); SetIndexBuffer(2,LA,INDICATOR_CALCULATIONS); SetIndexBuffer(3,LB,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+1MathAbs(alpha-tmp)) { df=MathAbs(alpha-tmp); pos=j; slope=tmp; } } //--- if(pos == -1)return false; y1=(i-vertexs[pos][0])*slope+vertexs[pos][1]; y0=y1-(i-from_x)*slope; return true; } //+------------------------------------------------------------------+ 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,double &y1,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; x0=from_x; y0=from_y; y1=y; alpha=aa; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void set_line(double &LN[],const double a,const double y,const int i,const int len) { LN[i-len]=EMPTY_VALUE; for(int j=0;j=0;j--) { vertices[n][0]=upper[j][0]; vertices[n][1]=upper[j][1]; n++; } for(int j=0;j