Skip to content

Trading

HaasScript supports two distinct trading styles. Managed trading is simpler and more restricted — the bot handles position size checks, open order blocking, and direction validation automatically. Unmanaged trading gives you full control over price, amount, order type, timeouts, and more, but requires you to handle all checks yourself.

Both styles share the same underlying order, position, and market information commands — see the dedicated pages on Order Handling, Position Information, and Signal Handling for those.

Managed Trading

Managed trading commands perform all necessary checks before executing a trade. They block execution when there are open orders, ensure the position doesn't exceed the configured trade amount, and prevent entering a position you're already in.

DoLong / DoBuy

Requests to buy (spot) or open a long position (leverage). If the bot is already bought/long, no trade is executed.

DoLong("my note")

DoShort / DoSell

Requests to sell (spot) or open a short position (leverage). No trade is executed if already short.

DoExitPosition

Exits any open position — closes a long or covers a short.

DoSignal

Takes a signal constant (e.g. SignalLong or SignalExitPosition) and calls the corresponding Do* command automatically. This is the bridge between easy indicators and trade execution.

local signal = GetUnanimousSignal(bbands, macd, rsi)
DoSignal(signal)

DoFlipPosition

Executes DoLong if the bot is short, or DoShort if the bot is long. Useful for take-profit with reversal logic.

if someCondition then
    DoFlipPosition()
end

Order Persistence

Order persistence can be enabled with EnableOrderPersistence(). When active, the bot will retry orders that were rejected due to:

  • Maker template rejections
  • Order timeouts
  • Connection issues or exchange overloads

Orders that failed due to insufficient funds, an unknown account ID, or an unknown market will not be retried.

Unmanaged Trading

Unmanaged trading places no restrictions on the script. You have full control over price, amount, order type, timeouts, trigger prices, and more. The commands will not block orders based on open orders, position direction, or size — all of those checks are your responsibility.

Each place-order command returns a unique order ID that you can use to monitor, cancel, or re-execute the order.

Spot Orders

Command Description
PlaceBuyOrder(price, amount, ...) Places a buy order on a spot market.
PlaceSellOrder(price, amount, ...) Places a sell order on a spot market.

Leverage / Margin Orders

Command Description
PlaceGoLongOrder(price, amount, ...) Opens a long position.
PlaceExitLongOrder(price, amount, ...) Closes a long position.
PlaceGoShortOrder(price, amount, ...) Opens a short position.
PlaceExitShortOrder(price, amount, ...) Closes a short position.

Support Commands

Command Description
PlaceCancelledOrder(orderId, [price]) Re-executes the remaining amount of a cancelled order. Returns a new order ID.
PlaceExitPositionOrder([positionId], [price], ...) Places an exit order at price (or the best bid/ask when price not set) for the bot's position. Handles fee correction automatically.

Common Parameters

All unmanaged order commands share these parameters:

Parameter Type Description
market string Market string from PriceMarket(), InputAccountMarket(), or InputMarket(). Uses the bot's configured market by default.
type enum Order type (default: limit).
note string A note visible in order history and open orders.
positionId string Unique identifier for managing multiple positions in unmanaged mode.
timeout number Order timeout in seconds (default: 600). Use a negative value to disable.
triggerPrice number Trigger price for conditional orders. Only use with native exchange order types.
reduceOnly boolean Prevents the order from flipping the position.
hiddenOrder boolean Keeps the order hidden from the order book (if supported by the exchange).

Trade Bot Container

The TradeBotContainer() ties together safeties, indicators, and insurances into a single execution flow.

TradeBotContainer(safetySignal, indicatorSignal, insuranceSignal)
  • The safety signal is a boolean — if true (safety active), DoExitPosition() is executed immediately.
  • The indicator signal is the final signal from your indicators (SignalLong, SignalShort, etc.).
  • The insurance signal is a boolean — execution proceeds only if the insurances agree.

Supporting Containers

Command Description
IndicatorContainer(signals[]) Merges multiple indicator signals into a single result with unanimous and consensus signals.
InsuranceContainer(signals[]) Returns true only if all insurance signals agree.
SafetyContainer(signals[]) Returns true if any safety signal is active.

Trade Market Information

These commands let you query market details without hard-coding exchange-specific values.

Market Identity

Command Description
BaseCurrency(market) Returns the base currency (e.g. BTC for BTC/USD).
QuoteCurrency(market) Returns the quote currency (e.g. USD for BTC/USD).
AmountLabel(market) / AmountCurrency(market) Returns the amount label (e.g. contract(s) on BitMEX XBTUSD).
ProfitLabel(market) / ProfitCurrency(market) Returns the profit label.
ContractName(market) Returns the contract name.
ContractType(market) Returns the contract type (VanillaContract, InverseContract, QuantoContract, or nil on spot/margin).
ContractValue(market) Returns the value of a single contract.
MarketType(market) Returns SpotTrading, MarginTrading, or LeverageTrading.

Amount & Price Limits

Command Description
AmountDecimals(market, amount) Number of decimal places allowed for order amounts.
PriceDecimals(market, price) / QuoteDecimals(market, price) Number of decimal places allowed for order prices.
AmountStep(market) Minimum amount step size for the market.
PriceStep(market) / QuoteStep(market) Minimum price step size for the market.
MinimumTradeAmount(market, price) Minimum trade amount considering decimal count, minimum amount, and volume.
IsTradeAmountEnough(market, price, amount, logWarning) Checks if the amount meets exchange minimums.
IsValidMarket(market) Returns true if the market string is valid.
ParseTradeAmount(market, price, amount) Adjusts an amount to exchange specifications.
ParseTradePrice(market, price) Adjusts a price to exchange specifications.

Fees

Command Description
MakersFee(market) Returns the market's maker fee.
TakersFee(market) Returns the market's taker fee.
SetFee(percentage, market) Overrides the fee percentage for backtesting.

Market Lists

Command Description
GetAccountMarkets(accountId) Returns all markets for an account.
GetExchangeMarkets(exchangeCode) Returns all markets for an exchange.
TradeMarketContainer(market) Returns a container with all market information (base/quote currency, contract details, fees, etc.).

Underlying & Margin

Command Description
UnderlyingAsset(market) Returns the underlying asset of the market.
UsedMargin(market) Returns the margin currently in use for the market.