Firetron's GetPriceSpread

stable
By Firetron in Miscellaneous Published December 2020 👁 1,273 views 💬 0 comments

Description

Creates a spread of prices that can be used in orders. Custom Command Dependencies: Firetron’s Fib
HaasScript
--  ============================================================================
--    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)

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!