Firetron's GetSpread
stableDescription
Creates a spread of values that can be used for order amounts or prices.
Custom Command Dependencies:
Firetron’s Fib
Test code:
if not Load('done', false) then
local p = {
base = 100,
count = 5,
direction = 'below',
factor = 2,
isPercent = false,
offset = 0,
type = 'fixed',
}
local spread = CC_GetSpread(p)
for i = 1, #spread do
Log('Spread Value: '..spread[i])
end
Save('done', true)
end
HaasScript
-- ============================================================================
-- Firetron's GetSpread
--
-- Creates a spread of values that can be used for order amounts or prices.
--
-- Custom Command Dependencies:
-- Firetron's Fib
--
-- Discord: @FiretronP75
-- ============================================================================
-- ========================================================
-- Variables
-- ========================================================
-- ------------------------------------
-- Definition
-- ------------------------------------
local dDefault
local dDescription
local dName
local dOutput
local dRequired
local dSuggestions
local dType
-- ------------------------------------
-- Enumeration
-- ------------------------------------
local Direction = {
above = 'above',
below = 'below',
}
local Type = {
doub = 'doub',
exp = 'exp',
fib = 'fib',
fixed = 'fixed',
mult = 'mult',
}
-- ------------------------------------
-- Parameter
-- ------------------------------------
local pBase
local pCount
local pDirection
local pFactor
local pIsPercent
local pOffset
local pOmni
local pType
-- ========================================================
-- Command Definition
-- ========================================================
dName = 'GetSpread'
dDescription = 'Creates a spread of values that can be used in orders.'
DefineCommand(dName, dDescription)
-- ========================================================
-- Parameter Definitions
-- ========================================================
dType = ListDynamicType
dName = 'omni'
dDescription = 'Set this to the ListDynamicType returned by InputSpread. All other parameters will be ignored.'
dRequired = false
dDefault = false
dSuggestions = 'InputSpread'
pOmni = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)
dType = NumberType
dName = 'base'
dDescription = 'Base value to spread out from. This value will not be included. The first value will be placed at the factor away from this value.'
dRequired = false
dDefault = 100
dSuggestions = 'Input'
pBase = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)
dType = NumberType
dName = 'count'
dDescription = 'How many values are in the spread.'
dRequired = false
dDefault = 5
dSuggestions = 'Input'
pCount = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)
dType = StringType
dName = 'direction'
dDescription = '"above" will spread values above the base value, "below" will spread values below the base value.'
dRequired = false
dDefault = Direction.below
dSuggestions = 'Input'
pDirection = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)
dType = NumberType
dName = 'factor'
dDescription = 'How far to space values apart. May be a doubled, exponential, fibonacci, fixed, or multiplied value.'
dRequired = false
dDefault = 2
dSuggestions = 'Input'
pFactor = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)
dType = BooleanType
dName = 'isPercent'
dDescription = 'Set to true to treat the factor as a percent.'
dRequired = false
dDefault = false
dSuggestions = 'Input'
pIsPercent = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)
dType = NumberType
dName = 'offset'
dDescription = 'How many values to skip over at the start of the spread. Does not effect the count.'
dRequired = false
dDefault = 0
dSuggestions = 'Input'
pOffset = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)
dType = StringType
dName = 'type'
dDescription = '"doub" for doubled factor, "exp" for exponential factor, "fib" for fibonacci factor, "fixed" for fixed factor, "mult" for multiplied factor.'
dRequired = false
dDefault = Type.fixed
dSuggestions = 'Input'
pType = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)
-- ========================================================
-- Parameter Overrides
-- ========================================================
if pOmni ~= false then
pBase = pOmni.base
pCount = pOmni.count
pDirection = pOmni.direction
pFactor = pOmni.factor
pIsPercent = pOmni.isPercent
pOffset = pOmni.offset
pType = pOmni.type
end
-- ========================================================
-- Parameter Validation
-- ========================================================
local isValidDirection =
pDirection == Direction.above or
pDirection == Direction.below
if not isValidDirection then
LogError('CC_GetSpread: Direction "'..pDirection..'" is not a valid direction parameter.')
end
-- ----------------
local isValidType =
pType == Type.doub or
pType == Type.exp or
pType == Type.fib or
pType == Type.fixed or
pType == Type.mult
if not isValidType then
LogError('CC_GetSpread: Type "'..pType..'" is not a valid type parameter.')
end
-- ========================================================
-- Functions
-- ========================================================
-- ------------------------------------
-- Command Getters
-- ------------------------------------
local GetFixedCommand = function ()
return pDirection == Direction.above
and Add
or Sub
end
-- ----------------
local GetPercentCommand = function ()
return pDirection == Direction.above
and AddPercentage
or SubPercentage
end
-- ----------------
local GetCommand = function ()
return pIsPercent
and GetPercentCommand()
or GetFixedCommand()
end
-- ------------------------------------
-- Value Getters
-- ------------------------------------
local GetDoubled = function (command, index)
local factor = pFactor
for i = 2, index do
factor = factor * 2
end
return command(pBase, factor)
end
-- ----------------
local GetExponential = function (command, index)
return command(pBase, Pow(pFactor, index))
end
-- ----------------
local GetFibonacci = function (command, index)
return command(pBase, pFactor * CC_Fib(index))
end
-- ----------------
local GetFixed = function (command, index)
return command(pBase, pFactor * index)
end
-- ----------------
local GetMultiplied = function (command, index)
return command(pBase, pFactor * index * index)
end
-- ----------------
local GetValue = function (command, index)
if pType == Type.doub then
return GetDoubled(command, index)
elseif pType == Type.exp then
return GetExponential(command, index)
elseif pType == Type.fib then
return GetFibonacci(command, index)
elseif pType == Type.fixed then
return GetFixed(command, index)
elseif pType == Type.mult then
return GetMultiplied(command, index)
end
end
-- ------------------------------------
-- Spread Getter
-- ------------------------------------
local GetSpread = function ()
local command = GetCommand()
local spread = {}
for index = 1, pCount do
spread[index] = GetValue(command, pOffset + index)
end
return spread
end
-- ========================================================
-- Output Definition
-- ========================================================
dType = ListDynamicType
dOutput = GetSpread()
dDescription = 'ListDynamic where each element is a number.'
dSuggestions = 'Trade'
DefineOutput(dType, dOutput, dDescription, dSuggestions)
0 Comments
Sign in to leave a comment.
No comments yet. Be the first!