Firetron’s GetPriceSpread

2 90 Views No Comments 2 months ago
  • Creates a spread of prices that can be used in orders.

    Custom Command Dependencies:
    Firetron’s Fib

    • This topic was modified 1 month, 2 weeks ago by Firetron. Reason: Corrections to descriptions, log, and output suggestion
    • This topic was modified 1 month, 2 weeks ago by Firetron. Reason: Changed command description
    • This topic was modified 1 month ago by Firetron. Reason: Added parameter: listDynamic
    • 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, 5 days ago by Firetron. Reason: Added fibPercent spreadType
    HaasScript Code
    --  ============================================================================
    --    Firetron's GetPriceSpread
    --
    --    Creates a spread of prices 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 pBasePrice
    local pCount
    local pDirection
    local pListDynamic
    local pOffset
    local pSpreadFactor
    local pSpreadType
    
    --  ========================================================
    --    Command Definition
    --  ========================================================
    
    name        = 'GetPriceSpread'
    description = 'Creates a spread of prices that can be used in orders.'
    DefineCommand(name, description)
    
    --  ========================================================
    --    Parameter Definitions
    --  ========================================================
    
    type             = ListDynamicType
    name             = 'listDynamic'
    description      = 'Set this to the ListDynamicType returned by InputPriceSpread. Will override all other parameters.'
    isRequired       = false
    defaultValue     = false
    inputSuggestions = 'InputPriceSpread'
    pListDynamic     = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)
    
    type             = NumberType
    name             = 'basePrice'
    description      = 'Price to spread out from. This price will not be included. The first price will be placed at the spread factor away from this price.'
    isRequired       = false
    defaultValue     = 1
    inputSuggestions = 'CurrentPrice'
    pBasePrice       = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)
    
    type             = NumberType
    name             = 'count'
    description      = 'How many prices are in the spread.'
    isRequired       = false
    defaultValue     = 1
    inputSuggestions = 'Input'
    pCount           = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)
    
    type             = StringType
    name             = 'direction'
    description      = '"above" will spread prices above the base price, "below" will spread prices below the base price.'
    isRequired       = false
    defaultValue     = 'below'
    inputSuggestions = 'Input'
    pDirection       = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)
    
    type             = NumberType
    name             = 'offset'
    description      = 'How many prices 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 far to space prices apart. May be an exponential, fibonacci, fibonacci percent, fixed, or percent value.'
    isRequired       = false
    defaultValue     = 1
    inputSuggestions = 'Input'
    pSpreadFactor    = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)
    
    type             = StringType
    name             = 'spreadType'
    description      = '"exp" if you specified an exponential spread, "fib" if you specified a fibonacci spread, "fibPercent" if you specified a fibonacci percent spread, "fixed" if you specified a fixed spread, "percent" if you specified a percent spread.'
    isRequired       = false
    defaultValue     = 'fixed'
    inputSuggestions = 'Input'
    pSpreadType      = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)
    
    if not (pListDynamic == false) then
      pBasePrice    = pListDynamic.basePrice
      pCount        = pListDynamic.count
      pDirection    = pListDynamic.direction
      pOffset       = pListDynamic.offset
      pSpreadFactor = pListDynamic.spreadFactor
      pSpreadType   = pListDynamic.spreadType
    end
    
    --  ========================================================
    --    Functions
    --  ========================================================
    
    local GetPrice = function (index, spread)
    
      local price
    
      if (pSpreadType == 'exp') and pDirection == 'below' then
    
        price = pBasePrice - (spread * index)
    
      elseif (pSpreadType == 'exp') and pDirection == 'above' then
    
        price = pBasePrice + (spread * index)
    
      elseif (pSpreadType == 'fib' or pSpreadType == 'fixed') and pDirection == 'below' then
    
        price = pBasePrice - spread
    
      elseif (pSpreadType == 'fib' or pSpreadType == 'fixed') and pDirection == 'above' then
    
        price = pBasePrice + spread
    
      elseif pSpreadType == 'fibPercent' and pDirection == 'below' then
    
        price = SubPercentage(pBasePrice, spread)
    
      elseif pSpreadType == 'fibPercent' and pDirection == 'above' then
    
        price = AddPercentage(pBasePrice, spread)
    
      elseif pSpreadType == 'percent' and pDirection == 'below' then
    
        price = SubPercentage(pBasePrice, spread)
    
      elseif pSpreadType == 'percent' and pDirection == 'above' then
    
        price = AddPercentage(pBasePrice, spread)
    
      else
    
        LogError('CC_GetPriceSpread has invalid direction or spreadType parameters.')
    
      end
    
      return price
    
    end
    
    --  ----------------
    
    local GetPriceSpread = function ()
    
      local priceSpread = {}
    
      for i = 1, pCount do
    
        local offsetIndex = pOffset + i
    
        local spread
    
        if pSpreadType == 'fib' or pSpreadType == 'fibPercent' then
    
          spread = pSpreadFactor * CC_Fib(offsetIndex)
    
        else
    
          spread = pSpreadFactor * offsetIndex
    
        end
    
        priceSpread[i] = GetPrice(offsetIndex, spread)
    
      end
    
      return priceSpread
    
    end
    
    --  ========================================================
    --    Output Definition
    --  ========================================================
    
    type              = ListDynamicType
    output            = GetPriceSpread()
    description       = 'List of prices.'
    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