//+------------------------------------------------------------------+ //| AutoTrendChannel.mq4 | //| Copyright © 2006, MetaQuotes Software Corp. | //| http://www.metaquotes.net | //+------------------------------------------------------------------+ #property copyright "Copyright © 2006, MetaQuotes Software Corp." #property link "http://www.metaquotes.net" /********************************************************************* * Author: Muhammad Hamizi Jaminan (hymns) * Original Author: ANG3110@latchess.com * * Date: December 14, 2006 * Custom Indicator: AutoTrendChannel.mq4 * Version: 1.0.1 * Description: Trend Detector * * Change Logs * Version 1.0.1 * - Rewriting Code. Multi Color. * Other function will add next release :) * * Version ang_AutoCh_HL-v1 * - Release by ANG3110@latchess.com **********************************************************************/ #property indicator_chart_window extern int Hours = 24; extern color col_bull = Blue; extern color col_mid = SkyBlue; extern color col_bear = Red; //------------------ double lr,lr0,lrp; double sx,sy,sxy,sx2,aa,bb; int p,fs; int f,f0,f1; double dh,dl,dh_1,dl_1,dh_2,dl_2; int ai_1,ai_2,bi_1,bi_2; double hai,lai,dhi,dli,dhm,dlm,ha0,hap,la0,lap; double price_p1,price_p0,price_p2,price_01,price_00,price_02; int p1,p0,p2,fp; string sName = "AutoTrendChannel"; //***************************************** int init() { p = Hours*60/Period(); if (fs==0) { fs = 1; } return(0); } //******************************* int deinit() { ObjectDelete("1"+sName); ObjectDelete("0"+sName); ObjectDelete("2"+sName); ObjectDelete("3"+sName); ObjectDelete("4"+sName); ObjectDelete("5"+sName); return(0); } //******************************* int start() { int i,n; //------------------------------------------------------------------------------ if (f==1) { p1=iBarShift(Symbol(),Period(),ObjectGet("1"+sName,OBJPROP_TIME1)); p0=iBarShift(Symbol(),Period(),ObjectGet("0"+sName,OBJPROP_TIME1)); p2=iBarShift(Symbol(),Period(),ObjectGet("2"+sName,OBJPROP_TIME1)); if (fp==0 && p!=p1) {p=p1; fp=1;} if (fp==0 && p!=p0) {p=p0; fp=1;} if (fp==0 && p!=p2) {p=p2; fp=1;} } //==================================================== sx=0; sy=0; sxy=0; sx2=0; for (n=0; n<=p; n++) { sx+=n; sy+=Close[n]; sxy+=n*Close[n]; sx2+=MathPow(n,2); } aa=(sx*sy-(p+1)*sxy)/(MathPow(sx,2)-(p+1)*sx2); bb=(sy-aa*sx)/(p+1); //---------------------------------------------------- for (i=0; i<=p; i++) { lr=bb+aa*i; dh=High[i]-lr; dl=Low[i]-lr; //---------------------------------------------------- if (i
=dh_1) {
dh_1=dh;
ai_1=i;
}
if (dl<=dl_1) {
dl_1=dl;
bi_1=i;
}
}
//----------------------------------------------------
if (i>=p/2) {
if (i==p/2) {
dh_2=0.0;
dl_2=0.0;
ai_2=i;
bi_2=i;
}
if (dh>=dh_2) {
dh_2=dh;
ai_2=i;
}
if (dl<=dl_2) {
dl_2=dl;
bi_2=i;
}
}
}
//-------------------------------------
lr0=bb;
lrp=bb+aa*(i+p);
//===================================================
if (MathAbs(ai_1-ai_2)>MathAbs(bi_1-bi_2)) f=1;
if (MathAbs(ai_1-ai_2) dhm && i>=p/2) {
ai_2=i;
f1=1;
}
}
if (f==0) break;
}
//----------------------------
for (i=0; i<=p; i++) {
hai=High[ai_1]*(i-ai_2)/(ai_1-ai_2)+High[ai_2]*(i-ai_1)/(ai_2-ai_1);
dli=Low[i]-hai;
if (i==0) dlm=0.0;
if (dli