This is the fifth article in a weekly series of tutorials explaining simple programming tasks on the Waves blockchain with the help of Google Blockly. We would like to thank Silas and Marc Jansen for writing this series of articles and are happy to reward their effort with a small grant from the WavesGo Education Fund.

This chapter explains how to program a simple trading bot. The idea is that the bot always sets the smallest sell order. For this, the bot looks for the smallest sell order currently available on the trading pair and undercuts it by, e.g., 10 wavelets, as buyers will always buy the cheapest sell order. If you would like to try it yourself, you can find the tool here

Implementation

First create a variable named “myLastPrice” and set it, to an extremely high value, e.g., 20,000 (to make sure we will immediately place a sell order). This value is always dependent on the respective Trading Pair, our example works on the Waves / BTC Trading Pair in testnet. Next, a while true loop is used, which is an endless loop, since we are continuous, as long as the bot is running, keep checking to see if someone has placed a cheaper sell order. This loop can be seen in Figure 1.

Figure 1: The variable “myLastPrice” is set to 20.000 and an endless loop is started.

Then it will be identified what the smallest sell order is. To do this, set the variable “currentHighestSellOrder” to the smallest sell order currently. To query the currently smallest sell order, a new block has been created, which is shown examplarily in Figure 2.

Figure 2: The block queries the smallest sell order.

This block contains two fields, in the first the asset id for which asset you want to have the price, in the second field you have to enter the asset id of the asset you want to pay for. Where to find this block is shown in Figure 3.

Figure 3: The menu in which one finds the block for querying the smallest or largest sell order.

After this, an if query is used to check if the variable “currentHighestSellOrder” is smaller than the variable “myLastPrice”. If that is the case, a sell order is placed. The corresponding block can be found in Figure 4.

Figure 4: The block for creating a sell order.

First, a seed is specified, which identifies the account from which the order should be placed. Then the asset id is used to indicate which asset you want to buy or sell. Furthermore, the asset id is indicated for the asset by which the order should be paid. At the end, the price and the number of assets is specified which should be bought or sold. This is done as usual in the smallest unit. Finally, it must be set whether you want to buy or sell with the order. In which category you find the block is shown in Figure 5.

Figure 5: You find the block to place an order in the category “Waves Transaction”

Finally, in case the lowest current sell order is below your last price, you set the sell order to the price “currentHighestSellOrder-10”, so that you are below the lowest order. When the sell order is set, the variable “myLastPrice” is set to “currentHighestSellOrder — 10”. This serves to ensure that the bot does not undercut itself. You can see the lower part in Figure 6 and the whole program code in Figure 7.

Figure 6: The part where the order is placed.

Figure 7: The complete code to the Trading Bot!

Limitations

As in the previous chapters, this is just an example. In practice, such a trading bot would not be implemented in a language like Blockly, but rather in Python or similar scripting languages. For this reason, in our last chapter, we will translate the appropriate Blockly program code into Python, connecting it to a programming language more commonly used for such things.

Find us online

Connect with us on social media to stay up to date:

Discord: https://discord.gg/cnFmDyA

Twitter: https://twitter.com/GoWavesgo

Facebook: https://www.facebook.com/WavesGo/

Email: go@wavesgo.com