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: a series of numbers, newest value first. Every command whose return type is documented as HaasNumberCollection returns one: price data, indicators and most Mathematical commands (Abs, Add, Max, Round, …), even for a single number: Abs(-5) is a one-value collection, not a Lua number. Commands documented as returning number (e.g. GetHigh, GetLow, Sd) already return a plain Lua number.
local prices = ClosePrices() -- HaasNumberCollection
local rsi = RSI(prices, 14) -- HaasNumberCollection
-- Comparisons, arithmetic and string concatenation work directly,
-- using the newest value
if rsi < 30 then DoLong() end
Log('RSI: ' .. rsi)
-- A plain Lua number: ArrayIndex (alias ArrayGet), 1-based, 1 = newest
local currentRsi = ArrayIndex(rsi, 1)
local previousRsi = ArrayIndex(rsi, 2)
-- rsi[2] is NOT a number: it is the series shifted one bar back
local previousSeries = rsi[2]
A collection is Lua userdata, so it does not work where Lua requires a real number: tonumber(rsi) returns nil and math.floor(rsi) raises "number expected, got userdata". Take the value out with ArrayIndex(x, 1) first. Only use it on a collection: given a plain number (e.g. the result of Sd) it returns nil. There are no .Value / .Values fields.
Parameter types and conversion
number|tableandHaasNumberCollectionparameters accept a number, a Lua array or a HaasNumberCollection. A Lua array is read like a collection: index 1 is the newest value.- A
numberparameter given a collection uses its newest value. - When two collections of different lengths are combined (
Add(a, b),a - b), the result has the length of the shorter one. ...number|table(and the other...types) means the parameter takes any number of arguments:Max(1, 5, 9).
Containers
Commands documented as returning dynamic with a Container fields table return named fields, readable by name or 1-based index. Field names are case-sensitive. An Element fields table instead means the command returns a Lua array of containers: read the fields on each element (list[1].market), not on the array.
local macd = MACD(ClosePrices(), 12, 26, 9)
local signalLine = macd.signal -- same as macd[2]; macd.Signal is nil
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
number- Lua number (floating point)string- Text databoolean- true/false valuestable- Lua table (e.g.StringExplodereturns an array of strings)function- Lua function (callback)dynamic- depends on the input or on what was stored; see the command's description
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 macdLine = macd.macd
local signalLine = macd.signal
local histogram = macd.hist
-- Moving Averages
local sma = SMA(ClosePrices(), 20)
local ema = EMA(ClosePrices(), 20)
Managed Trading (Recommended)¶
-- Simple long/short signals
if buyCondition then
DoLong() -- Enter long position
elseif sellCondition then
DoShort() -- Enter short position
end
-- Exit position
if exitCondition then
DoExitPosition()
end
-- Easy risk management
StopLoss(2.0) -- 2% stop loss
TakeProfit(5.0) -- 5% take profit
TrailingStopLoss(3.0) -- 3% trailing stop
Working with Arrays¶
local prices = ClosePrices()
-- Get specific index (1-based, 1 = most recent; 0 is an error)
local currentPrice = ArrayIndex(prices, 1)
local previousPrice = ArrayIndex(prices, 2)
-- Filter
local highPrices = FilterAbove(prices, 100)
Memory and State¶
-- Save values between executions
Save("myKey", ArrayIndex(rsi, 1))
local previousRsi = Load("myKey")
Command Index¶
HaasScript provides 570 functions organized into 6 logical domains:
Advanced Features¶
| Category | Functions |
|---|---|
| Charting | 30 |
| Custom Commands Helpers | 8 |
| Input Fields | 22 |
| Input Settings | 6 |
| Machine Learning | 3 |
| Settings | 24 |
| Social Media | 3 |
96 functions total — View full documentation →
Data & Prices¶
| Category | Functions |
|---|---|
| Price Data | 16 |
| Price Market Information | 12 |
| Profit Information | 6 |
| Time Information | 19 |
| Wallet | 11 |
64 functions total — View full documentation →
Constants¶
| Category | Enumerations |
|---|---|
| Enumerations Array filter Types | 4 |
| Enumerations Candle Pattern | 65 |
| Enumerations Charting | 31 |
| Enumerations Color | 20 |
| Enumerations Contract Type | 3 |
| Enumerations Data Types | 11 |
| Enumerations Margin Mode | 2 |
| Enumerations Moving Averages | 9 |
| Enumerations Order Types | 8 |
| 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 |
198 enumerations total — View full documentation →
Helpers & Utilities¶
| Category | Functions |
|---|---|
| Array Helpers | 21 |
| Equations | 15 |
| Flow Control | 4 |
| Mathematical | 49 |
| Memory Helpers | 11 |
| Miscellaneous | 8 |
| String Helpers | 10 |
118 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 | 13 |
| Easy Safeties | 18 |
| Order Handling | 8 |
| Order Information | 8 |
| Position Information | 14 |
| Position Prices | 9 |
| Signal Helpers | 23 |
| Trade Actions (Managed) | 5 |
| Trade Actions (Unmanaged) | 8 |
| Trade Bot | 4 |
| Trade Market Information | 25 |
135 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: