Firetron's PlaceSpreadOrder

stable
By Firetron in Miscellaneous Published October 2020 👁 1,387 views 💬 0 comments

Description

Creates a spread of orders. Custom Command Dependencies: None Test Script:
if not Load('done', false) then

  local currentPrice = CurrentPrice()[1]

  -- Test Long Spread

  Log('==================================================')
  Log('Testing Long Spread, CurrentPrice: '..currentPrice)
  Log('==================================================')

  local p = {
    amountList   = {1, 2, 3},
    direction    = 'GoLong',
    orderOptions = {
      market = PriceMarket(),
      type   = NoTimeOutOrderType,
      note   = 'Long Spread',
    },
    priceList    = {currentPrice, SubPercentage(currentPrice, 1), SubPercentage(currentPrice, 1)},
  }

  local longOrders = CC_PlaceSpreadOrder(p)

  for i = 1, #longOrders do

    Log('Long Order ID: '..longOrders[i])

  end

  -- Test Short Spread

  Log('==================================================')
  Log('Testing Short Spread, CurrentPrice: '..currentPrice)
  Log('==================================================')

  p.direction         = 'GoShort'
  p.orderOptions.note = 'Short Spread'
  p.priceList         = {currentPrice, AddPercentage(currentPrice, 1), AddPercentage(currentPrice, 1)}

  local shortOrders = CC_PlaceSpreadOrder(p)

  for i = 1, #shortOrders do

    Log('Short Order ID: '..shortOrders[i])

  end

  Save('done', true)

end
HaasScript
--  ============================================================================
--    Firetron's PlaceSpreadOrder
--
--    Creates a spread of orders.
--
--    Custom Command Dependencies:
--    None
--
--    Discord: @FiretronP75
--  ============================================================================

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

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

local defaultValue
local description
local inputSuggestions
local isRequired
local name
local output
local outputSuggestions
local type

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

local pAmountList
local pDirection
local pListDynamic
local pOrderOptions
local pPriceList

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

name        = 'PlaceSpreadOrder'
description = 'Creates a spread of orders.'
DefineCommand(name, description)

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

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             = ListDynamicType
name             = 'amountList'
description      = 'List of amounts to use.'
isRequired       = false
defaultValue     = {1, 2, 3}
inputSuggestions = 'CC_GetAmountSpread'
pAmountList      = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)

type             = StringType
name             = 'direction'
description      = 'Buy, Sell, GoLong, ExitLong, GoShort, ExitShort.'
isRequired       = false
defaultValue     = 'Buy'
inputSuggestions = 'Input'
pDirection       = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)

type             = ListDynamicType
name             = 'orderOptions'
description      = 'Parameters to give each order, other than price and amount. See PlaceGoLongOrder or PlaceGoShortOrder for help.'
isRequired       = false
defaultValue     = {market = PriceMarket()}
inputSuggestions = ''
pOrderOptions    = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)

type             = ListDynamicType
name             = 'priceList'
description      = 'List of prices to use.'
isRequired       = false
defaultValue     = {1, 2, 3}
inputSuggestions = 'CC_GetPriceSpread'
pPriceList       = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)

if not (pListDynamic == false) then
  pAmountList   = pListDynamic.amountList
  pDirection    = pListDynamic.direction
  pOrderOptions = pListDynamic.orderOptions
  pPriceList    = pListDynamic.priceList
end

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

local PlaceOrder = function (orderPrice, orderAmount, orderOptions)

  if pDirection == 'Buy' then

    return PlaceBuyOrder(orderPrice, orderAmount, orderOptions)

  elseif pDirection == 'Sell' then

    return PlaceSellOrder(orderPrice, orderAmount, orderOptions)

  elseif pDirection == 'GoLong' then

    return PlaceGoLongOrder(orderPrice, orderAmount, orderOptions)

  elseif pDirection == 'GoShort' then

    return PlaceGoShortOrder(orderPrice, orderAmount, orderOptions)

  elseif pDirection == 'ExitLong' then

    return PlaceExitLongOrder(orderPrice, orderAmount, orderOptions)

  elseif pDirection == 'ExitShort' then

    return PlaceExitShortOrder(orderPrice, orderAmount, orderOptions)

  else

    LogError('CC_PlaceSpreadOrder has invalid direction parameter.')

  end

end

--  ========================================================
--    Execution
--  ========================================================

output = {}

if not (#pAmountList == #pPriceList) then

  LogError('CC_PlaceSpreadOrder amountList and priceList are not the same length.')

end

local orderOptions = {
  market       = pOrderOptions.market,
  type         = pOrderOptions.type,
  note         = pOrderOptions.note,
  positionId   = pOrderOptions.positionId,
  timeout      = pOrderOptions.timeout,
  triggerPrice = pOrderOptions.triggerPrice,
}

for i = 1, #pAmountList do

  local orderAmount = pAmountList[i]
  local orderPrice  = pPriceList[i]

  local orderId = PlaceOrder(orderPrice, orderAmount, orderOptions)

  output[i] = orderId

end

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

description       = 'List of Order IDs'
outputSuggestions = 'Order'
DefineOutput(ListDynamicType, output, description, outputSuggestions)

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!