Loading…

Linear Regression And Channel

A linear regression fit is one of the most reused building blocks in technical analysis - the regression channel indicator, a trend-strength filter, a simple forecast of the next bar. The formula for slope and intercept is short, but two things around it are usually done carelessly: the result is returned as separate out-parameters that are easy to swap by accident (every argument is a double, so the compiler cannot catch it), and R-squared is often derived from a shortcut that only holds for a perfect fit, silently giving the wrong number the moment real noise is present. This library returns everything through one struct, SLinearRegression, so slope, intercept, R-squared and the residual standard deviation are named fields at the call site rather than positional doubles. R-squared is computed properly from the actual residuals (1 - SSres/SStot), which is correct for every fit, not just a perfect one. LinearRegressionFit takes arbitrary x and y arrays. LinearRegressionOnSeries is the convenience form for the common case - fitting price against bar index - so you do not have to build an x array of 0,1,2... by hand every time. LinearRegressionChannel turns the fit into the familiar regression channel: upper and lower bands at a chosen number of residual standard deviations from the fitted line at any x, the same construction the standard MetaTrader regression channel tool draws, available here as callable numbers instead of a chart object. The fit correctly refuses rather than returning garbage when the input cannot support a line: fewer than two points, or every x value identical (a vertical "line" has no defined slope). isValid on the result struct must be checked before the other fields are trusted - the demo exercises both failure cases explicitly. Notes and limitations - Always check SLinearRegression.isValid (or the boolean return of the fit function) before reading slope, intercept or r2 - an invalid fit leaves all numeric fields at 0.0, which looks like a real answer if you skip the check. - residualStdDev, and therefore the channel width, is only computed when there are more than 2 points (n-2 degrees of freedom for the residual variance); with exactly 2 points the line passes through both exactly and residualStdDev is 0.0, collapsing the channel onto the line itself. - This is ordinary least squares, which weighs every point equally. A single large outlier bar can pull the whole line toward it; if that matters, feed the fit a pre-cleaned array rather than raw prices. - For a curved relationship (a fit that should bend, not just tilt), use PolyRegression.mqh from this same catalogue instead - forcing a straight line onto curved data understates R-squared and misreads the trend. - No dependency on chart objects - this is pure numeric fitting. Drawing the channel on a chart is left to the caller, typically with ObjectCreate and the upper/lower values this library returns.

Script · MT4 · v1.0 · Free

About Linear Regression And Channel

Linear Regression And Channel is a script for MetaTrader 4. It has 2 settings, such as Point count and Channel deviations. MQL source code included.

Files in the download: LinearRegression.mqh, LinearRegression.txt, LinearRegressionDemo.mq4.

Settings

SettingDefaultWhat it does
Point count20How many synthetic points to fit
Channel deviations2.0Channel half-width in residual standard deviations

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Channels & Envelopes · Trend · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria