Announcement

Collapse
No announcement yet.

Array for CMA

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

  • Array for CMA

    I use several centered moving averages for trending purposes based on canned efs programs. Is there a way to compute using the last several values from the cma in a subsequent computation. For example if I want to use the current and prior 4 values (1,2,3,4,5) in a calculation and when a new tick occurs, the computation would be using (2,3,4,5,6) where 6 is the new cma value from the latest tick?

    Best Regards,

    Alan

  • #2
    Hello Alan,

    I provided some code examples on how to accomplish something similar to what you are asking in this post. Take a look at item 2. The pop/unshift array methods are what you need to do to store your indicator values. Then you will be able to reference the prior 5 values of your ma through a global array.
    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
      Array

      Jason,

      Thank you for the example. It was very helpful and runs great!

      In the example provided, "return vReturn" plots all array data elements on the chart as a line. The array has elements (0,1,2,3,4,etc) with 0 being the first element plotted on the most current bar. How would I modify the code so only elements (3,4,5,6,etc) plot as a line? When a new 0 is computed the old element 2 becomes element 3 and would be plotted on the chart. The line would always end on the 4th bar (element 3).

      Your assistance is greatly appreciated.

      Best Regards,

      Alan

      Comment


      • #4
        Hello Alan,

        It doesn't appear to me that you are using the array properly by returning it in the return statement if it contains values meant for a single line. Unless you are plotting the same line several times with a negative offsets essentially. If that's correct then all you need to do in your return statement is this:

        return new Array(vReturn[3], vReturn[4], vReturn[5], vReturn[6]);

        If you have several lines plotting on the chart that are based on unique data sets then you need a separate array for each line.

        arrayLine1 = new Array(7);
        arrayLine2 = new Array(7);
        arrayLine3 = new Array(7);

        You will update each array at new bar with the pop/unshift routine. Then in your return statement, return the desired array element from each array (or plotted line). Your return statement would look something like this:

        return new Array(arrayLine1[3], arrayLine2[4], arrayLine3[5]);
        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
          Array

          Jason,

          Thank you for the suggestions.

          The following code is the original reference you suggested. When the "return vReturn" line is used, a yellow circle and a single line are plotted. When I convert the return to limit the line to only 3 values, nothing plots. Any suggestions?

          Your assistance is greatly appreciated.

          Best regards,

          Alan



          /************************************************** *******
          Array of custom program
          ************************************************** ********/

          var aMyArray = null;
          var vReturn = null;

          function preMain() {
          setPriceStudy(true);
          setStudyTitle("Array1a");
          setCursorLabelName("vReturn");
          setDefaultBarFgColor(Color.red);
          }


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

          // make sure nRef is a positive whole number
          if (nRef != null) nRef = Math.abs(Math.round(nRef));
          if (nRef == null) nRef = 5;

          if (aMyArray == null) aMyArray = 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(-nRef, aMyArray[nRef], Shape.CIRCLE, null,
          Color.yellow, null, "myCircle");
          }


          return new Array (vReturn[3], vReturn[4], vReturn[5]);
          }

          Comment


          • #6
            Hello Alan,

            vReturn is not an Array in your code example, which is what I assumed. Change you return statement to:

            return new Array (aMyArray[3], aMyArray[4], aMyArray[5]);
            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
              Array

              Jason,

              I made the chage you suggested and now I have three lines which are ecnd centered rather than 1 which ends on the 4th bar. The attached document has an image of the chart.

              Your help is appreciated.

              Best Regards,

              Alan



              /************************************************** *******
              Array of custom program
              ************************************************** ********/

              var aMyArray = null;
              var vReturn = null;

              function preMain() {
              setPriceStudy(true);
              setStudyTitle("Array1a");
              setCursorLabelName("vReturn");
              setDefaultBarFgColor(Color.red);
              }


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

              // make sure nRef is a positive whole number
              if (nRef != null) nRef = Math.abs(Math.round(nRef));
              if (nRef == null) nRef = 5;

              if (aMyArray == null) aMyArray = 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(-nRef, aMyArray[nRef], Shape.CIRCLE, null,
              Color.yellow, null, "myCircle");
              }


              return new Array (aMyArray[3], aMyArray[4], aMyArray[5]);


              }
              Attached Files

              Comment


              • #8
                Hello Alan,

                I'm not sure what you are trying to accomplish. What do you mean by "ecnd centered?"
                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


                • #9
                  Array

                  Jason,

                  Sorry for the typo.

                  As an example, an end centered moving average is plotted on the last bar on the right side of the chart. A centered moving average of 5 bars is plotted on the third bar from the right side of the chart. The values are the same, they are just plotted in different locations.

                  Using the concept of changing the plotting location of the centered moving average as the example, I want to take the aMyArray as calculated in the sample code, do not plot array elements 0, 1, 2 and plot a line using array elements 3, 4, 5, ..... on a user specified set back from the right side of the chart like nRef in the sample code sets the circle back from the right side.

                  An alternative thought would be if I used the sample code and drew more circles on the chart by adding more draw commands and changing the nRef values in each draw command.

                  Your assistance is greatly appreciated.

                  Best regards,

                  Alan

                  Comment


                  • #10
                    Hello Alan,

                    I understand now, I think. The only way to plot a value on a bar other than the current bar being process is by using the setBar() function. In your return statement you need to return null values instead of the actual values so that nothing is plotted on the chart for the current bar being processed. This creates the container for the values you want to update later using the setBar() function. You return statement needs to look like this:

                    return new Array(null, null, null)

                    The values you will use in the setBar() function will be the current value of the cma on bar 0, which will then be plotted back on the desired bar rather than bar 0.
                    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


                    • #11
                      Array

                      Jason,

                      Thank you for the suggestion. I will work on it over the weekend.

                      My intent is the use this study without plotting the actual o/h/l/c data. The calculation will be an oscillator moving above and below zero. Will your suggestion plot a line much like a moving average where I can specify thickness, color, etc.?

                      Thank you for your assistance.

                      Best Regards,

                      Alan

                      Comment


                      • #12
                        Hello Allan,

                        Yep, you'll still do all the preMain stuff for color, thickness, plot type etc. as you normally would.
                        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


                        • #13
                          Array

                          Jason,

                          That is excellent. I truly appreciate the help.

                          Have a great weekend!

                          Best Regards,

                          Alan

                          Comment

                          Working...
                          X