Working with HaasScript
The HaasScript code editor is a text-based editor that allows you to create trade bot scripts using the Lua programming language. In this course, you will learn how to use the HaasScript code editor to create and edit trade bot scripts.
Setting up the code editor
To use the HaasScript code editor, you will need to install the TradeServer Enterprise or login to TradeServer Cloud and create a new Lua script
. To create a new script, click the Plus-sign (New Script) button in the top toolbar and give your script a name. This will open a new tab in the code editor.
Syntax
The HaasScript code editor uses the Lua programming language, which has a simple and easy-to-learn syntax. In Lua, you can use variables to store values, create functions to perform tasks, and use control structures such as if
statements and for
loops to control the flow of your code.
For example, here is a simple trade bot script that buys 100 units of an asset when the price is below a certain threshold:
local threshold = 100
local current_close = CurrentPrice().close
local pos_dir = GetPositionDirection()
if (pos_dir == NoPosition and current_close < threshold) then
PlaceBuyOrder(current_close, 100)
end
In this script, we define the threshold price and track the current close price and position direction. The if
statement checks whether the bot currently has no position and if the price went below our threshold. If both conditions are true, the script places a buy order of 100 units of the asset.
Script Parameters
Script input parameters are a way for users to customize the behavior of a HaasScript script without having to modify the script’s source code. They allow the user to input specific values, such as the number of periods to use for a moving average calculation, or to choose from a predefined set of options, such as the type of moving average to use.
In HaasScript, you can define script input parameters using the Input
, InputOptions
, and InputMaTypes
functions, to name a few.
The Input
function is used for inputting numbers, strings, and booleans.
local myInput = Input('A number', 20, 'Number of periods to use for calculation')
This creates an input parameter named “A number” with a default value of 20 and a tooltip of “Number of periods to use for calculation”.
The InputOptions
function creates an input parameter with a drop-down menu of fixed options.
local myInput = InputOptions('A option', 'Option1', {'Option1', 'Option2', 'Option3'}, 'Choose an option')
This creates an input parameter named “A option” with a default value of “Option1”, an option list of {‘Option1’, ‘Option2’, ‘Option3’} and a tooltip of “Choose an option”.
The InputMaTypes
function creates an input parameter with a pre-built drop-down menu for supported moving average types.
local myInput = InputMaTypes('MA Type', EmaType, 'Choose MA type')
This creates an input parameter named “MA Type” with a default value of EMAType and a tooltip of “Choose MA type”.
This is an example of a simple script that uses script input parameters:
-- Define script input parameters
local periodInput = Input('Periods', 20, 'Number of periods to use for calculation')
local maTypeInput = InputMaTypes('MA Type', EmaType, 'Choose MA type')
-- Pull close prices
local close = ClosePrices()
-- Use script input parameters in script
local ma = MA(close, periodInput, maTypeInput)
-- Plot the MA-line on the chart
Plot(0, 'MA', ma, Cyan)
In this script, we first define two script input parameters, one for the number of periods to use for the moving average calculation and another for the type of moving average to use.
We then use these input parameters in the script to produce the moving average, using the specified number of periods and moving average type. The script then plots the moving average on the chart as a line.
By using script input parameters, users of this script can easily customize the behavior of the script without having to modify the script’s source code – not to forget the ability to optimize settings in the HaasLabs.
Conclusion
With the knowledge gained from this course page, you should be well-equipped to create your own custom scripts and indicators using the HaasScript code editor. As you continue to work with the editor, you may want to refer to the official HaasScript documentation for further information and advanced features.