Firetron’s Median

2 154 Views 1 Comment 5 months ago
  • Finds the middle value of a table of values. If there is an even number of elements, takes the average of the 2 middle values.

    Custom Command Dependencies:
    None

    Test Code:

    if not Load('done', false) then
    
      local table = Grab(ClosePrices(), 0, 9)
    
      Log(' ')
      Log(table)
      Log('Unsorted Table:')
      Log(' ')
    
      Log(ArraySort(table))
      Log('Sorted Table:')
      Log(' ')
    
      local median = CC_Median(table)
    
      Log(median)
      Log('Median Value:')
      Log(' ')
    
      Save('done', true)
    
    end
    • This topic was modified 4 months, 2 weeks ago by Firetron. Reason: Used ArraySort instead of CC_MergeSort
    • This topic was modified 3 months, 1 week ago by Firetron. Reason: Added param isSorted
    • This topic was modified 3 months, 1 week ago by Firetron. Reason: Made it work for ListNumber as well as Table
    • This topic was modified 3 months, 1 week ago by Firetron. Reason: Changed test code
    HaasScript Code
    Sign in or Register to view
    • #2145
      Firetron
      Basic
      Up
      0
      Down

      Now works for ListNumber input as well as Table input.
      Now has an isSorted param, which you can set to true if you already sorted your list, so the command won’t sort it again, redundantly.

Login or Register to Comment

Unlock your crypto trading potential

Create a free account and enjoy everything we have to offer.

Join for Free