Proper stop losses coming soon
#1
[EDIT]
I'm working hard on implementing native trailing stop losses! Follow this PR: https://github.com/askmike/gekko/pull/2429
[/EDIT]

Stop losses are a very big part of trading successfully, whether it is automated trading or not. Gekko currently does not support proper stop losses (unless users implement those themselves in their strategies, which is far from ideal) due to the complexities around different exchanges API capabilities (most do not support native stop losses at all).

Now Gekko v0.6 is out I am planning to enhance Gekko Broker with proper stop losses while giving strategies a way to include stop losses when giving advice. I'm working on Gekko Plus where I am taking Gekko to the next level and I feel this has to include proper stop losses.

What is a stoploss?

A stoploss is an order designed to get out of a position that might turn the wrong direction, limiting risk. Imagine you are using Gekko to trade on USD/BTC and your strategy sees an opportunity to buy. It will trigger a buy advice and Gekko will gladly buy BTC with your USD. But what if your strategy turned out wrong and the price of BTC tanks? With a stoploss you can limit your potential loss to say 1% by automatically selling the BTC once the price drops 1%. Stop losses are not perfect: If there is a small dip in the price you will sell, even though the price might go up after.

Why are there currently no stop losses?

There are two big reasons for this:

1. Up until recently Gekko had a lot of limitations in the communication between strategies and the module responsible for executing (simulated or real) trade orders at the exchange. Gekko v0.6 introduced a new event system that allows for bi-directional communication needed to update the strategy in case a stop loss gets executed.
2. Not all exchanges support stop losses natively, in cases exchanges don't Gekko needs a way to offer them (by watching the price and trading whenever it hits X). Since v0.6 Gekko comes with a library that can be extended to support this: Gekko Broker.

What will Gekko support in the future?

I haven't fully figured this out yet (which is why I am posting this thread). But this is what I am currently thinking:

- introduce the concept of triggers such as:
  - stop loss (create an order when the price goes AGAINST your direction).
- trailing stop loss (same as above the the price trigger will move with the price, slowly securing more profit).
  - take profit (create an order when the price goes IN your direction - and you want to secure your profit).
- allow for strategies to specify how to execute the trigger, such as:
  - market order
  - sticky order
  - limit order

There is already a "tradeCompleted" event which can be extended to include information about what caused it (an advice, a stoploss or takeprofit trigger). As for now triggers can be passed along with "LONG" (buy) advice signals as to end a roundtrip early (before the strategy adviced to "SHORT" (sell)).

I'm still thinking about this execution part, I might split this design into two phases: first with only triggers and second with more order types.

What do you think?
  Reply
#2
I like the idea of adding more order types. These are the ones Binance lists  in their API Docs

Order types:
  • LIMIT
  • MARKET
  • STOP_LOSS
  • STOP_LOSS_LIMIT
  • TAKE_PROFIT
  • TAKE_PROFIT_LIMIT
  • LIMIT_MAKER

There is also some code that looks like it might help with adding stop loss to any order located here:
https://github.com/askmike/gekko/pull/1317/files

I have modified a simple strategy that uses an EMA cross to identify an exit which seems to function similarly to a trailing stop loss.

I will end up posting that strategy separately when I have verified a couple settings for optimal conditions.

Thanks again for all your work ...

TC Mabe
  Reply
#3
Quote:There is also some code that looks like it might help with adding stop loss to any order located here:
https://github.com/askmike/gekko/pull/1317/files

That code hooks into very old Gekko code, unfortunately we have to start from scratch implementing them in Gekko Broker instead.

Quote:I have modified a simple strategy that uses an EMA cross to identify an exit which seems to function similarly to a trailing stop loss.

Having stop losses at the strategy level is great, but it's not perfect for a few reasons: the biggest one being that the strategy can only check once every X minutes whether it should trigger a stop (X being the configured candle size). Besides that if we use Gekko Broker we can also use native stop losses for exchanges that support them (like on binance).
  Reply
#4
For my understanding: Isn't a stop-loss decision from the broker similar to a manual sell execution? It "disturbs" the backtested strategy. I also sometimes thought that a bigger candle size does not match the need for quick decisions, but a candle size of e.g. 1hr still fits best into the overall strategy. What about a variable candle size, e.g. after a long position, switch into a five minute candle size, in this case Gekko will be very reactive to losses. I can imagine some new events to reconfigure Gekko without restarting will be useful.
  Reply
#5
Quote:What about a variable candle size, e.g. after a long position, switch into a five minute candle size, in this case Gekko will be very reactive to losses. I can imagine some new events to reconfigure Gekko without restarting will be useful.

I am afraid this will add to much complexity to both aligning these different candles (in gekko's codebase) as well as the strategy API (we need to explain to everyone: "in THIS situation your update function in your strategy runs once every hour, but after THIS event it runs every 5 minutes, so be sure not to miss the event and register it properly").

Quote:I also sometimes thought that a bigger candle size does not match the need for quick decisions, but a candle size of e.g. 1hr still fits best into the overall strategy.

Stop losses will be optional (driven by the strategy which hopefully pulls this out of the strategy parameters). So if you don't want them you don't have to use them. Note that a lot of users have been requesting them, and they are considered basic building blocks for most trading systems as they are an extremely easy and simple way to manage risk.

The thing about stop losses is: If you run hourly candles and all of a sudden there is a price crash of 5% happening within 5 minutes the stoploss pulled you out of that position at whatever 1% (or whatever you set it at). Note that depending on implementation and the severity of the crash it might not be able to sell at only 1% (this depends on multiple factors).
  Reply
#6
I can imagine the complexity of variable candle sizes and the impact on the core. It is bit like the missing trading amount adjustment. The feature is not included, but the current behavior is straight and effective....ROI and top of the previous ROI on top of...
A possible workaround to have different candle sizes is to run two paper traders on different candle sizes and to have a trader with the ability to consume these paper trader signals. Have you seen any (community) approach using and implementing such a scenario? Either in process or out of process signals.

When the broker is acting on stop loss decisions, will the backtest reflect these decisions and its impact on the backtest result?
  Reply
#7
Quote:A possible workaround to have different candle sizes is to run two paper traders on different candle sizes and to have a trader with the ability to consume these paper trader signals. Have you seen any (community) approach using and implementing such a scenario? Either in process or out of process signals.

Someone who was contributing a lot in 2015 at some point forked Gekko to do exactly this (just for live trading, not backtesting). Can't remember exactly though, it's too long ago. I am not aware of anything that has been maintained up to (or close to) 0.6. I would very much love to see something like this Smile

Quote:When the broker is acting on stop loss decisions, will the backtest reflect these decisions and its impact on the backtest result?

Yes! The backtester uses the "paper trader" plugin (that runs in both live & backtest mode), the paper trader WILL implement a "simulated" stoploss.
  Reply
#8
@askmike i know your super busy right now but is there any update on when this will go live?

Thanks
  Reply
#9
ASAP! No timeline yet, unfortunately.
  Reply
#10
Trailing stop implementation started!

Follow this PR for details: https://github.com/askmike/gekko/pull/2429
  Reply


Forum Jump:


Users browsing this thread: