Announcement

Collapse
No announcement yet.

Find the highest, the second highest and third highest

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

  • Find the highest, the second highest and third highest

    Hi All,

    Is there any function or code that can determine the highest , second highest and third highest price over a specified numbers of bars ?

    Any help is appreciated.

    Thank you.

    Derek

  • #2
    Derek
    Save the values that you want to rank in an array and then sort the array (following image illustrates one way to do this)



    Also search the web for javascript sort and you will find many other examples on how to accomplish this
    Alex


    Originally posted by derekfan View Post
    Hi All,

    Is there any function or code that can determine the highest , second highest and third highest price over a specified numbers of bars ?

    Any help is appreciated.

    Thank you.

    Derek

    Comment


    • #3
      Just modified existing code to sort the aH array. Note: it just outputs the 3 highest values of the last 20 bars (user defined default). You can modify the script to do whatever you need with those values.

      Try:
      PHP Code:
      /*****************************************************************
      http://share.esignal.com/groupcontents.jsp?folder=&groupid=165
      Provided By : eSignal. (c) Copyright 2003
      Study:          x Bars Highest High and Lowest Low

      Notes:
      *This study draws two lines marking the Highest High and Lowest Low
       within the most recent X number of bars from the current bar.
      *Enter the number of bars to be included for the HH and LL via
       the "Edit Studies" option.
       
      Formula Parameters:     Defaults:
      Number of Bars          20
      Line Thickness          1
      High Bar Color          blue
      Low Bar Color           blue
      *****************************************************************/
      //debugClear();


      function preMain() {
          
      setPriceStudy(true);
          
      //setStudyTitle("x Bars HH LL ");
          
      setShowCursorLabel(false);
          
      setShowTitleParameters(false);
          
          var 
      fp1 = new FunctionParameter("xBars"FunctionParameter.NUMBER);
          
      fp1.setName("Number of Bars");
          
      fp1.setLowerLimit(1);
          
      fp1.setDefault(20);

          var 
      fp2 = new FunctionParameter("nThickness"FunctionParameter.NUMBER);
          
      fp2.setName("Line Thickness");
          
      fp2.setLowerLimit(1);
          
      fp2.setDefault(1);
          
          var 
      fp3 = new FunctionParameter("cH"FunctionParameter.COLOR);
          
      fp3.setName("High Bar Color");
          
      fp3.setDefault(Color.blue);

          var 
      fp4 = new FunctionParameter("cL"FunctionParameter.COLOR);
          
      fp4.setName("Low Bar Color");
          
      fp4.setDefault(Color.blue);
      }

      var 
      nBars 20;
      var 
      vHigh null;
      var 
      vLow null;
      var 
      aH null;
      var 
      aL null;
      var 
      bEdit true;

      function 
      main(xBarsnThicknesscHcL) {
          var 
      nIndex getCurrentBarIndex();
          var 
      nState getBarState();
          
          if (
      xBars != nullnBars Math.round(xBars);
          if (
      nIndex < -nBars+1) return;

          if (
      bEdit == true) {
              
      aH null;
              
      aL null;
              
      vHigh null;
              
      vLow null;
              
      bEdit false;
          }
          
          if (
      aH == null || aL == null) {
              
      aH = new Array(nBars);
              
      aL = new Array(nBars);
          }
          
          if (
      nState == BARSTATE_NEWBAR && vHigh != null && vLow != null) {
              
      aH.pop();
              
      aH.unshift(vHigh);
              
      aL.pop();
              
      aL.unshift(vLow);
          }
          
          var 
      high();
          
      aH[0] = h;
          var 
      low();
          
      aL[0] = l;
          
          if (
      vHigh == nullvHigh h;
          if (
      vLow == nullvLow l;
          
          if (
      nIndex == && aH[nBars-1] != null && aL[nBars-1] != null) {
              var 
      0;
              for (
      0nBars; ++i) {
                  
      vHigh Math.max(vHighaH[i]);
                  
      vLow Math.min(vLowaL[i]);
              }
              
      drawLineRelative(-nBars+1vHigh0vHighPS_SOLIDnThicknesscH"high");
              
      drawLineRelative(-nBars+1vLow0vLowPS_SOLIDnThicknesscL"low");
          }
          
      //ADDED CODE START
          
      if(isLastBarOnChart()){
              
      aH.sort(function(ab) {
                  return 
      b;
              });
              
      debugPrintln("103: "+aH[aH.length-1]+"\t"+aH[aH.length-2]+"\t"+aH[aH.length-3]);
          }
          
      //ADDED CODE ENDS
          
      return;

      Wayne

      PS: just saw reply by ACM but since I coded the sort already, I'll leave the post.
      Last edited by waynecd; 08-07-2014, 06:18 AM.

      Comment


      • #4
        Derek
        FWIW the example provided by waynecd will actually return incorrect results as new bars are added to the chart because once the array is sorted the highest value is removed on each subsequent new bar (and reversing the sort order simply creates the opposite problem)
        To correct this you could populate a new array using the values from the existing one and sort its values so as not to affect the original array (see illustration enclosed below). While at it you might as well reverse the sort as I show in my prior post as it is more intuitive/easier IMHO to look for elements [0],[1] etc rather than for [array.length-1], [array.length-2], etc.
        Alternatively you could just use the example I provided in the first place [or something more along those lines].
        Alex

        Comment

        Working...
        X