HaasScript Tutorial: First Script

This week we have something new and shiny to take a look at. It is a bit of a platform pivot and our first look at a new scripting language that is dedicated to the crypto space. Readers might be familiar with HaasOnline from their popular crypto-currency trading bots but perhaps few realize that they have a scripting language too. HaasScript is its name and it supports trading on a wide range of exchanges out of the box. This in itself is very appealing and as such, it is something which deserves a closer look over the coming months. Note: In the interests of full disclosure, HassOnline has provided a free license to evaluate HaasScript. However, they have not offered any incentive or made any request for an article to appear on Backtest Rookies. So let’s begin!

Alpha Preview

It should be noted that this article was written on a development release. As such, certain aspects could be subject to change in future releases. Having said that, an effort has been made to provide correct information at the time of writing. Additionally, it would be surprising if the platform and language radically change in the near future.

HaasScript Tutorial Scope

Regular readers might not be surprised to find out that we will follow the same tradition as our PineScript, QuantConnect and Backtrader first-script tutorials. This means that we are going to mirror the exact same strategy again. It provides an easy comparison for beginners who are looking to select a platform. The strategy we will develop is a simple RSI (Relative Strength Index) strategy that attempts to buy when oversold and simply exit the long position when the RSI is overbought. On our journey through this post we will cover the following core concepts:
  • Creating a new script and accessing the editor
  • Adding an Indicator
  • Accessing Indicator values
  • Buying crypto with DoLong()
  • Selling crypto with a DoExitPosition()


Before we begin, let’s talk a little about HassOnline. It is a little different to the other platforms usually covered on this site as it is a fully subscription-based product. There isn’t a completely free tier that will allow users to sharpen their teeth before committing to a full purchase. Having said that, I believe there is a limited free trial that will at least allow you to take a quick look before committing to a subscription. One thing to note is that although the name is HassOnline, it is not actually a cloud service. Subscribers will need to download the Haas Trading Server (HTS) which runs locally on your own machine. This is great if you want don’t want to be sending your API keys to an online service but you will be responsible for keeping the strategy running. Power cuts, laptops sleeping and system crashes happen from time to time and you should be prepared to tackle these issues. Fortunately, there does appear to be some functionality within the server to continue where a strategy left off following an unexpected shutdown. Having said that, it has not been tested here yet and so judgment on how well that works will be reserved for later.

Haas Jargon

Although not entirely required to follow this tutorial, it is worth noting that the world of Haas has a few interesting bits of Jargon that are used to refer to “modules” within a bot. These modules are like lego bricks you can put together to produce interesting combinations and custom trading bots with little programming knowledge. In fact, there is even a visual editor to allow you to create bots without programming at all. These terms do popup with HaasScript and therefore, they are definitely worth a quick mention here.
  • Indicators: Probably nothing controversial here. Indicators produce signals that can be used to make trading decisions. Just like any other platform. Indicators can be stacked easily to create confluence (where all indicators must agree to make a trade), consensus (where the majority of indicators must agree) or even generate signals by user-defined weighting.
  • Safeties: This is where the language starts to get a bit more flavourful. Safeties mostly cover snippets of code which fall into the position management and strategy category. Some examples include setting stop losses and take profits, limiting trading to a single direction and deciding when to deactivate a bot. Perhaps, confusingly, some safeties can actually be used to generate trading signals too. There are a lot of different safeties and as such, it could be worth a little further reading here: https://help.haasonline.com/docs/trade-bots/safeties
  • Insurances: This is a category of functionality generally to filter trades based on certain criteria. For example, filtering trades where the expected profit does not overcome the exchange fee or filtering trades in sideways moving markets. At a guess, the name insurances may have been coined for this group of modules because they might stop you from making silly mistakes. However, don’t let the naming give you a false sense of security. safeties will not guarantee safety and insurances will not dig you out of danger. They will provide some useful, re-usable code though that can be creatively applied if you understand what they are doing.


In the interests of keeping this article within a reasonable length, we will not cover the downloading and installation of the trade server. HassOnline has some pretty clear documentation on how to do this here: https://help.haasonline.com/docs/getting-started/installation Additionally, we will not cover the post-installation setup. This means using the startup wizard to connect to your preferred exchanges (known as price drivers) and set up some accounts. See here for more information: https://help.haasonline.com/docs/getting-started/configuration/price-drivers-and-accounts If you are not comfortable adding API keys to your accounts at this stage, you can choose to create a simulated account (paper trading) for testing. Do this using the pane on the right-hand side of the screen and then ticking the simulated checkbox like so. Picture showing the simulated account option in the trade server settings

Accessing HaasScript

Once you are all set up, accessing HaasScript can be done via the panel on the left-hand side. Simply click on the Script Editoroption to get started. When the editor opens, you will see a screen that might look intimidating at first, but is actually fairly straightforward and divided into clear sections. A picture showing the script editor that is clearly divided into three sections The left-hand column, (highlighted in pink) is used for changing settings related to the backtest. Here, you will be able to decide things like which exchange to perform the backtest on, which timeframe, the assumed fee amongst other things. Eventually, when your HaasScript has inputs, the inputs will appear here too. The next column, highlighted in blue is where we do our work. I.e open and close projects, write scripts and edit code. The final column is all for output after running a backtest. The column is broken down into two main windows. A console which can provide debugging information and a chart area which allows you to visually inspect what is happening with your strategy.

The editor

In order to run the example code, you will first need to create a new script then copy and paste the example code below. To do this;
  • Click on “New Script”A picture showing where to click to create a new script
  • A wizard will open that provides a few templates that can be used to kickstart your script. We will leave this as default. Just fill in a name for the script and optionally, a description.A picture showing how to fill out the wizard form
  • Hit “New” and then simply paste the example code into the editor and you should be left with something that looks like this.A picture showing the script editor after pasting our example code into it
Ok, now we have the code in the editor, let’s move onto the main course!

Example Code

-- Create an input for RSI length
local rsiLength = Input('RSI Period', 14)

local c = ClosePrices()

local MyRSI = RSI(c,rsiLength)

-- == Charts ==
PlotHorizontalLine(1, '', DarkGray, 70) -- Horizontal lines
PlotHorizontalLine(1, '', DarkGray, 30)
Plot(1, 'RSI', MyRSI, Aqua)

-- == Trading Logic
if GetPositionDirection() == NoPosition then
    if MyRSI < 30 then -- Enter Long when RSI is oversold
        DoLong('Enter long')
elseif GetPositionDirection() == PositionLong then
    if MyRSI > 70 then -- Exit Long when RSI is overbought
        DoExitPosition('Exit Long')

Code Commentary

On the first line of our script, we have a comment. All comments start with --. For those completely new to programming, a comment is just like writing a note in a notebook. We often write them above some code to explain your intentions. Following this, we create an input for the script using the input()function. This input will allow a user to toggle script settings without altering the code. In our example, we allow the user to adjust the RSI length. The value selected by the user is then saved in the rsiLength variable. Next, we need to get some price data for our RSI indicator. We do this by calling a function which will return closeprices to us. The is a little different from other platforms which might have special built-in variables for open,high,low and closebut it makes a lot of sense. By using a function users are able to select a few additional options for how they would like their data to be delivered. Hover over the function in the editor and you will see options like market, intervaland more. Concluding the setup, we create and plot out RSI indicator. There is a built-in function we can call to create our RSI indicator. All we have to do is pass it the length and close prices we created in the previous steps. Plotting is straightforward. We just tell it where to plot, give the plot a title and then pass it our RSI indicator to plot. The plot number corresponds to where we want to plot. If we selected 0 the RSI would appear on the main chart. and 1is a chart beneath. In our example code, we selected 1.


Once we are set up, all we need to do is execute some trades! To do this we checkifour RSI indicator value is below 30. When it is, a DoLong()function is called and enters the trade for us. The trade size is set using the size parameter on the settings panel on the left-hand side. If you are already long and still under 30 on the following bar, a second trade will not be placed. This avoids you accidentally amassing a large position. Finally, to exit the position, we do the same again. This time, we just make a check to see if we are over 70. If so, we call DoExitPosition()and this will exit our current position (if one exists!).

Running your script

Hit the play icon on the script editor to run a quick backtest. When this happens you should notice that:
  • The console prints a lot of output. This gives you the backtest results:A picture showing the backtest results
  • An input now appears in the settings pane for the RSI length. This means that any inputs you add will not be available until you run the code once.A picture showing that some settings do not appear until after you run a script for the first time.
You can now play around with settings like the timeframe, RSI length, fees, and the crypto pair to see how these variables affect your results.

Getting Help and Further Reading

To understand the syntax, edit scripts and ultimately write your own algorithms some further reading will be needed. You can find a syntax guide here: https://help.haasonline.com/api/haasscript/using-haasscript Additionally, it is worth noting that the editor supports code completion. So if you have something in mind that you want to do, try typing it into the editor. You will find that a lot of functions have logical names and can be code completed with suggestions. A picture showing code completion in the script editor. Finally, take a look at other example scripts by hitting the “load” button script edit. Here you will be able to load and learn from other working examples. A picture showing how to load scripts to learn from others examples  

Find This Post Useful?

If this post saved you time and effort, please consider donating a coffee to support the site!  

Dontate with PayPal using any payment method you are comfortable with.