From 1431f7cc3e0a5c01c7ea0baa4976f739f2ba317c Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 26 Jan 2023 19:53:24 +0100 Subject: [PATCH] Set position mode to one-way on startup --- docs/exchanges.md | 1 + freqtrade/exchange/bybit.py | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/docs/exchanges.md b/docs/exchanges.md index 54fd7eae3..12b6c874d 100644 --- a/docs/exchanges.md +++ b/docs/exchanges.md @@ -259,6 +259,7 @@ The configuration parameter `exchange.unknown_fee_rate` can be used to specify t Futures trading on bybit is currently supported for USDT markets, and will use isolated futures mode. Users with unified accounts (there's no way back) can create a Sub-account which will start as "non-unified", and can therefore use isolated futures. +On startup, freqtrade will set the position mode to "One-way Mode" for the whole (sub)account. This avoids making this call over and over again (slowing down bot operations), but means that changes to this setting may result in exceptions and errors. As bybit doesn't provide funding rate history, the dry-run calculation is used for live trades as well. diff --git a/freqtrade/exchange/bybit.py b/freqtrade/exchange/bybit.py index e08380213..dc9b5f621 100644 --- a/freqtrade/exchange/bybit.py +++ b/freqtrade/exchange/bybit.py @@ -3,10 +3,13 @@ import logging from datetime import datetime from typing import Any, Dict, List, Optional, Tuple +import ccxt + from freqtrade.constants import BuySell from freqtrade.enums import MarginMode, TradingMode -from freqtrade.exceptions import OperationalException +from freqtrade.exceptions import DDosProtection, OperationalException, TemporaryError from freqtrade.exchange import Exchange +from freqtrade.exchange.common import retrier from freqtrade.exchange.exchange_utils import timeframe_to_msecs @@ -63,6 +66,26 @@ class Bybit(Exchange): main and market['settle'] == 'USDT' ) + @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: + if self.trading_mode == TradingMode.FUTURES and not self._config['dry_run']: + position_mode = self._api.set_position_mode(False) + self._log_exchange_response('set_position_mode', position_mode) + except ccxt.DDoSProtection as e: + raise DDosProtection(e) from e + except (ccxt.NetworkError, ccxt.ExchangeError) as e: + raise TemporaryError( + f'Error in additional_exchange_init due to {e.__class__.__name__}. Message: {e}' + ) from e + except ccxt.BaseError as e: + raise OperationalException(e) from e + async def _fetch_funding_rate_history( self, pair: str,