Building a Simple Trading Bot with the Trality Rule Builder (No Code!)


15 March 20207 min read

Table of contents

The Trality Rule Builder helps you create your own crypto trading bots without the need to code. We are developing this rule-based bot creation editor to be as simple as possible and as sophisticated as necessary. Simplicity is achieved through a drag and drop mechanism to create the logic of your bot. Sophistication is added through an ever-increasing set of features to provide you with the flexibility needed for successful trading.

Probably the easiest way to get started is by showing you how to create a simple trading bot, step by step. This way you can familiarize yourself with the basic concepts of the Rule Builder and we can point you to more advanced configuration techniques.

For demonstration purposes we will create a simple trend following strategy on a single currency pair, namely BTCUSDT.

Create A New Bot

Let's get started! To create a bot with our Rule Builder, we simply name our bot and select "Rule" as bot type. We then choose an exchange to trade on, as well as a quoted asset for our algorithm. For now we support Binance, but shortly more Exchanges will follow. The quoted asset represents the currency that all trades are being executed against.

Figure 1: Creating a new rule-based trading bot in Trality

Choose your Assets

Now we choose the base asset(s) we want our bot to run on, i.e. which coins should be bought and sold with our quoted asset. For our exemplary trading bot, we will simply select BTC, hence our bot will trade on the symbol BTCUSDT.

Figure 2: Selecting base assets

Add your Parameters and General Settings

Before we actually start to define the actual strategy of our bot, we want to define general settings for it. For a detailed description of all parameters and settings, head over to the Trality Docs and check out the Rule Builder section.

For now we just set the "Maximum buy amount" to 95% of the portfolio value. Meaning we will, at any given time, hold 5% of our quoted asset.

Figure 3: Defining base settings and parameters for a trading bot

Define your Strategy

Now comes the interesting part. At the core of every trading bot is strategy design. At this point you specify actual trading signals. More precisely your strategy is comprised of various predefined strategies that will generate buy or sell signals.

Our goal is to define a simple trend following strategy that tries to identify entry points to buy BTC when an upward trend is detected and sell BTC when a reverse trend is happening. A popular choice for such a signal is the crossover of moving averages. For a detailed explanation, please check our previous blog post which implements the same MA cross strategy using our Python Editor and the Docs for a technical explanation.

To add our first signal, we go to the strategy tab and press the add new button. In the pop up menu we select MA-Crossover and tick the expert box to adjust the details of this predefined strategy.

Figure 4: Adding a buy signal to a trading bot

Here we can specify the type of strategy, the signal type (buy or sell), the interval and indicator parameters as well as keeping a signal. For a detailed description of please refer to the documentation. To avoid confusion, please note that "MA-type" selected here is the Exponential Moving Average (EMA).

It is important to note, that specifying the buy or sell signal will automatically resolve the direction for the signal, or technically the combination of indicators. This means MA-Crossover BUY generates a buy order whenever the shorter EMA(20) crosses the longer EMA(40) from below. Equally, MA-Crossover SELL sends a sell order when the long EMA(40) crosses the EMA(20) from above.  

We select the MA-Crossover on an hourly interval with EMA(20) and EMA(40) for both buy and sell types. The strategy overview shows our strategies with the signals they trigger, their names and intervals they run on. As a result we obtain the following preliminary strategy:

Figure 5: Building a simple EMA Cross strategy for a trading bot

Backtest your Strategy

A backtest is a historical simulation of your trading strategy. It can help you understand performance and risk in different market conditions. Please be aware, that history does not necessarily repeat itself. Besides,  there are some common mistakes in backtesting to keep in mind. We should think of backtesting as a tool for rejecting bad strategies, rather than validating good ones.

For our purpose we will test our bot on different timeframes and various scenarios, to find potential drawbacks of our algorithm. As a start we simply backtest our bot for the last 12 months. The backtest result of our strategy can be found in the right panel:

Figure 6: Backtesting a trading bot in Trality

It seems that trends are identified quite well by our trading bot. It outperforms a simple buy and hold strategy by far. Furthermore, running our strategy on the last 1 month, 3 months and 6 months also shows comparatively good results (not shown here - why don't you check for yourself). Also by looking at other pairs like ETHUSDT, this basic strategy seems to outperform a simple buy and hold strategy.

This seems a bit too good to be true. So let us look at a more difficult time period. We consider the first quarter of 2019 (01.01.2019 - 01.04.19). This period is characterized by wild swings and massive price jumps in BTCUSDT. We run the backtest and check our entry and exit signals in the symbols tab.

Figure 7: A second backtest with buy and sell signals

As we can see, while the market is up ~8% we are down ~5%. Two problems seem to arise for our strategy. First, in times of fast oscillating prices our EMA signals lie very close to each other and hence we open and close positions fairly often - accumulating trading fees and losses. Secondly, during these fast price swings our EMA cross sell signal tends to lag behind and we exit trades when momentum is already lost.

In the next step we refine our strategy and try to address the problems mentioned above.

Refine your Strategy

Early exit with RSI

In this section we try to find a way how to exit the market earlier to avoid the drawback mentioned previously. To do so we can look at the RSI strategy which relates the average up move to the average down move on a given time horizon. As a result, this signal can be used to measure the strength of an up versus down move. We add the RSI sell signal as follows:

Figure 8 Adding an RSI sell signal to our trading bot

Now we combine the two sell signals with Boolean logic by simply mouse-dragging one over the other. The resulting strategy then looks as follows. As we can see we can easily combine signal using any or and operations.

Figure 9: Refining the EMA Cross strategy with an RSI signal

Rerunning our refined strategy for the same 12 month period shows performance improvements.

Figure 10: Another backtest

However, testing for the first quarter 2019, the RSI signal does change our exit points at all and performance stays the same. Nevertheless it seems reasonable to combine trend direction (MA-Crossover) with an additional exit signal that measures strength of movement. Choosing the right moving average window as well as exit value for the RSI may actually depend on market volatility.

Cooldown Period for Buy Orders

We therefore want to find a way to reduce the number of successive buy signals. To do this we add a "cooldown" period for buy orders to our strategy. This can be helpful to avoid opening and closing positions too often in sideways markets. Setting the cooldown period to 8 hours, we prevent our bot to enter successive long positions within 8 hours.

On our 12 month backtest time window this restriction actually costs performance. In spite of reducing the number of trades and saving fee costs, it seems that the bot is prevented from entering into profitable trades.

Figure 11: Final backtest for today

Nevertheless, the cooldown period actually reduces our losses achieved within our testperiod from 01.01.2019 - 01.04.2019 to -4%.

In conclusion, we see that testing on different time frames as well as choosing different starting points is critical to understanding bot performance. Refining your strategy sometimes comes with tradeoffs that need to be carefully considered.

Research, Improve and Go Live

Above, we designed a very simplified example of how to set up a trading bot with Trality, and we are conscious about the fact that it does not constitute proper strategy research. In fact, as Lopez de Prado wrote in his seminal book on financial machine learning:

A common misunderstanding is to think of backtesting as a research tool. Researching and backtesting is like drinking and driving. Do not research under the influence of a backtest.

Nevertheless, we hope that our illustrations have convinced you how easily your investment ideas can be transformed into tradable strategies on Trality, which can then be modified, configured and tested in an intuitive way. Once confident with your strategy creation, deploying a trading bot for live trading is just as simple.

Disclaimer: Backtests are not indicative of future results, the above article is merely an opinion piece and does not represent any kind of trading advice or suggestions how trading bots or trading algorithms can or should be used.