Announcement

Collapse
No announcement yet.

Color Change LSMA

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

  • Color Change LSMA

    I am trying to change the color of a calced LSMA when the closing price is above or below the LSMA. How can I accomplish this by changing the code below? Currently it changes when the LSMA slope changes, fine on quiet days - but when there is a strong trend, the change lags considerably.

    I would like to accomplish this in one EFS file (I've seen solutions that use 2 different files, but not one). Thanks in advance for a prompt response.

    I assume this is the code that requires changing:

    if (LSMA_Array[0] > close) setBarFgColor(ColorUp,0);
    else setBarFgColor(ColorDn,0);

    ===================

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

    var fp1 = new FunctionParameter("nLength", FunctionParameter.NUMBER);
    fp1.setName("LSMA Length");
    fp1.setDefault(25);

    var fp2 = new FunctionParameter("ColorDn", FunctionParameter.COLOR);
    fp2.setName("Down Color");
    fp2.setDefault(Color.red);

    var fp3 = new FunctionParameter("ColorUp", FunctionParameter.COLOR);
    fp3.setName("Up Color");
    fp3.setDefault(Color.lime);
    }

    var vPrice = null;
    var vInit = false;
    var LSMA_Array = new Array();
    var SumBars = 0;
    var SumSqrBars = 0;
    var Slope = 0.0;

    function main(nLength,nOffset,ColorUp,ColorDn)
    {
    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;
    SumBars = nLength * (nLength - 1) * 0.5;
    SumSqrBars = (nLength - 1) * nLength * (2 * nLength - 1) / 6;
    }

    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(0);
    }
    vPrice[0] = (vClose + vHigh + vLow) / 3;

    if (vPrice[nLength-1] == null) return;

    var SumY = 0.0;
    var Sum1 = 0.0;
    for (i = 0; i < nLength; i++)
    {
    SumY += vPrice[i];
    Sum1 += i * vPrice[i];
    }
    var Sum2 = SumBars * SumY;
    var Num1 = nLength * Sum1 - Sum2;
    var Num2 = SumBars * SumBars - nLength * SumSqrBars;
    if (Num2 != 0) Slope = Num1 / Num2;
    var Intercept = (SumY - Slope * SumBars) / nLength;
    if (getBarState() == BARSTATE_NEWBAR) {
    if (LSMA_Array[nLength-1] != null) LSMA_Array.pop();
    LSMA_Array.unshift(0);
    }
    LSMA_Array[0] = Intercept + Slope * (nLength - 1);

    if (LSMA_Array[0] > close) setBarFgColor(ColorUp,0);
    else setBarFgColor(ColorDn,0);

    return LSMA_Array[0];
    }

  • #2
    TBond
    Replace close with vClose in if (LSMA_Array[0] > close) setBarFgColor(ColorUp,0);
    Alex

    Comment


    • #3
      Alex,

      Thanks - duh :-) I can't believe I missed that

      Comment

      Working...
      X