2024-05-12 14:58:33 +00:00
|
|
|
"""Gate.io exchange subclass"""
|
|
|
|
|
2021-08-20 04:51:04 +00:00
|
|
|
import logging
|
2022-03-26 15:19:29 +00:00
|
|
|
from datetime import datetime
|
2022-07-16 10:52:35 +00:00
|
|
|
from typing import Any, Dict, List, Optional, Tuple
|
2021-08-20 04:51:04 +00:00
|
|
|
|
2022-06-17 21:02:39 +00:00
|
|
|
from freqtrade.constants import BuySell
|
2023-02-11 12:02:55 +00:00
|
|
|
from freqtrade.enums import MarginMode, PriceType, TradingMode
|
2021-08-20 04:51:04 +00:00
|
|
|
from freqtrade.exchange import Exchange
|
2024-09-04 05:15:17 +00:00
|
|
|
from freqtrade.exchange.exchange_types import FtHas
|
2022-07-16 10:52:35 +00:00
|
|
|
from freqtrade.misc import safe_value_fallback2
|
2021-08-20 04:51:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-02-10 19:58:02 +00:00
|
|
|
class Gate(Exchange):
|
2021-08-20 04:51:04 +00:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2024-09-04 05:15:17 +00:00
|
|
|
_ft_has: FtHas = {
|
2021-08-20 04:51:04 +00:00
|
|
|
"ohlcv_candle_limit": 1000,
|
2024-05-12 14:58:33 +00:00
|
|
|
"order_time_in_force": ["GTC", "IOC"],
|
2022-03-09 07:05:21 +00:00
|
|
|
"stoploss_on_exchange": True,
|
2023-09-28 17:33:59 +00:00
|
|
|
"stoploss_order_types": {"limit": "limit"},
|
|
|
|
"stop_price_param": "stopPrice",
|
|
|
|
"stop_price_prop": "stopPrice",
|
2023-03-28 04:55:55 +00:00
|
|
|
"marketOrderRequiresPrice": True,
|
2024-06-20 16:24:43 +00:00
|
|
|
"trades_has_history": False, # Endpoint would support this - but ccxt doesn't.
|
2021-08-20 04:51:04 +00:00
|
|
|
}
|
2021-09-16 04:28:10 +00:00
|
|
|
|
2024-09-04 05:15:17 +00:00
|
|
|
_ft_has_futures: FtHas = {
|
2022-07-09 06:24:29 +00:00
|
|
|
"needs_trading_fees": True,
|
2023-03-28 04:55:55 +00:00
|
|
|
"marketOrderRequiresPrice": False,
|
2023-02-11 12:02:55 +00:00
|
|
|
"stop_price_type_field": "price_type",
|
|
|
|
"stop_price_type_value_mapping": {
|
|
|
|
PriceType.LAST: 0,
|
|
|
|
PriceType.MARK: 1,
|
|
|
|
PriceType.INDEX: 2,
|
|
|
|
},
|
2022-03-26 13:57:42 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
]
|
|
|
|
|
2022-06-17 21:02:39 +00:00
|
|
|
def _get_params(
|
2024-05-12 14:58:33 +00:00
|
|
|
self,
|
|
|
|
side: BuySell,
|
|
|
|
ordertype: str,
|
|
|
|
leverage: float,
|
|
|
|
reduceOnly: bool,
|
|
|
|
time_in_force: str = "GTC",
|
|
|
|
) -> Dict:
|
2022-06-17 21:02:39 +00:00
|
|
|
params = super()._get_params(
|
|
|
|
side=side,
|
|
|
|
ordertype=ordertype,
|
|
|
|
leverage=leverage,
|
|
|
|
reduceOnly=reduceOnly,
|
|
|
|
time_in_force=time_in_force,
|
|
|
|
)
|
2024-05-12 14:58:33 +00:00
|
|
|
if ordertype == "market" and self.trading_mode == TradingMode.FUTURES:
|
|
|
|
params["type"] = "market"
|
|
|
|
params.update({"timeInForce": "IOC"})
|
2022-06-17 21:02:39 +00:00
|
|
|
return params
|
|
|
|
|
2024-05-12 14:58:33 +00:00
|
|
|
def get_trades_for_order(
|
|
|
|
self, order_id: str, pair: str, since: datetime, params: Optional[Dict] = None
|
|
|
|
) -> List:
|
2022-03-26 15:19:29 +00:00
|
|
|
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.
|
2023-02-10 19:58:02 +00:00
|
|
|
# As such, futures orders on gate will not contain a fee, which causes
|
2022-03-26 13:57:42 +00:00
|
|
|
# a repeated "update fee" cycle and wrong calculations.
|
|
|
|
# Therefore we patch the response with fees if it's not available.
|
2024-04-18 20:51:25 +00:00
|
|
|
# An alternative also containing fees would be
|
2022-03-26 13:57:42 +00:00
|
|
|
# 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):
|
2024-05-12 14:58:33 +00:00
|
|
|
fee = trade.get("fee", {})
|
|
|
|
if fee and fee.get("cost") is None:
|
|
|
|
takerOrMaker = trade.get("takerOrMaker", "taker")
|
2022-03-26 15:19:29 +00:00
|
|
|
if pair_fees.get(takerOrMaker) is not None:
|
2024-05-12 14:58:33 +00:00
|
|
|
trades[idx]["fee"] = {
|
|
|
|
"currency": self.get_pair_quote_currency(pair),
|
|
|
|
"cost": trade["cost"] * pair_fees[takerOrMaker],
|
|
|
|
"rate": pair_fees[takerOrMaker],
|
2022-03-26 15:19:29 +00:00
|
|
|
}
|
|
|
|
return trades
|
2022-03-26 13:57:42 +00:00
|
|
|
|
2022-07-16 10:52:35 +00:00
|
|
|
def get_order_id_conditional(self, order: Dict[str, Any]) -> str:
|
2024-05-12 14:58:33 +00:00
|
|
|
return safe_value_fallback2(order, order, "id_stop", "id")
|
2022-07-16 10:52:35 +00:00
|
|
|
|
2024-04-20 07:26:50 +00:00
|
|
|
def fetch_stoploss_order(self, order_id: str, pair: str, params: Optional[Dict] = None) -> Dict:
|
2024-05-12 14:58:33 +00:00
|
|
|
order = self.fetch_order(order_id=order_id, pair=pair, params={"stop": True})
|
|
|
|
if order.get("status", "open") == "closed":
|
2024-03-17 14:17:04 +00:00
|
|
|
# Places a real order - which we need to fetch explicitly.
|
2024-05-12 14:58:33 +00:00
|
|
|
val = "trade_id" if self.trading_mode == TradingMode.FUTURES else "fired_order_id"
|
2024-03-17 14:17:04 +00:00
|
|
|
|
2024-05-12 14:58:33 +00:00
|
|
|
if new_orderid := order.get("info", {}).get(val):
|
2024-03-17 14:17:04 +00:00
|
|
|
order1 = self.fetch_order(order_id=new_orderid, pair=pair, params=params)
|
2024-05-12 14:58:33 +00:00
|
|
|
order1["id_stop"] = order1["id"]
|
|
|
|
order1["id"] = order_id
|
|
|
|
order1["type"] = "stoploss"
|
|
|
|
order1["stopPrice"] = order.get("stopPrice")
|
|
|
|
order1["status_stop"] = "triggered"
|
2024-03-17 14:17:04 +00:00
|
|
|
|
|
|
|
return order1
|
2022-07-15 12:35:49 +00:00
|
|
|
return order
|
2022-03-09 06:45:50 +00:00
|
|
|
|
2024-04-20 07:24:51 +00:00
|
|
|
def cancel_stoploss_order(
|
2024-05-12 14:58:33 +00:00
|
|
|
self, order_id: str, pair: str, params: Optional[Dict] = None
|
|
|
|
) -> Dict:
|
|
|
|
return self.cancel_order(order_id=order_id, pair=pair, params={"stop": True})
|