HaasScript Reference¶
Complete reference documentation for HaasScript, the Lua-based scripting language for automated cryptocurrency trading on Haasonline TradeServer.
Overview¶
HaasScript is a Lua-based scripting language that enables you to create custom trading strategies and indicators. Scripts are executed on each price tick or time interval, allowing you to:
- Access real-time and historical price data
- Calculate technical indicators (RSI, MACD, Bollinger Bands, etc.)
- Generate trading signals (long, short, exit)
- Manage positions and orders
- Implement risk management (stop loss, take profit)
- Create custom visualizations and charts
Type System¶
Core Data Types¶
HaasNumberCollection
The primary data type for numeric operations in HaasScript. Represents both single numbers and arrays of numbers (time series data).
Indexing a HaasNumberCollection with [N] does not return a single value like a regular Lua table. Instead, it returns a spliced sub-collection of all values from index N onwards (toward the oldest).
| Expression | What it returns |
|---|---|
rsi[1] |
The entire collection (splice from index 1). |
rsi[3] |
A spliced collection of all values from index 3 to the oldest (skipping indices 1 and 2). |
ArrayGet(rsi, 3) |
A single value at index 3. |
Index 1 is always the newest (current) value, and higher indices go further back in time.
local rsi = RSI(ClosePrices(), 14)
-- Comparison uses the latest value by default
if rsi < 30 then
DoBuy()
end
-- Getting a splice (for further calculations on older data)
local rsiPast = rsi[3] -- All RSI values from index 3 onward (skipping the 2 newest)
-- Getting a single value
local singleValue = ArrayGet(rsi, 3) -- The single RSI value at index 3 (2 ticks ago)
HaasSignal
Represents trading signals (long, short, exit). Generated by signal helper commands or managed trading actions.
HaasEnum
Enumeration type for constants (MA types, signal types, order types, etc.).
Primitive Types
nil- Absence of a value.boolean-true/falsevalues.number- Lua number (floating point).string- Text data.table- Key-value data structure. Note that time series data usesHaasNumberCollectioninstead, which has different indexing behaviour (see above).function- Callable function reference.userdata- External data (e.g. PositionContainer objects).
Execution Model¶
HaasScript bots execute in two phases:
1. Initialization Phase¶
Runs once when the bot starts. Use this phase to:
- Define input fields with
Input()commands - Set default configuration
- Initialize variables
2. Update Phase¶
Runs on each price tick or configured interval. Use this phase to:
- Fetch latest price data
- Calculate indicators
- Generate trading signals
- Manage positions
Example Script:
-- Simple RSI strategy
local period = Input("period", 14) -- Initialization
local prices = ClosePrices() -- Update phase
local rsi = RSI(prices, period)
if rsi < 30 then
DoLong() -- Buy signal
elseif rsi > 70 then
DoShort() -- Sell signal
end
Common Patterns¶
Getting Price Data¶
local closePrices = ClosePrices()
local highPrices = HighPrices()
local lowPrices = LowPrices()
local volume = GetVolume()
Technical Indicators¶
-- RSI (Relative Strength Index)
local rsi = RSI(ClosePrices(), 14)
-- MACD
local macd = MACD(ClosePrices(), 12, 26, 9)
local macdValue = macd.macd
local signalLine = macd.signal
local histogram = macd.hist
-- Moving Averages
local sma = SMA(ClosePrices(), 20)
local ema = EMA(ClosePrices(), 20)
Managed Trading¶
-- Simple long/short signals
if buyCondition then
DoLong() -- Enter long position
elseif sellCondition then
DoShort() -- Enter short position
end
-- Easy risk management
sl = StopLoss(2.0) -- 2% stop loss
tp = TakeProfit(5.0) -- 5% take profit
tsl = TrailingStopLoss(3.0) -- 3% trailing stop
exitCondition = sl or tp or tsl
-- Exit position
if exitCondition then
DoExitPosition()
end
Working with Arrays¶
HaasNumberCollection indexing works differently from a regular Lua table. This is a common point of confusion.
-- Regular Lua table: t[3] returns a single value
local t = {0, 1, 2, 3, 4}
Log(t[3]) -- 2
Log('t: '..t) -- ERROR: attempt to concatenate a table value
-- HaasNumberCollection: rsi[3] returns a splice (all values from index 3 onwards)
local rsi = RSI(ClosePrices(), 14)
Log(rsi[3]) -- [38.19, 42.93, 48.14, ...] (all values from index 3)
Log('RSI: '..rsi) -- RSI: 38.19 (concatenation coerces to a single value)
local splice = rsi[3] -- Splice: all values skipping the 2 newest
local single = ArrayGet(rsi, 3) -- Single value at index 3 (2 ticks ago)
Use ArrayGet() (or alias ArrayIndex()) when you need a single value at a specific position. These commands also work with Lua tables.
local prices = ClosePrices()
-- Get specific index (1 = most recent)
local currentPrice = ArrayIndex(prices, 1)
local previousPrice = ArrayIndex(prices, 2)
-- Filter
local highPrices = FilterAbove(prices, 100)
Memory and State¶
Save values between executions.
-- Load with default value
local tradeCount = Load('tradeCount', 0) -- Returns 0 if key doesn't exist
-- Increment and save
tradeCount = tradeCount + 1
Save('tradeCount', tradeCount)
Command Index¶
HaasScript provides 579 functions organized into 6 logical domains:
Advanced Features¶
| Category | Functions |
|---|---|
| Charting | 30 |
| Custom Commands Helpers | 8 |
| Input Fields | 23 |
| Input Settings | 6 |
| MAchine Learning | 2 |
| Settings | 24 |
| Social Media | 5 |
98 functions total — View full documentation →
Data & Prices¶
| Category | Functions |
|---|---|
| Price Data | 16 |
| Price Market Information | 12 |
| Profit Information | 6 |
| Time Information | 19 |
| Wallet | 10 |
63 functions total — View full documentation →
Constants¶
| Category | Enumerations |
|---|---|
| Enumerations Array filter Types | 4 |
| Enumerations Candle Pattern | 65 |
| Enumerations Charting | 31 |
| Enumerations Color | 20 |
| Enumerations Data Types | 12 |
| Enumerations Margin Mode | 2 |
| Enumerations Moving Averages | 9 |
| Enumerations Order Types | 9 |
| Enumerations Parameter Type | 9 |
| Enumerations Position | 3 |
| Enumerations Position Mode | 2 |
| Enumerations Signal | 9 |
| Enumerations Signal Types | 7 |
| Enumerations Source Price Types | 8 |
| Enumerations Trading | 3 |
| Enumerations Trading LR | 4 |
197 enumerations total — View full documentation →
Helpers & Utilities¶
| Category | Functions |
|---|---|
| Array Helpers | 22 |
| Equations | 15 |
| Flow Control | 5 |
| Mathematical | 49 |
| Memory Helpers | 11 |
| Miscellaneous | 9 |
| String Helpers | 10 |
121 functions total — View full documentation →
Technical Analysis¶
| Category | Functions |
|---|---|
| Easy Indicators | 50 |
| Technical Analysis | 98 |
| Technical Analysis Helpers | 9 |
157 functions total — View full documentation →
Trading & Positions¶
| Category | Functions |
|---|---|
| Easy Insurances | 14 |
| Easy Safeties | 19 |
| Order Handling | 8 |
| Order Information | 8 |
| Position Information | 14 |
| Position Prices | 11 |
| Signal Helpers | 24 |
| Trade Actions (Managed) | 5 |
| Trade Actions (Unmanaged) | 8 |
| Trade Bot | 4 |
| Trade Market Information | 25 |
140 functions total — View full documentation →
Best Practices¶
Performance¶
- Minimize API calls - Cache price data and indicator results
- Use appropriate periods - Longer indicator periods (1-450) are supported
- Avoid complex loops - Scripts timeout after 2 minutes
Risk Management¶
- Always use stop losses - Protect against unexpected moves
- Test thoroughly - Use backtesting before live trading
- Start small - Test with small position sizes
Debugging¶
- Use Log() commands - Output values for debugging
- Check execution logs - Review bot logs for errors
- Enable debug mode - Get detailed execution traces
Additional Resources¶
- Online Documentation: https://help.haasonline.com/haasscript/general/
- Community: Haasonline Discord
This documentation is auto-generated from the HaasScript codebase. To regenerate, run: