Elffuss BraidHub

kikocis/freqtrade public

Add kucoin exchange subclass

Kucoin has some specific orderbook restrictions

closes #4723
Changeset323404e4348f756ee62bfb0247f388133d2edb47cf1790d14c020fdb4727e6fd
AuthorMatthias <xmatthias@outlook.com> · 2021-04-13 12:28 +02:00
CommitterMatthias <xmatthias@outlook.com> · 2021-04-13 12:28 +02:00
Parents55e614fcfc74

Browse the files at this changeset

6 files changed

modified freqtrade/exchange/__init__.py +1 −0
@@ -15,3 +15,4 @@
1515 validate_exchanges)
1616 from freqtrade.exchange.ftx import Ftx
1717 from freqtrade.exchange.kraken import Kraken
18+from freqtrade.exchange.kucoin import Kucoin
modified freqtrade/exchange/bittrex.py +0 −4
@@ -12,10 +12,6 @@
1212 """
1313 Bittrex exchange class. Contains adjustments needed for Freqtrade to work
1414 with this exchange.
15-
16- Please note that this exchange is not included in the list of exchanges
17- officially supported by the Freqtrade development team. So some features
18- may still not work as expected.
1915 """
2016
2117 _ft_has: Dict = {
modified freqtrade/exchange/exchange.py +11 −3
@@ -63,6 +63,7 @@
6363 "trades_pagination": "time", # Possible are "time" or "id"
6464 "trades_pagination_arg": "since",
6565 "l2_limit_range": None,
66+ "l2_limit_range_required": True, # Allow Empty L2 limit (kucoin)
6667 }
6768 _ft_has: Dict = {}
6869
@@ -1154,14 +1155,20 @@
11541155 return self.fetch_order(order_id, pair)
11551156
11561157 @staticmethod
1157- def get_next_limit_in_list(limit: int, limit_range: Optional[List[int]]):
1158+ def get_next_limit_in_list(limit: int, limit_range: Optional[List[int]],
1159+ range_required: bool = True):
11581160 """
11591161 Get next greater value in the list.
11601162 Used by fetch_l2_order_book if the api only supports a limited range
11611163 """
11621164 if not limit_range:
11631165 return limit
1164- return min([x for x in limit_range if limit <= x] + [max(limit_range)])
1166+
1167+ result = min([x for x in limit_range if limit <= x] + [max(limit_range)])
1168+ if not range_required and limit > result:
1169+ # Range is not required - we can use None as parameter.
1170+ return None
1171+ return result
11651172
11661173 @retrier
11671174 def fetch_l2_order_book(self, pair: str, limit: int = 100) -> dict:
@@ -1171,7 +1178,8 @@
11711178 Returns a dict in the format
11721179 {'asks': [price, volume], 'bids': [price, volume]}
11731180 """
1174- limit1 = self.get_next_limit_in_list(limit, self._ft_has['l2_limit_range'])
1181+ limit1 = self.get_next_limit_in_list(limit, self._ft_has['l2_limit_range'],
1182+ self._ft_has['l2_limit_range_required'])
11751183 try:
11761184
11771185 return self._api.fetch_l2_order_book(pair, limit1)
added freqtrade/exchange/kucoin.py +24 −0
@@ -0,0 +1,24 @@
1+""" Kucoin exchange subclass """
2+import logging
3+from typing import Dict
4+
5+from freqtrade.exchange import Exchange
6+
7+
8+logger = logging.getLogger(__name__)
9+
10+
11+class Kucoin(Exchange):
12+ """
13+ Kucoin exchange class. Contains adjustments needed for Freqtrade to work
14+ with this exchange.
15+
16+ Please note that this exchange is not included in the list of exchanges
17+ officially supported by the Freqtrade development team. So some features
18+ may still not work as expected.
19+ """
20+
21+ _ft_has: Dict = {
22+ "l2_limit_range": [20, 100],
23+ "l2_limit_range_required": False,
24+ }
modified tests/exchange/test_ccxt_compat.py +10 −3
@@ -36,7 +36,12 @@
3636 'pair': 'BTC/USDT',
3737 'hasQuoteVolume': True,
3838 'timeframe': '5m',
39- }
39+ },
40+ 'kucoin': {
41+ 'pair': 'BTC/USDT',
42+ 'hasQuoteVolume': True,
43+ 'timeframe': '5m',
44+ },
4045 }
4146
4247
@@ -100,14 +105,16 @@
100105 assert 'asks' in l2
101106 assert 'bids' in l2
102107 l2_limit_range = exchange._ft_has['l2_limit_range']
108+ l2_limit_range_required = exchange._ft_has['l2_limit_range_required']
103109 for val in [1, 2, 5, 25, 100]:
104110 l2 = exchange.fetch_l2_order_book(pair, val)
105111 if not l2_limit_range or val in l2_limit_range:
106112 assert len(l2['asks']) == val
107113 assert len(l2['bids']) == val
108114 else:
109- next_limit = exchange.get_next_limit_in_list(val, l2_limit_range)
110- if next_limit > 200:
115+ next_limit = exchange.get_next_limit_in_list(
116+ val, l2_limit_range, l2_limit_range_required)
117+ if next_limit is None or next_limit > 200:
111118 # Large orderbook sizes can be a problem for some exchanges (bitrex ...)
112119 assert len(l2['asks']) > 200
113120 assert len(l2['asks']) > 200
modified tests/exchange/test_exchange.py +3 −0
@@ -1641,6 +1641,9 @@
16411641 # Going over the limit ...
16421642 assert Exchange.get_next_limit_in_list(1001, limit_range) == 1000
16431643 assert Exchange.get_next_limit_in_list(2000, limit_range) == 1000
1644+ # Without required range
1645+ assert Exchange.get_next_limit_in_list(2000, limit_range, False) is None
1646+ assert Exchange.get_next_limit_in_list(15, limit_range, False) == 20
16441647
16451648 assert Exchange.get_next_limit_in_list(21, None) == 21
16461649 assert Exchange.get_next_limit_in_list(100, None) == 100