LinearRegression-REAL_v1
Download scriptThis tool is designed to provide traders with a visual representation of price trends by calculating and plotting a Linear Regression Line directly on the trading chart. Unlike standard moving averages that simply follow recent price action, this script uses mathematical modeling to identify the "line of best fit" for a specific period of time.
How the Logic Works
The core objective of this script is to analyze a set number of previous price points (the "Period") and determine the most accurate straight line that could represent that data. Here is the step-by-step breakdown of how it processes information:
1. Data Collection & Period Selection The user defines a specific timeframe (period) for the calculation. The script looks back at the closing prices for this number of candles to gather the raw data needed for the analysis.
2. Mathematical Trend Analysis Instead of just connecting dots, the script performs a series of complex calculations to determine two key components of a trend:
- The Slope (Direction): It calculates the angle at which the price is moving. This helps identify whether the overall momentum is trending upward, downward, or remaining flat.
- The Intercept (Position): It determines where the trend line sits relative to the current price level.
3. "Best Fit" Calculation The logic uses a mathematical formula to minimize the distance between the actual price candles and the resulting line. This ensures that the line displayed on your chart represents the most accurate average path of the price movement over the chosen period, filtering out minor "noise" or temporary fluctuations.
4. Real-Time Visualization Once the math is complete for each point in time, the script draws a solid white line on your chart. This line acts as a visual guide, helping you see the underlying trend of the market. If the price is consistently above this line, it suggests bullish strength; if it is below, it may indicate bearish pressure.
Key Features for the User
- Dynamic Adaptation: The line updates automatically as new price data arrives, providing a constantly evolving view of the current trend.
- Customizable Sensitivity: By adjusting the "Period" input, you can choose to see a very short-term, reactive trend or a much longer-term, smoothed perspective of the market direction.
- Visual Clarity: The result is plotted directly on the main chart window, making it easy to integrate into your existing trading strategy without cluttering the interface.
//+------------------------------------------------------------------+
//| 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