Announcement

Collapse
No announcement yet.

Changing LSMA color based upon direction

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

  • Changing LSMA color based upon direction

    Is there a way to make the LSMA green when it rises and red when it falls? I've tried a couple of things, but my best results changed the color one bar too late.


    /*************************
    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
    */

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

    var vPrice = null;

    var vInit = false;

    function main(nLength,nOffset)
    {
    if (nLength == null) nLength = 25;
    if (nOffset == null) nOffset = 0;
    if (nBar == null) nBar = 0;

    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) {
    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;
    var i = 0;
    for (i = 0; i < nLength; i++)
    {
    if(getCurrentBarIndex() > -5 && getCurrentBarIndex() != 0) debugPrintln(vPrice[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 - nOffset);
    return LinearRegValue;
    }

  • #2
    How are you trying to change the color? Could you give an example of what code you are trying and what the results looked like?
    Regards,
    Jay F.
    Product Manager
    _____________________________________
    Have a suggestion to improve our products?
    Click Support --> Request a Feature in eSignal 11

    Comment


    • #3
      The code I'm using, which I cobbled from eSignal sample files, works fine EXCEPT that the color changes one bar late. So for example, if values have been rising, the LSMA line is green. However, if the current bar begins a decline in values (e.g. value of previous bar is 9102, current bar value is 9101, next bar value is 9100), the line does not turn red until the next bar prints.


      Here is the parent file:

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


      function main(inputLength, inputOffset, UpBarThickness, DnBarThickness, ColorUp, ColorDn) {
      if(inputLength == null)
      inputLength = 25;

      if(inputOffset == null)
      inputOffset = 0;

      if(UpBarThickness == null)
      UpBarThickness = 3;

      if(DnBarThickness == null)
      DnBarThickness = 3;

      /*** Valid Colors ***
      aqua, black, blue, brown, cyan, darkgreen,
      darkgrey, fushcia, green, grey, khaki,
      lightgrey, lightyellow, lime, magenta,
      maroon, navy, olive, paleyellow, purple,
      red, teal, white, yellow
      ************************/

      if (ColorUp == null) {
      vColorUp = Color.lime;
      } else {
      vColorUp = eval("Color."+ColorUp);
      }
      if (ColorDn == null) {
      vColorDn = Color.red;
      } else {
      vColorDn = eval("Color."+ColorDn);
      }


      var vNew, vOld;

      vNew = call("/downloads/LSMA_woodie.efs", inputLength, inputOffset, 0);
      vOld = call("/downloads/LSMA_woodie.efs", inputLength, inputOffset, -1);

      if(vNew > vOld) {
      setBarFgColor(vColorUp, 0);
      setBarThickness(UpBarThickness);
      } else if(vNew < vOld) {
      setBarFgColor(vColorDn, 0);
      setBarThickness(DnBarThickness);
      }
      return vNew;
      }

      ************************************
      ************************************
      Here is the LSMA_woodie code I'm calling.

      /*************************
      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
      */

      function preMain()
      {
      setPriceStudy(true);
      setStudyTitle("Woodies LSMA");
      setCursorLabelName("LSMA");
      setDefaultBarThickness(2);
      setDefaultBarFgColor(Color.cyan,3);
      }

      var vPrice = null;

      var vInit = false;
      var vHLC3 = 0;

      function main(nLength,nOffset,nBar)
      {
      if (nLength == null) nLength = 25;
      if (nOffset == null) nOffset = 0;
      if (nBar == null) nBar = 0;

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

      vClose = close(nBar);
      vHigh = high(nBar);
      vLow = low(nBar);

      if (vClose == null) return;
      if (vHigh == null) return;
      if (vLow == null) return;

      if (getBarState() == BARSTATE_NEWBAR) {
      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;
      var i = 0;
      for (i = 0; i < nLength; i++)
      {
      if(getCurrentBarIndex() > -5 && getCurrentBarIndex() != 0) debugPrintln(vPrice[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 - nOffset);
      return LinearRegValue;
      }

      Comment


      • #4
        There's nothing wrong with your code. This has to do with the way it's displayed. The LSMA is being colored from the time the condition is true to the next bar. It is designed to work from a given point forward. If you change the formula to place a colored dot at each LSMA value and color appropriately, it may make the results a little more visually pleasing.
        Regards,
        Jay F.
        Product Manager
        _____________________________________
        Have a suggestion to improve our products?
        Click Support --> Request a Feature in eSignal 11

        Comment


        • #5
          Thanks Jay for your help. I guess what I'm really asking is if there is a way to retroactively change the prior bar's color based upon the results of the current bar in order to make the presentation more visually accurate?

          Comment


          • #6
            Hello popegregory1,

            You can accomplish this task using the setBar() function. The following example would set the previous bar's LinearRegValue line to red.

            PHP Code:
            setBar(Bar.FgColor0Color.red); 
            Please visit the EFS Help Center & Library for more details and examples of setBar().
            Jason K.
            Project Manager
            eSignal - an Interactive Data company

            EFS KnowledgeBase
            JavaScript for EFS Video Series
            EFS Beginner Tutorial Series
            EFS Glossary
            Custom EFS Development Policy

            New User Orientation

            Comment


            • #7
              Thank you Jason. You guys are terrific

              Comment


              • #8
                Could someone please post the EFS that changes colours on the LSMA?

                RSy

                Comment


                • #9
                  RSy
                  They are actually two efs. The first one which is attached to this message needs to be saved to the Downloads subfolder of the Formulas folder.
                  Alex
                  Attached Files

                  Comment


                  • #10
                    RSy
                    Attached is the second formula that can be saved in any of the subfolders of the Formulas folder.
                    This is the efs you will be loading in the chart.
                    FYI both efs are a simple copy and paste of the code that is contained in an earlier message in this thread.
                    Alex
                    Attached Files

                    Comment

                    Working...
                    X