From 60478cb2135a2411f6b5bbbaeb9632a808f3a387 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Sat, 23 Oct 2021 22:01:44 -0600 Subject: [PATCH] Add fill_leverage_brackets and get_max_leverage back in --- freqtrade/exchange/exchange.py | 31 +++++++++++++++++++++++++++---- freqtrade/exchange/ftx.py | 15 --------------- freqtrade/optimize/backtesting.py | 1 - freqtrade/persistence/models.py | 14 -------------- tests/exchange/test_ftx.py | 17 ----------------- 5 files changed, 27 insertions(+), 51 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 0f7d6c07b..e29ef9df0 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -75,7 +75,6 @@ class Exchange: # funding_fee_times is currently unused, but should ideally be used to properly # schedule refresh times funding_fee_times: List[int] = [] # hours of the day - funding_rate_history: Dict = {} _supported_trading_mode_collateral_pairs: List[Tuple[TradingMode, Collateral]] = [ # TradingMode.SPOT always supported and not required in this list @@ -160,9 +159,6 @@ class Exchange: self._api_async = self._init_ccxt( exchange_config, ccxt_async, ccxt_kwargs=ccxt_async_config) - if self.trading_mode != TradingMode.SPOT: - self.fill_leverage_brackets() - logger.info('Using Exchange "%s"', self.name) if validate: @@ -185,6 +181,9 @@ class Exchange: self.markets_refresh_interval: int = exchange_config.get( "markets_refresh_interval", 60) * 60 + if self.trading_mode != TradingMode.SPOT: + self.fill_leverage_brackets() + def __del__(self): """ Destructor - clean up async stuff @@ -1637,6 +1636,30 @@ class Exchange: except ccxt.BaseError as e: raise OperationalException(e) from e + def fill_leverage_brackets(self): + """ + Assigns property _leverage_brackets to a dictionary of information about the leverage + allowed on each pair + Not used if the exchange has a static max leverage value for the account or each pair + """ + return + + def get_max_leverage(self, pair: Optional[str], nominal_value: Optional[float]) -> float: + """ + Returns the maximum leverage that a pair can be traded at + :param pair: The base/quote currency pair being traded + :nominal_value: The total value of the trade in quote currency (collateral + debt) + """ + market = self.markets[pair] + if ( + 'limits' in market and + 'leverage' in market['limits'] and + 'max' in market['limits']['leverage'] + ): + return market['limits']['leverage']['max'] + else: + return 1.0 + def _get_funding_fee( self, contract_size: float, diff --git a/freqtrade/exchange/ftx.py b/freqtrade/exchange/ftx.py index c668add2f..e78c43872 100644 --- a/freqtrade/exchange/ftx.py +++ b/freqtrade/exchange/ftx.py @@ -169,21 +169,6 @@ class Ftx(Exchange): return safe_value_fallback2(order, order, 'id_stop', 'id') return order['id'] - def fill_leverage_brackets(self): - """ - FTX leverage is static across the account, and doesn't change from pair to pair, - so _leverage_brackets doesn't need to be set - """ - return - - def get_max_leverage(self, pair: Optional[str], nominal_value: Optional[float]) -> float: - """ - Returns the maximum leverage that a pair can be traded at, which is always 20 on ftx - :param pair: Here for super method, not used on FTX - :nominal_value: Here for super method, not used on FTX - """ - return 20.0 - def _get_mark_price_history( self, pair: str, diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 24a3e744a..aaf875a94 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -125,7 +125,6 @@ class Backtesting: self.progress = BTProgress() self.abort = False - self.init_backtest() def __del__(self): diff --git a/freqtrade/persistence/models.py b/freqtrade/persistence/models.py index 623dd74d3..5496628f4 100644 --- a/freqtrade/persistence/models.py +++ b/freqtrade/persistence/models.py @@ -707,7 +707,6 @@ class LocalTrade(): return float(self._calc_base_close(amount, rate, fee) - total_interest) elif (trading_mode == TradingMode.FUTURES): - self.add_funding_fees() funding_fees = self.funding_fees or 0.0 if self.is_short: return float(self._calc_base_close(amount, rate, fee)) - funding_fees @@ -789,19 +788,6 @@ class LocalTrade(): else: return None - def add_funding_fees(self): - if self.trading_mode == TradingMode.FUTURES: - # TODO-lev: Calculate this correctly and add it - # if self.config['runmode'].value in ('backtest', 'hyperopt'): - # self.funding_fees = getattr(Exchange, self.exchange).calculate_funding_fees( - # self.exchange, - # self.pair, - # self.amount, - # self.open_date_utc, - # self.close_date_utc - # ) - return - @staticmethod def get_trades_proxy(*, pair: str = None, is_open: bool = None, open_date: datetime = None, close_date: datetime = None, diff --git a/tests/exchange/test_ftx.py b/tests/exchange/test_ftx.py index ca6b24d64..97093bdcb 100644 --- a/tests/exchange/test_ftx.py +++ b/tests/exchange/test_ftx.py @@ -250,20 +250,3 @@ def test_get_order_id(mocker, default_conf): } } assert exchange.get_order_id_conditional(order) == '1111' - - -@pytest.mark.parametrize('pair,nominal_value,max_lev', [ - ("ADA/BTC", 0.0, 20.0), - ("BTC/EUR", 100.0, 20.0), - ("ZEC/USD", 173.31, 20.0), -]) -def test_get_max_leverage_ftx(default_conf, mocker, pair, nominal_value, max_lev): - exchange = get_patched_exchange(mocker, default_conf, id="ftx") - assert exchange.get_max_leverage(pair, nominal_value) == max_lev - - -def test_fill_leverage_brackets_ftx(default_conf, mocker): - # FTX only has one account wide leverage, so there's no leverage brackets - exchange = get_patched_exchange(mocker, default_conf, id="ftx") - exchange.fill_leverage_brackets() - assert exchange._leverage_brackets == {}