Woodies_LSMA.efs

ICE Data Services -

Woodies_LSMA.efs  

 

File Name: Woodies_LSMA.efs


Description:
Calculates the Least Square Moving Average (LSMA) based on theories of "Woodie".

 

Formula Parameters:
nLength - Defines the length of the Moving Average. Default is 25.
nOffset - Defines how offset the MA is. Default is 0.

 

Notes:
NA

 

Download File:
Woodies_LSMA.efs




EFS Code:

/*************************
Copyright © eSignal, 2003
**************************
Description: Calculates the Least Square Moving Average (LSMA)
based on theories of "Woodie".
MA Length = 25
MA Offset = 0
MA Source = HLC/3

Version Control:

1.1 -- Corrected bug that didn't offset the LSMA properly.
1.2 -- Offset problem corrected.

*/



function preMain()
{
    setPriceStudy(true);    
    setStudyTitle("Woodies LSMA");
    setCursorLabelName("LSMA");
    setDefaultBarThickness(2);
}

var vPrice = null;

var vInit = false;

var LSMA_Array = new Array();

function main(nLength,nOffset)
{
    if (nLength == null) nLength = 25;
    if (nOffset == null) {
        nOffset = 0;
    } else {
        nOffset = Math.abs(Math.round(nOffset));
    }

    if (vInit == false) {
        vPrice = new Array(nLength);       
        vInit = true;
    }
    
    vClose = close();
    vHigh = high();
    vLow = low();
    
    if (vClose == null) return;
    if (vHigh == null) return;
    if (vLow == null) return;
    
    if (getBarState() == BARSTATE_NEWBAR) {
        if (vPrice[nLength-1] != null) vPrice.pop();
        vPrice.unshift(vHLC3);
    }
    vHLC3 = (vClose + vHigh + vLow) / 3;  
    vPrice[0] = vHLC3;
    
    if (vPrice[nLength-1] == null) return;
    
    var Num1 = 0.0;
    var Num2 = 0.0;
    var SumBars = nLength * (nLength - 1) * 0.5;
    var SumSqrBars = (nLength - 1) * nLength * (2 * nLength - 1) / 6;
    var SumY = 0.0;
    var Sum1 = 0.0;
    var Sum2 = 0.0;
    var Slope = 0.0;
    var Intercept = 0.0;

    for (i = 0; i < nLength; i++)
    {
        SumY += vPrice[i];
        Sum1 += i * vPrice[i];
    }
    Sum2 = SumBars * SumY;
    Num1 = nLength * Sum1 - Sum2;
    Num2 = SumBars * SumBars - nLength * SumSqrBars;
    if (Num2 != 0) Slope = Num1 / Num2;
    Intercept = (SumY - Slope * SumBars) / nLength;
    var LinearRegValue = Intercept + Slope * (nLength - 1);
    
    if (getBarState() == BARSTATE_NEWBAR) {
        if (LSMA_Array[nLength-1] != null) LSMA_Array.pop(); // v1.2
        LSMA_Array.unshift(LinearRegValue);
    }
    
    LSMA_Array[0] = LinearRegValue;
    
    /*   *** Possible offset bug ***
    if (nOffset > 0) {
        return LSMA_Array[nOffset-1];
    } else {
        return LinearRegValue;
    } */
    
    return LSMA_Array[nOffset]; // v1.2
}