Announcement

Collapse
No announcement yet.

recurrsive formula value

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

  • recurrsive formula value

    Hi,

    As I'm trying to write my own RSI formula, it requires the previous RSI values as part of the calculation, for example, mathematically

    nRSI = A * nRSI(-1) + B

    In EFS, who would I do that? This requires recurrsive capability. Not sure how I can do it in a neat fashion. One way that I can think of is to use a global array to keep track of it. I'm wondering if there's a better way of doing it.

    William

  • #2
    I am not sure where you are going with this, but here is an example of a recursive calculation that works quite well when calculating factorial.

    PHP Code:
    function factorial (int n)
    {
      if (
    == 0)
        return 
    1;
      else
        return (
    factorial (n-1));

    An array is another way to perform the calculation as well. You would have to get more specific for any help there. Hope this helps some.

    Comment


    • #3
      Sorry for not being clear in my post. What I meant was the following.

      PHP Code:
      //function myRSI.efs

      function main(nPricenLength) {

      var 
      nRSI 0;

      ...

      if (
      getCurrentBarCount()==nLength)
      {
         ...
         
      nRSI C;
         ...
      }
      else if (
      getCurrentBarCount()>nLength)
      {
         ...
         
      //nRSI(-1) means the value of variable nRSI at the previous bar
         
      nRSI nRSI(-1) + B
         ...
      }


      Comment


      • #4
        Steve,

        If I were to use a global array, I understand that I would have to declare it outside preMain(). Then the number of elements in the array would increase for every new bar. Do I simply increase the index of the array and assign a new value to that index?

        PHP Code:
        //declare outside preMain
        var arrayRSI = new Array;
        var 
        RSIindex=0
        ...
        //inside main()
        i=RSIindex++;
        arrayRSI[i] = arrayRSI[i-1] + B

        Or is there a cleaner way to do it?

        William

        Comment


        • #5
          William,

          Originally posted by wwong112
          Steve,

          If I were to use a global array, I understand that I would have to declare it outside preMain().
          and do not forget, outside main() as well...


          Regarding arrays, please take a look at my Functions fileshare, link below, and see if any of the efs's in the array folder will help out.

          Comment


          • #6
            Steve,

            Those samples in the Array folders do not answer my previous question:

            PHP Code:
            //declare outside preMain() and main()
            var arrayRSI = new Array;
            var 
            RSIindex=0;

            ...

            //inside main()
            i=RSIindex++;
            arrayRSI[i] = arrayRSI[i-1] + B
            can I simply increase the index to the global array arrayRSI like in the above?

            William

            Comment


            • #7
              William,

              Originally posted by wwong112

              Then the number of elements in the array would increase for every new bar. Do I simply increase the index of the array and assign a new value to that index?
              No, you use the pop() and shift() array methods to keep your array at the same size. Those methods are shown in my fileshare. Since the arrays will always be the same size, you can use the same relative index values for each subsequent calculation.

              If you were to venture beyond that folder and look elsewhere, I am sure you will see many examples of how to index through arrays.


              Originally posted by wwong112
              Steve,

              Those samples in the Array folders do not answer my previous question:

              PHP Code:
              //declare outside preMain() and main()
              var arrayRSI = new Array;
              var 
              RSIindex=0;

              ...

              //inside main()
              i=RSIindex++;
              arrayRSI[i] = arrayRSI[i-1] + B
              can I simply increase the index to the global array arrayRSI like in the above?

              William
              Sure, that will work.

              You can do pretty much whatever you set your mind to using arrays. You can even make the arrays work like your example.

              Your example is backwards from the way you will see them typically used. They are zero based, and the numbers in the square brackets increase in the positive direction, with the '0' index being the most recent and the '1' index being last, the '2' index being before that, etc. This can be done using the array methods I mentioned earlier. Pease note that this is different than the numbers you use to reference the eSignal built-in function values. These are also zero based, but they 'increase' in the negative direction.

              Perhaps if you were able to post an efs that functioned, as the posting guidelines recommend, I could help you out better. Presently, you have me grasping at straws trying to answer questions I believe are quite vague.

              Finally, there are many links out there that show you how arrays work. This is JavaScript 101. Please take some time to look through the the many resources out there that discuss how to use arrays. You can try searching 'JavaSript array' in Google or in the Knowledgebase. In my opinion, the Javascript reference contained in the Knowledgebase is one of the better ones out there

              Comment


              • #8
                This has been helpful. I'm now able to do what I wanted to do using the Array methods. Thank you very much, Steve.

                William

                Comment


                • #9
                  You are most welcome William.

                  Comment

                  Working...
                  X