Announcement

Collapse
No announcement yet.

MA max and min

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

  • MA max and min

    Can some one show me how to calculate the max and min of the most recent 25 values of a moving average?

  • #2
    max min of a ma

    this has some ideas, but looks like a cpu hog...

    /************************************************** **************************************************
    Copyright © eSignal, a division of Interactive Data Corporation. 2002. All rights reserved.
    This sample eSignal Formula Script (EFS) may be modified and saved under a new
    filename; however, eSignal is no longer responsible for the functionality once modified.
    eSignal reserves the right to modify and overwrite this EFS file with each new release.
    ************************************************** ************************************************** */
    function preMain() {
    setPriceStudy(true);
    }

    function main(nInputLength) {
    if(nInputLength == null)
    nInputLength = 20;

    var nLength = nInputLength;
    var i;
    var hh = 0;

    var vValues = low(-1, -nLength);
    if(vValues == null)
    return null;

    for(i = 0; i < nLength; i++) {
    if(i == 0) {
    hh = vValues[i];
    } else {
    hh = Math.min(hh, vValues[i]);
    }
    }
    return hh;
    }

    Comment


    • #3
      Hi dloomis,

      Try this:

      Code:
      var study = new MAStudy(10, 0, "Close", MAStudy.SIMPLE);
      
      function preMain() {
          setPriceStudy(true);
          setCursorLabelName(" Max MA ",0)
          setCursorLabelName(" Min MA ",1)    
      }
      
      var vMAvalues = new Array();
      var cntr = 0;
      
      function main(nPeriods) {
          if (nPeriods == null) {
              nPeriods = 25;
          }
          
          var maxMA = 0;
          var minMA = 0;
          
          var vMA = study.getValue(MAStudy.MA);
          if (vMA == null) {
              return;
          }
          
          if (getBarState() == BARSTATE_NEWBAR) {
              if (cntr < nPeriods) {
                  vMAvalues[cntr] = vMA;
                  cntr += 1
                  return;
              } else {
                  vMAvalues.pop();
                  vMAvalues.unshift(vMA);
                  for(i = 0; i < nPeriods; i++) {
                      if(i == 0) {
                          maxMA = vMAvalues[i];
                          minMA = vMAvalues[i];
                      } else {
                          maxMA = Math.max(maxMA, vMAvalues[i]);
                          minMA = Math.min(minMA, vMAvalues[i]);
                      }
                  }
                  return new Array(maxMA, minMA);
              }
          }
      }
      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


      • #4
        It all depends on what he is after...

        Do you want the max of te highs and the mon of the lows, or as Jason has done it the max and min of the close? The later will have very little difference between the two results...
        Garth

        Comment


        • #5
          c vs h vs l

          I usually use ma's based on the closing prices of the bars, thx Jason. I am currently using this...


          function preMain()
          {
          setPlotType(PLOTTYPE_HISTOGRAM,0);
          }

          function main(nInputLength) {
          if(nInputLength == null)
          nInputLength = 50;

          var nLength = nInputLength;
          var i;
          var hh = 0;
          var ll = 0;

          var vValues = low(-1, -nLength);
          if(vValues == null)
          return null;

          for(i = 0; i < nLength; i++) {
          if(i == 0) {
          hh = vValues[i];
          ll = vValues[i];
          } else {
          hh = Math.min(hh, vValues[i]);
          ll = Math.max(ll, vValues[i]);
          }
          }
          if(Math.abs(hh-ll)>7)
          return 5;
          }

          Comment

          Working...
          X