2021-08-20 04:51:04 +00:00
|
|
|
""" Gate.io exchange subclass """
|
|
|
|
import logging
|
2022-03-26 15:19:29 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from typing import Dict, List, Optional, Tuple
|
2021-08-20 04:51:04 +00:00
|
|
|
|
2022-02-01 18:53:38 +00:00
|
|
|
from freqtrade.enums import MarginMode, TradingMode
|
2021-10-03 12:14:16 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2021-08-20 04:51:04 +00:00
|
|
|
from freqtrade.exchange import Exchange
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Gateio(Exchange):
|
|
|
|
"""
|
|
|
|
Gate.io exchange class. Contains adjustments needed for Freqtrade to work
|
|
|
|
with this exchange.
|
|
|
|
|
|
|
|
Please note that this exchange is not included in the list of exchanges
|
|
|
|
officially supported by the Freqtrade development team. So some features
|
|
|
|
may still not work as expected.
|
|
|
|
"""
|
|
|
|
|
|
|
|
_ft_has: Dict = {
|
|
|
|
"ohlcv_candle_limit": 1000,
|
2022-01-06 15:13:10 +00:00
|
|
|
"ohlcv_volume_currency": "quote",
|
2022-03-12 18:23:20 +00:00
|
|
|
"stoploss_order_types": {"limit": "limit"},
|
2022-03-09 07:05:21 +00:00
|
|
|
"stoploss_on_exchange": True,
|
2021-08-20 04:51:04 +00:00
|
|
|
}
|
2021-09-16 04:28:10 +00:00
|
|
|
|
2022-03-26 13:57:42 +00:00
|
|
|
_ft_has_futures: Dict = {
|
|
|
|
"needs_trading_fees": True
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:53:38 +00:00
|
|
|
_supported_trading_mode_margin_pairs: List[Tuple[TradingMode, MarginMode]] = [
|
2021-10-20 11:43:46 +00:00
|
|
|
# TradingMode.SPOT always supported and not required in this list
|
2022-02-01 18:53:38 +00:00
|
|
|
# (TradingMode.MARGIN, MarginMode.CROSS),
|
|
|
|
# (TradingMode.FUTURES, MarginMode.CROSS),
|
2022-02-16 09:36:08 +00:00
|
|
|
(TradingMode.FUTURES, MarginMode.ISOLATED)
|
2021-10-20 11:43:46 +00:00
|
|
|
]
|
|
|
|
|
2021-10-03 12:14:16 +00:00
|
|
|
def validate_ordertypes(self, order_types: Dict) -> None:
|
|
|
|
super().validate_ordertypes(order_types)
|
|
|
|
|
2022-03-03 03:37:53 +00:00
|
|
|
if self.trading_mode != TradingMode.FUTURES:
|
|
|
|
if any(v == 'market' for k, v in order_types.items()):
|
|
|
|
raise OperationalException(
|
|
|
|
f'Exchange {self.name} does not support market orders.')
|
2022-03-09 06:45:50 +00:00
|
|
|
|
2022-03-26 15:19:29 +00:00
|
|
|
def get_trades_for_order(self, order_id: str, pair: str, since: datetime,
|
|
|
|
params: Optional[Dict] = None) -> List:
|
|
|
|
trades = super().get_trades_for_order(order_id, pair, since, params)
|
2022-03-26 13:57:42 +00:00
|
|
|
|
2022-03-26 15:19:29 +00:00
|
|
|
if self.trading_mode == TradingMode.FUTURES:
|
2022-03-26 13:57:42 +00:00
|
|
|
# Futures usually don't contain fees in the response.
|
|
|
|
# As such, futures orders on gateio will not contain a fee, which causes
|
|
|
|
# a repeated "update fee" cycle and wrong calculations.
|
|
|
|
# Therefore we patch the response with fees if it's not available.
|
|
|
|
# An alternative also contianing fees would be
|
|
|
|
# privateFuturesGetSettleAccountBook({"settle": "usdt"})
|
2022-03-26 14:15:16 +00:00
|
|
|
pair_fees = self._trading_fees.get(pair, {})
|
2022-03-26 15:19:29 +00:00
|
|
|
if pair_fees:
|
|
|
|
for idx, trade in enumerate(trades):
|
|
|
|
if trade.get('fee', {}).get('cost') is None:
|
|
|
|
takerOrMaker = trade.get('takerOrMaker', 'taker')
|
|
|
|
if pair_fees.get(takerOrMaker) is not None:
|
|
|
|
trades[idx]['fee'] = {
|
|
|
|
'currency': self.get_pair_quote_currency(pair),
|
2022-03-26 15:32:20 +00:00
|
|
|
'cost': trade['cost'] * pair_fees[takerOrMaker],
|
2022-03-26 15:19:29 +00:00
|
|
|
'rate': pair_fees[takerOrMaker],
|
|
|
|
}
|
|
|
|
return trades
|
2022-03-26 13:57:42 +00:00
|
|
|
|
2022-03-09 06:45:50 +00:00
|
|
|
def fetch_stoploss_order(self, order_id: str, pair: str, params={}) -> Dict:
|
|
|
|
return self.fetch_order(
|
|
|
|
order_id=order_id,
|
|
|
|
pair=pair,
|
|
|
|
params={'stop': True}
|
|
|
|
)
|
|
|
|
|
|
|
|
def cancel_stoploss_order(self, order_id: str, pair: str, params={}) -> Dict:
|
|
|
|
return self.cancel_order(
|
|
|
|
order_id=order_id,
|
|
|
|
pair=pair,
|
|
|
|
params={'stop': True}
|
|
|
|
)
|
2022-03-13 02:14:23 +00:00
|
|
|
|
2022-03-18 09:35:00 +00:00
|
|
|
def stoploss_adjust(self, stop_loss: float, order: Dict, side: str) -> bool:
|
2022-03-13 02:14:23 +00:00
|
|
|
"""
|
|
|
|
Verify stop_loss against stoploss-order value (limit or price)
|
|
|
|
Returns True if adjustment is necessary.
|
|
|
|
"""
|
2022-03-18 09:41:38 +00:00
|
|
|
return ((side == "sell" and stop_loss > float(order['stopPrice'])) or
|
|
|
|
(side == "buy" and stop_loss < float(order['stopPrice'])))
|