polyfit (var* Coeff, var* Data, int Length, int Order, var Weight) : var

Polynomial regression. Generates a polynomial of the form y = anxn + ... + a1x + a0  that is the best fit to a section of a price series or any other data series. This polynomial can be used for extrapolating the Data series into the future, and thus predicting future prices.

Parameters:

Coeff A var[8] array for storing the calculated polynomial coefficients an, or 0 for storing the coefficients internally.
Data Data series to be approximated by the polynomial.
Length Number of elements in the Data series to be approximated by the polynomial.
Order Order of the polynomial (1..7). Use 1 for linear regression, 2 for parabolic (quadratic) regression, and higher numbers for nth-order regression.
Weight Ratio of the weight of the last data value to the weight of the first value, for "fading-memory" polynomials. Use 1 for equal weights.

Returns

Correlation coefficient, normally in the 0..1 range. Gives the similarity of the price curve and the polynomial.

Modifies

Coeff - set to the coefficients of the polynomial, in the order of their index, starting with Coeff[0]. The remaining coefficients are set to 0.

 

polynom (var* Coeff, int Offs) : var

Returns the value of the polynomial with the given coefficients at a given offset.

Parameters:

Coeff A var[8] array that contains the polynomial coefficients an, or 0 for using the last coefficients generated by polyfit.
Offs The offset into the polyfit Data series of the returned polynomial value. Use negative offsets for extending the series into the future.

Returns

Value of the polynomial at the given bar offset.
 

Remarks:

Examples:

// least square moving average indicator
var LSMA(vars Data,int Period,int Offset)
{
  polyfit(0,Data,Period,1,0);
  return polynom(0,Offset);
}

// quadratic least square moving average indicator
var QLSMA(vars Data,int Period,int Offset)
{
  polyfit(0,Data,Period,2,0);
  return polynom(0,Offset);
}

// predict price change by parabolic regression
function run()
{
vars Diff = series(price(0)-price(1)); var Correlation = polyfit(0,Diff,15,2,1);
// sum up the differences for predicting the price change over the next 3 bars
var Change3 = polynom(0,-1)+polynom(0,-2)+polynom(0,-3);

plot("Prediction",price(0)+Change3,MAIN,BLUE);
plot("Correlation",Correlation,NEW,GREEN);
}

See also:

frechet, advise, predict

► latest version online