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);

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

4 Replies to “How to put a Stop Loss into your Gekko Strategy”

  1. I have noticed you don’t monetize your blog, don’t waste your
    traffic, you can earn additional cash every month because you’ve got high quality content.

    If you want to know how to make extra bucks, search for: Mertiso’s tips best
    adsense alternative

Leave a Reply

Your email address will not be published. Required fields are marked *