Market and exchange differences
Market and Exchange Differences
Understanding different market types and how HaasScript handles them is crucial for building strategies that work correctly across various trading scenarios.
Market Types
Spot Trading
Spot trading involves buying and selling actual assets. When you buy on spot, you own the asset.
Characteristics:
- You own the actual asset
- No leverage (1:1)
- Only long positions (buy low, sell high)
- Settlement is immediate
- Example: Buy BTC with USDT, own the BTC
HaasScript Behavior:
-- DoBuy() generates a BUY signal
DoBuy("Buy BTC")
-- DoSell() generates a SELL signal
DoSell("Sell BTC")
-- DoExitPosition() sends a SELL signal to close position
DoExitPosition("Close position")
Margin Trading
Margin trading allows you to trade with borrowed funds, enabling both long and short positions.
Characteristics:
- Trade with borrowed funds (leverage)
- Both long and short positions
- Interest costs on borrowed funds
- Example: Borrow USDT to buy BTC (long), or borrow BTC to sell (short)
HaasScript Behavior:
-- DoBuy() generates a LONG signal
DoBuy("Open long position")
-- DoSell() generates a SHORT signal
DoSell("Open short position")
-- DoExitPosition() sends an EXIT signal to close position
DoExitPosition("Close position")
Leverage Trading (Futures/Perpetuals)
Leverage trading involves futures contracts or perpetual swaps with leverage.
Characteristics:
- Futures contracts or perpetual swaps
- High leverage available (10x, 20x, 100x+)
- Both long and short positions
- Funding rates (for perpetuals)
- Liquidation risk
- Example: BTC/USDT Perpetual with 20x leverage
HaasScript Behavior:
-- DoBuy() generates a LONG signal
DoBuy("Open long position")
-- DoSell() generates a SHORT signal
DoSell("Open short position")
-- DoExitPosition() sends an EXIT signal to close position
DoExitPosition("Close position")
Signal Behavior by Market Type
HaasScript trading commands automatically adapt their behavior based on market type:
| Command | Spot | Margin/Leverage |
|---|---|---|
DoBuy() / DoLong()
|
Buy signal | Long signal |
DoSell() / DoShort()
|
Sell signal | Short signal |
DoExitPosition() |
Sell signal | Exit signal |
Example: Universal Strategy
local rsi = RSI(ClosePrices(), 14)
-- This works on any market type
if rsi < 30 then
DoLong("RSI oversold")
elseif rsi > 70 then
DoShort("RSI overbought")
end
-- Exit works on any market type
local positionDirection = GetPositionDirection()
if rsi > 60 and positionDirection == PositionLong then
DoExitPosition("Take profit")
end
Market Detection
HaasScript automatically detects your current market type and adjusts command behavior accordingly.
Check Current Market
local base = BaseCurrency()
local quote = QuoteCurrency()
local market = PriceMarket()
Log("Trading " .. base .. "/" .. quote .. " (id: " .. market .. ")")
-- Market type is handled automatically by HaasScript
Price Data by Market Type
-- Price data works the same across all market types
local currentPrice = CurrentPrice()
local closePrices = ClosePrices()
local highPrices = HighPrices()
local lowPrices = LowPrices()
-- Technical analysis works the same
local rsi = RSI(ClosePrices(), 14)
local macd = MACD(ClosePrices(), 12, 26, 9)
Position Handling
Spot Positions
-- On spot, you can only have long positions
if GetPositionDirection() == PositionBought then -- or PositionLong
Log("Holding spot position")
end
-- HasShortPosition() returns false on spot
if GetPositionDirection() == PositionSold then -- or PositionShort
Log("No short position on spot")
end
if GetPositionDirection() == NoPosition then
Log("Not bought yet")
end
Margin/Leverage Positions
-- On margin/leverage, you can have both
if GetPositionDirection() == PositionBought then -- or PositionLong
Log("Holding long position")
end
if GetPositionDirection() == PositionSold then -- or PositionShort
Log("Holding short position")
end
if GetPositionDirection() == NoPosition then
Log("No position open")
end
Exchange Differences
Order Types
Different exchanges support different order types:
-- Basic order types (supported by most exchanges)
PlaceGoLongOrder(price, amount, market, LimitOrderType, "Limit order")
PlaceGoLongOrder(price, amount, market, MarketOrderType, "Market order")
-- Advanced order types (exchange-specific)
PlaceGoLongOrder(price, amount, market, StopLimitOrderType, "Stop-limit order")
PlaceGoLongOrder(price, amount, market, StopMarketOrderType, "Stop-market order")
Price and Amount Precision
Exchanges have different precision requirements:
-- Get exchange-specific precision
local priceDecimals = PriceDecimals()
local amountDecimals = AmountDecimals()
local price = 45000.123456
local amount = 0.12345678
-- Round to exchange precision
local adjustedPrice = Round(price, priceDecimals)
local adjustedAmount = Round(amount, amountDecimals)
Minimum Order Sizes
-- Check minimum order size
local minAmount = MinimumTradeAmount()
local amount = 0.001
if amount < minAmount then
Log("Order amount too small")
end
Trading Constraints
Spot Constraints
-- On spot, you need the quote currency to buy
local account = AccountInformation()
local quoteBalance = Balance({coin = QuoteCurrency()})
if quoteBalance.available > 100 then
DoBuy("Sufficient balance to buy")
end
-- And to sell, you need the base currency
local baseBalance = Balance({coin = BaseCurrency()})
if baseBalance.available > 0.01 then
DoSell("Have assets to sell")
end
Margin/Leverage Constraints
-- On margin/leverage, you need sufficient collateral
local account = AccountGuid()
local availableMargin = Balance(account).available
if availableMargin > 100 then
DoLong("Sufficient margin to open position")
end
-- Calculate required margin
local price = CurrentPrice().close
local amount = 0.1
local leverage = 10
local requiredMargin = UsedMargin(PriceMarket(), price, amount, leverage)
if availableMargin > requiredMargin then
DoLong("Sufficient margin for position")
end
Market-Specific Strategies
Spot-Only Strategy
local rsi = RSI(ClosePrices(), 14)
-- Simple buy low, sell high
if rsi < 30 then
DoBuy("Oversold")
elseif rsi > 70 then
DoSell("Overbought")
end
Long/Short Strategy
local rsi = EasyRSI(1, 'RSI')
local macd = EasyMACD(2, 'MACD')
-- Trade both directions
if rsi == SignalLong and macd == SignalLong then
DoLong("Oversold + MACD bullish")
elseif rsi == SignalShort and macd == SignalShort then
DoShort("Overbought + MACD bearish")
end
Adapt to Market Type
if MarketType() == SpotTrading then
-- Run spot trading
elseif MarketType() == LeverageTrading then
-- Run margin/leverage trading
end
Best Practices
- Write universal strategies that work on all market types
- Check balances before placing orders
- Use proper signal commands (DoLong/DoShort) for automatic adaptation
- Handle position exits for both long and short
- Consider margin requirements when using leverage
- Test on multiple market types to ensure compatibility
- Account for exchange differences in order types and precision
- Monitor position state with HasLongPosition()/HasShortPosition()
Summary
- Spot: Buy/sell actual assets, long only
- Margin: Trade with borrowed funds, both long/short
- Leverage: Futures/perpetuals with high leverage, both long/short
- Automatic adaptation: DoLong/DoShort/DoExitPosition adapt to market type
- Universal strategies: Write once, works on all market types
- Exchange differences: Order types, precision, minimum sizes
- Consider constraints: Balance requirements, margin, collateral
Understanding market types ensures your strategy behaves correctly regardless of whether you're trading spot, margin, or futures.