Gekko trading bot usage

Now that we installed Gekko, we need to feed it with data, and make it useful. Please, keep in mind that a trading bot is in NO WAY an insurance to earn money, as it will depend on the logic of your strategy, and on the markets (currency & asset) you will use.

We will follow 4 simple steps :

  1. Feeding your local Gekko database with market’s data so that we will be able to locally make some tests with strategies
  2. Run a backtest, which means testing a strategy on the local Gekko database we previously populated
  3. Run a livebot with traderPaper plugin, to simulate how a strategy would perform on a live market
  4. Launch the strategy, live, which means we will allow Gekko to really make buy/sell (long/short) orders

Feeding Gekko with market’s data

This part is quite simple and we will first use the UI to do it, click on “Local Data” on the top menu.

If you try to click on “Scan available data”, Gekko will complain, this is normal as we didn’t provision anything yet.

Now click on “Go to the Importer” button below.

For my test I will choose Kraken, the Euro currency, and Ethereum asset, and use the default daterange but you can change it.

Beware, dowloading data from markets as Kraken using -for now- an anonymous access will often trigger some kind of bandwith and access limitations, so the download rate will be low, and if you request a huge period of data, it may take a long time. Also, you could have to relaunch it several time, and reduce the timeframe each time to complete the previous downloaded one.

Now, Click on Import.

Gekko starts to “automagiclly” download the data from Kraken, and records it in its local database.

To use Gekko’s CLI (Command Line Interface, ie. without the UI) to make the same thing is quite easy:

  • Modify Gekko’s config file

cd <gekko_installdir>

cp sample-config.js config.js

edit config.js with your favorite editor

  • Now, as explained in official documentation, make sure the candleWriter plugin is enabled (around line 263 in the out-of-the-box config.js file), so that gekko will be able to record the data

config.candleWriter = {
enabled: true
}

  • Now check the conf around line 345, and configure what daterange you need to import

config.importer = {
daterange: {
// NOTE: these dates are in UTC
from: “2017-11-01 00:00:00”,
to: “2017-11-20 00:00:00”
}
}

  • Now run gekko and tell him to import data

node gekko –config config.js –import


Now, if we take a look back in gekko’s UI, we can see that it detects 2 importations:

The first one, is the one we imported using the UI and from Kraken. The second one is from binance (I stopped it before it reached 2017-11-20). This is because we didn’t modify the config.watch section in config.js:

config.watch = {

// see https://gekko.wizb.it/docs/introduction/supported_exchanges.html
exchange: ‘binance’,
currency: ‘USDT’,
asset: ‘BTC’,

// You can set your own tickrate (refresh rate).
// If you don’t set it, the defaults are 2 sec for
// okcoin and 20 sec for all other exchanges.
// tickrate: 20
}

You are free to modify it, just make sure that you use an allowed combination market/currency/asset. Checking the console logs when you lanch the CLI will help you to analyze any error Gekko could encounter.

Backtesting a strategy on local data

Now that we have a populated database, let’s give a try on a strategy. The goal here is not to explain you what’s the best strategy or how to tune it. We just want to check how it works.

First, we use the UI: in the Backtest menu, click “Scan available data” button.

We can once again see the two series of data we imported previously, good. Now, below on the same screen, let’s choose the MACD strategy. This is a quite standard indicator (as well as the other ones out-of-the-gekko-box) and the strategy implementing it is also quite simple as we will see in a next article.

Note that what is displayed on this screen can be changed:

  1. The strategy in the drop-down list are the ones in
    <gekko_installdir>/strategies/*.js files (eg. MACD.js for the MACD strategy)
  2. The parameters on the right are from the <strategy_name>.toml files in
    <gekko_installdir>/config/strategies
  3. The paperTrader settings we will review below are stored in the
    <gekko_installdir>/config/plugins/paperTrader.toml file

We change the candle size to 3 hours, and use 4 candles of history to “warmup” the strategy and provide the indicator some historical data.

Now let’s have a look at the paperTrader settings and change it to reflect Kraken fees:

And we launch the backtest ! Wait .. no Launch button ? Oh we forgot to select a dataset. Go back to the top of the page and click on Kraken box. Scroll down and click “Backtest”.

After a very short time, because of a simple strategy and a very short timeframe in the dataset, you will see 3 sections of results:


Those are the general statistics. Simulated profit here may sound bad (-24%), but actually it is better than the standard performance of the market if you did no actions with your 1 asset and 100 currency configured in the paperTrader settings (-41%). We can see here that the strategy actually performed 38 trades.

Here we can see the whole timeframe with BUY/LONG orders (green dots) and SELL/SHORT orders (red dots). You can zoom in. As you can guess, when you see a green dot just before a big market fall, this is not so good.

Finally you can see here a detailled view of each roundtrip (a buy+sell or sell+buy so that a PnL can be calculated).

If you want to use the CLI this is not so complicated.

Edit your config file (remember ?
<gekko_installdir>/config.js) and:

  • Make sur the config.tradingAdvisor section (tip: around line 33) is configured as you expect. Basically, this is were you provide the same parameters as we saw in the UI to the strategy you choose. Here we still use the MACD.

config.tradingAdvisor = {
enabled: true,
method: ‘MACD‘,
candleSize: 180, //unit is minutes
historySize: 4, //this is the historySize in the UI
}

// MACD settings:
config.MACD = {
// EMA weight (α)
// the higher the weight, the more smooth (and delayed) the line
short: 10,
long: 21,
signal: 9,
// the difference between the EMAs (to act as triggers)
thresholds: {
down: -0.025,
up: 0.025,
// How many candle intervals should a trend persist
// before we consider it real?
persistence: 1
}
};

Make sure the paperTrader is enabled, to actually simulate orders (tip: around line 65).

config.paperTrader = {
enabled: true,
// report the profit in the currency or the asset?
reportInCurrency: true,
// start balance, on what the current balance is compared with
simulationBalance: {
// these are in the unit types configured in the watcher.
asset: 1,
currency: 100,
},
// how much fee in % does each trade cost?
feeMaker: 0.16,
feeTaker: 0.26,
feeUsing: ‘taker’,
// how much slippage/spread should Gekko assume per trade?
slippage: 0.05,
}

And the performanceAnalyser should also be enabled, line 100.

config.performanceAnalyzer = {
enabled: true,
riskFreeReturn: 5
}

Remember ? Gekko will use the market, currency and asset defined at the beginning of the file, as we saw when we imported data using the CLI. I want to use the dataset from Kraken, so I’ll edit it:

config.watch = {

// see https://gekko.wizb.it/docs/introduction/supported_exchanges.html
exchange: ‘kraken’,
currency: ‘EUR’,
asset: ‘ETH’,

// You can set your own tickrate (refresh rate).
// If you don’t set it, the defaults are 2 sec for
// okcoin and 20 sec for all other exchanges.
// tickrate: 20
}

Now scroll down to line 332 to edit the timerange we want to use. I’ll stick with the ‘scan’ option as I have only one dataset for kraken, so no conflict or choice to do, and kraken will use the whole timeframe available. If I want to reduce the timefram, I just have to comment out the daterange: ‘scan’ line, and uncomment and modify the 4 next lines.

config.backtest = {
daterange: ‘scan’,
// daterange: {
// from: “2018-03-01”,
// to: “2018-04-28”
//},
batchSize: 50
}

Now, we launch it with

node gekko –config config.js –backtest

As you can see we get the same feedback as the one we had on the UI, without the graphical timeline of course.

Testing your strategy on a live market with a Live “paperTrader” Gekko

On the UI, click on the “Live Gekkos” top menu. The Market watchers are modules launched to gather live data from the markets you will configure for each Live Gekko. The strat runners are the paperTraders module: they analyze the market and use the STRATegy you defined on those data, to provide you feedback about what orders were simulated, and their results.

Here I choosed the MACD strategy, and configured it with 5 minutes candles, for this example, as I didn’t want to wait for hours to take screenshots 🙂

Note that on the right, we choose the PAper Trader option, as we want to simulate the strategy on the live market.

Now let’s configure Kraken’s fees on the paper Trader options below.

And we start it. A lot of things will change on the screen, but during the 10 first minutes we won’t see anything interesting as Gekko is gathering data, then it needs 1 candle of 5 minutes of history to start to eventually take decisions, depending on the market and your strategy and your strategy’s settings.

While we are waiting for the first information to appear, let’s try to come back on the “Live Gekkos” page.

So here I think we can see a little bug in the UI, as it says the duration since we launched the Market Watcher (which was automatically launched by Gekko) is 2h32mn which is wrong. I think there is a mismatch between UTC times and local time to calculate the duration. Not a big issue.

The Strat runner we launched is running, we can see that it did nothing yet: 0 trade, PnL (Profit and Loss) is 0, etc. You can click on its line to come back to the detailled view of this Strat runner.

After a few warmup minutes …

But we still can’t see any roundtrip, as Gekko only performed one trade for now (Amount of trades: 1 in the Runtime section). While we are waiting, please note that you can also manually check Gekko’s logs on the filesystem:

cd <gecko_installdir>/logs

ls -al

tail -f *

Now, I will stop this test (just click on the big red button inside the Strat runner section), and switch to the CLI to launch it manually, as I prefer to not depend on a CLI to perform operations.

To use the CLI is easy, as we already configured the appropriate sections in config.js and especially we enabled the config.paperTrader. IF we want to launch a real live Gekko on a market, the paperTrader will need to be disabled in the conf.

We just have to launch:

node gekko –config config.js

As running a livebot is supposed to be a very long activity, I don’t want to depend on my terminal (which could be unfortunately closed) and want it to run in the background. I’ll still be able to check and analyze the logs to understand what happened.

So we will create a small shell script to launch our livebot through PM2 as explained in the Gekko Installation page, and in PM2 documentation.

gekko@bitbot:~/gekko$ cat start_livetestbot-DEMA_PERSO_SL.sh

!/bin/bash

rm logs/*
pm2 start gekko.js –name TESTBOT_MACD -e logs/err.log -o logs/out.log — –config config-livetestbot.js

gekko@bitbot:~/gekko$

Now when we use
start_livetestbot-DEMA_PERSO_SL.sh to launch our livebot, it will run in the background until you stop it with pm2 <id> stop, and you can check its logs in <gekko_installdir>/logs

Running a “real live” tradebot

In the UI, let’s try to run a real Gekko on Kraken. Please note that we choosed “tradebot” on the right, and that there is no more paperTrader settings, with your amount of currency, assets, etc.

This is normal as a live tradebot will gather your amount of assets and available currency directly from your trading portal (Kraken, Binance, etc.). Yes … we want real trading with real money !

But please remember our quick test with the stock MACD strategy was not so good ! So you need a lot of tests, and to understand the strategies and how to improve them, if you want to really go “live”. And even with good backtesting results, those results will only be good on PAST data, not incoming data from the markets. Therefore, even with a very good strategy on past data, you could have a very bad strategy with new data (this is called overfitting).

After this gentle reminder, let’s try to click on theblue “Start” button.

Yes, this is normal, we want to go live, so we need to configure our trading platform’s credentials somewhere.

You can do it through the UI once again:

Let’s try to add junk data, and see where it ends. I won’t explain how to create an account on a trading platform, I suppose you already know how to do it and already have one. Otherwise, search a little bit.

gekko@bitbot:~/gekko_test/gekko$ cat SECRET-api-keys.json
{“kraken”:{“key”:”AAABBBBCCCC”,”secret”:”DDDEEEFFFF”}}
gekko@bitbot:~/gekko_test/gekko$

Okay so the fake data I used for the test ended up in <gekko_installdir>/SECRET-api-keys.json. This is a file you can edit without the UI to add several keys, for several accounts you may have on several platforms. The keys are then used by Gekko to connect to the platforms under your account and perform potential real buy/sell operations. Once again, take care and be warned ! You will most probably loose money !

Another needed step is to reviw a little bit our config.js file:

  • config.paperTrader needs to be disabled for the real trading plugin to work.

config.paperTrader = {
enabled: false,

  • You will also need to enable the config.trader section (I need to check if the keys in SECRET-api-keys.json are still mandatory if also put here:

config.trader = {
enabled: true,
key: ‘AAAABBBBCCCCC’,
secret: ‘DDDDDDDEEEEEFFFFFFF’,
username: ”, // your username, only required for specific exchanges.
passphrase: ” // GDAX, requires a passphrase.
}

Now you can launch your trading bot through the UI. OR by the CLI, exactly the same way we did for the paperTrader as there was no option on the command line, we just modified the configuration do disable a module and enable another one:

node gekko –config your-config-file.js

Or with PM2 as we saw before.

Please, remember this is a very risky game. You will most probably loose money or assets. Standards strategies are provided by Gekko’s creator as examples, they need to be tweaked and this is not easy. Also, at tartup, Gekko may directly try to sell or buy, even if the market is not good. This needs to be controled and fixed by coding in the strategies. You need to understand what you do, and when to do it. I CAN NOT BE RESPONSIBLE FOR YOUR LOSSES.

2 thoughts on “Gekko trading bot usage

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.