Unmanaged Trading

Introduction

Unmanaged trading in HaasScript means that you are in full control of the orders. With this category of trading commands, you are able to create scripts that can handle multiple orders on multiple markets simultaneously.

Executing Orders

Placing orders

Executing orders in unmanaged trading happens with 6 commands: PlaceBuyOrder, PlaceGoLongOrder, PlaceExitLongOrder, PlaceSellOrder, PlaceGoShortOrder, and PlaceExitShortOrder. These commands will return an order identifier (order ID) which can be used to control specific orders in the order books or to get information about their current status.

This demo script places an order if non has been placed yet.

orderId = Load('orderId', '') -- Load the orderId, if any

if (orderId == '') then
  price = CurrentPrice().close
  amount = TradeAmount()

  orderId = PlaceBuyOrder(price,amount)
else
  -- Do something else
end

Save('orderId', orderId) 

PlaceBuyOrder

This unmanaged trading command is used for spot trading and sends out a buying order for the given price and amount.

PlaceSellOrder

This command is used for spot trading and sends out a selling order for the given price and amount.

PlaceGoLongOrder

This unmanaged trading command is used for futures trading and sends out an order to get long contracts for the given price and amount.

PlaceGoShortOrder

This unmanaged trading command is used for futures trading and sends out an order to get short contracts for the given price and amount.

PlaceExitLongOrder

This unmanaged trading command is used for futures trading and sends out an order to sell the long contracts for the given price and amount.

PlaceExitShortOrder

This unmanaged trading command is used for futures trading and sends out an order to sell the sell contracts for the given price and amount.

Order types

There are multiple order types that can be utilized in several commands, such as SetOrderType and the fore-mentioned PlaceOrder commands.

LimitOrderType

Executes a limit order which can be placed in the order books. The timeout parameter is used when using this order type. The default timeout is 10 minutes.

MarketOrderType

Executes a market order which will trade with the current market price. These order types execute immediately by filling the set trade amount with the orders from the opposite order book.

NoTimeOutOrderType

Executes a limit order which can be placed in the order books. The timeout parameter is ignored when using this order type.

MakerOrCancelOrderType

Executes a limit order which will either be placed in the order book or canceled if the order would execute immediately by filling another order. The timeout parameter is used when using this order type. The default timeout is 10 minutes. If the current exchange does not support this type, a normal limit order will be executed.


Managing orders

Order Management

Order management is crucial for the more advanced bots and trading systems. With these commands you are able to;

  • Cancel all or specific orders

  • Check if orders are open or filled

Cancelling orders

To cancel a specific order, we use CancelOrder.

orderId = Load('orderId', '') -- Load the orderId, if any

if (orderId == '') then
  -- Do something else
else
  CancelOrder(orderId)
end

Save('orderId', orderId) 

To cancel ALL orders, we use CancelAllOrders.

if IsAnyOrderOpen() then
  CancelAllOrders()
else
  -- Do something else
end

Open orders

There are multiple ways to check for open orders.

We can either do IsAnyOrderOpen, which checks for any open orders in the bot or a system. If the positionId parameter is not set, then the system will check for all orders that a bot or system has created. This command also works for managed trading.

if IsAnyOrderOpen() then
  -- Do something, there is a open order 
end

Or we can do IsOrderOpen to check for specific order.

orderId = Load('orderId', '') -- Load the orderId, if any

if IsOrderOpen(orderId) then
  -- Do something, the order is open
end

Order Information

Gather order information to keep an eye on your orders and let your system know what it needs to do next. Using this category of commands you can check if an order is partially or fully filled, how much the fee costs are, when was the order opened and much more.

Order containers and collections

To get all possible information about partially filled and filled orders, we use GetAllFilledOrders. This command returns a collection of all (partially) filled orders executed by the bot.

filledOrders = GetAllFilledOrders()

latestOrder = filledOrders[1]

price = latestOrder.price
executedAmount = latestOrder.executedAmount
isOpen = latestOrder.isOpen

For open orders, we use GetAllOpenOrders. This command returns a collection of all open orders executed by the bot.

openOrders = GetAllOpenOrders()

order = openOrders[1]

price = order.price
filledAmount = order.filledAmount
isCancelled = order.isCancelled

For all order-specific information, we use OrderContainer. The returned value is an array that contains the information.

orderId = Load('orderId', '') -- Load the orderId, if any

if (orderId == '') then
  -- Do something else
else
  order = OrderContainer(orderId)

  price = order.price
  feeCosts = order.feeCosts
  openTime = order.openTime
end

Cancelled amount

To get order's canceled amount, we use GetOrderCancelledAmount. This amount is what was left unfilled when the order was canceled, or the full amount when the order is completed.

tradeAmount = Input('Amount per order', 1)
orderId = Load('orderId', '') -- Load the orderId, if any

if (orderId == '') then
  -- Do something else
else
  cancelledAmount = GetOrderCancelledAmount(orderId)
  filledAmount = tradeAmount - cancelledAmount
end

Filled amount

To get the order's filled amount, we use GetOrderFilledAmount. This value tells how much of the order has been filled. The amount will be the same as the executed amount if the order is filled.

tradeAmount = Input('Amount per order', 1)
orderId = Load('orderId', '') -- Load the orderId, if any

if (orderId == '') then
  -- Do something else
else
  filledAmount = GetOrderFilledAmount(orderId)
  amountLeft = tradeAmount - filledAmount
end

Order timestamp

To get the time when the order was created, we use GetOrderOpenTime. The returned value is presented as minutes by default.

orderId = Load('orderId', '') -- Load the orderId, if any

if (orderId == '') then
  -- Do something else
else
  timeInMinutes = GetOrderOpenTime(orderId, false)
  timeInSeconds = GetOrderOpenTime(orderId, true)
end

Managing Positions

Position management

Positions in HaasScript are components which you can use to define whether a bot is "bought/long", "sold/short" or in "no position". They also store lots of information which you and your bots can use to monitor performance or even plan ahead.

Creating positions

Positions can be created simply by calling CreatePosition or doing a trade. You can define the position ID when placing orders and creating position, or you can leave it empty which will auto-generate it. You can also have multiple positions within one script (multi-market trading).

The ID can be used to check on information like last trade prices, (average) entry/exit price and position size, market, profit and much more.

CreatePosition

Doing a trade

Example #1: Auto-generated ID

orderId = PlaceBuyOrder(price, amount)
profit = GetPositionProfit()

Example #2: User-defined ID

CreatePosition(PositionLong, 9320, 1, 'BTC_USDT', -1, `My own guid-or-name`)
Log(GetPositionProfit(`My own guid-or-name`)) 

Position information

PositionContainer

PositionContainer is a collection of all possible information about your bot's position.

position = PositionContainer(positionId)

Log(position[1]) -- ID
Log(position[2]) -- Market
Log(position.isLong) -- IsLong?
Log(position[4]) -- IsShort?
Log(position[5]) -- Entry price
Log(position[6]) -- Position Size
Log(position.profit) -- Profit
Log(position.roi) -- ROI

Separate information commands

You are also able to extract specific information about bot's position by using LongAmount, ShortAmount, GetPositionMarket, GetPositionDirection, GetPositionEnterPrice, GetPositionAmount, GetPositionProfit and GetPositionROI.

market = GetPositionfarket(positionTd) Marke:

Log(positionld) -- ID
Log(LongAmount(market)) -- Long Position Size
Log(ShortAmount(market)) -- Short Position Size
Log(GetPositionDirection(positionTd)) -- Position Enum
Log(GetPositionEnterPrice(positionTd)) -- Entry Price
Log(GetPositionAmount(positionId)) -- Position-specific
Log(GetPositionProfit(positionld)) -- Profit
Log(GetPositionROI(positionId)) -- ROI

Position prices

To extract last trade prices, we use AverageEnterPrice, AverageExitPrice, LastExitLongPrice, LastExitShortPrice, LastLongPrice, LastShortPrice, and LastNoPositionPrice.

Log(AverageEnterPrice(positionid)) -- Average entry price
Log(AverageExitPrice(positionIa)) -- Average exit price
Log(LastExitLongPrice()) -- Last long exit price
Log(LastExitShortPrice()) -- Last short exit price
Log(LastLongPrice() -- Last long entry price
Log(LastShortPrice()) -- Last short entry price
Log(LastNoPositionPrice()) -- Last long/short exit price

Managing Wallet

Wallet commands

Wallet commands are a great way to check if you have enough assets for entries and exits.

Maximum entry amount

MaxLongAmount and MaxShortAmount will calculate the minimum/maximum trade amount which can be used to place entry orders.

entryAmount = MaxLongAmount()
orderId = PlaceGoLongOrder(price, entryAmount)

Maximum exit amount

MaxExitLongAmount and MaxExitShortAmount will calculate the maximum trade amount which can be used to place exit orders.

exitAmount = MaxExitShortAmount()
orderId = PlacxeExitShortOrder(price, exitAmount)

Coin amount and wallet check

WalletAmount will get the available amount in the wallet for a specific coin.

account = AccountGuid()

xrpAmount = WalletAmount(account, 'XRP')

With WalletCheck, you can make sure you have enough assets in the wallet before executing orders.

account = AccountGuid()

isEnoughXRP = WalletCheck(account, 'XRP', 1000)

Last updated