Site icon Thinkorswim Thinkscript Studies

3rd Order Polynomial Thinkscripts

Script only works on a 100 or less bars and don’t fed it with full long timeseries .There are three inputs. the sample length, the “y” which is currently the “close” and there is also “def x = data” string…you can put there just anything as a regressor.

Read more
#VM_3rd_Order_Polynomial 
#gRowEx 
input n = 50; 
input y = close; 
# y=ax^3+bx^2+cx+d 
script matrix_3x3 { 
    input a11 = 0; 
    input a12 = 0; 
    input a13 = 0; 
    input a21 = 0; 
    input a22 = 0; 
    input a23 = 0; 
    input a31 = 0; 
    input a32 = 0; 
    input a33 = 0; 
    plot delta = a11 * a22 * a33 + a12 * a23 * a31 + a13 * a21 * a32 - (a13 * a22 * a31 + a12 * a21 * a33 + a11 * a23 * a32); 
} 
script matrix_4x4 { 
    input a11 = 0; 
    input a12 = 0; 
    input a13 = 0; 
    input a14 = 0; 
    input a21 = 0; 
    input a22 = 0; 
    input a23 = 0; 
    input a24 = 0; 
    input a31 = 0; 
    input a32 = 0; 
    input a33 = 0; 
    input a34 = 0; 
    input a41 = 0; 
    input a42 = 0; 
    input a43 = 0; 
    input a44 = 0; 
    plot delta = a31 * matrix_3x3(a12, a13, a14, a22, a23, a24, a42, a43, a44) - a32 * matrix_3x3(a11, a13, a14, a21, a23, a24, a41, a43, a44) + a33 * matrix_3x3(a11, a12, a14, a21, a22, a24, a41, a42, a44) - a34 * matrix_3x3(a11, a12, a13, a21, a22, a23, a41, a42, a43); 
    ; 
} 
#the equations 
#a∑x3i+b∑x2i+c∑xi+nd=∑yi, 
#a∑x4i+b∑x3i+c∑x2i+d∑xi=∑xiyi, 
#a∑x5i+b∑x4i+c∑x3i+d∑x2i=∑x2iyi, 
#a∑x6i+b∑x5i+c∑x4i+d∑x3i=∑x3iyi; 
#======================= 
input fb = 1; 
def bar = if barnumber()>=fb then bar[1]+1 else 0; 
def x = bar; 
def sx3 = Sum(Power(x, 3), n); 
def sx2 = Sum(Sqr(x), n); 
def sx = Sum(x, n); 
def sx4 = Sum(Power(x, 4), n); 
def sx5 = Sum(Power(x, 5), n); 
def sx6 = Sum(Power(x, 6), n); 
def sy = Sum(y, n); 
def sxy = Sum(x * y, n); 
def sx2y = Sum(Sqr(x) * y, n); 
def sx3y = Sum(Power(x, 3) * y, n); 
#======================== 
#rewrite the equations with the inputs 
#======================== 
# sx3 sx2 sx   n  | a | sy 
# sx4 sx3 sx2 sx  | b | sxy 
# sx5 sx4 sx3 sx2 | c | sx2y 
# sx6 sx5 sx4 sx3 | d | sx3y 
#считаем определитель 
def delta = matrix_4x4(sx3, sx2, sx, n, sx4, sx3, sx2, sx, sx5, sx4, sx3, sx2, sx6, sx5, sx4, sx3); 
def delta_a = matrix_4x4(sy, sx2, sx, n, sxy, sx3, sx2, sx, sx2y, sx4, sx3, sx2, sx3y, sx5, sx4, sx3); 
def delta_b = matrix_4x4(sx3, sy, sx, n, sx4, sxy, sx2, sx, sx5, sx2y, sx3, sx2, sx6, sx3y, sx4, sx3); 
def delta_c = matrix_4x4(sx3,sx2,sy,n,sx4,sx3,sxy,sx,sx5,sx4,sx2y,sx2,sx6,sx5,sx3y,sx3); 
def delta_d = matrix_4x4(sx3,sx2,sx,sy,sx4,sx3,sx2,sxy,sx5,sx4,sx3,sx2y,sx6,sx5,sx4,sx3y); 
def a = delta_a/delta; 
def b = delta_b/delta; 
def c = delta_c/delta; 
def d = delta_d/delta; 
plot eq = a*power(x,3)+b*sqr(x)+c*x+d; 
Exit mobile version