Firetron's FractionMiddle

stable
By Firetron in Miscellaneous Published April 2021 👁 1,128 views 💬 0 comments

Description

Finds the middle value of a fractional portion of a table of values. Custom Command Dependencies: Firetron’s FractionGrab Firetron’s Median Test code:
if not Load('done', false) then

  local list = Grab(ClosePrices(), 0, 12)

  Log(' ')
  Log(list)
  Log('Unsorted List:')
  Log(' ')

  Log(ArraySort(list))
  Log('Sorted List:')
  Log(' ')

  local median = CC_FractionMiddle('median', list, 3, 4)

  Log(median)
  Log('Median Value of 3rd portion of 4:')
  Log(' ')

  local average = CC_FractionMiddle('average', list, 3, 3)

  Log(average)
  Log('Average Value of 3rd portion of 3:')
  Log(' ')

  median = CC_FractionMiddle('median', list, 1, 3)

  Log(median)
  Log('Median Value of 1st portion of 3:')
  Log(' ')

  Save('done', true)

end
HaasScript
--  ============================================================================
--    Firetron's FractionMiddle
--
--    Finds the middle value of a fractional range from an array, list, or table.
--
--    Custom Command Dependencies:
--    Firetron's FractionGrab
--    Firetron's Median
--
--    Discord: @FiretronP75
--  ============================================================================

--  ========================================================
--    Variables
--  ========================================================

--  ------------------------------------
--    Definition
--  ------------------------------------

local dDefault
local dDescription
local dName
local dOutput
local dRequired
local dSuggestions
local dType

--  ------------------------------------
--    Enumeration
--  ------------------------------------

local Type = {
  average = 'average',
  median  = 'median',
}

--  ------------------------------------
--    Function
--  ------------------------------------

local GetFractionMiddle

--  ------------------------------------
--    Parameter
--  ------------------------------------

local pIndex
local pIsSort
local pList
local pTotal
local pType

--  ========================================================
--    Command Definition
--  ========================================================

dName        = 'FractionMiddle'
dDescription = 'Finds the middle value of a fractional range from an array, list, or table.'
DefineCommand(dName, dDescription)

--  ========================================================
--    Parameter Definition
--  ========================================================

dType        = StringType
dName        = 'type'
dDescription = '"average" or "median".'
dRequired    = false
dDefault     = Type.average
dSuggestions = 'Input'
pType = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)

dType        = ListDynamicType
dName        = 'list'
dDescription = 'Array, List, or Table to grab a fractional range from.'
dRequired    = false
dDefault     = {3, 1, 2}
dSuggestions = 'Prices'
pList = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)

dType        = NumberType
dName        = 'index'
dDescription = 'Which part to grab. '
             ..'Unlike a fraction numerator, previous parts are not included. '
             ..'In a fraction, 2 would mean 1st and 2nd parts combined. '
             ..'But here, 2 would mean the 2nd part without the 1st.'
dRequired    = false
dDefault     = 1
dSuggestions = 'Input'
pIndex = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)

dType        = NumberType
dName        = 'total'
dDescription = 'How many parts to divide into. '
             ..'This is just like a fraction denominator.'
dRequired    = false
dDefault     = 1
dSuggestions = 'Input'
pTotal = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)

dType        = BooleanType
dName        = 'sort'
dDescription = 'Set to true to sort before dividing into parts.'
dRequired    = false
dDefault     = false
dSuggestions = 'Input'
pIsSort = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)

--  ========================================================
--    Parameter Validation
--  ========================================================

local isValidType =
  pType == Type.average or
  pType == Type.median

if not isValidType then

    LogError('CC_FractionMiddle:  Type "'..pType..'" is not a valid type parameter.')

end

--  ========================================================
--    Functions
--  ========================================================

GetFractionMiddle = function ()

  local list = CC_FractionGrab(pList, pIndex, pTotal, pIsSort)

  if pType == Type.average then return   Average(list) end
  if pType == Type.median  then return CC_Median(list, true) end

end

--  ========================================================
--    Output Definitions
--  ========================================================

dType        = NumberType
dOutput      = GetFractionMiddle()
dDescription = 'The middle value of a fractional range from an array, list, or table.'
dSuggestions = ''
DefineOutput(dType, dOutput, dDescription, dSuggestions)

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!