Announcement

Collapse
No announcement yet.

Referencing the value of a variable on some prior bar.

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

  • Referencing the value of a variable on some prior bar.

    If, for example I have:

    var X=((high(0)+low(0))/2);

    How can I reference the value of X on some previous bar? Could I use something like:

    var Y=(X.getValue(-10));

    Or do I need to create an array of X and reference it somehow through that?

    Thanks for helping a muppet like me. It is appreciated.

  • #2
    Re: Referencing the value of a variable on some prior bar.

    kengro
    It depends on several factors.
    If that variable is a series you can reference its prior values using the X.getValue(-10) method.
    If instead it is not a series but a value [as is the case of your example] then you need to use other methods.
    If it is being returned by the script then you could use the ref() function to retrieve its prior values (for the description and syntax of this function see the link to the corresponding article in the EFS KnowledgeBase).
    If as in your example the variable is calculated based on one or more series then you could just reference the prior values of that or those series eg
    var X = (high(-10)+low(-10))/2
    In other cases you may have to store the values in an array and reference the specific element of the array you are looking for or use a method similar to the one described in this thread.
    Alternatively you could calculate it in a separate function and call that function using the efsInternal() function which will create a series thereby allowing you to use the getValue() method of the series object (see the link to the article in the EFS KnowledgeBase for the description and syntax of the efsInternal() function)
    Alex


    Originally posted by kengro
    If, for example I have:

    var X=((high(0)+low(0))/2);

    How can I reference the value of X on some previous bar? Could I use something like:

    var Y=(X.getValue(-10));

    Or do I need to create an array of X and reference it somehow through that?

    Thanks for helping a muppet like me. It is appreciated.

    Comment


    • #3
      Thankyou Alex

      Thanks for your reply Alex. I don't know where you are in the world but a response at 2:37am Eastern is impressive (if that is where you are). Your help is always appreciated.
      I tried what you suggested but I could not get it to run. The code is intended to be used for calculating the relative volume for a stock for that day. The code I currently use for this purpose is here:

      function preMain() {
      setPriceStudy(true);
      setStudyTitle("RelVol");
      setCursorLabelName("RelVol");
      }
      var bInit = false;
      var xOpen = null;
      var nod = 5;
      var arr = ([0, 0, 0, 0, 0, 0]);
      function main() {
      if(isMonthly() || isWeekly() || isDaily())
      return;
      nTime = getValue( "rawtime", 0 );
      if(getFirstBarIndexOfDay( nTime ) == getCurrentBarIndex()) {
      val1 = arr[0];
      val2 = arr[1];
      val3 = arr[2];
      val4 = arr[3];
      val5 = arr[4];
      currIndex = getFirstBarIndexOfDay( nTime );
      var newArray = ([currIndex, val1, val2, val3, val4, val5]);
      nVal1 = newArray[0];
      nVal2 = newArray[1];
      nVal3 = newArray[2];
      nVal4 = newArray[3];
      nVal5 = newArray[4];
      nVal6 = newArray[5];
      arr = ([nVal1, nVal2, nVal3, nVal4, nVal5, nVal6]);
      }
      if(getCurrentBarIndex() > -1) {
      var addVol = ([0, 0, 0, 0, 0, 0]);
      for(var i = 1; i <= nod; i++) {
      if(arr[i] == 0) {
      }
      }
      numInt = getFirstBarIndexOfDay( nTime ) * -1;
      if(getFirstBarIndexOfDay( nTime ) != arr[0]) {
      var difference = getFirstBarIndexOfDay( nTime ) - arr[0];
      arr[0] = arr[0] + difference;
      arr[1] = arr[1] + difference;
      arr[2] = arr[2] + difference;
      arr[3] = arr[3] + difference;
      arr[4] = arr[4] + difference;
      arr[5] = arr[5] + difference;
      }
      for(var i = 0; i < numInt; i++) {
      addVol[0] = addVol[0] + getValue( "volume", arr[0] + i);
      addVol[1] = addVol[1] + getValue( "volume", arr[1] + i);
      addVol[2] = addVol[2] + getValue( "volume", arr[2] + i);
      addVol[3] = addVol[3] + getValue( "volume", arr[3] + i);
      addVol[4] = addVol[4] + getValue( "volume", arr[4] + i);
      addVol[5] = addVol[5] + getValue( "volume", arr[5] + i);
      }
      var str = addVol[0] + ":" + addVol[1] + ":" + addVol[2] + ":" + addVol[3] + ":" + addVol[4] + ":" + addVol[5];
      var totalAdd = 0;
      for(var i = 1; i <= nod; i++) {
      totalAdd = totalAdd + addVol[i];
      }
      var totalAccum = totalAdd / (nod);
      var endResult = roundVal(100 * addVol[0] / totalAccum);
      drawTextAbsolute( 0, 2, endResult, Color.black, null, Text.CENTER | Text.BOLD | Text.RELATIVETOBOTTOM , "Arial", 10, 4);
      }
      }

      function roundVal(val){
      var dec = 0;
      var result = Math.round(val*Math.pow(10,dec))/Math.pow(10,dec);
      return result;
      }

      The issue with this code is that it requires re-loading after the market is open (annoying since I run 200 charts) and if I try to setComputeOnClose() it returns no result. So, if it is easier to fix this problem, then I will not need the new code which is here:

      function preMain() {
      setStudyTitle("Cumulative Volume2 ");
      setCursorLabelName("CumVol2");
      setPlotType(PLOTTYPE_HISTOGRAM);
      setDefaultBarThickness(1);
      }

      var vVol = 0;
      var vLastVol = 0;
      var TotVol = 0;

      function main() {
      if (getBarState() == BARSTATE_NEWBAR) {
      if (getDay(0) != getDay(-1)) TotVol = 0;
      vLastVol = 0;
      } else {
      vLastVol = vVol;
      }

      vVol = volume();
      TotVol += (vVol - vLastVol);

      }

      Alternatively, in the new code, if I was to grab a value of the variable
      TotVol from 78 bars prior (code runs on 5 min charts), I could just use that. I hope that makes sense. Thanks alot mate. You're a good man.

      Comment


      • #4
        Re: Thankyou Alex

        kengro
        Assuming I understood what you are trying to do [ie compare the current cumulative volume to that of 78 bars back] then just create a new efs and using the efsExternal() function (see the link to the corresponding article in the EFS KnowledgeBase for the description and syntax of this function) call the second script you posted [to which you need to add a return statement for TotVol].
        This will create a series of the calculations returned by that script which will in turn allow you to retrieve its historical values using the getValue() method
        Alex


        Originally posted by kengro
        Thanks for your reply Alex. I don't know where you are in the world but a response at 2:37am Eastern is impressive (if that is where you are). Your help is always appreciated.
        I tried what you suggested but I could not get it to run. The code is intended to be used for calculating the relative volume for a stock for that day. The code I currently use for this purpose is here:

        function preMain() {
        setPriceStudy(true);
        setStudyTitle("RelVol");
        setCursorLabelName("RelVol");
        }
        var bInit = false;
        var xOpen = null;
        var nod = 5;
        var arr = ([0, 0, 0, 0, 0, 0]);
        function main() {
        if(isMonthly() || isWeekly() || isDaily())
        return;
        nTime = getValue( "rawtime", 0 );
        if(getFirstBarIndexOfDay( nTime ) == getCurrentBarIndex()) {
        val1 = arr[0];
        val2 = arr[1];
        val3 = arr[2];
        val4 = arr[3];
        val5 = arr[4];
        currIndex = getFirstBarIndexOfDay( nTime );
        var newArray = ([currIndex, val1, val2, val3, val4, val5]);
        nVal1 = newArray[0];
        nVal2 = newArray[1];
        nVal3 = newArray[2];
        nVal4 = newArray[3];
        nVal5 = newArray[4];
        nVal6 = newArray[5];
        arr = ([nVal1, nVal2, nVal3, nVal4, nVal5, nVal6]);
        }
        if(getCurrentBarIndex() > -1) {
        var addVol = ([0, 0, 0, 0, 0, 0]);
        for(var i = 1; i <= nod; i++) {
        if(arr[i] == 0) {
        }
        }
        numInt = getFirstBarIndexOfDay( nTime ) * -1;
        if(getFirstBarIndexOfDay( nTime ) != arr[0]) {
        var difference = getFirstBarIndexOfDay( nTime ) - arr[0];
        arr[0] = arr[0] + difference;
        arr[1] = arr[1] + difference;
        arr[2] = arr[2] + difference;
        arr[3] = arr[3] + difference;
        arr[4] = arr[4] + difference;
        arr[5] = arr[5] + difference;
        }
        for(var i = 0; i < numInt; i++) {
        addVol[0] = addVol[0] + getValue( "volume", arr[0] + i);
        addVol[1] = addVol[1] + getValue( "volume", arr[1] + i);
        addVol[2] = addVol[2] + getValue( "volume", arr[2] + i);
        addVol[3] = addVol[3] + getValue( "volume", arr[3] + i);
        addVol[4] = addVol[4] + getValue( "volume", arr[4] + i);
        addVol[5] = addVol[5] + getValue( "volume", arr[5] + i);
        }
        var str = addVol[0] + ":" + addVol[1] + ":" + addVol[2] + ":" + addVol[3] + ":" + addVol[4] + ":" + addVol[5];
        var totalAdd = 0;
        for(var i = 1; i <= nod; i++) {
        totalAdd = totalAdd + addVol[i];
        }
        var totalAccum = totalAdd / (nod);
        var endResult = roundVal(100 * addVol[0] / totalAccum);
        drawTextAbsolute( 0, 2, endResult, Color.black, null, Text.CENTER | Text.BOLD | Text.RELATIVETOBOTTOM , "Arial", 10, 4);
        }
        }

        function roundVal(val){
        var dec = 0;
        var result = Math.round(val*Math.pow(10,dec))/Math.pow(10,dec);
        return result;
        }

        The issue with this code is that it requires re-loading after the market is open (annoying since I run 200 charts) and if I try to setComputeOnClose() it returns no result. So, if it is easier to fix this problem, then I will not need the new code which is here:

        function preMain() {
        setStudyTitle("Cumulative Volume2 ");
        setCursorLabelName("CumVol2");
        setPlotType(PLOTTYPE_HISTOGRAM);
        setDefaultBarThickness(1);
        }

        var vVol = 0;
        var vLastVol = 0;
        var TotVol = 0;

        function main() {
        if (getBarState() == BARSTATE_NEWBAR) {
        if (getDay(0) != getDay(-1)) TotVol = 0;
        vLastVol = 0;
        } else {
        vLastVol = vVol;
        }

        vVol = volume();
        TotVol += (vVol - vLastVol);

        }

        Alternatively, in the new code, if I was to grab a value of the variable
        TotVol from 78 bars prior (code runs on 5 min charts), I could just use that. I hope that makes sense. Thanks alot mate. You're a good man.

        Comment

        Working...
        X