Firetron’s SpreadOrder

2 76 Views No Comments 4 weeks ago
  • Creates a spread of orders.

    Custom Command Dependencies:
    Firetron’s Fib

    Test Script:

    if not IsAnyOrderOpen() then
    
      local currentPrice = CurrentPrice()[1]
      
      -- Test Long Spread
    
      Log('==================================================')
      Log('Testing Long Spread, CurrentPrice: '..currentPrice)
      Log('==================================================')
      
      p = {
        amount      = 1,
        amountType  = 'per',
        count       = 6,
        direction   = PositionLong,
        orderParams = {
          market = PriceMarket(),
          type   = NoTimeOutOrderType,
          note   = 'Long Spread',
        },
        price       = currentPrice,
        spread      = 1,
        spreadType  = 'fixed',
      }
      
      local longOrders = CC_SpreadOrder(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        = PositionShort
      p.orderParams.note = 'Short Spread'
    
      local shortOrders = CC_SpreadOrder(p)
    
      for i = 1, #shortOrders do
    
        Log('Short Order ID: '..shortOrders[i])
    
      end
    
    end
    
    • This topic was modified 3 weeks, 3 days ago by Firetron. Reason: Returned Order IDs
    • This topic was modified 3 weeks, 3 days ago by Firetron. Reason: Updated test script
    • This topic was modified 1 week, 5 days ago by Firetron. Reason: Added fibonacci spread type
    • This topic was modified 1 week, 5 days ago by Firetron. Reason: Updated dependency list
    • This topic was modified 1 week, 5 days ago by Firetron. Reason: Added hyperlink to command
    HaasScript Code
    --  ============================================================================
    --    Firetron's SpreadOrder
    --
    --    Creates a spread of orders.
    --
    --    Custom Command Dependencies:
    --    Firetron's Fib
    --
    --    Discord: @FiretronP75
    --  ============================================================================
    
    --  ========================================================
    --    Variables
    --  ========================================================
    
    --  ------------------------------------
    --    Definition
    --  ------------------------------------
    
    local description
    local inputSuggestions
    local output
    local outputSuggestions
    
    --  ------------------------------------
    --    Parameter
    --  ------------------------------------
    
    local pAmount
    local pAmountType
    local pCount
    local pDirection
    local pOrderParams
    local pPrice
    local pSpread
    local pSpreadType
    
    --  ========================================================
    --    Command Definition
    --  ========================================================
    
    description = 'Creates a spread of orders.'
    DefineCommand('SpreadOrder', description)
    
    --  ========================================================
    --    Parameter Definition
    --  ========================================================
    
    description      = 'How much to order. May be total amount or amount per order.'
    inputSuggestions = 'Input'
    pAmount          = DefineParameter(NumberType, 'amount', description, false, 1, inputSuggestions)
    
    description      = '"total" if you specified a total amount, "per" if you specified an amount per order.'
    inputSuggestions = 'Input'
    pAmountType      = DefineParameter(StringType, 'amountType', description, false, 'per', inputSuggestions)
    
    description      = 'How many orders to place'
    inputSuggestions = 'Input'
    pCount           = DefineParameter(NumberType, 'count', description, false, 1, inputSuggestions)
    
    description      = 'PositionLong will spread long orders below the price, PositionShort will spread short orders above the price.'
    inputSuggestions = 'PositionEnum'
    pDirection       = DefineParameter(EnumType, 'direction', description, false, PositionLong, inputSuggestions)
    
    description      = 'Parameters to give each order, other than price and amount. See PlaceGoLongOrder or PlaceGoShortOrder for help.'
    inputSuggestions = ''
    pOrderParams     = DefineParameter(ListDynamicType, 'orderParams', description, false, {market = PriceMarket()}, inputSuggestions)
    
    description      = 'Price to spread out orders from. No order will be placed at this price. The first order will be placed at the spread distance away from this price.'
    inputSuggestions = 'CurrentPrice'
    pPrice           = DefineParameter(NumberType, 'price', description, false, 1, inputSuggestions)
    
    description      = 'How far to space orders apart. May be a percent, fibonacci, or fixed amount.'
    inputSuggestions = 'Input'
    pSpread          = DefineParameter(NumberType, 'spread', description, false, 1, inputSuggestions)
    
    description      = '"fib" if you specified a fibonacci spread, "fixed" if you specified a fixed spread, "percent" if you specified a percent spread.'
    inputSuggestions = 'Input'
    pSpreadType      = DefineParameter(StringType, 'spreadType', description, false, 'fixed', inputSuggestions)
    
    --  ========================================================
    --    Functions
    --  ========================================================
    
    local GetOrderAmount = function ()
    
      local orderAmount
    
      if pAmountType == 'per' then
    
        orderAmount = pAmount
    
      elseif pAmountType == 'total' then
    
        orderAmount = pAmount / pCount
    
      else
    
        LogError('CC_SpreadOrder has invalid amountType parameter.')
    
      end
    
      return orderAmount
    
    end
    
    --  ----------------
    
    local GetOrderPrice = function (spreadFactor)
    
      local orderPrice
    
      if (pSpreadType == 'fib' or pSpreadType == 'fixed') and pDirection == PositionLong then
    
        orderPrice = pPrice - spreadFactor
    
      elseif (pSpreadType == 'fib' or pSpreadType == 'fixed') and pDirection == PositionShort then
    
        orderPrice = pPrice + spreadFactor
    
      elseif pSpreadType == 'percent' and pDirection == PositionLong then
    
        orderPrice = SubPercentage(pPrice, spreadFactor)
    
      elseif pSpreadType == 'percent' and pDirection == PositionShort then
    
        orderPrice = AddPercentage(pPrice, spreadFactor)
    
      else
    
        LogError('CC_SpreadOrder has invalid direction or spreadType parameters.')
    
      end
    
      return orderPrice
    
    end
    
    --  ----------------
    
    local PlaceOrder = function (orderPrice, orderAmount)
    
      if pDirection == PositionLong then
    
        return PlaceGoLongOrder(orderPrice, orderAmount, pOrderParams)
    
      elseif pDirection == PositionShort then
    
        return PlaceGoShortOrder(orderPrice, orderAmount, pOrderParams)
    
      else
    
        LogError('CC_SpreadOrder has invalid direction parameter.')
    
      end
    
    end
    
    --  ========================================================
    --    Execution
    --  ========================================================
    
    output = {}
    
    local orderAmount = GetOrderAmount()
    
    for i = 1, pCount do
    
      local spreadFactor
    
      if pSpreadType == 'fib' then
        spreadFactor = pSpread * CC_Fib(i)
      else
        spreadFactor = pSpread * i
      end
    
      local orderPrice = GetOrderPrice(spreadFactor)
    
      local orderId = PlaceOrder(orderPrice, orderAmount)
    
      output[i] = orderId
    
    end
    
    --  ========================================================
    --    Output Definitions
    --  ========================================================
    
    description       = 'List of Order IDs'
    outputSuggestions = 'CancelOrder'
    DefineOutput(ListDynamicType, 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