test done

This commit is contained in:
Guitheg 2021-12-13 10:15:34 +01:00
parent 91e57f64d4
commit d08b0918ad
2 changed files with 59 additions and 48 deletions

View File

@ -390,6 +390,7 @@ def calculate_max_drawdown(trades: pd.DataFrame, *, date_col: str = 'close_date'
low_val = max_drawdown_df.loc[idxmin, 'cumulative']
return abs(min(max_drawdown_df['drawdown'])), high_date, low_date, high_val, low_val
# TODO : is supposed to work only with long positions
def calculate_trades_mdd(data: dict, trades: pd.DataFrame) -> float :
"""
Calculate Trades MDD (Max DrawDown) :
@ -410,9 +411,7 @@ def calculate_trades_mdd(data: dict, trades: pd.DataFrame) -> float :
trades_mdd_pair_list = []
for pair, df in data.items():
if df is None:
break
if isinstance(df, pd.DataFrame):
# Gather the opening and closing trade dates into one Dates DataFrame
open_close_trades = trades.loc[trades['pair']==pair][["open_date","close_date"]]
open_close_trades = pd.concat(

View File

@ -1,7 +1,7 @@
from math import isclose
from pathlib import Path
from unittest.mock import MagicMock
import numpy as np
import pytest
from arrow import Arrow
from pandas import DataFrame, DateOffset, Timestamp, to_datetime
@ -10,7 +10,7 @@ from freqtrade.configuration import TimeRange
from freqtrade.constants import LAST_BT_RESULT_FN
from freqtrade.data.btanalysis import (BT_DATA_COLUMNS, BT_DATA_COLUMNS_MID, BT_DATA_COLUMNS_OLD,
analyze_trade_parallelism, calculate_csum,
calculate_market_change, calculate_max_drawdown,
calculate_market_change, calculate_max_drawdown, calculate_trades_mdd,
combine_dataframes_with_mean, create_cum_profit,
extract_trades_of_period, get_latest_backtest_filename,
get_latest_hyperopt_file, load_backtest_data, load_trades,
@ -332,3 +332,15 @@ def test_calculate_max_drawdown2():
df = DataFrame(zip(values[:5], dates[:5]), columns=['profit', 'open_date'])
with pytest.raises(ValueError, match='No losing trade, therefore no drawdown.'):
calculate_max_drawdown(df, date_col='open_date', value_col='profit')
def test_calculate_trades_mdd(testdatadir):
backtest_file = testdatadir / "backtest-result_test.json"
trades = load_backtest_data(backtest_file)
pairlist = set(trades["pair"])
with pytest.raises(ValueError, match='All dataframe in candle data are None'):
calculate_trades_mdd({"BTC/BUSD" : None}, trades)
data = load_data(datadir=testdatadir, pairs=pairlist, timeframe='5m')
trades_mdd = calculate_trades_mdd(data, trades)
assert np.round(trades_mdd, 6) == 0.138943