Operators
Operators and concatenating strings
Operators and Concatenating Strings
String concatenation joins two or more strings together using the .. operator. This is essential for creating log messages, trade reasons, and combining text with data in your HaasScript strategies.
The Concatenation Operator
HaasScript uses two dots (..) to concatenate strings:
local firstName = "John"
local lastName = "Doe"
local fullName = firstName .. " " .. lastName
Log(fullName) -- Outputs: John Doe
Basic Concatenation
Combining Strings
local part1 = "Hello"
local part2 = "World"
local message = part1 .. " " .. part2
Log(message) -- Outputs: Hello World
Adding Spaces
Concatenation doesn't add spaces automatically—you must include them explicitly:
local word1 = "Hello"
local word2 = "World"
-- Wrong: No space
local message1 = word1 .. word2 -- Outputs: HelloWorld
-- Correct: Include space
local message2 = word1 .. " " .. word2 -- Outputs: Hello World
Automatic Type Conversion
HaasScript automatically converts numbers to strings during concatenation:
local rsi = 25.5
local message = "RSI value: " .. rsi
Log(message) -- Outputs: RSI value: 25.5
This works with all numeric types:
local price = 45000
local percentage = 2.5
local count = 100
local message = "Price: " .. price .. ", Change: " .. percentage .. "%, Trades: " .. count
Log(message) -- Outputs: Price: 45000, Change: 2.5%, Trades: 100
Trading Use Cases
Trade Reasons with Indicator Values
local rsi = RSI(ClosePrices(), 14)
if rsi < 30 then
DoLong("RSI oversold: " .. rsi)
elseif rsi > 70 then
DoShort("RSI overbought: " .. rsi)
end
Combining Multiple Indicators
local rsi = RSI(ClosePrices(), 14)
local macd = MACD(ClosePrices(), 12, 26, 9)
local message = "RSI: " .. rsi .. " | MACD: " .. macd
Log(message)
Dynamic Labels
local market = BaseCurrency() .. "_" .. QuoteCurrency()
local strategy = "RSI Strategy"
local timeframe = "15m"
local label = strategy .. " - " .. market .. " - " .. timeframe
Log(label)
Price and Percentage Messages
local entryPrice = 45000
local currentPrice = 46200
local profitPercent = ((currentPrice - entryPrice) / entryPrice) * 100
local message = "Entry: " .. entryPrice .. " | Current: " .. currentPrice .. " | Profit: " .. profitPercent .. "%"
Log(message)
Position Status
local hasLong = HasLongPosition()
local entryPrice = GetEntryPrice()
if hasLong then
local message = "Long position at " .. entryPrice
Log(message)
end
Common Mistakes
Using the Wrong Operator
local text1 = "Hello"
local text2 = "World"
-- Wrong: Using + (only for numbers)
local message = text1 + " " + text2 -- Error!
-- Correct: Using ..
local message = text1 .. " " .. text2
Forgetting Spaces
local firstName = "John"
local lastName = "Doe"
-- Wrong: No space
local name = firstName .. lastName -- Outputs: JohnDoe
-- Correct: Add space
local name = firstName .. " " .. lastName -- Outputs: John Doe
Concatenating with Nil
local value = nil -- Undefined variable
-- Error: Cannot concatenate nil
local message = "Value: " .. value
-- Correct: Check for nil first
if value ~= nil then
local message = "Value: " .. value
else
local message = "Value: (not set)"
end
Concatenating Booleans
local isActive = true
-- Error: Cannot concatenate boolean directly
local message = "Status: " .. isActive
-- Correct: Convert to string
local message = "Status: " .. tostring(isActive)
Log(message) -- Outputs: Status: true
Multi-Line Concatenation
Build longer messages by chaining the .. operator:
local rsi = RSI(ClosePrices(), 14)
local macd = MACD(ClosePrices(), 12, 26, 9)
local price = ClosePrices()
local message = "=== Analysis ===" .. "\n" ..
"Price: " .. price .. "\n" ..
"RSI: " .. rsi .. "\n" ..
"MACD: " .. macd
Log(message)
Building Messages Conditionally
Add information to a message based on conditions:
local rsi = RSI(ClosePrices(), 14)
local message = "RSI: " .. rsi
if rsi < 30 then
message = message .. " (OVERSOLD)"
elseif rsi > 70 then
message = message .. " (OVERBOUGHT)"
end
Log(message) -- Outputs: RSI: 25.5 (OVERSOLD)
Best Practices
-
Use
..for concatenation - not+or other operators - Add spaces manually - concatenation doesn't add them automatically
- Check for nil - before concatenating potentially empty variables
-
Convert booleans - use
tostring()for boolean values - Use parentheses for complex concatenation to improve clarity
- Build incrementally - add parts to a message step by step
- Use meaningful text - clear, descriptive labels in log messages
Summary
-
Operator:
..joins strings together - Automatic conversion: Numbers convert to strings automatically
- Manual spaces: You must add spaces explicitly
- Type conversion needed: Booleans and nil require explicit conversion
- Common uses: Log messages, trade reasons, dynamic labels
- Avoid mistakes: Use correct operator, check for nil, handle types properly
String concatenation is simple but essential for creating informative messages in your trading strategies. Master the .. operator to communicate what your strategy is doing and why.