linreg_v1

This indicator is designed to provide traders with a visual representation of the "best-fit" price trend over a specific period. Instead of just following the price like a standard moving average, it uses a mathematical approach called Linear Regression to smooth out market noise and identify the underlying direction of the asset.

Core Logic Overview

The script calculates a straight line that minimizes the distance between itself and the actual closing prices of the assets over a user-defined number of bars (defaulting to 60). Think of it as drawing the most accurate "path of least resistance" through a cluster of price points.

How it Works Step-by-Step

To determine where the line sits on your chart at any given moment, the script performs the following operations:

  • Data Collection: The tool looks back at the closing prices for the number of periods you have selected. It gathers all these data points into a set for analysis.
  • Calculating the Slope: The script calculates the "steepness" of the price movement. By analyzing how much the price rises or falls relative to the time passed, it determines whether the current trend is leaning upward, downward, or moving sideways.
  • Determining the Intercept: Once the steepness (slope) is known, the script calculates the starting point of that line. This ensures that the line aligns correctly with the historical data provided.
  • Projecting the Value: Finally, it identifies the specific value for the current point in time based on that calculated line. This result is then plotted as a smooth, continuous blue line on your trading chart.

Practical Application

Because this indicator uses regression logic rather than just averaging prices, it responds more fluidly to price changes. It helps traders filter out "fake" movements or minor volatility to see the larger, more reliable trend. By following the line, you can better visualize whether the current market momentum is sustained or if the price is deviating significantly from its historical average path.

Miscellaneous
Implements a curve of type %1
10 Views
0 Downloads
0 Favorites
linreg_v1
//+------------------------------------------------------------------+
//|                                                       linreg.mq4 |
//|                                         Copyright © 2006, ch33z3 |
//|                                   http://4xjournal.blogspot.com/ |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2006, ch33z3"
#property  link      "http://4xjournal.blogspot.com/"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Blue

extern int LRPeriod=60;

double ind_buffer[];

int init()
  {
   IndicatorBuffers(1);
   SetIndexStyle(0,DRAW_LINE,0,1);
   SetIndexBuffer(0,ind_buffer);
   return(0);
  }
  
int start()
  {
   int limit, i;
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   for(i=limit; i>=0; i--) {
      ind_buffer[i]=linreg(LRPeriod,i);
   }
   return(0);
  }
//+------------------------------------------------------------------+

double linreg(int p,int i)
   {
   double SumY=0;
   double Sum1=0;
   double Slope=0;
   double c;
   
   for (int x=0; x<=p-1;x++) {
      c=Close[x+i];
      SumY+=c;
      Sum1+=x*c; }
   double SumBars=p*(p-1)*0.5;
   double SumSqrBars=(p-1)*p*(2*p-1)/6;
	double Sum2=SumBars*SumY;
	double Num1=p*Sum1-Sum2;
	double Num2=SumBars*SumBars-p*SumSqrBars;
	if(Num2!=0) Slope=Num1/Num2;
	else Slope=0;
	double Intercept=(SumY-Slope*SumBars)/p;
	double linregval=Intercept+Slope*(p-1);
	return(linregval);
	}

Comments