MT4 Multiple Regression: more than 8 degrees with extrapolation

 

Hi all!

The public indicator i-regr is so useful but the maximum degree is 8 and no have extrapolation. Not yet.

By example, the statistical software Stata have max capacity of degrees of 11.000. For powerful machines.

My intention is to modify i-regr to support many degrees as possible and to have extrapolation.

A regression with number of observations equal to number of variables (degrees) provides the exact equation of entire waves analysed and a bit extrapolation is valuable.

Let´s do this?!

Huge efforts

 

Files:
i-regr.mq4  5 kb
 
Thank you WHRoeder
 

The coloured regression indicator in image above (i-Regr OTM Color Painel MT4) uses mean H+L+C+O/4 and is an optimized regression search. It tests all regressions from bars 25 to 1000 and degree 1 to 8. All results that turn the error of regression bigger on actual bar are discarded. Than the R squared is compared and is plotted the fittest regression. Shared below.

Now, I am constructing a better regression model, with more fit and extrapolation. I think that a extrapolate of 1 unit in an equation with 1000 degrees and 1000 bars give us an amazing number since the equation hit exactly all 1000 previous numbers. If we extrapolate our minds and take all historical data of prices of an instrument and do this type of regression we´ll have in our hands the best NN and AI possible and I will not be who goes against this universal truth of mathematics extracted (so much energy needed to change the route). But black swams happens, just adjust the risk.

Then I want this.

The code passed in another topic (Fitting a parabola) is not giving a line exact as the line graph (when Degree is equal to Bars), as see in image. How we fix this? Any idea from someone?

The Degree bigger than 80 is not yet possible too (I think it is a limitation of the matrices), how we fix this too?

The code:

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 LightSeaGreen
#property indicator_color2 Gold
#property indicator_color3 Gold

//-----
double reg[],reg1[],reg2[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
  SetIndexBuffer(0, reg);
  SetIndexBuffer(1, reg1);
  SetIndexBuffer(2, reg2);
  
  SetIndexStyle(0, DRAW_LINE);
  SetIndexStyle(1, DRAW_LINE);
  SetIndexStyle(2, DRAW_LINE);

  return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{

int CogDegree=80;
int CogBars=80;
int CogNW0LW1;

#define CogDegreeMAX  200 // ComputeCOG
#define CogDegree2MAX 400  // 2*MAX
#define COGCENTR     0
#define COGRMS       1
#define COGVALUES    2
double  cogvalues[][COGVALUES];

    if (CogDegree < 1  || CogDegree > CogDegreeMAX) {
        Alert("CogDegree must be 1-", CogDegreeMAX);
//        Log(  "CogDegree must be 1-", Cog.Degree.MAX);
        CogDegree  = MathMax(1, MathMin(CogDegreeMAX, CogDegree));      }
   if (CogBars < CogDegree){
        Alert("CogBars must be ",CogDegree," or greater");
//        Log(  "CogBars must be ",Cog.Degree," or greater");
      CogBars = CogDegree;                                }
   if (CogNW0LW1 < 0 || CogNW0LW1 > 1){
      Alert("CogNW0LW1 must be 0 or 1");
//      Log(  "CogNW0LW1 must be 0 or 1");
       CogNW0LW1 = 0;        }

   int iBar;
   int   nBars = iBars(NULL,0),
      iLimit   = MathMin(nBars, iBar+CogBars);
//    if (iLimit-iBar < CogDegree)         return;  // Bad call or no bars.
//   if (!ResizeBuffer(cogvalues, nBars))  return;  // Out of memory?
    double   Bi[CogDegreeMAX];
    double  EXn[CogDegree2MAX];       int CogDegree2 = CogDegree*2;
    double  Aii[CogDegreeMAX, CogDegreeMAX];
    for(int ii = 0; ii <= CogDegree;  ii++)             Bi[ii] = 0;
    for(    ii = 0; ii <= CogDegree2; ii++)            EXn[ii] = 0;
    for(int i = iBar; i < iLimit; i++){         // Count up for maximum accuracy
      if (CogNW0LW1 == 0)   double Xn = 1; else Xn = iLimit-i;  // 1*weight
        for(ii = 0; ii <= CogDegree;  ii++, Xn *= i){ Bi[ii]  += Xn*iOpen(NULL,0,i);
                                                       EXn[ii] += Xn;         }
        for(      ; ii <= CogDegree2; ii++, Xn *= i)  EXn[ii] += Xn;
    }
    for(int jj = 0; jj <= CogDegree; jj++) {
        for(ii = 0; ii <= CogDegree; ii++) {   Aii[ii, jj] = EXn[ii + jj]; }
    }

    for(int kk = 0; kk <= CogDegree; kk++) {           // Gaussian Elimination
        int ll = kk;    double mm = MathAbs(Aii[ll, kk]);
        for(ii = kk+1; ii <= CogDegree; ii++) {        // Largest magintude.
            double tt = MathAbs(Aii[ii, kk]);
            if (tt > mm) {  ll = ii;    mm = tt;    }
        }

        if (ll != kk) {                                 // Pivot.
            for(jj = 0; jj <= CogDegree; jj++) {
                tt          = Aii[kk, jj];
                Aii[kk, jj] = Aii[ll, jj];
                Aii[ll, jj] = tt;
            }
            tt = Bi[kk]; Bi[kk] = Bi[ll]; Bi[ll] = tt;
        }
        for(ii = kk + 1; ii <= CogDegree; ii++) {      // Reduce.
            double qq; qq = Aii[ii, kk] / Aii[kk, kk];  // Aii[ii,kk]=0
            for(jj = kk+1; jj <= CogDegree; jj++) {
                Aii[ii, jj] -= qq * Aii[kk, jj];
            }
            Bi[ii] -= qq * Bi[kk];
    }   }
    double  x[CogDegreeMAX];                          // Back substitution
    x[CogDegree] = Bi[CogDegree] / Aii[CogDegree, CogDegree];
    for (ii = CogDegree-1; ii >= 0; ii--) {
        tt = Bi[ii];
        for(jj = ii+1; jj <= CogDegree; jj++){
            tt -= Aii[ii, jj] * x[jj];
        }
        x[ii] = tt/Aii[ii, ii];
    }
    // Least squares completed.  Compute residual and final values.
    double  Ews = 0, Ew = 0,     w = 1.;
    for(i = iBar; i < iLimit; i++){
        double                                              Y  = x[0];
        for(kk = 1, Xn=i; kk <= CogDegree; kk++, Xn *= i)  Y += x[kk] * Xn;
        double residual = MathMax(iHigh(NULL,0,i) - Y, Y - iLow(NULL,0,i));
        if (CogNW0LW1 == 1)   w = iLimit-i;
        Ews += w * residual*residual;   Ew += w;
    }
    double ms = Ews / Ew,   rms = MathSqrt(ms);
    for(i = iLimit-1; i >= iBar; i--){
                                                            Y  = x[0];
        for(kk = 1, Xn=i; kk <= CogDegree; kk++, Xn *= i)  Y += x[kk] * Xn;
        cogvalues[i][COGCENTR] = Y;
        cogvalues[i][COGRMS] = rms;
        reg[i+1] = Y;
        reg1[i+1] = Y + 2*rms;
        reg2[i+1] = Y - 2*rms;
    }

  return(0);
}
//+------------------------------------------------------------------+

 

 
This make money.
 

The free internet is so beauty.

80 degrees found.

R-squared equal to one more than 8 degrees not yet

Any help will be so much appreciated!

The results were positive.

8 Lows, Highs, Closes & Opens equations with their extrapolations (4 in one bar).

The remark was when all extrapolation bars indicate the same direction in next bar. I took than a picture of signals.

In initial years of Eur/Usd this one eight was massive assertive, these last years not so pretty much. In Nikkei I saw a little times a fun contrary effect.

This with only eight.

Let´s to 1000.

To 100000.

To All!

Picture of positive signals. (last years of most traded currencies)Prediction, Forecast or Antecipation?!

 

Indicators that sight same direction first extrapolation (HLOC) of Regression (80 Degrees, 80 Bars).

We are correct, how more deepness is the Regression, more it tend to be assertive.

http://www.4shared.com/get/hMCeIObZba/Kore_Pred_80_80_4.html

http://www.4shared.com/get/N5TQXp7Mba/Kore_i-regr_80_ext.html

 
noob01:

This with only eight.

Let´s to 1000.

To 100000.

To All!

The higher, the more floating point resolution required. Beyond 8 double will not be sufficient. You going to implement your own?
Reason: