LinearSquareRegrChannel.efs EFSLibrary - Discussion Board
File Name: LinearSquareRegrChannel.efs
Description:
Linear-Square Regression Channel
Formula Parameters:
Degree : 1
kstd : 2
Bars : 48
Shift : 0
Line Color 1 : Green
Line Color 2 : Green
Line Color 3 : Red
Line Color 4 : Red
Notes:
Download File:
LinearSquareRegrChannel.efs
EFS Code:
/********************************* Provided By: eSignal (Copyright c eSignal), a division of Interactive Data Corporation. 2009. All rights reserved. This sample eSignal Formula Script (EFS) is for educational purposes only and may be modified and saved under a new file name. eSignal is not responsible for the functionality once modified. eSignal reserves the right to modify and overwrite this EFS file with each new release. Description: Linear-Square Regression Channel Version: 1.0 06/23/2009 Formula Parameters: Default: Degree 1 kstd 2 Bars 48 Shift 0 Line Color 1 Green Line Color 2 Green Line Color 3 Red Line Color 4 Red Notes: **********************************/ var fpArray = new Array(); var bInit = false; function preMain() { setPriceStudy(true); setShowCursorLabel(false); setShowTitleParameters(false); setStudyTitle("Linear-Square Regression Channel"); var x = 0; fpArray[x] = new FunctionParameter("Degree", FunctionParameter.NUMBER); with(fpArray[x++]) { setLowerLimit(0); setUpperLimit(10); setDefault(1); } fpArray[x] = new FunctionParameter("kstd", FunctionParameter.NUMBER); with(fpArray[x++]) { setLowerLimit(1); setDefault(2); } fpArray[x] = new FunctionParameter("Bars", FunctionParameter.NUMBER); with(fpArray[x++]) { setLowerLimit(1); setDefault(48); } fpArray[x] = new FunctionParameter("Shift", FunctionParameter.NUMBER); with(fpArray[x++]) { setLowerLimit(0); setDefault(0); } fpArray[x] = new FunctionParameter("Line1", FunctionParameter.COLOR); with(fpArray[x++]) { setName("Line Color 1"); setDefault(Color.green); } fpArray[x] = new FunctionParameter("Line2", FunctionParameter.COLOR); with(fpArray[x++]) { setName("Line Color 2"); setDefault(Color.green); } fpArray[x] = new FunctionParameter("Line3", FunctionParameter.COLOR); with(fpArray[x++]) { setName("Line Color 3"); setDefault(Color.red); } fpArray[x] = new FunctionParameter("Line4", FunctionParameter.COLOR); with(fpArray[x++]) { setName("Line Color 4"); setDefault(Color.red); } } var fxl = new Array(); var fxh = new Array(); var sql = new Array(); var sqh = new Array(); var ai = new Array(); ai[0] = new Array(); ai[1] = new Array(); ai[2] = new Array(); ai[3] = new Array(); ai[4] = new Array(); ai[5] = new Array(); ai[6] = new Array(); ai[7] = new Array(); ai[8] = new Array(); ai[9] = new Array(); ai[10] = new Array(); ai[11] = new Array(); var b = new Array(); var c = new Array(); var x = new Array(); var y = new Array(); var sx = new Array(); var xHigh = null; var xLow = null; function main(Degree, kstd, Bars, Shift, Line1, Line2, Line3, Line4) { var nBarState = getBarState(); var i0 = 0; var sum, suml, sumh; var ip, p, n, f; var qq, mm, tt; var ii, jj, kk, ll, nn; var mi = 0; if (nBarState == BARSTATE_ALLBARS) { if (Degree == null) Degree = 1; if (kstd == null) kstd = 2; if (Bars == null) Bars = 48; if (Shift == null) Shift = 0; if (Line1 == null) Line1 = Color.green; if (Line2 == null) Line2 = Color.green; if (Line3 == null) Line3 = Color.red; if (Line4 == null) Line4 = Color.red; } if (bInit == false) { xHigh = high(); xLow = low(); bInit = true; } if (Degree > 10) return; var ip = Bars; var p = ip; sx[1] = p + 1; nn = Degree + 1; for (mi = 1; mi <= nn * 2 - 2; mi++) { sum = 0; for (n = i0; n <= i0 + p; n++) { sum += Math.pow(n, mi); } sx[mi + 1] = sum; } for (mi = 1; mi <= nn; mi++) { suml = 0.00000; sumh = 0.00000; for (n = i0; n <= i0 + p; n++) { if (mi == 1) { suml += xLow.getValue( - n); sumh += xHigh.getValue( - n); } else { suml += xLow.getValue( - n) * Math.pow(n, mi - 1); sumh += xHigh.getValue( - n) * Math.pow(n, mi - 1); } } b[mi] = suml; c[mi] = sumh; } for (jj = 1; jj <= nn; jj++) { for (ii = 1; ii <= nn; ii++) { kk = ii + jj - 1; ai[ii][jj] = sx[kk]; } } for (kk = 1; kk <= nn - 1; kk++) { ll = 0; mm = 0; for (ii = kk; ii <= nn; ii++) { if (Math.abs(ai[ii][kk]) > mm) { mm = Math.abs(ai[ii][kk]); ll = ii; } } if (ll == 0) return (0); if (ll != kk) { for (jj = 1; jj <= nn; jj++) { tt = ai[kk][jj]; ai[kk][jj] = ai[ll][jj]; ai[ll][jj] = tt; } tt = b[kk]; b[kk] = b[ll]; b[ll] = tt; tt = c[kk]; c[kk] = c[ll]; c[ll] = tt; } for (ii = kk + 1; ii <= nn; ii++) { qq = ai[ii][kk] / ai[kk][kk]; for (jj = 1; jj <= nn; jj++) { if (jj == kk) { ai[ii][jj] = 0; } else { ai[ii][jj] = ai[ii][jj] - qq * ai[kk][jj]; } } b[ii] = b[ii] - qq * b[kk]; c[ii] = c[ii] - qq * c[kk]; } } x[nn] = b[nn] / ai[nn][nn]; y[nn] = c[nn] / ai[nn][nn]; for (ii = nn - 1; ii >= 1; ii--) { mm = 0; tt = 0; for (jj = 1; jj <= nn - ii; jj++) { mm = mm + ai[ii][ii + jj] * x[ii + jj]; x[ii] = (1 / ai[ii][ii]) * (b[ii] - mm); tt = tt + ai[ii][ii + jj] * y[ii + jj]; y[ii] = (1 / ai[ii][ii]) * (c[ii] - tt); } } for (n = i0; n <= i0 + p; n++) { suml = 0; sumh = 0; for (kk = 1; kk <= Degree; kk++) { suml += x[kk + 1] * Math.pow(n, kk); sumh += y[kk + 1] * Math.pow(n, kk); } fxl[n] = x[1] + suml; fxh[n] = y[1] + sumh; } suml = 0; sumh = 0; for (n = i0; n <= i0 + p; n++) { suml += Math.pow(xLow.getValue( - n) - fxl[n], 2); sumh += Math.pow(xHigh.getValue( - n) - fxh[n], 2); } suml = Math.sqrt(suml / (p + 1)) * kstd; sumh = Math.sqrt(sumh / (p + 1)) * kstd; for (n = i0; n <= i0 + p; n++) { sqh[n] = fxh[n] + sumh; sql[n] = fxl[n] - suml; if (n > 0) { drawLineRelative( - n + Shift, sqh[n], -n + Shift + 1, sqh[n - 1], PS_SOLID, 2, Line1, "Line1" + n); drawLineRelative( - n + Shift, sql[n], -n + Shift + 1, sql[n - 1], PS_SOLID, 2, Line2, "Line2" + n); drawLineRelative( - n + Shift, fxh[n], -n + Shift + 1, fxh[n - 1], PS_SOLID, 2, Line3, "Line3" + n); drawLineRelative( - n + Shift, fxl[n], -n + Shift + 1, fxl[n - 1], PS_SOLID, 2, Line4, "Line4" + n); } } return; }