DinexSpace Linear Regression Library
- 라이브러리
- 버전: 1.0
DinexSpace Linear Regression Library is a library for MetaTrader 5 (MT5) that fits a straight line to the last n values of a series, for your indicator or Expert Advisor. You push each new value and read the slope, the value of the line, R squared, the standard error and how far the newest value is from the line, without writing the running sums yourself.
How it works
- Call DxLrCreate with the window (3 to 100000 values). You get a handle.
- Call DxLrPush for each new value (for example the close of each new bar).
- When the window is full, read DxLrSlope, DxLrR2, DxLrStdError, DxLrZ, or DxLrFitted for the value of the line at any position. Before that the functions return EMPTY_VALUE.
- Or give a whole array to DxLrSeries and get the series back.
What you get
- The whole fit: slope, intercept, value of the line at any position (also one step ahead), R squared, standard error, residual and z.
- One value at a time: each new value updates the sums; they are recalculated from the stored values often enough to keep the numbers exact.
- Batch call: an array in, the whole series out, with EMPTY_VALUE before the window is full.
- Checked: the example EA compares every output at every bar with a separate two-pass calculation, on closes and on values with a large offset, and on a long synthetic series.
- Status codes with plain-English texts: bad window, bad handle, bad value, no instance available.
- Light: each program has its own instances. No DLLs, no web requests, no Sleep. The library reads no market data.
How to use it
- Copy DinexLinearRegression.ex5 to MQL5\Libraries.
- Add the import block with the functions you use (shown in the images and in the example EA).
- Open a regression, push each new value, read the result.
Definitions (window of n values, x = 0 for the oldest ... n − 1 for the newest, least squares)
- Slope: change of the line for one step of x. Intercept: value of the line at x = 0. Fitted(i): intercept + slope x i; i may lie outside the window, n is one step ahead.
- End: the value of the line at the newest value. Residual: newest value − end.
- R squared: 1 − SSE / SST. It is not defined when all the values are equal (EMPTY_VALUE).
- Standard error: square root of SSE / (n − 2). z: residual divided by the standard error (EMPTY_VALUE when the standard error is 0). A fit whose squared error is below 1e-13 of the sum of squares of the window counts as exact.
Example EA (included in the description images)
On every new bar the example pushes the close price into two regressions of different windows and into a third one that gets the close plus a large offset, and at the end compares every output with a separate calculation. It also runs a long synthetic series, results worked out by hand, the error codes and the whole-array call. It prints the number of checks and the number of failed checks.
Notes
- The library works on the numbers you give it. It reads no market data, gives no entries or exits and does not predict any move. A line fitted to past values describes those values.
- Up to 256 instances, window from 3 to 100000 values. The sums are recalculated from the stored values at every push for windows up to 64 values and every 64th push for larger windows.
Requirements
- MetaTrader 5, any symbol and timeframe.
