LUA's text-manipulation methods
Lua's Text-Manipulation Methods
In addition to HaasScript's built-in string commands, you can use Lua's standard library string functions. These provide additional text manipulation capabilities for your trading strategies.
Lua String Functions
| Function | Description |
|---|---|
string.len() |
Gets the length of a string |
string.upper() |
Converts string to uppercase |
string.lower() |
Converts string to lowercase |
string.sub() |
Extracts a substring |
string.rep() |
Repeats a string |
string.reverse() |
Reverses a string |
string.format() |
Formats strings (like printf) |
string.gsub() |
Global substitution |
string.byte() / string.char()
|
Character conversions |
string.len()
Gets the length of a string.
string.len(value)
-- Short form: #value
Basic Usage
local text = "Hello World"
local length = string.len(text)
Log(length) -- Outputs: 11
-- Short form
Log(#text) -- Outputs: 11
Trading Use Case
local market = "BTC_USDT"
if #market > 8 then
Log("Market name is long")
end
string.upper()
Converts all characters to uppercase.
string.upper(value)
Basic Usage
local text = "hello"
local upper = string.upper(text)
Log(upper) -- Outputs: HELLO
Trading Use Case
local market = "btc_usdt"
local upperMarket = string.upper(market)
Log("Trading: " .. upperMarket) -- Outputs: Trading: BTC_USDT
string.lower()
Converts all characters to lowercase.
string.lower(value)
Basic Usage
local text = "HELLO"
local lower = string.lower(text)
Log(lower) -- Outputs: hello
Trading Use Case
local market1 = "BTC_USDT"
local market2 = "btc_usdt"
if string.lower(market1) == string.lower(market2) then
Log("Markets match (case-insensitive)")
end
string.sub()
Extracts a substring. Note: Lua uses 1-based indexing, unlike HaasScript's SubString which uses 0-based.
string.sub(value, start [, end])
Basic Usage
local text = "Hello World"
local substring = string.sub(text, 1, 5)
Log(substring) -- Outputs: Hello
From Position to End
local text = "Hello World"
local fromPos = string.sub(text, 7)
Log(fromPos) -- Outputs: World
Trading Use Case
local market = "BTC_USDT"
local base = string.sub(market, 1, 3) -- BTC
local quote = string.sub(market, 5) -- USDT
Log("Base: " .. base .. ", Quote: " .. quote)
string.rep()
Repeats a string a specified number of times.
string.rep(value, n)
Basic Usage
local text = "BTC"
local repeated = string.rep(text, 3)
Log(repeated) -- Outputs: BTCBTCBTC
Trading Use Case
local separator = string.rep("=", 20)
local message = separator .. "\nTrade Report\n" .. separator
Log(message)
-- Outputs:
-- ====================
-- Trade Report
-- ====================
string.reverse()
Reverses a string.
string.reverse(value)
Basic Usage
local text = "Hello"
local reversed = string.reverse(text)
Log(reversed) -- Outputs: olleH
Trading Use Case
local text = "12345"
local reversed = string.reverse(text)
Log(reversed) -- Outputs: 54321
string.format()
Formats strings using format specifiers (similar to printf in C).
string.format(format, ...)
Basic Format Specifiers
| Specifier | Type | Example |
|---|---|---|
%d |
Integer | string.format("%d", 42) |
%f |
Float | string.format("%f", 3.14) |
%.Nf |
Float with N decimals | string.format("%.2f", 3.14159) |
%s |
String | string.format("%s", "text") |
Basic Usage
local price = 45000.50
local message = string.format("Price: %.2f", price)
Log(message) -- Outputs: Price: 45000.50
Formatting Multiple Values
local price = 45000
local rsi = 25.5
local volume = 1000
local message = string.format("Price: %d, RSI: %.1f, Volume: %d", price, rsi, volume)
Log(message)
-- Outputs: Price: 45000, RSI: 25.5, Volume: 1000
Formatting Percentages
local profit = 2.666666
local message = string.format("Profit: %.2f%%", profit)
Log(message) -- Outputs: Profit: 2.67%
Currency Formatting
local price = 45000.50
local message = string.format("$%.2f", price)
Log(message) -- Outputs: $45000.50
Scientific Notation
local value = 15000000
local message = string.format("%.2e", value)
Log(message) -- Outputs: 1.50e+07
Padding and Alignment
local text = "BTC"
local padded = string.format("[%10s]", text)
Log(padded) -- Outputs: [ BTC] (right-aligned, 10 chars)
local leftPadded = string.format("[%-10s]", text)
Log(leftPadded) -- Outputs: [BTC ] (left-aligned, 10 chars)
string.gsub()
Global substitution - replaces all occurrences of a pattern.
string.gsub(value, pattern, replacement)
Basic Usage
local text = "Hello World"
local replaced = string.gsub(text, "World", "HaasScript")
Log(replaced) -- Outputs: Hello HaasScript
Replacing Multiple Occurrences
local text = "BTC, ETH, LTC"
local replaced = string.gsub(text, ",", " - ")
Log(replaced) -- Outputs: BTC - ETH - LTC
Removing Characters
local text = "Price: $45,000.50"
local cleaned = string.gsub(text, "[,$]", "")
Log(cleaned) -- Outputs: Price: 45000.50
Trading Use Case
local message = "Entry: LONG at 45000 - Exit: SHORT at 46000"
local standardized = string.gsub(message, "LONG", "Long")
Log(standardized)
-- Outputs: Entry: Long at 45000 - Exit: Short at 46000
string.byte() / string.char()
Convert between characters and their numeric values.
string.byte(value [, index])
string.char(code1, code2, ...)
Basic Usage
local text = "A"
local code = string.byte(text)
Log(code) -- Outputs: 65 (ASCII code)
local character = string.char(65)
Log(character) -- Outputs: A
Comparison with HaasScript Commands
| Lua Function | HaasScript Command | Difference |
|---|---|---|
string.sub() |
SubString() |
Lua uses 1-based, HaasScript uses 0-based |
string.len() |
N/A | Use #value or string.len(value)
|
string.find() |
StringIndexOf() |
Similar functionality |
| N/A | StringContains() |
HaasScript-specific |
| N/A | StringExplode() |
HaasScript-specific |
| N/A | StringJoin() |
HaasScript-specific |
Common Patterns
Formatting Trade Logs
local entryPrice = 45000.50
local exitPrice = 46200.75
local profit = ((exitPrice - entryPrice) / entryPrice) * 100
local log = string.format("Entry: %.2f, Exit: %.2f, Profit: %.2f%%",
entryPrice, exitPrice, profit)
Log(log)
-- Outputs: Entry: 45000.50, Exit: 46200.75, Profit: 2.67%
Cleaning Data
local priceText = "$45,000.50"
local clean1 = string.gsub(priceText, "[,$]", "")
local clean2 = string.gsub(clean1, " ", "")
Log(clean2) -- Outputs: 45000.50
Standardizing Text
local market = "btc_usdt"
local standardized = string.upper(market)
Log(standardized) -- Outputs: BTC_USDT
Creating Separators
local separator = string.rep("-", 30)
Log(separator)
Log("Trade Report")
Log(separator)
-- Outputs:
-- ------------------------------
-- Trade Report
-- ------------------------------
Best Practices
-
Use
string.format()for complex formatting with decimals -
Use
#valuefor simple length checks (shorter thanstring.len()) -
Use
string.gsub()for text cleaning and standardization -
Use
string.upper()/lower()for case-insensitive comparisons -
Remember indexing:
string.sub()uses 1-based,SubString()uses 0-based -
Use
string.rep()for creating visual separators in logs - Combine with HaasScript commands for maximum flexibility
Summary
- string.len() / #: Get string length
- string.upper()/lower(): Change case
- string.sub(): Extract substrings (1-based indexing)
- string.rep(): Repeat strings
- string.reverse(): Reverse strings
- string.format(): Format with specifiers (very useful for numbers)
- string.gsub(): Global substitution
- string.byte()/char(): Character conversions
Lua's string functions complement HaasScript's built-in commands. Use them together for comprehensive text manipulation in your trading strategies.