DTS-Ang_AutoCh_HL-v2
Download scriptThis script is designed to provide a visual representation of market trends and price corridors on a trading chart. Rather than just showing past data, it uses mathematical modeling to project potential price boundaries based on historical behavior over a specific timeframe (user-defined in hours).
Core Methodology
The logic operates through three primary phases:
1. Trendline Calculation The script begins by analyzing historical closing prices over a set period of time. It calculates a "Linear Regression" line. In simple terms, this is a mathematical "best fit" line that identifies the general direction of the market—whether it is trending upwards, downwards, or moving sideways—based on the average movement of price points.
2. Dynamic Boundary Identification Once the primary trend is established, the script looks for extreme price actions (highs and lows) relative to that trend line. It splits the analysis into two halves:
- It identifies how far the price "swings" away from the trend in the past.
- It determines the nature of these swings—specifically whether the market is defining its range based on maximum peaks or minimum valleys.
3. Projection and Visualization The script then projects these findings forward onto the current chart to create a visual corridor. This results in three distinct lines:
- Maximum Boundary: Represents the upper limit of the current trend's price action.
- Average Boundary: A central path representing the "equilibrium" or middle ground of the movement.
- Minimum Boundary: Represents the lower limit of the current trend's price action.
User Experience
For the user, this means the script automatically draws a visual "channel." By looking at these lines, a trader can see where the price has historically stayed within bounds and where it might be expected to travel next based on the current momentum. The script handles all the complex math—calculating regressions and identifying swing points—to provide a clean, three-line visual guide that updates as new data becomes available.
// ----------------------------------------------------
//
// Ang_AUtoCH_HL-v2.mq4
// by ANG3110@latchess.com
//
// ----------------------------------------------------
#property copyright "ANG3110@latchess.com"
#property indicator_chart_window
//----------------------------------
extern int Hours=720; /// this is the number of hours 168= 1week 720 is one month
extern color col=Gold;
extern color col2=Magenta;
//------------------
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="ang_autoch_h2";
//*****************************************
int init() {
p=Hours*60/Period();
if (fs==0) {
fs=1;
}
return(0);
}
//*******************************
int deinit()
{
ObjectDelete("Week_Max"); // MasterWhite
ObjectDelete("Week_Average"); // MasterWhite
ObjectDelete("Week_Min"); // MasterWhite
}
//*******************************
int start()
{
int i,n;
//------------------------------------------------------------------------------
if (f==1)
{
p1=iBarShift(Symbol(),Period(),ObjectGet("Week_Max",OBJPROP_TIME1)); // MasterWhite
p0=iBarShift(Symbol(),Period(),ObjectGet("Week_Average",OBJPROP_TIME1)); // MasterWhite
p2=iBarShift(Symbol(),Period(),ObjectGet("Week_Min",OBJPROP_TIME1)); // MasterWhite
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<p/2) {if (i==0) {dh_1=0.0; dl_1=0.0; ai_1=i; bi_1=i;}
if (dh>=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)<MathAbs(bi_1-bi_2)) f=2;
if (MathAbs(ai_1-ai_2)==MathAbs(bi_1-bi_2)) {if (MathAbs(dh_1-dh_2)<MathAbs(dl_1-dl_2)) f=1; if (MathAbs(dh_1-dh_2)>=MathAbs(dl_1-dl_2)) f=2;}
//=================================================
if (f==1) {
for (n=0; n<=20; n++) { f1=0;
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);
if (i==0 || i==p/2) dhm=0.0;
if (High[i]-hai>dhm && i<p/2) {ai_1=i; f1=1;}
if (High[i]-hai>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<dlm) dlm=dli;}
ha0=High[ai_1]*(0-ai_2)/(ai_1-ai_2)+High[ai_2]*(0-ai_1)/(ai_2-ai_1);
hap=High[ai_1]*(p-ai_2)/(ai_1-ai_2)+High[ai_2]*(p-ai_1)/(ai_2-ai_1);
//----------------------------
//i have changed the code so the name of the trend does not change // Fadi
price_p1=hap;
price_p0=hap+dlm/2;
price_p2=hap+dlm;
price_01=ha0;
price_00=ha0+dlm/2;
price_02=ha0+dlm;
}
//=================================================
if (f==2)
{
for (n=0; n<=20; n++) { f1=0;
for (i=0; i<=p; i++) {lai=Low[bi_1]*(i-bi_2)/(bi_1-bi_2)+Low[bi_2]*(i-bi_1)/(bi_2-bi_1);
if (i==0 || i==p/2) dlm=0.0;
if (Low[i]-lai<dlm && i<p/2) {bi_1=i; f1=1;}
if (Low[i]-lai<dlm && i>=p/2) {bi_2=i; f1=1;}}
if (f==0) break;}
//----------------------------
for (i=0; i<=p; i++) {lai=Low[bi_1]*(i-bi_2)/(bi_1-bi_2)+Low[bi_2]*(i-bi_1)/(bi_2-bi_1);
dhi=High[i]-lai;
if (i==0) dhm=0.0; if (dhi>dhm) dhm=dhi;}
la0=Low[bi_1]*(0-bi_2)/(bi_1-bi_2)+Low[bi_2]*(0-bi_1)/(bi_2-bi_1);
lap=Low[bi_1]*(p-bi_2)/(bi_1-bi_2)+Low[bi_2]*(p-bi_1)/(bi_2-bi_1);
//----------------------------------------------------------------
//i have changed the code so the name of the trend does not change // Fadi
price_p1=lap;
price_p0=lap+dhm/2;
price_p2=lap+dhm;
price_01=la0;
price_00=la0+dhm/2;
price_02=la0+dhm;
}
//===================================================================================
ObjectCreate("Week_Max",2, 0,Time[p],price_p1,Time[0],price_01); // MasterWhite
ObjectCreate("Week_Average",2, 0,Time[p],price_p0,Time[0],price_00); // MasterWhite
ObjectCreate("Week_Min",2, 0,Time[p],price_p2,Time[0],price_02); // MasterWhite
//-----------------------------------------------------------------
// Color
//-----------------------------------------------------------------
ObjectSet("Week_Max",OBJPROP_COLOR,col); // MasterWhite
ObjectSet("Week_Average",OBJPROP_COLOR,col2); // MasterWhite
ObjectSet("Week_Min",OBJPROP_COLOR,col); // MasterWhite
//-----------------------------------------------------------------
// Style
//-----------------------------------------------------------------
ObjectSet("Week_Max",OBJPROP_STYLE,STYLE_SOLID); // MasterWhite
ObjectSet("Week_Average",OBJPROP_STYLE,STYLE_DOT); // MasterWhite
ObjectSet("Week_Min",OBJPROP_STYLE,STYLE_SOLID); // MasterWhite
//-----------------------------------------------------------------
// Size
//-----------------------------------------------------------------
ObjectSet("Week_Max",OBJPROP_WIDTH,2); // MasterWhite
ObjectSet("Week_Average",OBJPROP_WIDTH,2); // these vaules control the width of the line
ObjectSet("Week_Min",OBJPROP_WIDTH,2); // MasterWhite
//-----------------------------------------------------------------
// Value
//-----------------------------------------------------------------
ObjectSet("Week_Max",OBJPROP_TIME1,Time[p]); // MasterWhite
ObjectSet("Week_Max",OBJPROP_PRICE1,price_p1); // MasterWhite
ObjectSet("Week_Max",OBJPROP_TIME2,Time[0]); // MasterWhite
ObjectSet("Week_Max",OBJPROP_PRICE2,price_01); // MasterWhite
//
ObjectSet("Week_Average",OBJPROP_TIME1,Time[p]); // MasterWhite
ObjectSet("Week_Average",OBJPROP_PRICE1,price_p0); // MasterWhite
ObjectSet("Week_Average",OBJPROP_TIME2,Time[0]); // MasterWhite
ObjectSet("Week_Average",OBJPROP_PRICE2,price_00); // MasterWhite
//
ObjectSet("Week_Min",OBJPROP_TIME1,Time[p]); // MasterWhite
ObjectSet("Week_Min",OBJPROP_PRICE1,price_p2); // MasterWhite
ObjectSet("Week_Min",OBJPROP_TIME2,Time[0]); // MasterWhite
ObjectSet("Week_Min",OBJPROP_PRICE2,price_02); // MasterWhite
//==================================================================
f=1; p1=p; p0=p; p2=p; fp=0;
//*************************************************************************************
return(0);
}
//=====================================================================================
Comments