Don't calculate funding_fees on every iteration

This commit is contained in:
Matthias
2023-12-10 13:23:40 +01:00
parent 966eb59fd3
commit 074343f0f1
+19 -11
View File
@@ -283,11 +283,13 @@ class Backtesting:
else: else:
self.detail_data = {} self.detail_data = {}
if self.trading_mode == TradingMode.FUTURES: if self.trading_mode == TradingMode.FUTURES:
self.funding_fee_timeframe: str = self.exchange.get_option('mark_ohlcv_timeframe')
self.funding_fee_timeframe_secs: int = timeframe_to_seconds(self.funding_fee_timeframe)
# Load additional futures data. # Load additional futures data.
funding_rates_dict = history.load_data( funding_rates_dict = history.load_data(
datadir=self.config['datadir'], datadir=self.config['datadir'],
pairs=self.pairlists.whitelist, pairs=self.pairlists.whitelist,
timeframe=self.exchange.get_option('mark_ohlcv_timeframe'), timeframe=self.funding_fee_timeframe,
timerange=self.timerange, timerange=self.timerange,
startup_candles=0, startup_candles=0,
fail_without_data=True, fail_without_data=True,
@@ -299,7 +301,7 @@ class Backtesting:
mark_rates_dict = history.load_data( mark_rates_dict = history.load_data(
datadir=self.config['datadir'], datadir=self.config['datadir'],
pairs=self.pairlists.whitelist, pairs=self.pairlists.whitelist,
timeframe=self.exchange.get_option('mark_ohlcv_timeframe'), timeframe=self.funding_fee_timeframe,
timerange=self.timerange, timerange=self.timerange,
startup_candles=0, startup_candles=0,
fail_without_data=True, fail_without_data=True,
@@ -744,20 +746,26 @@ class Backtesting:
return t return t
return None return None
def _run_funding_fees(self, trade: Trade, current_time: datetime): def _run_funding_fees(self, trade: Trade, current_time: datetime, force: bool = False):
""" """
Calculate funding fees if necessary and add them to the trade. Calculate funding fees if necessary and add them to the trade.
""" """
if self.trading_mode == TradingMode.FUTURES: if self.trading_mode == TradingMode.FUTURES:
trade.set_funding_fees(
self.exchange.calculate_funding_fees( if (
self.futures_data[trade.pair], force
amount=trade.amount, or (current_time.timestamp() % self.funding_fee_timeframe_secs) == 0
is_short=trade.is_short, ):
open_date=trade.date_last_filled_utc, # Funding fee interval.
close_date=current_time trade.set_funding_fees(
self.exchange.calculate_funding_fees(
self.futures_data[trade.pair],
amount=trade.amount,
is_short=trade.is_short,
open_date=trade.date_last_filled_utc,
close_date=current_time
)
) )
)
def get_valid_price_and_stake( def get_valid_price_and_stake(
self, pair: str, row: Tuple, propose_rate: float, stake_amount: float, self, pair: str, row: Tuple, propose_rate: float, stake_amount: float,