Announcement

Collapse
No announcement yet.

Reference to previous bar functions values

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

  • Reference to previous bar functions values

    Is it possible to refer to the previous bar value of a study/function/formula the same way as it is available for price values?

    MyVar = close(-1) is a valid statement, but
    MyVar = study.getValue(MAStudy.MA)(-1) or
    MyVar = study.getValue(MAStudy.MA(-1)) is not

    Can you please help? Being able to refer to previous values is mandatory in any meaningful strategy.
    Thank you
    Mihai Buta

  • #2
    Re: Reply to post 'Reference to previous bar unction values'

    ma50=study.getValue(MAStudy.MA,-1);


    ----- Original Message -----
    From: <[email protected]>
    To: <[email protected]>
    Sent: Monday, January 20, 2003 9:11 PM
    Subject: Reply to post 'Reference to previous bar unction values'


    > Hello dloomis,
    >
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >

    Comment


    • #3
      the formatting is here...




      getValue(bartype [, nOffset [, nNumBars, [, Symbol ] ] ] )

      bartype – "Open", "High", "Low", "Close", "Time", "Volume"

      nOffset – offset from the "relative bar". The relative bar is maintained by the formula engine. It is the index of the bar currently being computed.

      nNumBars – Number of bars to return

      Symbol- "Symbol,Interval" to request

      Bars are organized from 0 being the most recent bar to –N where –N is the oldest bar. If 100 bars are available, bar 0 will be the most recent. Bar –99 will be the oldest.

      Samples:

      Returns a single value (the open for the current bar)

      var vValue = getValue("Open")

      var vValue = getValue("Open", 0, 1);

      Returns an array of 10 values from the current bar to nNumBars ago.

      var vValues = getValue("Open", 0, -10)

      Comment


      • #4
        Thank you David.
        Mihai Buta

        Comment


        • #5
          Reference to past values for variables

          Can we refer to past values of variables the same way as for studies?
          What is the syntax?

          For example:
          var myVar = 0;

          if myVar(-1) > 0 & .... {...}

          Is this a valid syntax?
          Thank you.
          Mihai Buta
          Mihai Buta

          Comment


          • #6
            Hello Mihai,

            You can do this with ref(). Please read the following thread.

            Syntax and usage for ref()
            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, but this refers to indicators/studies.
              I want to refer to variables inside my routines.
              Is there a solution?

              Mihai Buta
              Mihai Buta

              Comment


              • #8
                Just save the variables yourself. One easy way to do this if you wish to refer to many past variables is by using arrays.

                If you only want to look one or two variables back, you can just declare three variables and copy from one to the next.

                Look at the MA formula's for examples of the later.

                there is nothing in the engine to do this for you.
                Garth

                Comment


                • #9
                  Sorry to be dim, but I'm still confused over the ref function. I've downloaded the sample ref efs files, which show examples that use a built in function (MAStudy.Simple).

                  How do you use ref to return the value of a custom function x bars ago?

                  The ultimate ideal is to have not only a way to refer to this in a fuction, but also to plot it - so you could for example have a study curve overlaid on price bars and offset it forwards or backwards in time.

                  Comment


                  • #10
                    Re: Reply to post 'Reference to previous bar functions values'

                    Hi Andy,
                    You are not the only one (to be confused).
                    The real solution is to save your previous values as global variables or
                    arrays (like I did and works).
                    Refering to previous values with (...., -1) like it should work, actually
                    does not.
                    Good Luck!
                    Mihai

                    ----- Original Message -----
                    From: <[email protected]>
                    To: <[email protected]>
                    Sent: Monday, January 27, 2003 2:35 AM
                    Subject: Reply to post 'Reference to previous bar functions values'


                    > Hello mbuta,
                    >
                    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    >
                    Mihai Buta

                    Comment


                    • #11
                      Hello andyw,

                      ref() can be used to reference the past values of the indicators that are returned or plotted on your chart ( i.e. return new Array(vValue1, vValue2); ). The values returned to your chart can be produced from a custom or built-in function.

                      Without knowing the specifics of what you are trying to do, let me give you a simple example based on some assumptions. Let's say the formula will plot a single line, which is the midpoint from 10 bars ago. Click here to download CustomFuncRef.efs.

                      CustomFuncRef.efs:
                      PHP Code:
                      function preMain() {
                          
                      setPriceStudy(true)
                          
                      setStudyTitle("Custom Function");
                          
                      setDefaultBarFgColor(Color.blue)
                          
                      setCursorLabelName("Value ");

                      }

                      var 
                      myArray = new Array(10);
                      var 
                      myValue null;

                      function 
                      main() {   
                          var 
                      nBarState getBarState();
                          
                          if (
                      nBarState == BARSTATE_NEWBAR && myValue != null) {
                              
                      myArray.pop();              //removes last array element
                              
                      myArray.unshift(myValue);   //inserts array element to the front of the array 
                          
                      }

                          
                      myValue = ((high() + low()) / 2);
                          if (
                      myValue == null) {
                              return;
                          } 
                          
                          if (
                      myArray[9] != null) {
                              return 
                      myArray[9];
                          } else {
                              return;
                          }

                      In this example, we created a global array to keep track of the past values of myValue, which also needs to be initialized globally. If you want to reference the custom value from more than 10 bars ago, simply increase the size the myArray and change the last if statement to access the array element in question. Keep in mind that arrays are zero based. In our example above, myArray[9] is the 10th array element of myArray, which is the midpoint for bar index of -10.

                      Hope this helps.
                      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


                      • #12
                        ref() only works for referring to previous RETURNED values in your formula. Ie:

                        PHP Code:
                        function main() {
                           
                        blah blah
                         
                           
                        return something;

                        doing ref(-1) will only return the value of 'something' from the previous bar.

                        If you want to SAVE a variable that is not in the returned data for the next iteration, use a global variable, that is, one declared outside any functions:

                        PHP Code:
                        var vSomethingSaved 0;

                        function 
                        preMain() {
                        }

                        function 
                        main() {
                            
                        blah blah
                            
                        // here, vSomethingSaved is reset with the previous value
                            // of vSomethingSaved + 5;
                            
                        vSomethingSaved vSomethingSaved 5;

                            return 
                        somethingElse;

                        You can't use (....-1) on vSomethingSaved, etc.. because vSomethingSaved is a variable, not a function. You can only assign and read stuff to/from variables.

                        Comment


                        • #13
                          Jason, Dion

                          Many thanks - works a treat!

                          A

                          Comment

                          Working...
                          X