//+------------------------------------------------------------------+ //| SimpleAutoTL_v2.mq5 | //| Simple Auto TL v2.0 Copyright 2016, fxborg | //| http://fxborg-labo.hateblo.jp/ | //+------------------------------------------------------------------+ #property copyright "Copyright 2016, fxborg" #property link "http://fxborg-labo.hateblo.jp/" #property version "2.0" #property indicator_chart_window #property indicator_buffers 9 #property indicator_plots 0 #property indicator_type1 DRAW_LINE #property indicator_color1 clrRed #property indicator_width1 1 #property indicator_type2 DRAW_LINE #property indicator_color2 clrRed #property indicator_width2 1 int WinNo=ChartWindowFind(); //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ input int InpFastPeriod=20; // Fast Period input int InpHiLoPeriod=60; // HiLo Period input double InpSize=0.6; // Modoshi Size input bool InpShowHistory=true; // Show History input int InpKeepPeriod=500; // Keep Period input int InpMaxBars=1000; // MaxBars input color InpColor=clrDodgerBlue; // Line Color double InpXSize=0.3; // X Size int UP_FLG=0; int DN_FLG=1; int UP_I=2; int UP=3; int DN_I=4; int DN=5; int A=6; int B=7; double wk[][8]; double HI[]; double LO[]; double HI2[]; double LO2[]; double LATR[]; double UPPER_X[]; double LOWER_X[]; double UPPER[]; double LOWER[]; int LAtrPeriod=100; double LAtrAlpha=2.0/(LAtrPeriod+1.0); int LineNo=0; double xFactor; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int OnInit() { ObjectDeleteByName("AutoTL"); SetIndexBuffer(0,UPPER,INDICATOR_DATA); SetIndexBuffer(1,LOWER,INDICATOR_DATA); SetIndexBuffer(2,LATR,INDICATOR_CALCULATIONS); SetIndexBuffer(3,HI,INDICATOR_CALCULATIONS); SetIndexBuffer(4,LO,INDICATOR_CALCULATIONS); SetIndexBuffer(5,UPPER_X,INDICATOR_CALCULATIONS); SetIndexBuffer(6,LOWER_X,INDICATOR_CALCULATIONS); SetIndexBuffer(7,HI2,INDICATOR_CALCULATIONS); SetIndexBuffer(8,LO2,INDICATOR_CALCULATIONS); return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { ObjectDeleteByName("AutoTL"); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ 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[]) { for(int i=(int)MathMax(prev_calculated-1,0); iHI[i-1])? HI[i]:UPPER[i-1]; LOWER[i]=(LO[i]HI[i-1])? i:UPPER_X[i-1]; LOWER_X[i]=(LO[i]LOWER_X[i] && i-LOWER_X[i]>=InpFastPeriod) { if(HI2[i]==high[i] ) { up=high[i]; up_i=i; up_flg=1; wk[i][UP]=up; wk[i][UP_I]=(int)up_i; wk[i][UP_FLG]=(int)up_flg; } if(up_flg==1 && high[i]+size1) { double best_d=0; int best=0; for(int j=0;jbest_d) { best=j; best_d=d; } } if(best_d>0) { int n=(InpShowHistory)? i : 1; drawTrend(1,n,InpColor,(int)lower[best+1][0],lower[best+1][1],(int)lower[best][0],lower[best][1],time,STYLE_SOLID,1,true); } } } } if(UPPER_X[i]=InpFastPeriod) { if(LO2[i]==low[i]) { dn=low[i]; dn_i=i; dn_flg=1; wk[i][DN]=dn; wk[i][DN_I]=(int)dn_i; wk[i][DN_FLG]=(int)dn_flg; } if(dn_flg==1 && low[i]-size > dn ) { dn_flg=0; wk[i][DN_FLG]=(int)wk[i][DN_FLG]; double upper[][2]; // update tl convex_upper(upper,high,dn_i,dn_i-(int(UPPER_X[i])-1)); int sz=int(ArraySize(upper)*0.5); if(sz>1) { double best_d=0; int best=0; for(int j=0;jbest_d) { best=j;best_d=d; } } if(best_d>0) { int n=(InpShowHistory)? i : 2; drawTrend(1,n,InpColor,(int)upper[best+1][0],upper[best+1][1],(int)upper[best][0],upper[best][1],time,STYLE_SOLID,1,true); } } } } //--- } //--- return(rates_total); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void drawTrend(int no1,int no2, const color clr,const int x0,const double y0,const int x1,const double y1, const datetime &time[],const ENUM_LINE_STYLE style,const int width,const bool isRay) { if(-1=0; i--) { string objName=ObjectName(0,i); if(StringSubstr(objName,0,length)==prefix) { ObjectDelete(0,objName); } } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void ObjectDeleteByBarNo(string prefix,int no) { int total=ObjectsTotal(0), length=StringLen(prefix); for(int i=total-1; i>=0; i--) { string objName=ObjectName(0,i); if(StringSubstr(objName,0,length)==prefix) { string res[]; StringSplit(objName,'#',res); if(ArraySize(res)==2 && int(res[1])=x2 || y1<=y2)return 0.0; double a= (y2-y1)/(x2-x1); double b=y1-a*x1; //b=y-ax double x0=(top-b)/a; //x=(y-b)/a double y3 = a*i+b; //y=ax+b return xfacter*(i-x0)*(top-y3); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double dimension_up(double x1,double y1,double x2,double y2,double btm,double i,double xfacter) { if(x1>=x2 || y1>=y2)return 0.0; double a= (y2-y1)/(x2-x1); double b=y1-a*x1; //b=y-ax double x0=(btm-b)/a; //x=(y-b)/a double y3 = a*i+b; //y=ax+b return xfacter*(i-x0)*(y3-btm); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void convex_upper(double &upper[][2],const double &high[],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); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void convex_lower(double &lower[][2],const double &low[],const int i,const int len) { ArrayResize(lower,len,len); int 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); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double cross(const double ox,double oy, const double ax,double ay, const double bx,double by) { return ((ax - ox) * (by - oy) - (ay - oy) * (bx - ox)); } //+------------------------------------------------------------------+