Announcement

Collapse
No announcement yet.

VMA Study Issue

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

  • VMA Study Issue

    Hi, Im trying to replicate tradingview VMA indicator:
    Here is the link : https://www.tradingview.com/script/6...rage-LazyBear/

    Vma value on my study is empty :

    Click image for larger version

Name:	Screen Shot 2019-08-29 at 10.09.38 AM.png
Views:	650
Size:	26.0 KB
ID:	268373

    Note : I think problems starts from line 136 function fxs.

    Here is my complete code :


    var fpArray = new Array();
    function preMain()
    {
    setPlotType(PLOTTYPE_INSTANTCOLORLINE);
    setPriceStudy(true);
    setStudyTitle("VMA");
    setCursorLabelName("VMA", 0);
    setDefaultBarThickness(2, 0);

    var x=0;
    fpArray[x] = new FunctionParameter("gNbrLength", FunctionParameter.NUMBER);
    with(fpArray[x++])
    {
    setName("Number of Length");
    setLowerLimit(1);
    setDefault(10);
    }
    fpArray[x] = new FunctionParameter("gColorUp", FunctionParameter.COLOR);
    with(fpArray[x++])
    {
    setName("Up Color");
    setDefault(Color.lime);
    }
    fpArray[x] = new FunctionParameter("gColorDn", FunctionParameter.COLOR);
    with (fpArray[x++])
    {
    setName("Down Color");
    setDefault(Color.red);
    }
    fpArray[x] = new FunctionParameter("gColorFlat", FunctionParameter.COLOR);
    with (fpArray[x++])
    {
    setName("Flat Color");
    setDefault(Color.blue);
    }
    }

    var bMainInit = false;
    var xSrc = null;
    var xK = null;
    var xPdm = null;
    var xMdm = null;
    var xPdms = null;
    var xMdms = null;
    var xS = null;
    var xPdi = null;
    var xMdi = null;
    var xPdiS = null;
    var xMdiS = null;
    var xD = null;
    var xS1 = null;
    var xIS = null;
    var xHhv = null;
    var xLlv = null;
    var xD1 = null;
    var xVi = null;
    var xVMA = null;

    function main(gNbrLength, gColorFlat,gColorUp, gColorDn)
    {
    if(!bMainInit)
    {
    xK = efsInternal("fK",gNbrLength);
    xSrc = close();
    xPdm = efsInternal("fPDM",xSrc);
    xMdm = efsInternal("fMdm",xSrc);
    xPdms = efsInternal("fPdms",xK, xPdms, xPdm);
    xMdms = efsInternal("fMdms",xK, xMdms, xMdm);
    xS = efsInternal("fxs",xPdms, xMdms);
    /*xPdi = efsInternal("fPdi",xPdms, xS);
    xMdi = efsInternal("fMdi",xMdms, xS);
    xPdiS = efsInternal("fPdiS",xK, xPdiS, xPdi);
    xMdiS = efsInternal("fMdiS",xK, xMdiS, xMdi);
    xD = efsInternal("fD",xPdiS, xMdiS);
    xS1 = efsInternal("fS1",xPdiS, xMdiS);
    xIS = efsInternal("fIS",xK, xIS, xD, xS1);
    xHhv = efsInternal("fHhv",xIS, gNbrLength);
    xLlv = efsInternal("fLlv",xIS, gNbrLength);
    xD1 = efsInternal("fD1",xHhv, xLlv);
    xVi = efsInternal("fVi",xIS, xLlv, xD1);
    xVMA = efsInternal("fVMA",xK, xVi, xVMA, xSrc);
    //*/
    bMainInit = true;

    }

    return xS.getValue(0);
    }

    function fVMA(xK, xVi, xVMA, xSrc)
    {
    if (xVMA == null){
    return ((1 - xK.getValue(0)*xVi.getValue(0)) * 0 + xK.getValue(0)*xVi.getValue(0) * xSrc.getValue(0));
    } else {
    return ((1 - xK.getValue(0)*xVi.getValue(0)) * xVMA.getValue(-1) + xK.getValue(0)*xVi.getValue(0) * xSrc.getValue(0));
    }
    }

    function fK(gNbrLength)
    {
    return 1.0/gNbrLength;
    }

    function fPDM(xSrc)
    {
    return Math.max((xSrc.getValue(0) - xSrc.getValue(-1)), 0);
    }

    function fMdm(xSrc)
    {
    return Math.max((xSrc.getValue(-1) - xSrc.getValue(0)), 0);
    }

    function fPdms(xK, xPdms, xPdm)
    {
    if (xPdms == null){
    return ((1 - xK.getValue(0)) * 0 + xK.getValue(0)*xPdm.getValue(0));
    } else{
    return ((1 - xK.getValue(0)) * xPdms.getValue(-1) + xK.getValue(0)*xPdm.getValue(0));
    }

    }

    function fMdms(xK, xMdms, xMdm)
    {

    if (xPdms == null){
    return ((1 - xK.getValue(0)) * 0 + xK.getValue(0)*xMdm.getValue(0));
    } else {
    return ((1 - xK.getValue(0)) * xMdms.getValue(-1) + xK.getValue(0)*xMdm.getValue(0));
    }

    }

    function fxs(xPdms, xMdms)
    {
    return xPdms.getValue(0) + xMdms.getValue(0);
    }

    function fPdi(xPdms, xS)
    {
    if (xPdms.getValue(0) == 0 && xS.getValue(0)) {
    return 0;
    } else {
    return xPdms.getValue(0)/xS.getValue(0);
    }

    }

    function fMdi(xMdms, xS)
    {
    return xMdms.getValue(0) / xS.getValue(0);
    }

    function fPdiS(xK, xPdiS, xPdi)
    {
    if (xPdiS == null){
    return ((1 - xK.getValue(0)) * 0 + xK.getValue(0)*xPdi.getValue(0));
    } else {
    return ((1 - xK.getValue(0)) * xPdiS.getValue(-1) + xK.getValue(0)*xPdi.getValue(0));
    }
    }

    function fMdiS(xK, xMdiS, xMdi)
    {
    if (xMdiS == null){
    return ((1 - xK.getValue(0)) * 0 + xK.getValue(0)*xMdi.getValue(0));
    } else {
    return ((1 - xK.getValue(0)) * xMdiS.getValue(-1) + xK.getValue(0)*xMdi.getValue(0));
    }

    }

    function fD(xPdiS, xMdiS)
    {
    return Math.abs(xPdiS.getValue(0) - xMdiS.getValue(0));
    }

    function fS1(xPdiS, xMdiS)
    {
    return xPdiS.getValue(0) + xMdiS.getValue(0);
    }

    function fIS(xK, xIS, xD, xS1)
    {
    if (xIS == null){
    return ((1 - xK.getValue(0)) * 0 + xK.getValue(0)*xD.getValue(0)/xS1.getValue(0));
    } else {
    return ((1 - xK.getValue(0)) * xIS.getValue(-1) + xK.getValue(0)*xD.getValue(0)/xS1.getValue(0));
    }

    }

    function fHhv(xIS, gNbrLength)
    {
    return highest(gNbrLength, xIS);
    }

    function fLlv(xIS, gNbrLength)
    {
    return lowest(gNbrLength, xIS);
    }

    function fD1(xHhv, xLlv)
    {
    return xHhv.getValue(0) - xLlv.getValue(0);
    }

    function fVi(xIS, xLlv, xD1)
    {
    return (xIS.getValue(0) - xLlv.getValue(0))/xD1.getValue(0);
    }





  • #2
    Hello sun0781,
    Thanks for introducing me to Lazy Bear’s website…what a prolific coder he is! Below you’ll find the JavaScript/eSignal version of what you’re looking for…happy coding!

    // @author LazyBear - List of all my indicators:
    // https://docs.google.com/document/d/1...it?usp=sharing
    // study(title="Variable Moving Average [LazyBear]", shorttitle="VMA_LB", overlay=true)
    var fpArray = [];
    var xVMA = null;
    var xIS = null;
    var nK = null;
    var nVMALength = null;

    function preMain(){
    setPlotType(PLOTTYPE_INSTANTCOLORLINE);
    setPriceStudy(true);
    setStudyTitle("LazyBearVMA");
    setCursorLabelName("VMA", 0);
    setDefaultBarThickness(1, 0);

    fpArray[0] = new FunctionParameter("VMALength", FunctionParameter.NUMBER);
    fpArray[0].setName("VMA Length");
    fpArray[0].setLowerLimit(2);
    fpArray[0].setDefault(6);
    }

    function main(VMALength){
    if(getBarState() == BARSTATE_ALLBARS){
    nVMALength = VMALength;
    nK = 1 / nVMALength;
    xIS = efsInternal("Calc_IS", nK);
    xVMA = efsInternal("LazyBearVMA", xIS, nVMALength, nK);
    }
    xVMA.getValue(0) > xVMA.getValue(-1) ? setBarFgColor(Color.green) :
    xVMA.getValue(0) < xVMA.getValue(-1) ? setBarFgColor(Color.RGB(192, 0, 0)) :
    xVMA.getValue(0) == xVMA.getValue(-1) ? setBarFgColor(Color.white) : setBarFgColor(Color.grey);
    return xVMA.getValue(0);
    }

    var nPdmS_1 = 0;
    var nMdmS_1 = 0;
    var nPdiS_1 = 0;
    var nMdiS_1 = 0;
    var nIS_1 = 0;
    var nVMA_1 = 0;

    function Calc_IS(n_K){
    if(getCurrentBarCount() < 2) return;

    var nPdm = Math.max((close(0) - close(-1)), 0);
    var nMdm = Math.max((close(-1) - close(0)), 0);
    var nPdmS = (1 - n_K) * nPdmS_1 + n_K * nPdm;
    var nMdmS = (1 - n_K) * nMdmS_1 + n_K * nMdm;
    var nS = nPdmS + nMdmS;
    var nPdi = nPdmS / nS;
    var nMdi = nMdmS / nS;
    var nPdiS = (1 - n_K) * nPdiS_1 + n_K * nPdi;
    var nMdiS = (1 - n_K) * nMdiS_1 + n_K * nMdi;
    var nD = Math.abs(nPdiS - nMdiS);
    var nS1 = nPdiS + nMdiS;
    var nIS = (1 - n_K) * nIS_1 + n_K * nD / nS1;
    nPdmS_1 = nPdmS;
    nMdmS_1 = nMdmS;
    nPdiS_1 = nPdiS;
    nMdiS_1 = nMdiS;
    nIS_1 = nIS;
    return nIS;
    }

    function LazyBearVMA(xIS, n_VMALength, n_K){
    if(getCurrentBarCount() < n_VMALength + 2) return;

    var nHHV = highest(n_VMALength, xIS);
    var nLLV = lowest(n_VMALength, xIS);
    var nD1 = nHHV - nLLV;
    var nVi = (xIS.getValue(0) - nLLV) / nD1;
    var nVMA = (1 - n_K * nVi) * nVMA_1 + n_K * nVi * close(0);
    nVMA_1 = nVMA;
    return nVMA;
    }

    Comment


    • #3
      In my code above, another line needs to be added immediately after the
      if(getBarState() == BARSTATE_ALLBARS){.... }
      if(getCurrentBarCount() > nVMALength + 4) needs to be before the code for changing the line colors, because xVMA is going to be null for the first nVMALength + 2 bars, for some reason I didn't get an error message when I wrote it, but I got one tonight and realized my mistake. So to clarify, change the above code as follows:
      if(getBarState() == BARSTATE_ALLBARS){
      nVMALength = VMALength;
      nK = 1 / nVMALength;
      xIS = efsInternal("Calc_IS", nK);
      xVMA = efsInternal("LazyBearVMA", xIS, nVMALength, nK);
      }
      if(getCurrentBarCount() > nVMALength + 4)

      xVMA.getValue(0) > xVMA.getValue(-1) ? setBarFgColor(Color.green) :
      xVMA.getValue(0) < xVMA.getValue(-1) ? setBarFgColor(Color.RGB(192, 0, 0)) :
      xVMA.getValue(0) == xVMA.getValue(-1) ? setBarFgColor(Color.white) : setBarFgColor(Color.grey);
      return xVMA.getValue(0);
      }

      Comment


      • #4
        In real time the script wasn’t updating correctly due to the global variables for the 2 functions updating continuously instead of just once per bar as they need to be updating, so I updated the script again to fix this issue…I think that I finally have the bugs worked out.

        // LetUsLearn converted the Pine Script below of LazyBear to eSignal's EFS.
        // @author LazyBear - List of all my indicators:
        // https://docs.google.com/document/d/1...it?usp=sharing
        // study(title="Variable Moving Average [LazyBear]", shorttitle="VMA_LB", overlay=true)
        var fpArray = [];
        var xVMA = null;
        var xIS = null;
        var nK = null;
        var nVMALength = null;

        function preMain(){
        setPlotType(PLOTTYPE_INSTANTCOLORLINE);
        setPriceStudy(true);
        setStudyTitle("LazyBearVMA");
        setCursorLabelName("VMA", 0);
        setDefaultBarThickness(1, 0);

        fpArray[0] = new FunctionParameter("VMALength", FunctionParameter.NUMBER);
        fpArray[0].setName("VMA Length");
        fpArray[0].setLowerLimit(2);
        fpArray[0].setDefault(6);
        }

        function main(VMALength){
        if(getBarState() == BARSTATE_ALLBARS){
        nVMALength = VMALength;
        nK = 1 / nVMALength;
        xIS = efsInternal("Calc_IS", nK);
        xVMA = efsInternal("LazyBearVMA", xIS, nVMALength, nK);
        }
        if(getCurrentBarCount() > nVMALength + 4)
        xVMA.getValue(0) > xVMA.getValue(-1) ? setBarFgColor(Color.green) :
        xVMA.getValue(0) < xVMA.getValue(-1) ? setBarFgColor(Color.RGB(192, 0, 0)) :
        xVMA.getValue(0) == xVMA.getValue(-1) ? setBarFgColor(Color.white) : setBarFgColor(Color.grey);
        return xVMA.getValue(0);
        }

        var nPdmS = 0;
        var nPdmS_1 = 0;
        var nMdmS = 0;
        var nMdmS_1 = 0;
        var nPdiS = 0;
        var nPdiS_1 = 0;
        var nMdiS = 0;
        var nMdiS_1 = 0;
        var nIS = 0;
        var nIS_1 = 0;
        var nVMA = 0;
        var nVMA_1 = 0;

        function Calc_IS(n_K){
        if(getCurrentBarCount() < 2) return;
        if(getBarState() == BARSTATE_NEWBAR){
        nPdmS_1 = nPdmS;
        nMdmS_1 = nMdmS;
        nPdiS_1 = nPdiS;
        nMdiS_1 = nMdiS;
        nIS_1 = nIS;
        }
        var nPdm = Math.max((close(0) - close(-1)), 0);
        var nMdm = Math.max((close(-1) - close(0)), 0);
        nPdmS = (1 - n_K) * nPdmS_1 + n_K * nPdm;
        nMdmS = (1 - n_K) * nMdmS_1 + n_K * nMdm;
        var nS = nPdmS + nMdmS;
        var nPdi = nPdmS / nS;
        var nMdi = nMdmS / nS;
        nPdiS = (1 - n_K) * nPdiS_1 + n_K * nPdi;
        nMdiS = (1 - n_K) * nMdiS_1 + n_K * nMdi;
        var nD = Math.abs(nPdiS - nMdiS);
        var nS1 = nPdiS + nMdiS;
        nIS = (1 - n_K) * nIS_1 + n_K * nD / nS1;
        return nIS;
        }

        function LazyBearVMA(xIS, n_VMALength, n_K){
        if(getCurrentBarCount() < n_VMALength + 2) return;
        if(getBarState() == BARSTATE_NEWBAR) nVMA_1 = nVMA;
        var nHHV = highest(n_VMALength, xIS);
        var nLLV = lowest(n_VMALength, xIS);
        var nD1 = nHHV - nLLV;
        var nVi = (xIS.getValue(0) - nLLV) / nD1;
        nVMA = (1 - n_K * nVi) * nVMA_1 + n_K * nVi * close(0);
        return nVMA;
        }

        Comment


        • #5
          Is anybody getting tired of my revisions? While the above code will work and you won't see any difference in the values of most of the bars on the chart, technically it isn't correct. I have figured out that when a series object is used as a parameter for another series object as xIS is for
          xVMA that xIS will load twice, with the last values from the first load becoming the first values that the second load uses as its first value due to the global values that are carried forward. As such, this revision re-Zeros the global variables of xIS before the second run happens. In the program above, it's more of a technical issue that didn't affect something like probably 80% or so of the programs last values, but in other programs I don't doubt that this would be an issue! The change that I made was to add 2 global values and then the code below to xIS:
          var bDoneZeroingGlobals = false;
          var nCountCalc_ISLoads = 0;
          if(!bDoneZeroingGlobals && getCurrentBarIndex() == 0){
          nCountCalc_ISLoads++;
          if(nCountCalc_ISLoads >= 2) bDoneZeroingGlobals = true;
          else{
          nPdmS = 0;
          nMdmS = 0;
          nPdiS = 0;
          nMdiS = 0;
          nIS = 0;
          }
          // Calc_IS is loaded by 2 series objects...first as a parameter to xVMA,
          // and then for the xIS object itself, so the global variables MUST be
          // reset back to zero, otherwise the 2nd run will start with the values
          // that the first run ended at and they will become the values of xIS!!!
          }
          So in total the latest and hopefully the LAST revision is as follows:

          //
          LetUsLearn
          converted the Pine Script below of LazyBear to eSignal's EFS.
          // @author LazyBear - List of all my indicators:
          // https://docs.google.com/document/d/1...it?usp=sharing
          // study(title="Variable Moving Average [LazyBear]", shorttitle="VMA_LB", overlay=true)
          var fpArray = [];
          var xVMA = null;
          var xIS = null;
          var nK = null;
          var nVMALength = null;

          function preMain(){
          setPlotType(PLOTTYPE_INSTANTCOLORLINE);
          setPriceStudy(true);
          setStudyTitle("LazyBearVMA");
          setCursorLabelName("VMA", 0);
          setDefaultBarThickness(1, 0);

          fpArray[0] = new FunctionParameter("VMALength", FunctionParameter.NUMBER);
          fpArray[0].setName("VMA Length");
          fpArray[0].setLowerLimit(2);
          fpArray[0].setDefault(6);
          }

          function main(VMALength){
          if(getBarState() == BARSTATE_ALLBARS){
          nVMALength = VMALength;
          nK = 1 / nVMALength;
          xIS = efsInternal("Calc_IS", nK);
          xVMA = efsInternal("LazyBearVMA", xIS, nVMALength, nK);
          }
          if(getCurrentBarCount() > nVMALength + 4)
          xVMA.getValue(0) > xVMA.getValue(-1) ? setBarFgColor(Color.RGB(0, 255, 0)) :
          xVMA.getValue(0) < xVMA.getValue(-1) ? setBarFgColor(Color.RGB(192, 0, 0)) :
          xVMA.getValue(0) == xVMA.getValue(-1) ? setBarFgColor(Color.white) : setBarFgColor(Color.grey);
          return xVMA.getValue(0);
          }

          var nPdmS = 0;
          var nPdmS_1 = 0;
          var nMdmS = 0;
          var nMdmS_1 = 0;
          var nPdiS = 0;
          var nPdiS_1 = 0;
          var nMdiS = 0;
          var nMdiS_1 = 0;
          var nIS = 0;
          var nIS_1 = 0;
          var nVMA = 0;
          var nVMA_1 = 0;
          var bDoneZeroingGlobals = false;
          var nCountCalc_ISLoads = 0;

          function Calc_IS(n_K){
          if(getCurrentBarCount() < 2) return;
          if(getBarState() == BARSTATE_NEWBAR){
          nPdmS_1 = nPdmS;
          nMdmS_1 = nMdmS;
          nPdiS_1 = nPdiS;
          nMdiS_1 = nMdiS;
          nIS_1 = nIS;
          }
          var nPdm = Math.max((close(0) - close(-1)), 0);
          var nMdm = Math.max((close(-1) - close(0)), 0);
          nPdmS = (1 - n_K) * nPdmS_1 + n_K * nPdm;
          nMdmS = (1 - n_K) * nMdmS_1 + n_K * nMdm;
          var nS = nPdmS + nMdmS;
          var nPdi = nPdmS / nS;
          var nMdi = nMdmS / nS;
          nPdiS = (1 - n_K) * nPdiS_1 + n_K * nPdi;
          nMdiS = (1 - n_K) * nMdiS_1 + n_K * nMdi;
          var nD = Math.abs(nPdiS - nMdiS);
          var nS1 = nPdiS + nMdiS;
          nIS = (1 - n_K) * nIS_1 + n_K * nD / nS1;

          if(!bDoneZeroingGlobals && getCurrentBarIndex() == 0){
          nCountCalc_ISLoads++;
          if(nCountCalc_ISLoads >= 2) bDoneZeroingGlobals = true;
          else{
          nPdmS = 0;
          nMdmS = 0;
          nPdiS = 0;
          nMdiS = 0;
          nIS = 0;
          }
          // Calc_IS is loaded by 2 series objects...first as a parameter to xVMA,
          // and then for the xIS object itself, so the global variables MUST be
          // reset back to zero, otherwise the 2nd run will start with the values
          // that the first run ended at and they will become the values of xIS!!!
          }
          return nIS;
          }

          function LazyBearVMA(xIS, n_VMALength, n_K){
          if(getCurrentBarCount() < n_VMALength + 2) return;
          if(getBarState() == BARSTATE_NEWBAR) nVMA_1 = nVMA;
          var nHHV = highest(n_VMALength, xIS);
          var nLLV = lowest(n_VMALength, xIS);
          var nD1 = nHHV - nLLV;
          var nVi = (xIS.getValue(0) - nLLV) / nD1;
          nVMA = (1 - n_K * nVi) * nVMA_1 + n_K * nVi * close(0);
          return nVMA;
          }

          Comment

          Working...
          X