In this tutorial I’ll show you how to setup an automated trading bot that buys ETH on Stellar at a discount and sells it at a premium. This describes market making in a nutshell. It can be done for fun or for profit and it adds liquidity to Stellar’s Distributed Exchange, which is a good thing for Stellar. We’ll use Kelp- an open source, all-purpose trading bot developed by the Stellar Development Foundation.
When you make market, your bot will be holding variable amounts of two assets and trading between them back and forth. This is basically how all market makers work- whether it’s a set algorithm like Uniswap or something you have full control over like Kelp. So you should pick assets you’re comfortable holding. For this tutorial we’ll make the ETH/XLM market but you can easily adapt the config for other pairs.
You also need a reference price feed or ticker that tells you the current market price of asset A in relation to asset B. Our bot will be setting its offers based on this so it needs to be representative of the crypto market. Big exchanges work well and also provide the tickers for free. For this tutorial we’ll use Binance, but again Kelp allows you to use any exchange you like.
Exchanges typically only support a few markets (asset pairs) for a given token. This is the other constraint to keep in mind when picking which Stellar market to make. For ETH, Binance offers ETH/USDT, ETH/BTC, and XLM/ETH. Thankfully Stellar’s universal marketplace has anchored assets for USD, BTC, and ETH so we could even run additional market makers for these as well!
Let’s get started!
We’ll run our trading bot on a Ubuntu VM on AWS. Kelp itself is supported on all the major platforms.
Our bot will be running on the Stellar live network. If you’re following along please be careful. Start conservatively with a small amount. And as always, never trade more than what you can afford to lose.
Kelp uses CCXT to pull tickers from exchanges. So let’s first download the version of CCXT-Rest used by Kelp here: https://github.com/stellar/kelp/releases/tag/ccxt-rest_v0.0.4
wget https://github.com/stellar/kelp/releases/download/ccxt-rest_v0.0.4/ccxt-rest_linux-x64.zipunzip ccxt-rest_linux-x64.zip
Then we launch CCXT which will run as a HTTP server on port 3000. Binance does not require API keys to pull market tickers so we’re all set with our price feed.
Next we’ll download and extract the latest stable release of Kelp from its repository: https://github.com/stellar/kelp/releases
wget https://github.com/stellar/kelp/releases/download/v1.9.0/kelp-v1.9.0-linux-amd64.tartar xvf kelp-v1.9.0-linux-amd64.tar
To run Kelp we have to give it a main configuration file and a strategy file. Kelp comes built in with several strategies that are customizable and ready to use. For market making we’ll use the Buy Sell strategy.
This is our main config file main.cfg with comments removed for brevity. We’ll explain the important parts below. You can find the entire file in our sample Github repo:
TRADING_SECRET_SEED="<your account seed>"ASSET_CODE_A="XLM"ASSET_CODE_B="ETH"
ASSET_CODE_A and ASSET_CODE_B should match the Exchange ticker for your price feed. Because we’re using Binance’s XLM/ETH ticker, our “Asset A” should be set to XLM and “Asset B” set to a Stellar anchored ETH asset. This basically means our price reference is “buying ETH with XLM”.
One of the nice things about Stellar’s distributed exchange is there’s no shortage of options for anchored assets. For this tutorial we’ll use the Stellarport ETH asset which is issued by GBVO…225PL5
The other configuration options I left as default.
TRADING_SECRET_SEED is set to the private key of our Stellar account that Kelp will use to trade. It must have trustlines for any anchored asset we trade, which is Stellarport ETH in our case. To add a trustline in NiceTrade you can use the Add Trustline button in the Account section.
Our account also must have some balance for at least one of the two assets we’re making market for. I loaded my account with 30 XLM (the base asset).
Next let’s create our Buy/Sell Config file buysell.cfg. Again, comments are removed for brevity. We’ll break down this config piece by piece.
First, we need to point Kelp to the data feed we’ll use on CCXT, which is Binance XLM/ETH for this tutorial. This is what the DATA_ configuration are for.
AMOUNT_OF_A_BASE is the base asset (XLM) amount that will be scaled by the level config and we set that to 1.
Next we setup Levels which are tiers of bid and ask offers. As we mentioned before, market makers buy assets at a discount and sell them at a premium to the market price. Kelp’s Buy Sell strategy achieves this by setting Bid offers at a lower price than market price and Ask offers at higher prices. The difference or the spread determines their profit.
Professional market makers typically setup more multiple levels of bid and asks to take advantage of volatile market movements. Each additional level has a higher spread and so they typically also have higher asset amounts for even greater profit.
For our first level, our bid and ask offer will be set 0.025% from the middle, which equates to a 0.5% spread. We set our base asset multiple as 1X, meaning the offers will sell or buy 1 XLM.
Our second level has 1% spread and 2X multiple, so the offers will sell or buy 2 XLM. Our third level has 2% spread and 2X multiple.
And that’s it. Let’s start running Kelp and get some tea while it diligently trades for us!
./kelp trade -c main.cfg -s buysell -f buysell.cfg
We can see Kelp in action placing 3 tiered offers in the XLM/Stellarport ETH market. Because our account currently only has XLM the offers are one sided in the order book. At the current time, the price of XLM/ETH on Binance is ~0.000220.
We check back after a while, and it looks like our Kelp has already successfully made its first pair of profitable trades! It used 1.19 XLM to buy ETH at a small discount and soon after sold ETH back at a small premium, netting a small gain of 0.011 XLM.
When you make market, you place offers for assets at a price you set that can be consumed by other users of the Stellar distributed exchange. As a result you add liquidity to Stellar while being compensated by the spread. As a market maker, you risk exposure holding both assets in whatever market you’re making. This tends to work better when the market moves sideways but could leave you over holding one asset if market moves sharply in one direction.
Unlike something like Uniswap, Kelp gives you a lot of control over your own trading strategy. You could adjust the spread and base asset amount to your personal risk threshold. Unlike other distributed exchanges, it’s virtually free to transact on Stellar so feel free to experiment with your Kelp trading bot configurations. Kelp and Stellar makes it easy for you to start small and scale up later.
That wraps up this tutorial. This content was created as part of the Kelp Overview StellarBattle. If you have questions about this tutorial please reach out to me on Keybase @dexter0x8. Kelp is a powerful trading bot with a lot to offer. Its strategies could be used for market making, ICO token distribution, price discovery, and more! Check out its repository for more: https://github.com/stellar/kelp