Use HaasScript text-based editor and SMA indicator for a simple trade bot

    • haasscript
    • pinescript
    • simple trade bot
    • sma indicator
    posted 3 years ago
    by Pshai
    Tutorial Featured Image


    In this quick and simple guide we will be going through the steps of using HaasScript and our text-based editor to generate signals with the SMA technical indicator, plotting the results on our charts, and backtesting the script to see how it performs with historical price data.

    Getting Started

    Before we get started you will need:

    • An active simple or advanced license for HaasOnline Trade Server
    • Hardware that meets our software's minimum specifications
    • Broadband internet

    If you don't have access to one or more of the above, you can choose to test on one of our demo servers. Please remember these machines are public and you should never add any private information.

    Creating a New Script

    First things first. On the left hand side we have our main menu panel. From there, we will select the [Script Editor] under [HaasScript].


    A whole new world opens. It's blank, but full of mystery. Let's see where we can create a new script.


    Aha! Here it is. Let's click it.


    A new window will pop up. Here you can give your script a name and description. The [Type] is set to [Script] by default, but there's more options and templates available. We'll stick with [Script] for now. Give your script a name and click [New].


    Configure HaasScript Settings

    The first thing we might want to do is to select the market we want to use our bot for. I have selected the market [BTC/USDT] in [Binance] and I will be using [5 minutes] as my main (default) interval.


    Adding Price Data & Indicators

    Okay! Let's start typing in some code! First thing we will need are the historical close prices. I will be assigning that to a local variable called [close]. (TIP: You don't have to do this, this is purely for simplicity's sake) The intellisense feature shows me suggestions of what I might be looking for while I'm typing. By highlighting the first selection and hitting [TAB], it will auto-complete the text.


    Okay awesome. We have our command and here we can see a tool-tip showing us what the command likes to eat. These are all optional parameters for this command so we don't have to define them. I will leave them as defaults and just close the parentheses. The interval for this price data will be the [Main Interval] which we set earlier.


    Next, we will most definitely want some indicators to bake our raw price data. I will be using two SMA's with the period lengths of 28 and 100.


    To see our SMA's as lines on our charts we will need to call the [Plot] command. I have done that as shown. After this, hit CTRL+R to run a quick backtest.


    Adding Trade Logic

    Nice! We have our SMA's on the chart. Currently there is no trading logic so no trades occurred during our quick backtest. Let's do that next!


    There we go. This is a very simple logic for buying and selling. It will buy when [smaFast] is above [smaSlow] and when [close] is above [smaFast] and sell when it is the opposite. Super simple, easy! Let's hit that CTRL+R again and see what happens!


    Nice! Now we got some actual results. In the [Compile Log] we can see the performance report for our script. It's not perfect, I know, but I'm sure you will know what to do to make it better.



    I hope this quick and simple guide gives you a better understanding of what HaasScript is capable of.

    • If you found this guide interesting, check out the guide where we recreate this strategy using the HaasScript visual designer
    • For the complete list of built-in commands and documentation of all our features, check out our HaasScript documentation
    • You can use HaasScript to recreate other popular strategies and indicators from languages such as PineScript

    If you enjoyed this guide or have comments please post them down below.

    — Pshai

Be the first to comment

The community content is generated by people from the community and is actively moderated. However, we do not endorse or claim to maintain any user generated content, links, tutorials, guides, projects, or questions that may appear in community sections.