i-regr
Download scriptThis indicator is designed to map out the underlying trend of a financial instrument by creating a "Regression Channel." Unlike standard moving averages that simply smooth out price data, this tool uses advanced mathematical modeling to project a path that best fits the historical price movement.
Core Logic and Functionality
1. Polynomial Trend Modeling The heart of this script is a variable polynomial regression. In simple terms, rather than assuming a price move is a straight line, the indicator allows for "degrees" of curvature. By adjusting the degree, the tool can model everything from basic linear trends to more complex, sweeping curves. It looks at a specific window of past prices (the "bars") and calculates a mathematical curve that minimizes the distance between the actual price points and the calculated line.
2. The Regression Line (The Median) The script first identifies the "best-fit" path through the data. It does this by building a matrix of historical values and solving it using a process known as Gaussian elimination. This results in a central trend line (the median) that represents the theoretical "core" direction of the price action over the selected period.
3. Standard Deviation Channels Once the central trend line is established, the indicator calculates the "Standard Deviation." This measures how much the actual market price fluctuates away from the mathematical center.
- It calculates the variance between the real prices and the predicted regression path.
- It then applies a multiplier (the standard deviation constant) to create an upper and lower boundary.
4. Visual Output The final result is a three-line channel displayed on your chart:
- The Center Line: Represents the calculated polynomial trend.
- The Upper Boundary: Represents the "high" end of normal price volatility based on the regression.
- The Lower Boundary: Represents the "low" end of normal price volatility.
Summary for the User
Instead of just telling you what the price was, this indicator attempts to show you the mathematical shape of current price behavior. It creates a dynamic envelope; when the price stays within the upper and lower lines, it is considered to be following the established trend. When the price breaks outside of these boundaries, it may indicate that the current market move is deviating significantly from the recent mathematical norm.
//
// Regression Channel with variable polynomial degree indicator
//
// original by Boris
// www.iticsoftware.com
// http://www.mql5.com/en/code/8417
//
// V1.1 by graziani:
// -> minor changes for MT4 b600 compatibility
//
#property copyright "© 2008 BJF Trading Group"
#property link "www.iticsoftware.com"
#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 LimeGreen
#property indicator_color2 Gold
#property indicator_color3 Gold
input int degree=3;
input double kstd=2.0;
input int bars=250;
input int shift=0;
//-----
double fx[],sqh[],sql[];
double ai[10,10],b[10],x[10],sx[20];
double sum;
int ip,p,n,f;
double qq,mm,tt;
int ii,jj,kk,ll,nn;
double sq;
int i0=0;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
/*
void clear()
{
int total = ObjectsTotal();
for (int i=total-1; i >= 0; i--)
{
string name = ObjectName(i);
if (StringFind(name, prefix) == 0) ObjectDelete(name);
}
}
*/
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
SetIndexBuffer(0,fx); // Áóôåðû ìàññèâîâ èíäèêàòîðà
SetIndexBuffer(1,sqh);
SetIndexBuffer(2,sql);
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexStyle(2,DRAW_LINE);
SetIndexEmptyValue(0, 0.0);
SetIndexEmptyValue(1, 0.0);
SetIndexEmptyValue(2, 0.0);
SetIndexShift(0,shift);
SetIndexShift(1,shift);
SetIndexShift(2,shift);
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//clear();
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
if(Bars < bars) return(-1);
//----
int mi; // ïåðåìåííàÿ èñïîëüçóþùàÿñÿ òîëüêî â start
ip=bars;
p=ip; // òèïà ïðèñâàèâàíèå
sx[1]=p+1; // ïðèìå÷àíèå - [] - îçíà÷àåò ìàññèâ
nn=degree+1;
SetIndexDrawBegin(0,Bars-p-1);
SetIndexDrawBegin(1,Bars-p-1);
SetIndexDrawBegin(2,Bars-p-1);
//----------------------sx-------------------------------------------------------------------
for(mi=1;mi<=nn*2-2;mi++) // ìàòåìàòè÷åñêîå âûðàæåíèå - äëÿ âñåõ mi îò 1 äî nn*2-2
{
sum=0;
for(n=i0;n<=i0+p;n++)
{
sum+=MathPow(n,mi);
}
sx[mi+1]=sum;
}
//----------------------syx-----------
for(mi=1;mi<=nn;mi++)
{
sum=0.00000;
for(n=i0;n<=i0+p;n++)
{
if(mi==1) sum+=Close[n];
else sum+=Close[n]*MathPow(n,mi-1);
}
b[mi]=sum;
}
//===============Matrix=======================================================================================================
for(jj=1;jj<=nn;jj++)
{
for(ii=1; ii<=nn; ii++)
{
kk=ii+jj-1;
ai[ii,jj]=sx[kk];
}
}
//===============Gauss========================================================================================================
for(kk=1; kk<=nn-1; kk++)
{
ll=0;
mm=0;
for(ii=kk; ii<=nn; ii++)
{
if(MathAbs(ai[ii,kk])>mm)
{
mm=MathAbs(ai[ii,kk]);
ll=ii;
}
}
if(ll==0) return(0);
if(ll!=kk)
{
for(jj=1; jj<=nn; jj++)
{
tt=ai[kk,jj];
ai[kk,jj]=ai[ll,jj];
ai[ll,jj]=tt;
}
tt=b[kk];
b[kk]=b[ll];
b[ll]=tt;
}
for(ii=kk+1;ii<=nn;ii++)
{
qq=ai[ii,kk]/ai[kk,kk];
for(jj=1;jj<=nn;jj++)
{
if(jj==kk) ai[ii,jj]=0;
else ai[ii,jj]=ai[ii,jj]-qq*ai[kk,jj];
}
b[ii]=b[ii]-qq*b[kk];
}
}
x[nn]=b[nn]/ai[nn,nn];
for(ii=nn-1;ii>=1;ii--)
{
tt=0;
for(jj=1;jj<=nn-ii;jj++)
{
tt=tt+ai[ii,ii+jj]*x[ii+jj];
x[ii]=(1/ai[ii,ii])*(b[ii]-tt);
}
}
//===========================================================================================================================
for(n=i0;n<=i0+p;n++)
{
sum=0;
for(kk=1;kk<=degree;kk++)
{
sum+=x[kk+1]*MathPow(n,kk);
}
fx[n]=x[1]+sum;
}
//-----------------------------------Std-----------------------------------------------------------------------------------
sq=0.0;
for(n=i0;n<=i0+p;n++)
{
sq+=MathPow(Close[n]-fx[n],2);
}
sq=MathSqrt(sq/(p+1))*kstd;
for(n=i0;n<=i0+p;n++)
{
sqh[n]=fx[n]+sq;
sql[n]=fx[n]-sq;
}
return(0);
}
//+------------------------------------------------------------------+
Comments