From 33a63562cbacdeb858a29b09c4a11b9d3f31ae7b Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 3 Mar 2020 07:23:38 +0100 Subject: [PATCH] make drawdown function less restrictive --- freqtrade/data/btanalysis.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/freqtrade/data/btanalysis.py b/freqtrade/data/btanalysis.py index 799f15011..394c40112 100644 --- a/freqtrade/data/btanalysis.py +++ b/freqtrade/data/btanalysis.py @@ -190,21 +190,26 @@ def create_cum_profit(df: pd.DataFrame, trades: pd.DataFrame, col_name: str, return df -def calculate_max_drawdown(trades: pd.DataFrame) -> Tuple[float, pd.Timestamp, pd.Timestamp]: +def calculate_max_drawdown(trades: pd.DataFrame, date_col: str = 'close_time', + value_col: str = 'profitperc' + ) -> Tuple[float, pd.Timestamp, pd.Timestamp]: """ Calculate max drawdown and the corresponding close dates :param trades: DataFrame containing trades (requires columns close_time and profitperc) + :param date_col: Column in DataFrame to use for dates (defaults to 'close_time') + :param value_col: Column in DataFrame to use for values (defaults to 'profitperc') :return: Tuple (float, highdate, lowdate) with absolute max drawdown, high and low time :raise: ValueError if trade-dataframe was found empty. """ if len(trades) == 0: raise ValueError("Trade dataframe empty.") - profit_results = trades.sort_values('close_time') + profit_results = trades.sort_values(date_col) max_drawdown_df = pd.DataFrame() - max_drawdown_df['cumulative'] = profit_results['profitperc'].cumsum() + max_drawdown_df['cumulative'] = profit_results[value_col].cumsum() max_drawdown_df['high_value'] = max_drawdown_df['cumulative'].cummax() max_drawdown_df['drawdown'] = max_drawdown_df['cumulative'] - max_drawdown_df['high_value'] - high_date = profit_results.loc[max_drawdown_df['high_value'].idxmax(), 'close_time'] - low_date = profit_results.loc[max_drawdown_df['drawdown'].idxmin(), 'close_time'] + + high_date = profit_results.loc[max_drawdown_df['high_value'].idxmax(), date_col] + low_date = profit_results.loc[max_drawdown_df['drawdown'].idxmin(), date_col] return abs(min(max_drawdown_df['drawdown'])), high_date, low_date