2024-05-12 14:58:33 +00:00
|
|
|
"""Bybit exchange subclass"""
|
|
|
|
|
2020-12-20 18:59:46 +00:00
|
|
|
import logging
|
2023-08-31 06:07:44 +00:00
|
|
|
from datetime import datetime, timedelta
|
2024-10-04 04:46:45 +00:00
|
|
|
from typing import Any, Optional
|
2020-12-20 18:59:46 +00:00
|
|
|
|
2023-01-26 18:53:24 +00:00
|
|
|
import ccxt
|
|
|
|
|
2022-12-31 14:53:43 +00:00
|
|
|
from freqtrade.constants import BuySell
|
2023-09-10 16:10:38 +00:00
|
|
|
from freqtrade.enums import CandleType, MarginMode, PriceType, TradingMode
|
2023-10-12 04:27:29 +00:00
|
|
|
from freqtrade.exceptions import DDosProtection, ExchangeError, OperationalException, TemporaryError
|
2020-12-20 18:59:46 +00:00
|
|
|
from freqtrade.exchange import Exchange
|
2023-01-26 18:53:24 +00:00
|
|
|
from freqtrade.exchange.common import retrier
|
2024-11-04 06:23:25 +00:00
|
|
|
from freqtrade.exchange.exchange_types import CcxtOrder, FtHas
|
2023-08-31 06:07:44 +00:00
|
|
|
from freqtrade.util.datetime_helpers import dt_now, dt_ts
|
2020-12-20 18:59:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Bybit(Exchange):
|
|
|
|
"""
|
|
|
|
Bybit 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-05-12 14:58:33 +00:00
|
|
|
|
2024-02-18 10:40:50 +00:00
|
|
|
unified_account = False
|
2020-12-20 18:59:46 +00:00
|
|
|
|
2024-09-04 05:15:17 +00:00
|
|
|
_ft_has: FtHas = {
|
2023-08-08 09:36:48 +00:00
|
|
|
"ohlcv_candle_limit": 1000,
|
2023-07-12 09:39:32 +00:00
|
|
|
"ohlcv_has_history": True,
|
2023-12-04 06:23:52 +00:00
|
|
|
"order_time_in_force": ["GTC", "FOK", "IOC", "PO"],
|
2024-09-04 04:57:13 +00:00
|
|
|
"ws_enabled": True,
|
2024-06-20 16:24:43 +00:00
|
|
|
"trades_has_history": False, # Endpoint doesn't support pagination
|
2024-10-10 17:48:27 +00:00
|
|
|
"exchange_has_overrides": {
|
|
|
|
# Bybit spot does not support fetch_order
|
|
|
|
# Unless the account is unified.
|
|
|
|
# TODO: Can be removed once bybit fully forces all accounts to unified mode.
|
|
|
|
"fetchOrder": False,
|
|
|
|
},
|
2022-11-10 06:09:54 +00:00
|
|
|
}
|
2024-09-04 05:15:17 +00:00
|
|
|
_ft_has_futures: FtHas = {
|
2022-11-10 06:09:54 +00:00
|
|
|
"ohlcv_has_history": True,
|
2022-12-30 06:47:00 +00:00
|
|
|
"mark_ohlcv_timeframe": "4h",
|
2023-02-01 05:55:51 +00:00
|
|
|
"funding_fee_timeframe": "8h",
|
2023-01-08 10:40:24 +00:00
|
|
|
"stoploss_on_exchange": True,
|
|
|
|
"stoploss_order_types": {"limit": "limit", "market": "market"},
|
2023-09-26 04:45:48 +00:00
|
|
|
# bybit response parsing fails to populate stopLossPrice
|
|
|
|
"stop_price_prop": "stopPrice",
|
2023-02-04 16:38:39 +00:00
|
|
|
"stop_price_type_field": "triggerBy",
|
|
|
|
"stop_price_type_value_mapping": {
|
|
|
|
PriceType.LAST: "LastPrice",
|
|
|
|
PriceType.MARK: "MarkPrice",
|
|
|
|
PriceType.INDEX: "IndexPrice",
|
|
|
|
},
|
2024-10-10 17:48:27 +00:00
|
|
|
"exchange_has_overrides": {
|
|
|
|
"fetchOrder": True,
|
|
|
|
},
|
2020-12-20 18:59:46 +00:00
|
|
|
}
|
2021-10-01 02:18:56 +00:00
|
|
|
|
2024-10-04 04:46:45 +00:00
|
|
|
_supported_trading_mode_margin_pairs: list[tuple[TradingMode, MarginMode]] = [
|
2021-10-06 07:39:02 +00:00
|
|
|
# TradingMode.SPOT always supported and not required in this list
|
2022-02-01 18:53:38 +00:00
|
|
|
# (TradingMode.FUTURES, MarginMode.CROSS),
|
2022-12-31 10:06:52 +00:00
|
|
|
(TradingMode.FUTURES, MarginMode.ISOLATED)
|
2021-10-06 07:39:02 +00:00
|
|
|
]
|
2022-05-27 08:18:04 +00:00
|
|
|
|
|
|
|
@property
|
2024-10-04 04:46:45 +00:00
|
|
|
def _ccxt_config(self) -> dict:
|
2022-05-27 08:18:04 +00:00
|
|
|
# Parameters to add directly to ccxt sync/async initialization.
|
|
|
|
# ccxt defaults to swap mode.
|
|
|
|
config = {}
|
|
|
|
if self.trading_mode == TradingMode.SPOT:
|
2024-05-12 14:58:33 +00:00
|
|
|
config.update({"options": {"defaultType": "spot"}})
|
2022-05-27 08:18:04 +00:00
|
|
|
config.update(super()._ccxt_config)
|
|
|
|
return config
|
2022-12-30 06:47:00 +00:00
|
|
|
|
2024-10-04 04:46:45 +00:00
|
|
|
def market_is_future(self, market: dict[str, Any]) -> bool:
|
2022-12-31 08:07:07 +00:00
|
|
|
main = super().market_is_future(market)
|
|
|
|
# For ByBit, we'll only support USDT markets for now.
|
2024-05-12 14:58:33 +00:00
|
|
|
return main and market["settle"] == "USDT"
|
2022-12-31 08:07:07 +00:00
|
|
|
|
2023-01-26 18:53:24 +00:00
|
|
|
@retrier
|
|
|
|
def additional_exchange_init(self) -> None:
|
|
|
|
"""
|
|
|
|
Additional exchange initialization logic.
|
|
|
|
.api will be available at this point.
|
|
|
|
Must be overridden in child methods if required.
|
|
|
|
"""
|
|
|
|
try:
|
2024-05-12 14:58:33 +00:00
|
|
|
if not self._config["dry_run"]:
|
2024-02-18 10:40:50 +00:00
|
|
|
if self.trading_mode == TradingMode.FUTURES:
|
|
|
|
position_mode = self._api.set_position_mode(False)
|
2024-05-12 14:58:33 +00:00
|
|
|
self._log_exchange_response("set_position_mode", position_mode)
|
2024-02-18 10:40:50 +00:00
|
|
|
is_unified = self._api.is_unified_enabled()
|
|
|
|
# Returns a tuple of bools, first for margin, second for Account
|
|
|
|
if is_unified and len(is_unified) > 1 and is_unified[1]:
|
|
|
|
self.unified_account = True
|
2024-09-07 16:28:56 +00:00
|
|
|
logger.info(
|
|
|
|
"Bybit: Unified account. Assuming dedicated subaccount for this bot."
|
2024-05-12 14:58:33 +00:00
|
|
|
)
|
2024-02-18 10:40:50 +00:00
|
|
|
else:
|
|
|
|
self.unified_account = False
|
|
|
|
logger.info("Bybit: Standard account.")
|
2023-01-26 18:53:24 +00:00
|
|
|
except ccxt.DDoSProtection as e:
|
|
|
|
raise DDosProtection(e) from e
|
2024-04-13 09:12:10 +00:00
|
|
|
except (ccxt.OperationFailed, ccxt.ExchangeError) as e:
|
2023-01-26 18:53:24 +00:00
|
|
|
raise TemporaryError(
|
2024-05-12 14:58:33 +00:00
|
|
|
f"Error in additional_exchange_init due to {e.__class__.__name__}. Message: {e}"
|
|
|
|
) from e
|
2023-01-26 18:53:24 +00:00
|
|
|
except ccxt.BaseError as e:
|
|
|
|
raise OperationalException(e) from e
|
|
|
|
|
2023-08-08 18:23:01 +00:00
|
|
|
def ohlcv_candle_limit(
|
2024-05-12 14:58:33 +00:00
|
|
|
self, timeframe: str, candle_type: CandleType, since_ms: Optional[int] = None
|
|
|
|
) -> int:
|
2024-10-27 08:53:51 +00:00
|
|
|
if candle_type == CandleType.FUNDING_RATE:
|
2023-08-08 18:23:01 +00:00
|
|
|
return 200
|
|
|
|
|
|
|
|
return super().ohlcv_candle_limit(timeframe, candle_type, since_ms)
|
|
|
|
|
2023-03-21 18:29:27 +00:00
|
|
|
def _lev_prep(self, pair: str, leverage: float, side: BuySell, accept_fail: bool = False):
|
2022-12-31 14:53:43 +00:00
|
|
|
if self.trading_mode != TradingMode.SPOT:
|
2024-05-12 14:58:33 +00:00
|
|
|
params = {"leverage": leverage}
|
2022-12-31 14:53:43 +00:00
|
|
|
self.set_margin_mode(pair, self.margin_mode, accept_fail=True, params=params)
|
|
|
|
self._set_leverage(leverage, pair, accept_fail=True)
|
|
|
|
|
|
|
|
def _get_params(
|
|
|
|
self,
|
|
|
|
side: BuySell,
|
|
|
|
ordertype: str,
|
|
|
|
leverage: float,
|
|
|
|
reduceOnly: bool,
|
2024-05-12 14:58:33 +00:00
|
|
|
time_in_force: str = "GTC",
|
2024-10-04 04:46:45 +00:00
|
|
|
) -> dict:
|
2022-12-31 14:53:43 +00:00
|
|
|
params = super()._get_params(
|
|
|
|
side=side,
|
|
|
|
ordertype=ordertype,
|
|
|
|
leverage=leverage,
|
|
|
|
reduceOnly=reduceOnly,
|
|
|
|
time_in_force=time_in_force,
|
|
|
|
)
|
|
|
|
if self.trading_mode == TradingMode.FUTURES and self.margin_mode:
|
2024-05-12 14:58:33 +00:00
|
|
|
params["position_idx"] = 0
|
2022-12-31 14:53:43 +00:00
|
|
|
return params
|
|
|
|
|
2022-12-31 10:19:27 +00:00
|
|
|
def dry_run_liquidation_price(
|
|
|
|
self,
|
|
|
|
pair: str,
|
2024-05-12 14:58:33 +00:00
|
|
|
open_rate: float, # Entry price of position
|
2022-12-31 10:19:27 +00:00
|
|
|
is_short: bool,
|
|
|
|
amount: float,
|
|
|
|
stake_amount: float,
|
|
|
|
leverage: float,
|
|
|
|
wallet_balance: float, # Or margin balance
|
2024-08-31 06:20:14 +00:00
|
|
|
open_trades: list,
|
2022-12-31 10:19:27 +00:00
|
|
|
) -> Optional[float]:
|
|
|
|
"""
|
|
|
|
Important: Must be fetching data from cached values as this is used by backtesting!
|
|
|
|
PERPETUAL:
|
|
|
|
bybit:
|
|
|
|
https://www.bybithelp.com/HelpCenterKnowledge/bybitHC_Article?language=en_US&id=000001067
|
|
|
|
|
|
|
|
Long:
|
|
|
|
Liquidation Price = (
|
|
|
|
Entry Price * (1 - Initial Margin Rate + Maintenance Margin Rate)
|
|
|
|
- Extra Margin Added/ Contract)
|
|
|
|
Short:
|
|
|
|
Liquidation Price = (
|
|
|
|
Entry Price * (1 + Initial Margin Rate - Maintenance Margin Rate)
|
|
|
|
+ Extra Margin Added/ Contract)
|
|
|
|
|
|
|
|
Implementation Note: Extra margin is currently not used.
|
|
|
|
|
|
|
|
:param pair: Pair to calculate liquidation price for
|
|
|
|
:param open_rate: Entry price of position
|
|
|
|
:param is_short: True if the trade is a short, false otherwise
|
|
|
|
:param amount: Absolute value of position size incl. leverage (in base currency)
|
|
|
|
:param stake_amount: Stake amount - Collateral in settle currency.
|
|
|
|
:param leverage: Leverage used for this position.
|
|
|
|
:param trading_mode: SPOT, MARGIN, FUTURES, etc.
|
|
|
|
:param margin_mode: Either ISOLATED or CROSS
|
|
|
|
:param wallet_balance: Amount of margin_mode in the wallet being used to trade
|
|
|
|
Cross-Margin Mode: crossWalletBalance
|
|
|
|
Isolated-Margin Mode: isolatedWalletBalance
|
2024-08-31 06:20:14 +00:00
|
|
|
:param open_trades: List of other open trades in the same wallet
|
2022-12-31 10:19:27 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
market = self.markets[pair]
|
|
|
|
mm_ratio, _ = self.get_maintenance_ratio_and_amt(pair, stake_amount)
|
|
|
|
|
|
|
|
if self.trading_mode == TradingMode.FUTURES and self.margin_mode == MarginMode.ISOLATED:
|
2024-05-12 14:58:33 +00:00
|
|
|
if market["inverse"]:
|
|
|
|
raise OperationalException("Freqtrade does not yet support inverse contracts")
|
2022-12-31 10:19:27 +00:00
|
|
|
initial_margin_rate = 1 / leverage
|
|
|
|
|
|
|
|
# See docstring - ignores extra margin!
|
|
|
|
if is_short:
|
|
|
|
return open_rate * (1 + initial_margin_rate - mm_ratio)
|
|
|
|
else:
|
|
|
|
return open_rate * (1 - initial_margin_rate + mm_ratio)
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise OperationalException(
|
2024-05-12 14:58:33 +00:00
|
|
|
"Freqtrade only supports isolated futures for leverage trading"
|
|
|
|
)
|
2023-01-04 17:23:56 +00:00
|
|
|
|
2023-01-24 06:21:16 +00:00
|
|
|
def get_funding_fees(
|
2024-05-12 14:58:33 +00:00
|
|
|
self, pair: str, amount: float, is_short: bool, open_date: datetime
|
|
|
|
) -> float:
|
2023-01-04 17:23:56 +00:00
|
|
|
"""
|
2023-01-24 06:21:16 +00:00
|
|
|
Fetch funding fees, either from the exchange (live) or calculates them
|
|
|
|
based on funding rate/mark price history
|
|
|
|
:param pair: The quote/base pair of the trade
|
|
|
|
:param is_short: trade direction
|
|
|
|
:param amount: Trade amount
|
|
|
|
:param open_date: Open date of the trade
|
|
|
|
:return: funding fee since open_date
|
|
|
|
:raises: ExchangeError if something goes wrong.
|
2023-01-04 17:23:56 +00:00
|
|
|
"""
|
2023-01-24 06:21:16 +00:00
|
|
|
# Bybit does not provide "applied" funding fees per position.
|
|
|
|
if self.trading_mode == TradingMode.FUTURES:
|
2023-10-12 04:27:29 +00:00
|
|
|
try:
|
2024-05-12 14:58:33 +00:00
|
|
|
return self._fetch_and_calculate_funding_fees(pair, amount, is_short, open_date)
|
2023-10-12 04:27:29 +00:00
|
|
|
except ExchangeError:
|
|
|
|
logger.warning(f"Could not update funding fees for {pair}.")
|
2023-01-24 06:21:16 +00:00
|
|
|
return 0.0
|
2023-08-31 06:07:44 +00:00
|
|
|
|
2024-11-04 06:23:25 +00:00
|
|
|
def fetch_orders(
|
|
|
|
self, pair: str, since: datetime, params: Optional[dict] = None
|
|
|
|
) -> list[CcxtOrder]:
|
2023-08-31 06:07:44 +00:00
|
|
|
"""
|
|
|
|
Fetch all orders for a pair "since"
|
|
|
|
:param pair: Pair for the query
|
|
|
|
:param since: Starting time for the query
|
|
|
|
"""
|
|
|
|
# On bybit, the distance between since and "until" can't exceed 7 days.
|
|
|
|
# we therefore need to split the query into multiple queries.
|
|
|
|
orders = []
|
|
|
|
|
|
|
|
while since < dt_now():
|
|
|
|
until = since + timedelta(days=7, minutes=-1)
|
2024-05-12 14:58:33 +00:00
|
|
|
orders += super().fetch_orders(pair, since, params={"until": dt_ts(until)})
|
2023-08-31 06:07:44 +00:00
|
|
|
since = until
|
|
|
|
|
|
|
|
return orders
|
2023-09-03 15:05:57 +00:00
|
|
|
|
2024-11-04 06:23:25 +00:00
|
|
|
def fetch_order(self, order_id: str, pair: str, params: Optional[dict] = None) -> CcxtOrder:
|
2024-09-25 17:02:32 +00:00
|
|
|
if self.exchange_has("fetchOrder"):
|
|
|
|
# Set acknowledged to True to avoid ccxt exception
|
|
|
|
params = {"acknowledged": True}
|
|
|
|
|
2023-09-03 15:05:57 +00:00
|
|
|
order = super().fetch_order(order_id, pair, params)
|
2024-09-25 17:02:32 +00:00
|
|
|
if not order:
|
2024-09-25 17:11:01 +00:00
|
|
|
order = self.fetch_order_emulated(order_id, pair, {})
|
2023-09-03 15:05:57 +00:00
|
|
|
if (
|
2024-05-12 14:58:33 +00:00
|
|
|
order.get("status") == "canceled"
|
|
|
|
and order.get("filled") == 0.0
|
|
|
|
and order.get("remaining") == 0.0
|
2023-09-03 15:05:57 +00:00
|
|
|
):
|
|
|
|
# Canceled orders will have "remaining=0" on bybit.
|
2024-05-12 14:58:33 +00:00
|
|
|
order["remaining"] = None
|
2023-09-03 15:05:57 +00:00
|
|
|
return order
|
2024-05-09 17:42:20 +00:00
|
|
|
|
|
|
|
@retrier
|
2024-10-04 04:46:45 +00:00
|
|
|
def get_leverage_tiers(self) -> dict[str, list[dict]]:
|
2024-05-09 17:42:20 +00:00
|
|
|
"""
|
2024-05-16 17:25:19 +00:00
|
|
|
Cache leverage tiers for 1 day, since they are not expected to change often, and
|
|
|
|
bybit requires pagination to fetch all tiers.
|
2024-05-09 17:42:20 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Load cached tiers
|
2024-05-16 17:25:19 +00:00
|
|
|
tiers_cached = self.load_cached_leverage_tiers(
|
|
|
|
self._config["stake_currency"], timedelta(days=1)
|
|
|
|
)
|
2024-05-09 17:42:20 +00:00
|
|
|
if tiers_cached:
|
2024-05-16 17:25:19 +00:00
|
|
|
return tiers_cached
|
2024-05-09 17:42:20 +00:00
|
|
|
|
|
|
|
# Fetch tiers from exchange
|
2024-05-16 17:25:19 +00:00
|
|
|
tiers = super().get_leverage_tiers()
|
2024-05-09 17:42:20 +00:00
|
|
|
|
2024-05-12 14:58:33 +00:00
|
|
|
self.cache_leverage_tiers(tiers, self._config["stake_currency"])
|
2024-05-09 17:42:20 +00:00
|
|
|
return tiers
|