Announcement

Collapse
No announcement yet.

Woodies_LSMA.efs and inv()

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Woodies_LSMA.efs and inv()

    How can we add int(1) at Woodies_LSMA.efs.
    I try the following but not working
    /*************************
    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("AAA_Woodies LSMA");
    setCursorLabelName("LSMA");
    setDefaultBarFgColor(Color.red, 0);
    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(inv(1));
    vHigh = high(inv(1));
    vLow = low(inv(1));

    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
    }

  • #2
    Elias
    The script you posted requires some further changes in order to be used with multiple intervals.
    First comment out (or delete) the following lines inside function main

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


    Then remove all the instances of inv(1) that you added to the original script and replace the following line

    function main(nLength,nOffset)

    with

    function calcLSMA(nLength,nOffset,nInterval)

    With these changes you essentially converted function main into another function called calcLSMA. You now need to create a function main which will contain the command required to call that function and to return the values to the chart. To do this insert the following after the closing bracket of the preMain function and before var vPrice = null;

    PHP Code:
    function main(nLength,nOffset){
        
        if (
    nLength == nullnLength 25;
        if (
    nOffset == null) {
            
    nOffset 0;
        } else {
            
    nOffset Math.abs(Math.round(nOffset));
        }
        var 
    lsma efsInternal("calcLSMA",nLength,nOffset,inv(1));

        return (
    lsma);

    At this point the script uses the efsInternal() function to call the calcLSMA function and in passing the interval it controls the context in which that function is running.
    For a complete description and examples on how to use efsInternal() see this thread which you can refer to in the future if you need to modify other scripts.
    Alex

    Comment


    • #3
      Still few mistakes here where I reached my work
      Any Ideas
      Elias
      ........................
      function preMain()
      {
      setPriceStudy(true);
      setStudyTitle("AAA_Woodies LSMA");
      setCursorLabelName("LSMA");
      setDefaultBarFgColor(Color.red, 0);
      setDefaultBarThickness(2);
      }

      function main(nLength,nOffset){

      if (nLength == null) nLength = 25;
      if (nOffset == null) {
      nOffset = 0;
      } else {
      nOffset = Math.abs(Math.round(nOffset));
      }
      var lsma = efsInternal("calcLSMA",nLength,nOffset,inv(1));
      return (lsma);

      var vPrice = null;
      var vInit = false;
      var LSMA_Array = new Array();
      function calcLSMA(nLength,nOffset,nInterval)
      {

      if (vInit == false)
      vPrice = new Array(nLength);
      vInit = true;

      Close = 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) ;

      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;
      }
      }

      Comment


      • #4
        Elias
        Please use the same formula that you posted originally. The one you just posted is not the same.
        Alex

        Comment


        • #5
          Alex Sorry you rigth this is where I am after correcting once with your suggestions posted below

          Elias
          ..........
          *************************
          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
          }

          Comment


          • #6
            Elias
            To this script now apply the modifications I provided earlier using the same order in which they are listed
            Alex

            Comment


            • #7
              Alex works fine Thanks alot once more

              Elias

              Comment


              • #8
                Elias
                You are most welcome
                Alex

                Comment


                • #9
                  lsma efs

                  can the complete code be put in an .efs file for download?

                  Thanks,
                  Pamela

                  Comment


                  • #10
                    Pamela
                    You can find a pre-programmed LSMA study which is completely customizable and is preset for use with multiple intervals and/or symbols here
                    For information on this efs and its requirements see this post
                    Alex

                    Comment


                    • #11
                      lsma Angle help

                      Hello Alex,

                      I am using the LSMA Angle .esf from your library and was wondering if you can provide some assistance to the following scenario:

                      Based on your "amLinRegAngle.efs" I would like to create an array for the angle of the last X bars...

                      what modification is required in the code to accomplist this...


                      thaks
                      sam

                      Comment


                      • #12
                        sam
                        You don't need to create an array as those values can be accessed directly using the .getValue() method (just like with the eSignal built-in studies) eg xLRA.getValue(0) for the value at the current bar, xLRA.getValue(-1) for the value at the prior bar, xLRA.getValue(-2) for the value two bars back etc
                        Alex

                        Comment

                        Working...
                        X