Built-in Functions
Understanding Base/QuoteCurrency(), CreateMarket()
Understanding Base/QuoteCurrency(), CreateMarket()
These fundamental HaasScript commands help you identify and work with trading markets. Understanding how to retrieve currency information and construct market strings is essential for building trading strategies that work across different markets.
Currency Pairs in Trading
In cryptocurrency and forex trading, markets are expressed as currency pairs:
BTC/USDT
- Base Currency (BTC): The first currency - what you're buying or selling
- Quote Currency (USDT): The second currency - what you're pricing the base in
Examples:
-
BTC/USDT: Buy/sell BTC using USDT -
ETH/BTC: Buy/sell ETH using BTC -
LTC/USDT: Buy/sell LTC using USDT
BaseCurrency()
Returns the base currency of the current market.
BaseCurrency([market])
Basic Usage
local base = BaseCurrency()
Log(base) -- Outputs: BTC (if current market is BTC/USDT)
With Market Parameter
local market = "BINANCE_ETH_USDT_"
local base = BaseCurrency(market)
Log(base) -- Outputs: ETH
Trading Use Case
local baseCurrency = BaseCurrency()
if baseCurrency == "BTC" then
Log("Trading BTC pair")
elseif baseCurrency == "ETH" then
Log("Trading ETH pair")
end
QuoteCurrency()
Returns the quote currency of the current market.
QuoteCurrency([market])
Basic Usage
local quote = QuoteCurrency()
Log(quote) -- Outputs: USDT (if current market is BTC/USDT)
With Market Parameter
local market = "BINANCE_LTC_BTC_"
local quote = QuoteCurrency(market)
Log(quote) -- Outputs: BTC
Trading Use Case
local quoteCurrency = QuoteCurrency()
if quoteCurrency == "USDT" then
Log("Trading against USDT")
elseif quoteCurrency == "BTC" then
Log("Trading against BTC")
end
Combining Base and Quote Currencies
Get Full Market Name
local base = BaseCurrency()
local quote = QuoteCurrency()
local market = base .. "/" .. quote
Log("Trading market: " .. market)
Check Market Type
local base = BaseCurrency()
local quote = QuoteCurrency()
if quote == "USD" or quote == "USDT" or quote == "EUR" then
Log("Trading against fiat currency")
elseif quote == "BTC" then
Log("Trading against BTC")
else
Log("Trading against altcoin")
end
Filter by Base Currency
local base = BaseCurrency()
-- Only trade BTC pairs
if base == "BTC" then
-- Your strategy logic
end
Filter by Quote Currency
local quote = QuoteCurrency()
-- Only trade against USDT
if quote == "USDT" then
-- Your strategy logic
end
CreateMarket()
Creates a market string that can be used with various HaasScript commands.
CreateMarket([priceSource], [baseCurrency], [quoteCurrency], [contractName])
Basic Usage
local market = CreateMarket()
Log(market) -- Outputs current market string
Create Custom Market
local market = CreateMarket("", "BTC", "USDT", "")
Log(market) -- Outputs: EXCHANGE_BTC_USDT_
With Price Source
local market = CreateMarket("BINANCE", "ETH", "USDT", "")
Log(market) -- Outputs: BINANCE_ETH_USDT_
With Contract Name (Futures)
local market = CreateMarket("", "BTC", "USDT", "PERPETUAL")
Log(market) -- Outputs: EXCHANGE_BTC_USDT_PERPETUAL
Using CreateMarket() in Strategies
Switch Markets
-- Create market for BTC/USDT
local btcMarket = CreateMarket("", "BTC", "USDT", "")
-- Use this market for price data
local price = CurrentPrice(btcMarket).close
Log("BTC price: " .. price)
Compare Multiple Markets
local btcMarket = CreateMarket("", "BTC", "USDT", "")
local ethMarket = CreateMarket("", "ETH", "USDT", "")
local btcPrice = CurrentPrice(btcMarket).close
local ethPrice = CurrentPrice(ethMarket).close
Log("BTC: " .. btcPrice .. ", ETH: " .. ethPrice)
Arbitrage Checks
local binanceBTC = CreateMarket("BINANCE", "BTC", "USDT", "")
local coinbaseBTC = CreateMarket("COINBASE", "BTC", "USDT", "")
local binancePrice = CurrentPrice(binanceBTC).close
local coinbasePrice = CurrentPrice(coinbaseBTC).close
if binancePrice > coinbasePrice then
Log("Binance price higher: " .. binancePrice)
end
Dynamic Market Selection
local preferredBase = "BTC"
local preferredQuote = "USDT"
local market = CreateMarket("", preferredBase, preferredQuote, "")
local currentPrice = CurrentPrice(market).close
Log("Price of " .. preferredBase .. "/" .. preferredQuote .. ": " .. currentPrice)
Common Patterns
Market Information Logging
local base = BaseCurrency()
local quote = QuoteCurrency()
local price = CurrentPrice().close
Log("=== Market Info ===")
Log("Market: " .. base .. "/" .. quote)
Log("Current Price: " .. price)
Log("Base: " .. base)
Log("Quote: " .. quote)
Validate Market
local base = BaseCurrency()
local quote = QuoteCurrency()
if base == "" or quote == "" then
Log("Error: Invalid market")
else
Log("Trading " .. base .. "/" .. quote)
end
Convert Between Markets
local base = BaseCurrency()
local quote = QuoteCurrency()
-- If trading against BTC, show USDT equivalent
if quote == "BTC" then
local btcMarket = CreateMarket("", "BTC", "USDT", "")
local baseMarket = CreateMarket("", base, quote, "")
local btcPrice = CurrentPrice(btcMarket).close
local basePrice = CurrentPrice(baseMarket).close
local value = Round(btcPrice * basePrice, 4)
Log("1 " .. base .. " = " .. value .. " USDT")
end
Portfolio Tracking
local base = BaseCurrency()
local quote = QuoteCurrency()
local position = GetPositionAmount()
if position > 0 then
local value = position * CurrentPrice().close
Log("Position: " .. position .. " " .. base .. " (" .. value .. " " .. quote .. ")")
end
Working with Multiple Exchanges
Cross-Exchange Comparison
local binance = CreateMarket("BINANCE", "BTC", "USD", "")
local coinbase = CreateMarket("COINBASE", "BTC", "USDT", "")
local kraken = CreateMarket("KRAKEN", "BTC", "EUR", "")
local binancePrice = CurrentPrice(binance).close
local coinbasePrice = CurrentPrice(coinbase).close
local krakenPrice = CurrentPrice(kraken).close
Log("Binance: " .. binancePrice)
Log("Coinbase: " .. coinbasePrice)
Log("Kraken: " .. krakenPrice)
Get Best Price
local exchanges = {"BINANCE", "COINBASE", "KRAKEN"}
local bestPrice = 0
local bestExchange = ""
for i = 1, #exchanges do
local market = CreateMarket(exchanges[i], "BTC", "USDT", "")
local price = CurrentPrice(market).close
if price > bestPrice then
bestPrice = price
bestExchange = exchanges[i]
end
end
Log("Best price: " .. bestPrice .. " on " .. bestExchange)
Futures Contracts
Create Perpetual Market
local perpetual = CreateMarket("", "BTC", "USDT", "Perpetual")
Log(perpetual) -- Outputs: BTC_USDT_Perpetual
Best Practices
- Use BaseCurrency() and QuoteCurrency() to understand your current market
- Use CreateMarket() to build market strings for other markets
- Combine with Price() to get prices for different markets
- Check market validity before executing trades
- Log market information for debugging and monitoring
- Use for arbitrage by comparing prices across exchanges
- Filter trades by base or quote currency
- Handle futures contracts with the contractName parameter
Summary
- BaseCurrency(): Returns the base currency (what you're trading)
- QuoteCurrency(): Returns the quote currency (what you're pricing in)
- CreateMarket(): Creates market strings for different exchanges and contracts
- Common uses: Market identification, filtering, cross-exchange comparison, arbitrage
- Combine with: Unmanaged trading commands, price sources, and other market-related commands
- Important: Base is first (BTC), Quote is second (USDT) in BTC/USDT
These commands are fundamental for building strategies that work across multiple markets and exchanges. Master them to create flexible, multi-market trading strategies.