Add "migrate funding fee timeframe" logic

This commit is contained in:
Matthias 2024-01-04 16:44:17 +01:00
parent a12f368796
commit 983764ad0a
5 changed files with 62 additions and 3 deletions

View File

@ -531,7 +531,7 @@ def download_data_main(config: Config) -> None:
"Please use `--dl-trades` instead for this exchange "
"(will unfortunately take a long time)."
)
migrate_data(config)
migrate_data(config, exchange)
pairs_not_available = refresh_backtest_ohlcv_data(
exchange, pairs=expanded_pairs, timeframes=config['timeframes'],
datadir=config['datadir'], timerange=timerange,

View File

@ -403,6 +403,30 @@ class IDataHandler(ABC):
return
file_old.rename(file_new)
def fix_funding_fee_timeframe(self, ff_timeframe: str):
"""
Temporary method to migrate data from old funding fee timeframe to the correct timeframe
Applies to bybit and okx, where funding-fee and mark candles have different timeframes.
"""
paircombs = self.ohlcv_get_available_data(self._datadir, TradingMode.FUTURES)
funding_rate_combs = [
f for f in paircombs if f[2] == CandleType.FUNDING_RATE and f[1] != ff_timeframe
]
for pair, timeframe, candletype in funding_rate_combs:
old_name = self._pair_data_filename(self._datadir, pair, timeframe, candletype)
new_name = self._pair_data_filename(self._datadir, pair, ff_timeframe, candletype)
if not Path(old_name).exists():
logger.warning(f'{old_name} does not exist, skipping.')
continue
if Path(new_name).exists():
logger.warning(f'{new_name} already exists, skipping.')
continue
Path(old_name).rename(new_name)
def get_datahandlerclass(datatype: str) -> Type[IDataHandler]:
"""

View File

@ -158,7 +158,7 @@ class Backtesting:
self._can_short = self.trading_mode != TradingMode.SPOT
self._position_stacking: bool = self.config.get('position_stacking', False)
self.enable_protections: bool = self.config.get('enable_protections', False)
migrate_data(config)
migrate_data(config, self.exchange)
self.init_backtest()

View File

@ -1,6 +1,12 @@
from typing import Optional
from freqtrade.exchange import Exchange
from freqtrade.util.migrations.binance_mig import migrate_binance_futures_names # noqa F401
from freqtrade.util.migrations.binance_mig import migrate_binance_futures_data
from freqtrade.util.migrations.funding_fee_mig import migrate_funding_fee_timeframe
def migrate_data(config):
def migrate_data(config, exchange: Optional[Exchange] = None):
migrate_binance_futures_data(config)
migrate_funding_fee_timeframe(config, exchange)

View File

@ -0,0 +1,29 @@
import logging
from typing import Optional
from freqtrade.constants import Config
from freqtrade.data.history.idatahandler import get_datahandler
from freqtrade.enums import TradingMode
from freqtrade.exchange import Exchange
logger = logging.getLogger(__name__)
def migrate_funding_fee_timeframe(config: Config, exchange: Optional[Exchange]):
if (
config.get('trading_mode', TradingMode.SPOT) != TradingMode.FUTURES
):
# only act on futures
return
if not exchange:
from freqtrade.resolvers import ExchangeResolver
exchange = ExchangeResolver.load_exchange(config, validate=False)
ff_timeframe = exchange.get_option('funding_fee_timeframe')
logger.warning('Migrating funding fees to correct timeframe.')
dhc = get_datahandler(config['datadir'], config['dataformat_ohlcv'])
dhc.fix_funding_fee_timeframe(ff_timeframe)