Linear Regression Line2
Download scriptThis tool is designed to provide traders with a clear visual representation of a market's underlying trend by using a mathematical concept known as Linear Regression. Instead of simply following the price from point to point, this indicator calculates a "line of best fit" over a specific timeframe to help smooth out the market noise.
How the Logic Works
Trend Smoothing and Analysis The primary goal of this script is to identify the general direction of price movement. It looks at a specific window of historical data (defined by the user as the "Period") and calculates a straight line that minimizes the distance between itself and all the closing prices within that window. This allows traders to see whether the market is trending upward, downward, or moving sideways, independent of minor, short-term fluctuations.
The Mathematical Foundation To create this line, the script performs several behind-the-scenes calculations for every point on the chart:
- Data Collection: It gathers the closing prices for the number of bars specified in the period settings.
- Correlation Calculation: It analyzes the relationship between time (the sequence of bars) and price.
- Slope and Intercept: The script determines the "slope" (how steep the trend is) and the "intercept" (where the trend sits relative to the current price).
- Error Prevention: The logic includes built-in safeguards. If the data is too flat or uniform to create a standard calculation, the script applies a tiny adjustment factor to prevent the tool from breaking or producing errors on the chart.
Practical Use for Traders
By plotting this orange line on your chart, you can better visualize the "equilibrium" of the market. Because the line represents a statistical average of price movement over the chosen period:
- Trend Identification: It helps confirm if the current price action is staying above or below the historical average trend.
- Noise Reduction: It filters out the "jitter" of live trading, providing a cleaner view of where the price "should" be based on recent history.
- Dynamic Updating: The line automatically recalculates with every new price tick, ensuring that the visual trend stays current as new data arrives.
//+------------------------------------------------------------------+
//| Linear Regression Line.mq4 |
//| MQL Service |
//| scripts@mqlservice.com |
//| modified by: Ronald Verwer/ROVERCOM |
//| 09 June 2006 |
//+------------------------------------------------------------------+
#property copyright "MQL Service"
#property link "www.mqlservice.com"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Orange
#property indicator_width1 2
//---- input parameters
extern int LRLPeriod=14;
//---- buffers
double LRLBuffer[];
int shift=0;
int n=0;
double sumx=0, sumy=0, sumxy=0, sumx2=0, sumy2=0;
double m=0, yint=0, r=0;
//+------------------------------------------------------------------+
//| INITIALIZATION FUNCTION |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,LRLBuffer);
IndicatorDigits(Digits);
if(LRLPeriod < 2) LRLPeriod = 2;
IndicatorShortName("Linear Regression Line ("+LRLPeriod+")");
SetIndexDrawBegin(0,LRLPeriod+2);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+4);
//----
return(0);
}
//+------------------------------------------------------------------+
//| DEINITIALIZATION FUNCTION |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| ITERATION FUNCTION |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars=IndicatorCounted();
if(counted_bars<0) counted_bars=0;
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
for(int shift=limit-1; shift>=0; shift--)
{
sumx = 0;
sumy = 0;
sumxy = 0;
sumx2 = 0;
sumy2 = 0;
for(n = 0; n <= LRLPeriod-1; n++)
{
sumx = sumx + n;
sumy = sumy + Close[shift + n];
sumxy = sumxy + n * Close[shift + n];
sumx2 = sumx2 + n * n;
sumy2 = sumy2 + Close[shift + n] * Close[shift + n];
}
if(LRLPeriod*sumx2-sumx*sumx==0)
{m=(LRLPeriod*sumxy-sumx*sumy)/(0.00000001);}
else
{m=(LRLPeriod*sumxy-sumx*sumy)/(LRLPeriod*sumx2-sumx*sumx);}
yint=(sumy+m*sumx)/LRLPeriod;
if(MathSqrt((LRLPeriod*sumx2-sumx*sumx)*(LRLPeriod*sumy2-sumy*sumy))==0)
{r=(LRLPeriod*sumxy-sumx*sumy)/(0.00000001);}
else
{r=(LRLPeriod*sumxy-sumx*sumy)/MathSqrt((LRLPeriod*sumx2-sumx*sumx)*(LRLPeriod*sumy2-sumy*sumy));}
LRLBuffer[shift]=yint-m*LRLPeriod;
//Print (" "+shift+" "+LRLBuffer[shift]);
}
return(0);
}
//+------------------------------------------------------------------+
Comments