Announcement

Collapse
No announcement yet.

average with sorting

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

  • average with sorting

    I would like to calculate an average as follows :
    1. Take the daily Hi-Lo-Range of the last 20 days.
    2. Sort this values ascending in sequence.
    3. Delete the highest and the lowest 5 values starting from the lowest.
    4. Compute from the remaining 10 values an average and stddev.
    5. Plot the average on an 5 minute chart.

    Could somebody help with step 2 and 3?

    Thanks

  • #2
    Augustus:

    The following function will calculate the two values and return them in an array:

    PHP Code:
    function CustomAverageRangenBars ) {
    var 
    x=0;
    var 
    nRemove     null;
    var 
    nMin         null;
    var 
    nMax         null;
    var 
    nPrd         null;
    var 
    aVals         null;
    var 
    nAvg         0;
    var 
    nSumAvg        0;

        
    //smallest value you can pass to this function is 4
        
    if ( nBars<nBars 4;


        
    nRemove Math.floornBars/);
        
    nMin nRemove;
        
    nMax nBars-nRemove;
        
    nPrd nMax-nMin;
        
    aVals = new Array( nBars );
        

        
    //gather the range values over the specified period
        
    while( x<nBars ) {
            
    aVals[x] = ( high(-x)-low(-x) );
            
    x++;
        }
        
        
    //sort the array 
        
    aVals.sort();
        
        
    //calculate the average. We are ignoring 1/4 of the lower values
        //and 1/4 of the higher values
        
    x=nMin;
        while( 
    x<nMax ) {
            
    nAvg += aVals[x];
            
    x++;
        }
        
    nAvg /= nPrd;
        
        
        
    //calculate the standard deviation using the same process
        
    x=nMin;
        while( 
    x<nMax ) {
            
    nSumAvg += Math.pow( ( aVals[x]-nAvg ), );
            
    x++;
        }
        
    nSumAvg /= nPrd;
        
    nStdDev Math.sqrtnSumAvg );
        
        return new Array( 
    nAvgnStdDev );

    You would just call it from within the main() function as needed. Remember that the return value will be an array so you need to treat it as such:


    MyVal = CustomAverageRange( 20 );

    //Average will be in array index 0 and standard deviation will be
    //in array index 1

    theAverage = MyVal[0];
    theStdDev = MyVal[1];

    Chris

    Comment


    • #3
      Hi Chris,

      many thanks for your help.

      As aVals.sort() was not sorting all values sequential, i added the function SortMe from one of your older VP-codes.

      function SortMe( arg1, arg2 ) {
      if (arg1<arg2) {
      return( -1 )
      }
      else {
      return( 1 );
      }
      }

      It now works with aVals.sort(SortMe) .

      Regards
      Last edited by augustus; 10-11-2005, 06:36 PM.

      Comment

      Working...
      X