make drawdown function less restrictive
This commit is contained in:
parent
88e7cab5b9
commit
33a63562cb
@ -190,21 +190,26 @@ def create_cum_profit(df: pd.DataFrame, trades: pd.DataFrame, col_name: str,
|
|||||||
return df
|
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
|
Calculate max drawdown and the corresponding close dates
|
||||||
:param trades: DataFrame containing trades (requires columns close_time and profitperc)
|
: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
|
:return: Tuple (float, highdate, lowdate) with absolute max drawdown, high and low time
|
||||||
:raise: ValueError if trade-dataframe was found empty.
|
:raise: ValueError if trade-dataframe was found empty.
|
||||||
"""
|
"""
|
||||||
if len(trades) == 0:
|
if len(trades) == 0:
|
||||||
raise ValueError("Trade dataframe empty.")
|
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 = 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['high_value'] = max_drawdown_df['cumulative'].cummax()
|
||||||
max_drawdown_df['drawdown'] = max_drawdown_df['cumulative'] - max_drawdown_df['high_value']
|
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
|
return abs(min(max_drawdown_df['drawdown'])), high_date, low_date
|
||||||
|
Loading…
Reference in New Issue
Block a user