Announcement

Collapse
No announcement yet.

returning array largest value

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

  • returning array largest value

    Hi,
    Could someone please show me how to do these two things with the code below?
    1. Return the largest value in the array 'aArray' and,
    2. Print this value to the formula output window.
    Thanks kindly, please excuse my lack of knowledge.

    function preMain() {
    setStudyTitle("1");
    setShowCursorLabel(true);
    setShowTitleParameters(true);
    setDefaultBarFgColor(Color.black);
    setCursorLabelName("Run%");
    setPlotType(PLOTTYPE_FLATLINES);
    }

    var aArray = new Array();

    function main(){

    aArray = (((high(0)-open(0))/close(-1))*100);

    return aArray;

  • #2
    I have not used these but there are two functions that may do what you want.

    highest()
    hhv()

    There are companion functions
    lowest()
    llv()

    Comment


    • #3
      Thanks jgr for your speedy reply. I tried to use highest and hhv, but I couldn't make it work. I have re-written the code but I still need it to return the highest value in the array xRun. Anyway, thanks for your efforts, hopefully someone else can help me. I am embarrassed about the length of time I have spent on it. Cheers.

      function main(){

      xHigh = high();
      xOpen = open();
      xClose = close()
      xPrevClose = xClose.getValue(-1);
      xRunArray = new Array;
      xRunArray = xRun;

      xRun = (((xHigh - xOpen)/xPrevClose)*100);

      return ???;
      }

      Comment


      • #4
        Re: returning array largest value

        Well, from the looks of your code, you're not deploying the array properly. Here, let me try to help...

        First, you want to use the BARSTATE to handle the operations of the array. You want to PUSH a new value into the array for every new bar. If it's not a new bar, then you want to update the last array item.

        For finding the max value, I normally create my own functions to handle whatever I need. So I used my normal routine to handle your needs.

        Hope this helps.

        PHP Code:


        function preMain() {
            
        setStudyTitle("1");
            
        setShowCursorLabel(true);
            
        setShowTitleParameters(true);
            
        setDefaultBarFgColor(Color.black);
            
        setCursorLabelName("Run%");
            
        setPlotType(PLOTTYPE_FLATLINES);
           }
            
        var 
        aArray = new Array();  //  create your array
        var nBarCounter 0;  //  bar counter

            
        function main(){

          var 
        nState getBarState();
          if ( (
        nState == BARSTATE_NEWBAR) ) {
            
        nBarCounter++;
            
        aArray.push( ((high(0)-open(0))/close(-1))*100);
          }
          if (
        aArray.length 0) {  // there are items in the array, so update last
             
        aArray[(aArray.length-1)] = (((high(0)-open(0))/close(-1))*100);

          }
         
          if (
        getCurrentBarIndex() == 0) {  // efs running on last bar on chart

            
        debugPrintln("Largest Item In Array : "+fFindMaxaArray());
          }

         return 
        aArray[(aArray.length-1)] ;
        }


        function 
        fFindMaxaArray() {
         var 
        aArray.length-1;
         var 
        aamaxvalue 0;
          if (
        aArray.length 0) {
           while (
        >= 0) {
              
        aamaxvalue Math.max(aamaxvalueaArray[x]);
              
        x--;
           }
          }
          return 
        aamaxvalue;

        Originally posted by kengro
        Hi,
        Could someone please show me how to do these two things with the code below?
        1. Return the largest value in the array 'aArray' and,
        2. Print this value to the formula output window.
        Thanks kindly, please excuse my lack of knowledge.

        function preMain() {
        setStudyTitle("1");
        setShowCursorLabel(true);
        setShowTitleParameters(true);
        setDefaultBarFgColor(Color.black);
        setCursorLabelName("Run%");
        setPlotType(PLOTTYPE_FLATLINES);
        }

        var aArray = new Array();

        function main(){

        aArray = (((high(0)-open(0))/close(-1))*100);

        return aArray;
        Last edited by Doji3333; 07-06-2009, 07:54 AM.
        Brad Matheny
        eSignal Solution Provider since 2000

        Comment


        • #5
          I am so amateur. Anyway, thanks for your help, it really is appreciated. Anyway, I tried to run what you gave me and got no return on the chart window and got 'Largest Item In Array : 0' in the formula window. Thanks again mate.

          Comment


          • #6
            yeah, there was an error in my code. The aArray.push line of code was incorrect. I've edited the code example below to resolve the problem. Please give it a try now.
            Brad Matheny
            eSignal Solution Provider since 2000

            Comment


            • #7
              Ahhh... Nice. Now I can relax. It worked nicely. Thanks Brad, you're a good man.

              Comment

              Working...
              X