gekko icon indicating copy to clipboard operation
gekko copied to clipboard

How to handle market outages [trading halted]

Open xhad opened this issue 10 years ago • 4 comments

I have seen this happen a few times from Bitfinex. The market often gets suspended at highly volatile moments, which misprices BFX relative to other markets. Once BFX opens trading, there is a very massive price move.

How could Gekko handle trading suspensions with an open trade?

I'm thinking something like a 'circuit breaker' that detects when there is unusual activity at the market Gekko is watching, and then calls a market order function that waits for trading to resume and exits with a market order?

You can see the situation here: https://goo.gl/photos/i1Tq8anx9Eu3YHRa9

https://bitfinex.statuspage.io/

xhad avatar Jun 21 '16 03:06 xhad

A market order would certainly be a bad idea here as indicated at BFX:

Monitoring - Trading will resume at 4:00 UTC. Order cancels can be submitted now, but they will not process until 4:00 UTC. Please note: When trading resumes there will likely be wide spreads. We caution users to be cognizant of this fact and warn against using market orders. As trading goes on, the order book will fill in and this will become less of an issue. Please be aware of market conditions when placing your orders.

xhad avatar Jun 21 '16 04:06 xhad

When the market opened, this is how Gekko handled it:

2016-06-21 13:00:26 (DEBUG):    Requested / trade data from Bitfinex ...
2016-06-21 13:00:35 (DEBUG):    Processing 34 new trades.
2016-06-21 13:00:35 (DEBUG):    From 2016-06-21 04:00:18 UTC to 2016-06-21 04:00:26 UTC. (a few seconds)
2016-06-21 13:00:35 (DEBUG):    calculated MACD properties for candle:
2016-06-21 13:00:35 (DEBUG):             short: 734.67739809
2016-06-21 13:00:35 (DEBUG):             long: 742.87382459
2016-06-21 13:00:35 (DEBUG):             macd: -8.19642650
2016-06-21 13:00:35 (DEBUG):             signal: -5.41896557
2016-06-21 13:00:35 (DEBUG):             macdiff: -2.77746093
2016-06-21 13:00:35 (DEBUG):    In downtrend since 1 candle(s)
2016-06-21 13:00:35 (INFO):     Trader Received advice to go short Selling  BTC

2016-06-21 13:00:35 (INFO):     We have new trading advice!
2016-06-21 13:00:35 (INFO):              Position: short
2016-06-21 13:00:35 (INFO):              Market price: 694.82
2016-06-21 13:00:35 (INFO):              Based on market time: 2016-06-21 00:57:00

2016-06-21 13:00:35 (DEBUG):    calculated MACD properties for candle:
2016-06-21 13:00:35 (DEBUG):             short: 727.43059844
2016-06-21 13:00:35 (DEBUG):             long: 738.50529508
2016-06-21 13:00:35 (DEBUG):             macd: -11.07469664
2016-06-21 13:00:35 (DEBUG):             signal: -6.55011178
2016-06-21 13:00:35 (DEBUG):             macdiff: -4.52458486
2016-06-21 13:00:35 (DEBUG):    In downtrend since 2 candle(s)
2016-06-21 13:00:35 (DEBUG):    calculated MACD properties for candle:
2016-06-21 13:00:35 (DEBUG):             short: 721.50139872
2016-06-21 13:00:35 (DEBUG):             long: 734.53390462
2016-06-21 13:00:35 (DEBUG):             macd: -13.03250590
2016-06-21 13:00:35 (DEBUG):             signal: -7.84659061
2016-06-21 13:00:35 (DEBUG):             macdiff: -5.18591529
2016-06-21 13:00:35 (DEBUG):    In downtrend since 3 candle(s)
2016-06-21 13:00:35 (DEBUG):    calculated MACD properties for candle:
2016-06-21 13:00:35 (DEBUG):             short: 716.65023532
2016-06-21 13:00:35 (DEBUG):             long: 730.92354965
2016-06-21 13:00:35 (DEBUG):             macd: -14.27331434
2016-06-21 13:00:35 (DEBUG):             signal: -9.13193535
2016-06-21 13:00:35 (DEBUG):             macdiff: -5.14137898
2016-06-21 13:00:35 (DEBUG):    In downtrend since 4 candle(s)
2016-06-21 13:00:38 (INFO):     attempting to SELL 0.0684236 BTC at Bitfinex
2016-06-21 13:00:38 (INFO):     Send advice via email.
2016-06-21 13:00:46 (DEBUG):    Requested / trade data from Bitfinex ...
2016-06-21 13:00:55 (DEBUG):    Processing 124 new trades.
2016-06-21 13:00:55 (DEBUG):    From 2016-06-21 04:00:26 UTC to 2016-06-21 04:00:46 UTC. (a few seconds)

xhad avatar Jun 21 '16 04:06 xhad

Hmm this is a hard one. So we need to know how we want Gekko to handle these situations and then we need to detect them.

The important thing to know is that when there is no trading activity Gekko will not create any orders anyway, so for as long the market is frozen Gekko is not going to do anything anyway. (Gekko reacts on market events, when there are non Gekko idles). But the volatile time just after this can negatively impact performance.

How to handle

I think in these cases the market will be volatile and there will be huge spreads, so it is probably best to wait until X minutes or trades after a freeze before acting upon any decision (So cancel existing orders and do not create new ones). This means just keeping the current existing position.

How to detect

I am thinking this has to be exchange specific anyway, for Bitfinex we could do something like: Create a new plugin that watches the RSS feed of the status page. If something new pops up that says "Trading has been paused" we broadcast a message like "market frozen" and the portfolio manager (the module responsible for creating orders) will see that and not do anything until an "market frozen".


While this is possible, I wouldn't consider it high priority: Most people use Gekko to detect trends (like "I think the price will go higher from here") and I have yet to see that this actually has an impact on the overal trend (or trend detection). Worst case is Gekko misidentifies the volatile market as a trend and bets the wrong way (and this happens very rarely).

askmike avatar Jun 21 '16 06:06 askmike

In these kinds of scenarios it is very hard to do anything correctly. And as soon as the API is down Gekko is unable to get any orders / etc. through anyway. I am open for pull requests (see above comment on example implementation) but will close this issue now as it's not a pressing issue for Gekko currently.

askmike avatar Jun 22 '16 10:06 askmike