Long Gekko backtest causing econnect timeout solution

When I run backtests over 3-5 months that use multiple talib (yuck!) indicators I often get an econnect error. This was a major pain to fix, but seems to be to do with the socket timeout. Below is the fix:

Edit server.js and add the line in bold below the websocket server reference:

const wss = new WebSocketServer({ server: server });
wss._server.timeout = 50000000;

18 month speed back test

Not using Talib was a revelation to me, I was running back tests for hours and it was driving me mad. After working with the GA and the time back tests were taking I just gave up and instead started trying to work on native Gekko indicators.

The following back test took over 30 minutes to run with talib.

How to put a Stop Loss into your Gekko Strategy

This gets asked for a lot and is actually really easy to do, even for a noob coder like me.

In plain english the idea is to calculate a stop loss at the point of a buy and then trigger is alongside your sell logic.

In code terms it looks something like this:


//In the strategy init create the stoploss variable:
this.stop = "";


if(logic that determines a sell || this.stop != "" && price<this.stop){
       
//if you want to show a stoploss being triggered in the console:
        if(this.stop != "" && price<this.stop){
            console.log("stoplosss triggered - "+ this.stop);
        }

        this.direction="short";
        if(this.trend == "long"){
            this.stop = "";
            this.trend = this.direction;
            this.advice(this.direction);
        }
   
    }else if(logic that determines a buy){
        if(this.stop==""){
//sets up the stoploss, you should make the .2 a variable in the strategy config really
            this.stop = price-(price*.2);
            this.direction="long";
            this.trend = this.direction;
            this.advice(this.direction);
        }
    }

Because at the time of writing Gekko is an all or nothing bot, ie it trades 100% of current/asset per instruction, there is likely no need to expand upon this stop loss. However, once you can control the amount per trade you can start building in more complex stop loss rules based upon confidence levels in your strategies.

Avoid memory issues when running long backtests

Node has a memory limit of 1.5gig. In version 0.5.0 something caused long backtests (e.g. 16 month 1m candles) to fail with memory exhaustion.

Initially not using talib indicators seemed to be the fix. However, I’ve recently encountered the issue again when only using native indicators which is very annoying when running a GA over night…

Anyway, I am a noob, all I needed was to expand the memory. You do this by adding the flag listed below to the node command. Now, it seems all is good.

node –max-old-space-size=8192 server.js

 

Talib and windows

Talib is a nodejs library that Gekko can use to run more complex technical indicators that are not natively included in Gekko. Things like Bollinger Bands etc. But there are a couple of things to consider:

  1. Talib is just horrifically slow, so much so it basically renders using the Genetic Algorithm backtester pointless. I should note that the speed does not impact live trading, just backtesting.
  2. Talib is horrible to get working with Windows. Some people have achieved it and there is a useful thread here, but after the 3rd day I gave up. I recommend that you use Bash on Windows and install Nodejs and Gekko on that. It’s absolutely brilliant, and one of the best things Microsoft have ever created.

Here is a guide to installing Nodejs on Windows Bash.

Candleprops – huh?

For Talib strategies Gekko writes out the last 1000 candle data elements into arrays called Candleprops. I find these arrays incredibly helpful when writing strategies as you can do time based look backs easily rather than manually managing your own history arrays in a strategy.

If you want to use the candleprops array in a non talib using Strategy then do the following:

open plugins/baseTradingMethods.js and commend out the line below and the corresponding close bracket:

// if(this.asyncTick) {

Then go to your strategy and write console.log(this); in the update method and look for the candleprops arrays.

Note: baseTradingMethod is a core Gekko file so you should make this change only if you’re comfortable managing this change yourself.

How do get the candle timestamp in a strategy

I found this really helpful and used it when writing out backtest / live trading to a csv because I want to see the exact timestamp of a candle (typically used for comparison between candles and technical analysis outputs such as RSI, DMI etc).

First thing to do is to always use the strategy update method and not check. The update method has access to way more objects and properties than check. Then the reference is very simple and looks like:

candle.start.toDate();

 

 

Adding all gekko backtest tick data and indicators to csv

If you’re like me you want to see exactly what your indicators are doing vs the candle data. You can do this really easily by writing everything out to a csv file. Below is the simple instructions that you need to do to achieve this:

Writing CSV of strat output candles

Install fs

Add this to the top of the strat: var fsw = require(‘fs’);

Add this in .update under your logic etc I’ve used adx as an example:

grreadtime = candle.start.toDate();

headertxt = “date,price,adx,buys (USD),sells (BTC)\n”;

outtxt = grreadtime+”,”+ price+”,”+adxresult+”,”+buytime+”,”+selltime+”\n”;

if(headerset==””){

fsw.appendFileSync(this.fname, headertxt, encoding=’utf8′); headerset = “1”;

}

fsw.appendFileSync(this.fname, outtxt, encoding=’utf8′);

outtxt = “”;