polyfit (var* coeff, vars Data, int TimePeriod, int order, var weight) : var

Polynomial regression. Generates a polynomial 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, or 0 for storing the coefficients internally.
Data Data series to be approximated by the polynomial.
TimePeriod Number of elements (1..1000) in the Data series to be approximated by the polynomial.
order Order of the polynom (1..7). Use 1 for linear regression, 2 for parabolic 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].

 

polynom (var* coeff, int num) : var

Returns the value of the polynom with the given coefficients at a given bar number.

Parameters:

coeff A var[8] array that contains the polynomial coefficients, or 0 for using the last coefficients generated by polyfit.
num The bar offset of the returned polynomial value (0 = current bar). Use negative bar numbers for predicting the future.

Returns

The predicted data at the given bar number, based on the polynomial.

Remarks:

Example:

function run()
{
vars Diff = series(price(0)-price(1)); var Correlation = polyfit(0,Diff,15,2,1);
// sum 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:

detect, advise, predict

► latest version online