Add wallets to backtesting

This commit is contained in:
Matthias 2021-01-28 07:06:58 +01:00
parent f0a154692d
commit 9361aa1c95

View File

@ -28,6 +28,7 @@ from freqtrade.plugins.protectionmanager import ProtectionManager
from freqtrade.resolvers import ExchangeResolver, StrategyResolver from freqtrade.resolvers import ExchangeResolver, StrategyResolver
from freqtrade.strategy.interface import IStrategy, SellCheckTuple, SellType from freqtrade.strategy.interface import IStrategy, SellCheckTuple, SellType
from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper
from freqtrade.wallets import Wallets
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -114,6 +115,8 @@ class Backtesting:
if self.config.get('enable_protections', False): if self.config.get('enable_protections', False):
self.protections = ProtectionManager(self.config) self.protections = ProtectionManager(self.config)
self.wallets = Wallets(self.config, self.exchange)
# Get maximum required startup period # Get maximum required startup period
self.required_startup = max([strat.startup_candle_count for strat in self.strategylist]) self.required_startup = max([strat.startup_candle_count for strat in self.strategylist])
# Load one (first) strategy # Load one (first) strategy
@ -176,6 +179,10 @@ class Backtesting:
PairLocks.reset_locks() PairLocks.reset_locks()
Trade.reset_trades() Trade.reset_trades()
def update_wallets(self):
if self.wallets:
self.wallets.update(log=False)
def _get_ohlcv_as_lists(self, processed: Dict[str, DataFrame]) -> Dict[str, Tuple]: def _get_ohlcv_as_lists(self, processed: Dict[str, DataFrame]) -> Dict[str, Tuple]:
""" """
Helper function to convert a processed dataframes into lists for performance reasons. Helper function to convert a processed dataframes into lists for performance reasons.
@ -276,8 +283,10 @@ class Backtesting:
trade.close_date = sell_row[DATE_IDX] trade.close_date = sell_row[DATE_IDX]
trade.sell_reason = SellType.FORCE_SELL trade.sell_reason = SellType.FORCE_SELL
trade.close(sell_row[OPEN_IDX], show_msg=False) trade.close(sell_row[OPEN_IDX], show_msg=False)
trade.is_open = True # Deepcopy object to have wallets update correctly
trades.append(trade) trade1 = deepcopy(trade)
trade1.is_open = True
trades.append(trade1)
return trades return trades
def backtest(self, processed: Dict, stake_amount: float, def backtest(self, processed: Dict, stake_amount: float,
@ -346,6 +355,7 @@ class Backtesting:
and tmp != end_date and tmp != end_date
and row[BUY_IDX] == 1 and row[SELL_IDX] != 1 and row[BUY_IDX] == 1 and row[SELL_IDX] != 1
and not PairLocks.is_pair_locked(pair, row[DATE_IDX])): and not PairLocks.is_pair_locked(pair, row[DATE_IDX])):
self.update_wallets()
# Enter trade # Enter trade
trade = Trade( trade = Trade(
pair=pair, pair=pair,
@ -372,6 +382,7 @@ class Backtesting:
trade_entry = self._get_sell_trade_entry(trade, row) trade_entry = self._get_sell_trade_entry(trade, row)
# Sell occured # Sell occured
if trade_entry: if trade_entry:
self.update_wallets()
# logger.debug(f"{pair} - Backtesting sell {trade}") # logger.debug(f"{pair} - Backtesting sell {trade}")
open_trade_count -= 1 open_trade_count -= 1
open_trades[pair].remove(trade) open_trades[pair].remove(trade)
@ -384,6 +395,7 @@ class Backtesting:
tmp += timedelta(minutes=self.timeframe_min) tmp += timedelta(minutes=self.timeframe_min)
trades += self.handle_left_open(open_trades, data=data) trades += self.handle_left_open(open_trades, data=data)
self.update_wallets()
return trade_list_to_dataframe(trades) return trade_list_to_dataframe(trades)
@ -425,6 +437,7 @@ class Backtesting:
enable_protections=self.config.get('enable_protections', False), enable_protections=self.config.get('enable_protections', False),
) )
backtest_end_time = datetime.now(timezone.utc) backtest_end_time = datetime.now(timezone.utc)
print(self.wallets.get_all_balances())
self.all_results[self.strategy.get_strategy_name()] = { self.all_results[self.strategy.get_strategy_name()] = {
'results': results, 'results': results,
'config': self.strategy.config, 'config': self.strategy.config,