Announcement

Collapse
No announcement yet.

how to reference previous bar value of indicator

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

  • how to reference previous bar value of indicator

    Hi,
    is there a simple way to reference the previous bar value of an indicator, or does it differ from indicator to indicator?

    I have tried x(-1) but it doesn't seem to work. May be I am doing something wrong or maybe its not as simple as that???

    This of course from a newbie!
    Sajal

  • #2
    Hello sajaldeyin,

    The function you will need to use to reference the indicator values returned to the chart on previous bars is ref(). This link is from our on-line EFS Help Center & Library. Be sure to visit the link to this resource at the bottom of this post.

    There are a couple of example formulas with detailed notes that will help explain and demonstrate how to use this function. Please visit the threads below. If you have any questions, let me know.

    RefUsage.efs
    RefUsage2.efs
    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


    • #3
      Hi Jason,
      Thanks very much for your response. I did go thru the links for thr ref() but being a starter in efs, I am having little luck. If you or someone can help me on this, I'll appreciate it.
      I am working on the DemandIndex.efs from file sharing and what I am trying to accomplish is:

      Current bar's DMI color = green if previous bar's DMI =0.2

      I know how to write the code for the color part but getting stumped on how to code: "if DMI(-1)=0.2". I tried declaring a variable DMI_1 for the previous DMI, but can't make it work yet.

      Here is the link to the efs code:



      EFS - Misc

      DemandIndex.efs
      Sajal

      Comment


      • #4
        Hello Sajal,

        I have something else you can try for this scenario. Incorporate the following into your code. You were on the right track it sounds like. Make the DMI_1 a global variable. Also, pull DMI out of main() and make it global as well. So on line 49, remove the DMI = 1. Then at the top of main add a routine that checks for the instance of a new bar and set DMI_1 = DMI. Then you will have the two values you need for the comparison to determine the colors you want.

        PHP Code:
        var DMI 1;
        var 
        DMI_1 1;

        function 
        main() {
            if (
        getBarState() == BARSTATE_NEWBAR) {
                
        DMI_1 DMI;
            }
            
            
        // main code
            
            
        if (DMI_1 == 0.2) {
                
        // do your thing
            
        }
            
            return 
        DMI;

        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


        • #5
          sajaldeyin,

          Not sure how this works in the case of non-builtin studies such as the DemandIndex, but for standard built-in studies (MA, RSI, CCI, Stochastic, MACD, etc.), I use this:

          (Using moving average, for example)

          var studyMA = new MAStudy(20, 0, "Close", MAStudy.EXPONENTIAL);

          function main() {

          //define MA for the current bar
          var vMA = studyMA.getValue(MAStudy.MA);
          if (vMA == null)
          return;

          //define MA for the previous bar (-1 bar)
          var vLastMA = studyMA.getValue(MAStudy.MA, -1);
          if (vLastMA == null)
          return;

          //use defined variables to do whatever
          if (vMA > vLastMA) {
          setBarFgColor(Color.green);
          }
          }

          Jason (or anyone else that knows for sure), can this approach be used with non-builtin studies such as the DemandIndex? If so, what is the code to define the variables? (e.g. in the case of the DemandIndex, using vDMI and vLastDMI.)

          Comment


          • #6
            Hi Jason,
            Yes it worked following your instructions. Thank you very much for taking the time and effort to help me out. I really appreciate it.
            Take care,

            ps, BTW I have been trying to post this reply for the last several hours and I kept getting a next page full of err lines stating, "....cannot access mailserver...."
            Hope it works now, since I see Lancer's post.

            Hi Lancer,
            Thanks for your response. I'll let Jason or other more knowledgeable person respond to your question, as I myself have a long way to go with efs.
            Take care
            Sajal

            Comment


            • #7
              Hello Lancer,

              Referencing the return values of a custom indicator from previous bar's can be done 2 ways.

              1. ref(nIndex) was designed for this specific purpose. It gives us a quick way to reference the historical return values by passing it an index value similar to how we can pass an index to the getValue() function like in your example.

              var vLastMA = studyMA.getValue(MAStudy.MA, -1);

              ref(-x) will return the array that was returned on bar -x from the return statement in main. For a more detailed code example, visit the links to refUsage.efs and refUsage2.efs earlier in this thread.

              2. The other common, but more advanced method, is to code a routine that uses a combination of a user defined input and a global array. Here's the basic idea and a code example. Create a user input (nRef), a global array (aMyArray) and a global variable (vReturn). nRef will be a user-defined input that allows the user to specify the maximum number of previous return values the formula needs to reference. aMyArray will be our global array that stores only the most recent X values of our custom indicator (vReturn) determined by nRef. Within main, we will need to two things. Update aMyArray so that its index values match the bar index values whenever we get a new bar in the chart. aMyArray[0] will always have the current bars return value. aMyArray[1] will always have bar -1's return value. aMyArray[2] stores bar -2 etc. The only difference is the sign. Here's the code example, which returns the midpoint of each bar to the chart. Then using the values from aMyArray, the formula will draw a yellow circle on the midpoint value that is at bar -5.

              PHP Code:
              function preMain() {
                  
              setPriceStudy(true);
                  
              setStudyTitle("test");
                  
              setCursorLabelName("vReturn");
                  
              setDefaultBarFgColor(Color.red);
              }


              var 
              aMyArray null;
              var 
              vReturn null;

              function 
              main(nRef) {
                  var 
              nState getBarState();

                  
              // make sure nRef is a positive whole number
                  
              if (nRef != nullnRef Math.abs(Math.round(nRef));
                  if (
              nRef == nullnRef 5;    
                  
                  if (
              aMyArray == nullaMyArray = new Array(nRef+1);
                  
                  if (
              nState == BARSTATE_NEWBAR && vReturn != null) {
                      
              // remove the last element of the array
                      
              aMyArray.pop(); 
                      
              // insert new element to the front of the array at [0]
                      
              aMyArray.unshift(vReturn);
                  }
                  
                  
              // calculate custom indicator
                  
              vReturn = (high() + low()) / 2;
                  
              // immediatly store the current value of vReturn in element [0]
                  
              aMyArray[0] = vReturn;
                  

                  
              // this section will use the previous vReturn values stored in aMyArray
                  // first, make sure the array has been populated
                  // if the last element [nRef] of aMyArray is null, do nothing
                  
              if (aMyArray[nRef] != null) {
                      
              // the array is complete
                      // we can use nRef here because arrays are zero based
                      // when aMyArray was initialized we used nRef+1 for a size of 6
                      // aMyArray[5] is the 6th and last element if nRef = 5 above
                      
              drawShapeRelative(-nRefaMyArray[nRef], Shape.CIRCLEnull,
                          
              Color.yellownull"myCircle");
                  }
                  

                  return 
              vReturn;

              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

              Working...
              X