Announcement

Collapse
No announcement yet.

Previous bar's values

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

  • Previous bar's values

    Received via PM

    I would like to know esignal formula/ function call which gives previous bar information (i.e. High, Low, close) and MA based on previous bar. I would like to use this information for a MA crossover formula

    As far as the OHLCVOi values are concerned you would use for example close(-1) to get the prior bar's close value or high(-2) to get tyhe high of two periods ago (the current bar is always indexed to 0).
    With regards to indicators it will depend on how they are coded. If for example you were using the builtinMA.efs that is availble in the Builtin subfolder of Formulas then you would write
    study.getValue(MAStudy.MA,-1)
    to retrieve the prior bar's MA value.
    Hope this helps
    Alex

  • #2
    true for all Builtin studies?

    Is this:

    study.getValue(MAStudy.MA,-1)

    true for any builtin study, like MACD, RSI, BB and such?

    I am having the same problem now.

    Regards,

    Thomas.

    Comment


    • #3
      Thomas
      If I understand your question correctly then yes that syntax applies to all the builtins.
      Alex

      Comment


      • #4
        Let me add a bit to what Alex said. In some cases, asking a builtin for its value one bar ago doesn't make a lot of sense (some of the GET built in's for example - there may be others as well). In these cases using that syntax may not get the results you are trying to achieve. But for all cases where values are produced on a bar by bar basis, then the syntax will work.

        Garth
        Garth

        Comment


        • #5
          For non-builtin (custom) studies, what is the syntax?

          For example, for the custom study "C":

          var C = (ySum - (b * xSum)) / nLength;

          if (C > C(-1)) {
          setBarFgColor(Color.green, 0);
          }

          To return the value of "C" for the previous bar (represented above as "C(-1)"), what should be used?

          Comment


          • #6
            Lancer
            There are various ways of doing it.
            If you only want two or three values back then probably the easiest is to declare C, C1 and C2 as global variables (ie outside of main()) and then first thing in main() write
            if(getBarState()==BARSTATE_NEWBAR){
            C2=C1;
            C1=C;
            }

            At that point C1 is the value of C one bar back and C2 is the value of C two bars back.
            Other ways are to use the ref() function or to create an Array.
            Alex

            Comment


            • #7
              Hmm. Attached is an EFS file. See line 44 and 47 for the code example I used in my prior post.

              To return the value of "a" for the previous bar (represented as "a(-1)" in the formula), what is needed?
              Attached Files

              Comment


              • #8
                Lancer
                The attached revision will do what you want
                Alex

                Attached Files

                Comment


                • #9
                  Thanks Alex. To summarize for future readers of this thread, to return a non-builtin (custom) study value for the prior bar:

                  1. Declare global variables outside of function main() (before or after premain), e.g.:
                  var a = null; //the current bar (modify per your variable name)
                  var a_1 = null; //the previous bar (modify per your variable name)

                  2. First thing in function main(), include:
                  if(getBarState()==BARSTATE_NEWBAR){
                  a_1 = a; //modify per your variable names
                  }

                  3. Use variables in your code, for example:
                  if (a > a_1) {
                  setBarFgColor(Color.green, 0);
                  }

                  Comment


                  • #10
                    This question is a followon.

                    What if I wish to reference the value of a custom variable some number of bars back larger than one or two, say 10 bars. The below is a simple sample, that does not work. It assumes that I want to use the value of a variable 10 bars back to calculate a new variable. (I left all the premain stuff out). How would one do this?

                    Thanks.
                    bigtee



                    function main() {
                    //LLV of close last two periods
                    var vLLVc = 0;
                    if (close(0) <= close(-1)) {
                    vLLVc = close(0);
                    } else {
                    vLLVc = close(-1);
                    }

                    var vtd1 = close - vLLVc[10];

                    return vtd1;
                    }

                    Comment


                    • #11
                      bigtee
                      Enclosed below is how I would do it
                      Alex

                      PHP Code:
                      function preMain(){
                      setPriceStudy(false);
                      }

                      var 
                      aValue null
                      var 
                      vLLVc null;   

                      function 
                      main(Length) {

                      if(
                      Length==null)Length=10;

                          if(
                      getBarState()==BARSTATE_NEWBAR){
                              
                      vLLVc=close(-1);
                          }
                              
                          
                      vLLVc Math.min(vLLVc,close(0));
                              
                          if (
                      aValue == null) {
                              
                      aValue = new Array(Length);
                          }
                          
                          if (
                      getBarState() == BARSTATE_NEWBAR) {
                              
                      aValue.pop();  
                              
                      aValue.unshift(vLLVc); 
                          } else {
                              
                      aValue[0] = vLLVc;
                          }

                          var 
                      vtd1 =  close(0) - aValue[Length-1];

                      return 
                      vtd1;

                      Comment


                      • #12
                        Alex, thanks, but it doesn't work. The LLV part does but the array part does not. As a result there is no plot. Any ideas?

                        bigtee

                        Comment


                        • #13
                          bigtee
                          As far as I can see it is working here (see image)
                          Alex

                          Comment


                          • #14
                            Alex, you are right. I have no idea why mine didn't work so I erased it, started over, and it now works.

                            Thank you.

                            By the way the techniques in this thread are great. Someone should archive them some where as examples so they can be easily found. Referencing a past value of a custom variable is used very often in indicator development.

                            bigtee

                            Comment


                            • #15
                              bigtee
                              Glad to hear it is working.
                              FWIW I am in the midst of compiling a brief write up on retrieving historical bars of custom variables that will be posted in the EFS Reference Guide forum
                              Alex

                              Comment

                              Working...
                              X