_TRO_Test
Download scriptThis tool is designed to analyze price trends on a trading chart by calculating a "Linear Regression" value. Instead of simply following the price up and down like a standard moving average, this script uses mathematical modeling to determine the overall direction and trajectory of the market over a specific period.
How it Works
The logic operates by looking at a user-defined window of recent price data (set by the "LRPeriod" variable). It treats these price points as coordinates on a graph and performs a calculation to find the "line of best fit."
Think of it like drawing a perfectly straight line through a scattered group of dots: the goal is to find the path that stays as close to all those points as possible. By identifying this straight-line trend, the script can filter out minor market "noise" and show you the primary direction in which the price is moving.
Key Components of the Logic
- Trend Calculation: The script calculates the "Slope," which represents the steepness and direction of the trend. A positive slope indicates an upward trajectory, while a negative slope indicates a downward move.
- Point Identification: For every point on your chart, the script looks back at the previous number of bars (the period) to compute the intercept—the starting point of that best-fit line.
- Current Value Mapping: Once it determines the best-fit line for that period, it identifies the specific value at the current moment. This helps traders visualize where the price "should" be based on its recent momentum.
Summary for the User
In plain terms, this script acts as a trend stabilizer. It looks at a block of history, calculates the most accurate straight line that connects those prices, and then displays that line's current position. It helps you see the "big picture" of market movement by smoothing out the small, erratic zig-zags of daily price action.
//+------------------------------------------------------------------+
//| linreg.mq4 |
#property indicator_chart_window
#property indicator_buffers 1
extern int LRPeriod=60;
double ind_buffer[], Slope_buffer[] ;
double Slope=0;
int init()
{
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);
Slope_buffer[i]= Slope ;
}
return(Slope);
}
//+------------------------------------------------------------------+
double linreg(int p,int i)
{
double SumY=0;
double Sum1=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