/*********************************Provided By: eSignal (Copyright ) eSignal), a division of Interactive Data Corporation. 2007. 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 Regression IndicatorParameters: Default: Periods 20**********************************/function preMain() { setPriceStudy(true); setStudyTitle("Linear Regression Indicator"); setCursorLabelName("LR", 0); setDefaultBarFgColor(Color.red, 0); setDefaultBarThickness(2, 0); var fp1 = new FunctionParameter("nLength", FunctionParameter.NUMBER); fp1.setName("Periods"); fp1.setLowerLimit(1); fp1.setDefault(20);}var bInit = false;var xLR = null;function main(nLength) { if (bInit == false) { xLR = efsInternal("calcLR", nLength); bInit = true; } var nLR = xLR.getValue(0); return nLR;}function calcLR(nLen) { // y = Ax + B; // A = SUM( (x-xAVG)*(y-yAVG) ) / SUM( (x-xAVG)^2 ) // A = slope // B = yAVG - (A*xAVG); if (close(-(nLen-1)) != null) { var xSum = 0; var ySum = 0; var i = 0; for (i = 0; i < nLen; i++) { xSum += i; ySum += close(-i); } var xAvg = xSum/nLen; var yAvg = ySum/nLen; var aSum1 = 0; var aSum2 = 0; i = 0; for (i = 0; i < nLen; i++) { aSum1 += (i-xAvg) * (close(-i)-yAvg); aSum2 += (i-xAvg)*(i-xAvg); } var A = (aSum1 / aSum2); var B = yAvg - (A*xAvg); } return B;} |