From 45f2d018959edf180cc79f940edb41a0504621cb Mon Sep 17 00:00:00 2001 From: Jean-Baptiste LE STANG Date: Wed, 3 Jan 2018 11:19:46 +0100 Subject: [PATCH] - add a profit/loss counter - the use of the sell_signal is conditional now (taken from the config) --- freqtrade/optimize/backtesting.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index e87af5aad..491e5c573 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -41,7 +41,7 @@ def generate_text_table( floatfmt = ('s', 'd', '.2f', '.8f', '.1f') tabular_data = [] headers = ['pair', 'buy count', 'avg profit %', - 'total profit ' + stake_currency, 'avg duration'] + 'total profit ' + stake_currency, 'avg duration', 'profit','loss'] for pair in data: result = results[results.currency == pair] tabular_data.append([ @@ -50,6 +50,8 @@ def generate_text_table( result.profit_percent.mean() * 100.0, result.profit_BTC.sum(), result.duration.mean() * ticker_interval, + result.profit.sum(), + result.loss.sum() ]) # Append Total @@ -59,12 +61,15 @@ def generate_text_table( results.profit_percent.mean() * 100.0, results.profit_BTC.sum(), results.duration.mean() * ticker_interval, + results.profit.sum(), + results.loss.sum(), + ]) return tabulate(tabular_data, headers=headers, floatfmt=floatfmt) def backtest(stake_amount: float, processed: Dict[str, DataFrame], - max_open_trades: int = 0, realistic: bool = True, sell_profit_only: bool = False, stoploss: int = -1.00) -> DataFrame: + max_open_trades: int = 0, realistic: bool = True, sell_profit_only: bool = False, stoploss: int = -1.00, use_sell_signal: bool = False) -> DataFrame: """ Implements backtesting functionality :param stake_amount: btc amount to use for each trade @@ -113,7 +118,7 @@ def backtest(stake_amount: float, processed: Dict[str, DataFrame], current_profit_percent = trade.calc_profit_percent(rate=row2.close) if (sell_profit_only and current_profit_percent < 0) : continue - if min_roi_reached(trade, row2.close, row2.date) or row2.sell == 1 or current_profit_percent < stoploss: + if min_roi_reached(trade, row2.close, row2.date) or (row2.sell == 1 and use_sell_signal) or current_profit_percent <= stoploss: current_profit_btc = trade.calc_profit(rate=row2.close) lock_pair_until = row2.Index @@ -122,11 +127,13 @@ def backtest(stake_amount: float, processed: Dict[str, DataFrame], pair, current_profit_percent, current_profit_btc, - row2.Index - row.Index + row2.Index - row.Index, + current_profit_btc > 0, + current_profit_btc < 0 ) ) break - labels = ['currency', 'profit_percent', 'profit_BTC', 'duration'] + labels = ['currency', 'profit_percent', 'profit_BTC', 'duration', 'profit','loss'] return DataFrame.from_records(trades, columns=labels) @@ -174,7 +181,7 @@ def start(args): # Execute backtest and print results results = backtest( - config['stake_amount'], preprocessed, max_open_trades, args.realistic_simulation, config.get('experimental',{}).get('sell_profit_only', False), config.get('stoploss') + config['stake_amount'], preprocessed, max_open_trades, args.realistic_simulation, config.get('experimental',{}).get('sell_profit_only', False), config.get('stoploss'), config.get('experimental',{}).get('use_sell_signal',False) ) logger.info( '\n====================== BACKTESTING REPORT ================================\n%s',