Scripts vs Commands
Scripts vs Commands
Understanding the distinction between scripts and commands is fundamental to working effectively with HaasScript. These two concepts work together to create functional trading strategies.
What You'll Learn
Throughout this course, you'll learn:
- Essential built-in functions for data access, indicators, and trading
- How to structure scripts using built-in functions
- When to create custom commands for reusable logic
- Custom command creation with DefineCommand(), DefineParameter(), and DefineOutput()
- Best practices for organizing and sharing both scripts and commands
What is a Script?
A script is a complete trading strategy file that you create and deploy. It contains logic, calculations, and trading instructions that define how your bot behaves.
Script Characteristics
- Self-contained: A single file that runs independently
- Reusable: Can be deployed to multiple markets simultaneously
- Configurable: Uses input parameters to adjust behavior without code changes
- Persistent: Runs continuously until stopped or deactivated
- Exportable: Can be shared with other traders or imported into different accounts
Example Script Structure
-- Simple RSI Strategy (this is a script)
-- Define configurable parameters
local rsiPeriod = Input("RSI Period", 14)
local buyLevel = Input("Buy Level", 30)
-- Calculate indicators using commands
local rsi = RSI(ClosePrices(), rsiPeriod)
-- Execute trading logic using commands
if rsi < buyLevel then
DoLong("RSI oversold")
end
This entire file is one script that you save, test, backtest, and deploy.
What is a Command?
A command is a custom reusable code block that you create using HaasScript's command definition system. Once created, commands appear in your command/block list and can be used in both the text-based and visual editors.
Command Characteristics
-
User-created: You define commands using
DefineCommand()and related functions - Reusable: Can be used across multiple scripts
- Self-documenting: Include name, description, parameters, and output definitions
- Editor-agnostic: Work in both code and visual editors
- Organized: Appear in your personal command library for easy access
Command Structure
Custom commands use a specific structure:
-- Define the command
DefineCommand("MyCustomRSI", "Calculates custom RSI signal")
-- Define input parameters
local period = DefineParameter(NumberType, 'period', 'RSI period', true, 14)
local prices = DefineParameter(ListNumberType, 'prices', 'Price data', true, ClosePrices())
-- Command logic
local rsi = RSI(prices, period)
local signal = SignalNone
if rsi < 30 then
signal = SignalLong
elseif rsi > 70 then
signal = SignalShort
else
signal = SignalNone
end
-- Define output
DefineOutput(EnumType, signal, 'Trading signal output')
This custom command can now be used like any other HaasScript function, but requires a prefix:
local signal = CC_MyCustomRSI(14, ClosePrices())
DoSignal(signal)
How Scripts and Commands Work Together
Scripts use both built-in functions (pre-made HaasScript functions) and custom commands (your own reusable code blocks).
Think of it like a workshop:
- Built-in functions are the tools - hammers, saws, drills that come with the workshop (RSI, DoLong, ClosePrices, etc.)
- Custom commands are specialized jigs - you build these once and use them repeatedly for specific tasks
- Scripts are the projects - what you build using both tools and specialized jigs
Example: Using Both Built-in Functions and Custom Commands
-- This is a script (the entire file)
local rsiPeriod = Input("RSI Period", 14) -- Built-in function: Input()
-- Use a custom command you created
local signal = CC_MyCustomRSI(rsiPeriod, ClosePrices())
-- Use built-in functions to execute trades
if signal == SignalLong then
DoLong("Custom RSI signal") -- Built-in function: DoLong()
end
This script uses:
-
Built-in functions:
Input(),ClosePrices(),DoLong() -
Custom command:
CC_MyCustomRSI()- your reusable code block -
Built-in signal constants:
SignalLong
Built-in Functions
HaasScript includes 600+ built-in functions that you use directly in your scripts. These are pre-made and cover everything from data access to trading execution.
Common Built-in Function Categories
Trading Functions
-
DoLong(),DoShort(),DoExitPosition() -
StopLoss(),TakeProfit(),TrailingStopLoss()
Technical Analysis Functions
-
RSI(),MACD(),SMA(),EMA(),BBANDS()
Data Functions
-
ClosePrices(),HighPrices(),LowPrices(),Volume()
Helper Functions
-
Input(),Log(),Max(),Min(),Abs()
These are called "commands" in HaasScript terminology—they're built-in functions that you use directly.
Custom commands allow you to extend HaasScript's functionality and create your own reusable building blocks.
Key Differences
| Aspect | Scripts | Custom Commands | Built-in Functions |
|---|---|---|---|
| Purpose | Complete trading strategies | Reusable logic blocks | Specific tasks |
| Independence | Runs standalone | Called by scripts | Used directly |
| Creation | Written by you | Created with DefineCommand() | Pre-built in HaasScript |
| Deployment | Deployed to markets | Available in command list | Always available |
| Reusability | As complete files | Across multiple scripts | Across all scripts |
Practical Analogies
Workshop Analogy
- Built-in functions are basic tools - hammer, saw, drill, screwdriver
- Custom commands are specialized jigs - you build these for specific repetitive tasks
- Scripts are finished products - tables, chairs, cabinets you build using both
Programming Analogy
- Built-in functions are standard library functions - like Math.max() or Array.push() in JavaScript
- Custom commands are your own functions - reusable code you write and import
- Scripts are complete programs - main execution files that use both library and custom functions
Common Misconceptions
"Custom Commands Are Just Built-in Functions"
No. Built-in functions are pre-made and always available. Custom commands are code blocks you create yourself for reusability.
"I Need to Create Custom Commands for Every Strategy"
No. Most strategies work fine with just built-in functions. Create custom commands only when you have reusable logic you'll use across multiple scripts.
"Scripts Must Use Custom Commands"
No. Scripts can use built-in functions exclusively. Custom commands are optional and only useful when you have repetitive logic patterns.