LinearRegression-REAL
Download scriptThis tool is designed to provide a visual trend analysis for your trading charts by identifying the "line of best fit" for price action. In simple terms, it takes a specific number of recent data points—defined by the user-set period—and calculates a straight line that most accurately represents the average path of the market's movement during that timeframe.
How the Logic Works
1. Data Collection & Period Sensitivity The script looks at a fixed window of time (the "Period"). For every point on your chart, it examines the closing prices of the last several candles. By analyzing this specific window, the tool helps you distinguish between short-term "noise" (random price fluctuations) and the underlying trend.
2. Statistical Calculation Instead of simply connecting dots from one price to another, the logic uses a mathematical method called Linear Regression. It calculates:
- The Slope: This determines the direction and steepness of the trend (whether the price is moving up, down, or staying flat).
- The Intercept: This finds the starting point of the line relative to your data window.
- The Correlation: The logic internally measures how closely the actual price candles hug the calculated line.
3. Dynamic Rendering The script processes these calculations for every historical candle on your chart. It then places a smooth, continuous white line on your display. This line serves as a baseline: if the current price is consistently above this line, it suggests upward momentum; if it is below, it suggests downward pressure.
Key Takeaways for Traders
- Trend Identification: It provides a clear visual of the "mean" price path, making it easier to see if the market is trending or consolidating.
- Smoothing Effect: Because it uses a regression formula rather than just following the most recent candle, the resulting line is much smoother and less reactive to minor, insignificant price spikes.
- Customizable Timeframes: By adjusting the period, you can choose whether you want to see a "fast" trend (reacting quickly to new data) or a "slow" trend (showing the broader market direction over a longer duration).
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| Linear Regression Line.mq4 |
//| MQL Service |
//| scripts@mqlservice.com |
//+------------------------------------------------------------------+
#property copyright "MQL Service"
#property link "www.mqlservice.com"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 White
#property indicator_width1 2
//---- input parameters
extern int LRLPeriod = 4;
//---- buffers
double LRLBuffer[];
int shift = 0;
int n = 0;
double sumx = 0;
double sumy = 0;
double sumxy = 0;
double sumx2 = 0;
double sumy2 = 0;
double yint = 0;
double r = 0;
double m = 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];
}
double temp = LRLPeriod * sumx2 - sumx * sumx;
if (temp == 0)
temp = .0000001;
// m = (LRLPeriod * sumxy - sumx * sumy) / (LRLPeriod * sumx2 - sumx * sumx);
m = (LRLPeriod * sumxy - sumx * sumy) / temp;
temp = LRLPeriod;
if (temp == 0)
temp = .0000001;
yint = (sumy + m * sumx) / temp; // was LRLPeriod (obviously)
temp = MathSqrt((LRLPeriod * sumx2 - sumx * sumx) * (LRLPeriod * sumy2 - sumy * sumy));
if (temp == 0)
temp = .0000001;
r = (LRLPeriod * sumxy - sumx * sumy) / temp;
// MathSqrt((LRLPeriod * sumx2 - sumx * sumx) * (LRLPeriod * sumy2 - sumy * sumy));
LRLBuffer[shift] = yint - m * LRLPeriod;
//Print (" "+shift+" "+LRLBuffer[shift]);
}
return(0);
}
//+------------------------------------------------------------------+
Comments