📰 Latest: HaasOnline Academy Is Back — Structured Education for Smarter Trade Bots
Account
Strings

Built-in text-manipulation commands

Built-in Text-Manipulation Commands

HaasScript provides several built-in commands specifically designed for string manipulation. These commands help you parse, search, split, and join text data in your trading strategies.

HaasScript String Commands

Command Description
StringContains() Checks if a substring exists within a string
StringExplode() / StringSplit() Splits a string into an array based on a delimiter
StringFromQuery() Parses query strings (URL parameters)
StringIndexOf() Gets the position of a substring within a string
StringJoin() Joins two strings with an optional separator
SubString() Extracts a portion of a string

StringContains

Checks whether a specified substring occurs within a string.

StringContains(value, searchValue, [ignoreCase])

Basic Usage

local text = "BTC_USDT is trading"
local hasBTC = StringContains(text, "BTC")

if hasBTC then
    Log("Market involves BTC")
end

Case-Insensitive Search

local text = "Bitcoin is trading"
local hasBTC = StringContains(text, "bitcoin", true)  -- true = ignore case

if hasBTC then
    Log("Found bitcoin (case-insensitive)")
end

Trading Use Case

local market = BaseCurrency() .. "_" .. QuoteCurrency()
local preferredMarkets = "BTC,ETH,LTC"

if StringContains(preferredMarkets, BaseCurrency()) then
    Log("Trading preferred market")
end

StringExplode / StringSplit

Splits a string into an array of substrings based on a delimiter.

StringExplode(value, delimiter)
-- Alias: StringSplit(value, delimiter)

Basic Usage

local text = "BTC,ETH,LTC"
local markets = StringExplode(text, ",")

-- markets is now an array: {"BTC", "ETH", "LTC"}
Log(markets[1])  -- Outputs: BTC
Log(markets[2])  -- Outputs: ETH

Parsing Market Names

local market = "BTC_USDT"
local parts = StringExplode(market, "_")

local baseCurrency = parts[1]   -- BTC
local quoteCurrency = parts[2]  -- USDT

Log("Base: " .. baseCurrency)
Log("Quote: " .. quoteCurrency)

Splitting Multiple Values

local data = "45000,25.5,100"
local values = StringExplode(data, ",")

local price = tonumber(values[1])
local rsi = tonumber(values[2])
local volume = tonumber(values[3])

Log("Price: " .. price .. ", RSI: " .. rsi .. ", Volume: " .. volume)

Splitting by Spaces

local message = "RSI MACD EMA"
local indicators = StringExplode(message, " ")

-- indicators: {"RSI", "MACD", "EMA"}
for i = 1, #indicators do
    Log("Indicator: " .. indicators[i])
end

StringFromQuery

Parses query strings (URL parameters) by splitting on '&' and '='.

StringFromQuery(value)

Basic Usage

local queryString = "symbol=BTC_USDT&interval=15m&strategy=RSI"
local parsed = StringFromQuery(queryString)

-- Returns an object with the parsed data
Log(parsed.symbol)     -- BTC_USDT
Log(parsed.interval)   -- 15m
Log(parsed.strategy)   -- RSI

Trading Use Case

local settings = "market=BTC_USDT&rsi=14&leverage=5"
local config = StringFromQuery(settings)

local market = config.market
local rsiPeriod = tonumber(config.rsi)
local leverage = tonumber(config.leverage)

Log("Config: " .. market .. ", RSI: " .. rsiPeriod .. ", Leverage: " .. leverage)

StringIndexOf

Gets the zero-based index of the first occurrence of a specified substring.

StringIndexOf(value, searchValue, [ignoreCase])

Basic Usage

local text = "Hello World"
local index = StringIndexOf(text, "World")

Log(index)  -- Outputs: 6 (zero-based index)

Finding Substring Position

local market = "BTC_USDT"
local underscorePosition = StringIndexOf(market, "_")

if underscorePosition > 0 then
    Log("Underscore found at position: " .. underscorePosition)
end

Case-Insensitive Search

local text = "Bitcoin Trading"
local index = StringIndexOf(text, "BITCOIN", true)  -- true = ignore case

Log(index)  -- Outputs: 0 (found at start)

Trading Use Case

local logMessage = "Entry: LONG at 45000 - RSI: 25.5"
local rsiPosition = StringIndexOf(logMessage, "RSI:")

if rsiPosition > 0 then
    Log("RSI information found in log")
end

StringJoin

Concatenates two strings with an optional separator.

StringJoin(value1, value2, [separator])

Basic Usage

local part1 = "BTC"
local part2 = "USDT"
local market = StringJoin(part1, part2, "_")

Log(market)  -- Outputs: BTC_USDT

Joining Without Separator

local text1 = "Hello"
local text2 = "World"
local combined = StringJoin(text1, text2, " ")

Log(combined)  -- Outputs: Hello World

Trading Use Case

local base = BaseCurrency()
local quote = QuoteCurrency()
local market = StringJoin(base, quote, "_")

local strategy = "RSI Strategy"
local timeframe = "15m"
local label = StringJoin(strategy, timeframe, " - ")

Log("Trading: " .. market)
Log("Strategy: " .. label)

Joining Dynamic Values

local indicator = "RSI"
local value = "25.5"
local message = StringJoin(indicator, value, ": ")

Log(message)  -- Outputs: RSI: 25.5

SubString

Extracts a substring from a string by start position and length.

SubString(value, start, length)

Basic Usage

local text = "Hello World"
local substring = SubString(text, 0, 5)

Log(substring)  -- Outputs: Hello

Extracting Currency Codes

local market = "BTC_USDT"
local baseCurrency = SubString(market, 0, 3)    -- BTC
local quoteCurrency = SubString(market, 4, 4)   -- USDT

Log("Base: " .. baseCurrency)
Log("Quote: " .. quoteCurrency)

Extracting Fixed-Width Data

local data = "4500025.5100"  -- Price, RSI, Volume (fixed positions)
local price = SubString(data, 0, 5)    -- 45000
local rsi = SubString(data, 5, 4)      -- 25.5
local volume = SubString(data, 9, 3)   -- 100

Log("Price: " .. price .. ", RSI: " .. rsi .. ", Volume: " .. volume)

Extracting from Specific Position

local message = "Entry: LONG at 45000"
local priceText = SubString(message, 15, 5)  -- Extract "45000"

Log("Price: " .. priceText)

Common Patterns

Parsing Market Data

local market = "BTC_USDT"
local parts = StringExplode(market, "_")

local base = parts[1]
local quote = parts[2]

Log("Trading " .. base .. " against " .. quote)

Building Labels

local strategy = "RSI"
local timeframe = "15m"
local label = StringJoin(strategy, timeframe, " - ")

Log(label)  -- Outputs: RSI - 15m

Searching Log Messages

local log = "Entry: LONG - RSI: 25.5, MACD: 15.2"

if StringContains(log, "RSI") then
    Log("Log contains RSI information")
end

local macdPosition = StringIndexOf(log, "MACD")
if macdPosition > 0 then
    Log("MACD found at position: " .. macdPosition)
end

Extracting Data

local data = "BTC,ETH,LTC,XRP"
local markets = StringExplode(data, ",")

for i = 1, #markets do
    Log("Market " .. i .. ": " .. markets[i])
end

Best Practices

  1. Use StringContains for simple existence checks
  2. Use StringExplode to parse delimited data (CSV, spaces, etc.)
  3. Use StringJoin to build strings with separators
  4. Use StringIndexOf when you need the position of a substring
  5. Use SubString for fixed-width extraction
  6. Use StringFromQuery for URL parameter parsing
  7. Check array bounds after using StringExplode
  8. Handle case sensitivity with the ignoreCase parameter
  9. Convert to numbers with tonumber() when extracting numeric data

Summary

  • StringContains: Check if a substring exists
  • StringExplode/Split: Split strings into arrays
  • StringFromQuery: Parse URL query strings
  • StringIndexOf: Find position of substrings
  • StringJoin: Join strings with separators
  • SubString: Extract portions of strings

These built-in commands handle most common string manipulation tasks in HaasScript. Use them to parse market data, build messages, and process text in your trading strategies.