Firetron’s GetAmountSpread

1 95 Views No Comments 1 month ago
  • Creates a spread of amounts that can be used in orders.

    Custom Command Dependencies:
    Firetron’s Fib

    • This topic was modified 1 month ago by Firetron. Reason: Added parameter: listDynamic
    • This topic was modified 1 month ago by Firetron. Reason: Added "mult" option
    • This topic was modified 1 month ago by Firetron. Reason: Added "exp" option
    • This topic was modified 1 month ago by Firetron. Reason: Added parens around comparison
    • This topic was modified 3 weeks, 4 days ago by Firetron. Reason: Added hyperlink to command
    • This topic was modified 3 weeks, 3 days ago by Firetron. Reason: Added offset parameter
    • This topic was modified 2 weeks, 4 days ago by Firetron. Reason: Added parameter: total
    HaasScript Code
    --  ============================================================================
    --    Firetron's GetAmountSpread
    --
    --    Creates a spread of amounts that can be used in orders.
    --
    --    Custom Command Dependencies:
    --    Firetron's Fib
    --
    --    Discord: @FiretronP75
    --  ============================================================================
    
    --  ========================================================
    --    Variables
    --  ========================================================
    
    --  ------------------------------------
    --    Definition
    --  ------------------------------------
    
    local defaultValue
    local description
    local inputSuggestions
    local isRequired
    local name
    local output
    local outputSuggestions
    local type
    
    --  ------------------------------------
    --    Parameter
    --  ------------------------------------
    
    local pCount
    local pListDynamic
    local pOffset
    local pSpreadFactor
    local pSpreadType
    local pTotal
    
    --  ========================================================
    --    Command Definition
    --  ========================================================
    
    name        = 'GetAmountSpread'
    description = 'Creates a spread of amounts that can be used in orders.'
    DefineCommand(name, description)
    
    --  ========================================================
    --    Parameter Definitions
    --  ========================================================
    
    type             = ListDynamicType
    name             = 'listDynamic'
    description      = 'Set this to the ListDynamicType returned by InputAmountSpread. Will override all other parameters.'
    isRequired       = false
    defaultValue     = false
    inputSuggestions = 'InputAmountSpread'
    pListDynamic     = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)
    
    type             = NumberType
    name             = 'count'
    description      = 'How many amounts are in the spread.'
    isRequired       = false
    defaultValue     = 1
    inputSuggestions = 'Input'
    pCount           = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)
    
    type             = NumberType
    name             = 'offset'
    description      = 'How many amounts to skip over at the start of the spread.'
    isRequired       = false
    defaultValue     = 0
    inputSuggestions = 'Input'
    pOffset          = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)
    
    type             = NumberType
    name             = 'spreadFactor'
    description      = 'How much to order. May be total amount or amount per order or fibonacci amount per order or multiplied amount per order or exponential ammount per order.'
    isRequired       = false
    defaultValue     = 1
    inputSuggestions = 'Input'
    pSpreadFactor    = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)
    
    type             = StringType
    name             = 'spreadType'
    description      = '"total" if you specified a total amount, "per" if you specified an amount per order, "fib" if you specified a fibonacci amount per order, "mult" if you specified an amount to be multiplied by the order index, "exp" if you specified an amount to be exponentially multiplied by the order index.'
    isRequired       = false
    defaultValue     = 'per'
    inputSuggestions = 'Input'
    pSpreadType      = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)
    
    type             = NumberType
    name             = 'total'
    description      = 'Total amount to divide up. Set to 0 to disable. If enabled, each value in the amount spread will be converted to a rationed amount of the total. When the spreadType is "total" this is redundant.'
    isRequired       = false
    defaultValue     = 0
    inputSuggestions = 'Input'
    pTotal           = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)
    
    if not (pListDynamic == false) then
      pCount        = pListDynamic.count
      pOffset       = pListDynamic.offset
      pSpreadFactor = pListDynamic.spreadFactor
      pSpreadType   = pListDynamic.spreadType
      pTotal        = pListDynamic.total
    end
    
    --  ========================================================
    --    Functions
    --  ========================================================
    
    local GetAmount = function (index, spread)
    
      local amount
    
      if pSpreadType == 'exp' then
    
        amount = spread * (index * index)
    
      elseif pSpreadType == 'fib' or pSpreadType == 'per' then
    
        amount = spread
    
      elseif pSpreadType == 'mult' then
    
        amount = spread * index
    
      elseif pSpreadType == 'total' then
    
        amount = spread / pCount
    
      else
    
        LogError('CC_GetAmountSpread has invalid spreadType parameter.')
    
      end
    
      return amount
    
    end
    
    --  ----------------
    
    local GetAmountSpread = function ()
    
      local amountSpread = {}
    
      for i = 1, pCount do
    
        local offsetIndex = pOffset + i
    
        local spread
    
        if pSpreadType == 'fib' then
    
          spread = pSpreadFactor * CC_Fib(offsetIndex)
    
        else
    
          spread = pSpreadFactor
    
        end
    
        amountSpread[i] = GetAmount(offsetIndex, spread)
    
      end
    
      return amountSpread
    
    end
    
    --  ----------------
    
    local SumListDynamic = function (listDynamic)
    
      local sum = 0
    
      for i = 1, #listDynamic do
    
        sum = sum + listDynamic[i]
    
      end
    
      return sum
    
    end
    
    --  ----------------
    
    local GetRatioAmountSpread = function ()
    
      local amountSpread = GetAmountSpread()
    
      if pTotal == 0 then return amountSpread end
    
      local ratioAmountSpread = {}
    
      local sum = SumListDynamic(amountSpread)
    
      for i = 1, #amountSpread do
    
        local ratio = amountSpread[i] / sum
    
        ratioAmountSpread[i] = ratio * pTotal
    
      end
    
      return ratioAmountSpread
    
    end
    
    --  ========================================================
    --    Output Definition
    --  ========================================================
    
    type              = ListDynamicType
    output            = GetRatioAmountSpread()
    description       = 'List of amounts.'
    outputSuggestions = 'Trade'
    DefineOutput(type, output, description, outputSuggestions)
Login or Register to Comment

Unlock your crypto trading potential

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

Join for Free