LogoLogo
Back to HaasOnline.comSwitch to Developer API
4.x
4.x
  • Welcome
  • TradeServer Cloud
    • Overview
    • Security
    • Troubleshooting
  • TradeServer Enterprise
    • Overview
    • Updates
    • Security
    • Installation
      • Windows
      • MacOS
      • Linux
    • Configuration
      • License Key
      • Login credentials
      • Hosting
        • Linux VPS Hosting
    • Special features
      • Price history
      • Backups
      • Machine Learning
        • Built-in Machine Learning
        • Custom Machine Learning
      • Scripted Exchange API (Coming Soon)
      • Local API (Coming Soon)
    • Troubleshooting
  • Interface
    • General Interface
      • Left Menu
      • Upper Right Items
        • Account data
        • Execute trade
        • AI Chatbots
          • Chatbot Julia
          • Chatbot David
          • Chatbot Thomas
        • Notifications
        • Menu
          • Exchange accounts
          • Settings
          • License information
          • Change Logs
          • Logout
      • Status bar
        • Price tickers
    • Dashboard
    • Bots
      • Bot management
      • Bot templates
    • HaasScript
      • Web Editor
      • Backtest Lab
        • Lab test setup
      • Backtest History
      • Manage Scripts
      • Manage Signals
    • Tools
      • Marketview
      • Market intelligence
      • Exchange profiler
      • Markets explorer
      • News
    • Finance
      • Portfolio
      • External Wallet
  • Haasscript
    • About
      • Change Log
    • Tutorials
      • Bot building
        • Trade-bot building
        • Visual bot building
        • Scripted bot building
      • Usage
        • HaasScript
          • Interval
          • Markets
          • Input Fields
          • Memory Management
          • Signal Handling
          • Position Information
          • Positions Handling
            • Fee correction
          • Order Handling
          • Trading
          • Charting
          • Optimizations
        • Visual Editor
          • Blocks
          • Parameters
          • Flow Control
          • Interaction
        • Script Editor
          • Classes
        • Managed Trading
        • Unmanaged Trading
    • Commands
      • Trade Bots
        • TradeBot Containers
        • Easy Indicators
        • Easy Safeties
        • Easy Insurances
      • Helpers
        • Array Helpers
        • Custom Command Helpers
        • Enumerations
        • Equations
        • Mathematical
        • Memory Helpers
        • Signal Helpers
        • String Helpers
        • Miscellaneous
        • Technical Analysis Helpers
      • Input data
        • Bot Settings
        • Input fields
        • Price Market Information
        • Script Settings
        • Time Information
        • Trade Market Information
      • Prices
      • Technical Analysis
      • Charting
      • Trading
        • Managed Trade Commands
        • Unmanaged Trade Commands
        • Order Information
        • Order Handeling
        • Position Information
        • Position Price Information
        • Profit Information
        • Wallet Information
      • Social Media
    • Built-in Bots
      • Version 3 bots
        • C# Scripted Bot
        • Email bot
        • Trendline Bot
        • Triangle Arbitrage Bot
      • Accumulation Bot
      • Enhanced RSI bot
      • FlashCrash (Grid) Bot
      • Crypto Index Bot
      • Intellibot Alice
      • Inter Exchange Arbitrage Bot
      • MadHatter Bot
      • Market Making Bot
      • Order Bot
      • PingPong Bot
      • Scalper bot
      • Zone-Recovery Bot
    • Community Bots
      • Simple Grid Bot (SPOT)
      • Simple Grid Bot (FUTURES)
      • Simple Market Maker (SPOT)
      • Simple Market Maker (FUTURES)
  • Usage
    • First Usage
    • Setup Exchange
      • Adding an API to the Platform
      • Binance
      • Binance US
      • Bit2me
      • Bitfinex
      • Bitget
      • BitMEX
      • ❌Bitpanda
      • Bitstamp
      • ❌Bittrex
      • Bybit
      • ❌Cex.IO (Legacy API)
      • ❌Coinbase
      • ❌Crypto.com
      • Deribit
      • Gemini
      • Gleec
      • ❌HitBTC
      • Huobi
      • ❌Ionomy
      • Kraken
      • Kraken Futures
      • KuCoin
      • KuCoin Futures
      • LBank
      • ❌OKCoin
      • OKX
      • OKX-Futures
      • OKX-Swap
      • Phemex
      • Poloniex
      • Poloniex Futures
      • WooX
    • Setup Telegram Notification Bot
    • Setup Discord Notification Bot
    • Use Signals
  • Other Resources
    • YouTube
    • Guides & Tutorials
    • Questions & Answers
    • Community Projects
  • Need Help?
    • Ask on Discord
    • Submit Support Ticket
Powered by GitBook
On this page
  • Creating your first scripted bot
  • Step 1: Creating a scripted bot
  • Step 2: Scripting the bot
  • Adding insurances
  • Adding safeties

Was this helpful?

  1. Haasscript
  2. Tutorials
  3. Bot building

Scripted bot building

PreviousVisual bot buildingNextUsage

Last updated 1 year ago

Was this helpful?

Creating your first scripted bot

In this small tutorial, we are going to show you have you can create a very basic scripted bot.

Step 1: Creating a scripted bot

To create a scripted bot, you need to go to the left menu and select the "Web-editor". Once selected a new page will show up. On top of this page you will find a toolbar, click on the most left icon which display icon to create a new script. If all goes well then you will get a windows on your screen, asking you whats sort of script you like to create. Select script and give the script a name.

Step 2: Scripting the bot

In this case, we are going to make a scripted RSI bot.

Lets think wat we need to make a RSI bot...

First, we of course, need the RSI indicator. This RSI indicator needs to be setup with 2 variables. The prices and the period. You can use the ClosePrices() command to get the close prices and we can give this to the RSI. As for the period, lets create a input field for this using the Input() command.

We want to see the RSI, so we need to plot to the chart. This can be done using the Plot() command. The Plot commands asks us to give it a chart index, a line name, the data, and optionally the color.

And, we need some trade logic. This can be created by some simple if..else statements where the managed trading commands DoBuy() and DoSell() get used.

TIP: When scripting, pay attention to the intelli-text, which will show when typing.

If we convert this to a haasscript then we get;

-- Inputfields
local period = Input('Period', 14)
local buyLevel = Input('Buy level', 30)
local sellLevel = Input('Sell level', 70)

-- Getting required data
local cp = ClosePrices()

-- Calculations
local rsi = RSI(cp, period)

-- Plotting
Plot(1, 'RSI', rsi, SkyBlue)

-- Trading logic
if rsi > sellLevel then
  DoSell()
end

if rsi < buyLevel then
  DoBuy()
end

This example is enough to get a simple scripted RSI bot running.

Adding insurances

Just like in trade-bots, you can script your own insurances too.

For example, suppose we want to add a way to "never sell cheaper then we have bought". Well, that would result in a script like this:

-- Inputfields
local period = Input('Period', 14)
local buyLevel = Input('Buy level', 30)
local sellLevel = Input('Sell level', 70)

-- Getting required data
local cp = ClosePrices()
local lastBuyPrice = LastLongPrice() -- On spot this returns the last buying price
local priceNow = CurrentPrice().close

-- Calculations
local rsi = RSI(cp, period)

-- Plotting
Plot(1, 'RSI', rsi, SkyBlue)

-- Trading logic
if rsi > sellLevel then
  if priceNow > lastBuyPrice then -- This makes sure we never sell cheaper then we bought
    DoSell()
  end
end

if rsi < buyLevel then
  DoBuy()
end

Adding safeties

Again, just like in trade-bots, you can script your own safeties too.

For example, suppose we want to add a basic stop-loss. Well, that would result in a script like this:

-- Inputfields
period = Input('Period', 14)
buyLevel = Input('Buy level', 30)
sellLevel = Input('Sell level', 70)
stopLoss = Input('Stoploss', 5)

-- Getting required data
local cp = ClosePrices()
local profitsNow = GetCurrentROI()

-- Calculations
rsi = RSI(cp, period)

-- Plotting
Plot(1, 'RSI', rsi, SkyBlue)

-- Trading logic
if rsi > sellLevel then
    DoSell()
end

if profitsNow < -stopLoss then -- The stop-loss
  DoSell()
else
  if rsi < buyLevel then
    DoBuy()
  end
end