Moving Linear Regression by. Dr. Mel Raiman

ICE Data Services -


mr_movlinreg2.efs  EFSLibrary - Discussion Board
  

File Name: mr_movlinreg2.efs


Description:
Moving Linear Regression by. Dr. Mel Raiman


Formula Parameters:
LR Periods : 3
LR1 Channels StDev : 1
LR2 Channels StDev : 2
MA Periods : 4
MA Price Source : Close
MA Type : Exponential
LR Thickness : 2
LR Color : Lime
MA Color : Red

Notes:
Version 2.0 - Added second set of regression lines at +/-
2 standard deviations.

Download File:
mr_movlinreg2.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:            Moving Linear Regression by. Dr. Mel Raiman        Version:            3.0  04/16/2009Formula Parameters:                 Defaults:    LR Periods                      3    LR1 Channels StDev              1    LR2 Channels StDev              2    MA Periods                      4    MA Price Source                 Close    MA Type                         Exponential    LR Thickness                    2    LR Color                        Lime    MA Color                        RedNotes:  Version 2.0 - Added second set of regression lines at +/-   2 standard deviations. *****************************************************************/var fpArray = new Array();var bInit = true;function preMain() {    setPriceStudy(true);    setStudyTitle("Moving LR 2");    setCursorLabelName("Mov LR", 0);    setCursorLabelName("MA", 1);    setDefaultBarFgColor(Color.lime, 0);    setDefaultBarFgColor(Color.red, 1);    setDefaultBarThickness(1, 0);    setDefaultBarThickness(1, 1);    setShowTitleParameters(false);    var x = 0;    fpArray[x] = new FunctionParameter("nLRlen", FunctionParameter.NUMBER);    with(fpArray[x++]) {        setName("LR Periods");        setLowerLimit(1);        setDefault(3);    }    fpArray[x] = new FunctionParameter("nLRstdev1", FunctionParameter.NUMBER);    with(fpArray[x++]) {        setName("LR1 Channels StDev");        setLowerLimit(0);        setDefault(1);    }    fpArray[x] = new FunctionParameter("nLRstdev2", FunctionParameter.NUMBER);    with(fpArray[x++]) {        setName("LR2 Channels StDev");        setLowerLimit(0);        setDefault(2);    }    fpArray[x] = new FunctionParameter("nMAlen", FunctionParameter.NUMBER);    with(fpArray[x++]) {        setName("MA Periods");        setLowerLimit(2);        setDefault(4);    }    fpArray[x] = new FunctionParameter("sMAPriceSource", FunctionParameter.STRING);    with(fpArray[x++]) {        setName("MA Price Source");        addOption("open");        addOption("high");        addOption("low");        addOption("close");        addOption("hl2");        addOption("hlc3");         addOption("ohlc4");        setDefault("close");    }    fpArray[x] = new FunctionParameter("sMAType", FunctionParameter.STRING);    with(fpArray[x++]) {        setName("MA Type");        addOption("sma");        addOption("ema");        addOption("wma");        addOption("vwma");        setDefault("ema");    }    fpArray[x] = new FunctionParameter("nLRThickness", FunctionParameter.NUMBER);    with(fpArray[x++]) {        setName("LR Thickness");        setLowerLimit(1);        setDefault(2);    }    fpArray[x] = new FunctionParameter("nMAThickness", FunctionParameter.NUMBER);    with(fpArray[x++]) {        setName("MA Thickness");        setLowerLimit(1);        setDefault(2);    }    fpArray[x] = new FunctionParameter("nLRColor", FunctionParameter.COLOR);    with(fpArray[x++]) {        setName("LR Color");        setDefault(Color.lime);    }    fpArray[x] = new FunctionParameter("nMAColor", FunctionParameter.COLOR);    with(fpArray[x++]) {        setName("MA Color");        setDefault(Color.red);    }}var xMA = null;var xLRvaluesIn_A = null;var xLRvaluesIn_B = null;var xLRvalues23_A = null;var xLRvalues23_B = null;var xStDev = null;function main(nLRlen, nLRstdev1, nLRstdev2, nMAlen, sMAPriceSource, sMAType,             nLRThickness, nMAThickness, nLRColor, nMAColor) {    var nBarState = getBarState();    var vStDev = null;    var A = 0;    var B = 0;    var A2 = 0;    var B2 = 0;    var vMA = 0;    if (nBarState == BARSTATE_ALLBARS) {        if (nLRlen == null) nLRlen = 10;        if (nLRstdev1 == null) nLRstdev1 = 1;        if (nLRstdev2 == null) nLRstdev2 = 2;        if (nMAlen == null) nMAlen = 4;        if (sMAPriceSource == null) sMAPriceSource = "close";        if (sMAType == null) sMAType = "ema";        if (nLRThickness == null) nLRThickness = 2;        if (nMAThickness == null) nMAThickness = 2;        if (nLRColor == null) nLRColor = Color.lime;        if (nMAColor == null) nMAColor = Color.red;    }    if (bInit == true) {        xMA = eval(sMAType)(nMAlen, eval(sMAPriceSource)());        xLRvaluesIn_A = efsInternal("LinReg", nLRlen);        xLRvaluesIn_B = getSeries(xLRvaluesIn_A, 1);        xLRvalues23_A = efsInternal("LinReg", 23);        xLRvalues23_B = getSeries(xLRvalues23_A, 1);        xStDev = efsInternal("StDev", 23);        setDefaultBarThickness(nLRThickness, 0);        setDefaultBarThickness(nMAThickness, 1);        setDefaultBarFgColor(nLRColor, 0);        setDefaultBarFgColor(nMAColor, 1);        bInit = false;    }    vMA = xMA.getValue(0);    A = xLRvaluesIn_A.getValue(0);    B = xLRvaluesIn_B.getValue(0);    vStDev = xStDev.getValue(0);    A2 = xLRvalues23_A.getValue(0);    B2 = xLRvalues23_B.getValue(0);    if (vMA == null || B == null || B2 == null) return;    // Basis    drawLineRelative(0, B2, -(23 - 1), (A2 * (23 - 1)) + B2,         PS_SOLID, 2, Color.cyan, "Basis");    // LR1    drawLineRelative(0, B2 + (nLRstdev1 * vStDev), -(23 - 1), (A2 * (23 - 1)) + B2 + (nLRstdev1 * vStDev),         PS_SOLID, 2, Color.cyan, "Upr1");    drawLineRelative(0, B2 - (nLRstdev1 * vStDev), -(23 - 1), (A2 * (23 - 1)) + B2 - (nLRstdev1 * vStDev),         PS_SOLID, 2, Color.cyan, "Lwr1");    // LR2    drawLineRelative(0, B2 + (nLRstdev2 * vStDev), -(23 - 1), (A2 * (23 - 1)) + B2 + (nLRstdev2 * vStDev),         PS_SOLID, 2, Color.RGB(0, 140, 140), "Upr2");    drawLineRelative(0, B2 - (nLRstdev2 * vStDev), -(23 - 1), (A2 * (23 - 1)) + B2 - (nLRstdev2 * vStDev),         PS_SOLID, 2, Color.RGB(0, 140, 140), "Lwr2");    return new Array(B, vMA);}var xSMAy = null;var bSecondInit = false;function LinReg(nLRlen) {    var xSum = 0;    var yAvg = 0;    if (bSecondInit == false) {        xSMAy = sma(nLRlen);        bSecondInit = true;    }    yAvg = xSMAy.getValue(0);    if (yAvg == null) return;    var xAvg = ((0 + (nLRlen - 1)) * nLRlen / 2) / nLRlen;    var aSum1 = 0;    var aSum2 = 0;    i = 0;    for (i = 0; i < nLRlen; ++i) {        aSum1 += (i - xAvg) * (close( - i) - yAvg);        aSum2 += (i - xAvg) * (i - xAvg);    }    var A = (aSum1 / aSum2);    var B = yAvg - (A * xAvg);    return new Array(A, B);}function StDev(nLength) {    var sumX = 0;    var sumX2 = 0;    var nClose = close( - nLength);    if (nClose == null) return;    for (i = 0; i < nLength; ++i) {        nClose = close( - i);        sumX += nClose;        sumX2 += (nClose * nClose);    }    var meanX = (sumX / nLength);    var stdev = Math.sqrt((sumX2 / nLength) - (meanX * meanX));    return stdev;}