Fix some tests after drawdown calculation change
This commit is contained in:
parent
7a2b50ce8b
commit
09fae25c94
@ -47,10 +47,9 @@ class CalmarHyperOptLoss(IHyperOptLoss):
|
|||||||
|
|
||||||
# calculate max drawdown
|
# calculate max drawdown
|
||||||
try:
|
try:
|
||||||
_, _, _, high_val, low_val = calculate_max_drawdown(
|
_, _, _, _, _, max_drawdown = calculate_max_drawdown(
|
||||||
results, value_col="profit_abs"
|
results, value_col="profit_abs"
|
||||||
)
|
)
|
||||||
max_drawdown = (high_val - low_val) / high_val
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
max_drawdown = 0
|
max_drawdown = 0
|
||||||
|
|
||||||
|
@ -55,7 +55,8 @@ class MaxDrawdown(IProtection):
|
|||||||
|
|
||||||
# Drawdown is always positive
|
# Drawdown is always positive
|
||||||
try:
|
try:
|
||||||
drawdown, _, _, _, _ = calculate_max_drawdown(trades_df, value_col='close_profit')
|
# TODO: This should use absolute profit calculation, considering account balance.
|
||||||
|
drawdown, _, _, _, _, _ = calculate_max_drawdown(trades_df, value_col='close_profit')
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return False, None, None
|
return False, None, None
|
||||||
|
|
||||||
|
@ -280,23 +280,23 @@ def test_create_cum_profit1(testdatadir):
|
|||||||
|
|
||||||
|
|
||||||
def test_calculate_max_drawdown(testdatadir):
|
def test_calculate_max_drawdown(testdatadir):
|
||||||
filename = testdatadir / "backtest-result_test.json"
|
filename = testdatadir / "backtest-result_new.json"
|
||||||
bt_data = load_backtest_data(filename)
|
bt_data = load_backtest_data(filename)
|
||||||
drawdown, hdate, lowdate, hval, lval = calculate_max_drawdown(bt_data)
|
drawdown_abs, hdate, lowdate, hval, lval, drawdown = calculate_max_drawdown(bt_data, value_col="profit_abs")
|
||||||
assert isinstance(drawdown, float)
|
assert isinstance(drawdown, float)
|
||||||
assert pytest.approx(drawdown) == 0.21142322
|
assert pytest.approx(drawdown) == 0.59495234
|
||||||
assert isinstance(hdate, Timestamp)
|
assert isinstance(hdate, Timestamp)
|
||||||
assert isinstance(lowdate, Timestamp)
|
assert isinstance(lowdate, Timestamp)
|
||||||
assert isinstance(hval, float)
|
assert isinstance(hval, float)
|
||||||
assert isinstance(lval, float)
|
assert isinstance(lval, float)
|
||||||
assert hdate == Timestamp('2018-01-24 14:25:00', tz='UTC')
|
assert hdate == Timestamp('2018-01-25 01:30:00', tz='UTC')
|
||||||
assert lowdate == Timestamp('2018-01-30 04:45:00', tz='UTC')
|
assert lowdate == Timestamp('2018-01-30 094:45:00', tz='UTC')
|
||||||
|
|
||||||
underwater = calculate_underwater(bt_data)
|
underwater = calculate_underwater(bt_data)
|
||||||
assert isinstance(underwater, DataFrame)
|
assert isinstance(underwater, DataFrame)
|
||||||
|
|
||||||
with pytest.raises(ValueError, match='Trade dataframe empty.'):
|
with pytest.raises(ValueError, match='Trade dataframe empty.'):
|
||||||
drawdown, hdate, lowdate, hval, lval = calculate_max_drawdown(DataFrame())
|
calculate_max_drawdown(DataFrame())
|
||||||
|
|
||||||
with pytest.raises(ValueError, match='Trade dataframe empty.'):
|
with pytest.raises(ValueError, match='Trade dataframe empty.'):
|
||||||
calculate_underwater(DataFrame())
|
calculate_underwater(DataFrame())
|
||||||
@ -331,12 +331,13 @@ def test_calculate_max_drawdown2():
|
|||||||
# sort by profit and reset index
|
# sort by profit and reset index
|
||||||
df = df.sort_values('profit').reset_index(drop=True)
|
df = df.sort_values('profit').reset_index(drop=True)
|
||||||
df1 = df.copy()
|
df1 = df.copy()
|
||||||
drawdown, hdate, ldate, hval, lval = calculate_max_drawdown(
|
drawdown, hdate, ldate, hval, lval, drawdown_rel = calculate_max_drawdown(
|
||||||
df, date_col='open_date', value_col='profit')
|
df, date_col='open_date', value_col='profit')
|
||||||
# Ensure df has not been altered.
|
# Ensure df has not been altered.
|
||||||
assert df.equals(df1)
|
assert df.equals(df1)
|
||||||
|
|
||||||
assert isinstance(drawdown, float)
|
assert isinstance(drawdown, float)
|
||||||
|
assert isinstance(drawdown_rel, float)
|
||||||
# High must be before low
|
# High must be before low
|
||||||
assert hdate < ldate
|
assert hdate < ldate
|
||||||
# High value must be higher than low value
|
# High value must be higher than low value
|
||||||
|
@ -103,7 +103,7 @@ def test_generate_backtest_stats(default_conf, testdatadir, tmpdir):
|
|||||||
assert strat_stats['backtest_end'] == max_date.strftime(DATETIME_PRINT_FORMAT)
|
assert strat_stats['backtest_end'] == max_date.strftime(DATETIME_PRINT_FORMAT)
|
||||||
assert strat_stats['total_trades'] == len(results['DefStrat']['results'])
|
assert strat_stats['total_trades'] == len(results['DefStrat']['results'])
|
||||||
# Above sample had no loosing trade
|
# Above sample had no loosing trade
|
||||||
assert strat_stats['max_drawdown'] == 0.0
|
assert strat_stats['max_drawdown_account'] == 0.0
|
||||||
|
|
||||||
# Retry with losing trade
|
# Retry with losing trade
|
||||||
results = {'DefStrat': {
|
results = {'DefStrat': {
|
||||||
@ -143,7 +143,7 @@ def test_generate_backtest_stats(default_conf, testdatadir, tmpdir):
|
|||||||
assert 'strategy_comparison' in stats
|
assert 'strategy_comparison' in stats
|
||||||
strat_stats = stats['strategy']['DefStrat']
|
strat_stats = stats['strategy']['DefStrat']
|
||||||
|
|
||||||
assert strat_stats['max_drawdown'] == 0.013803
|
assert pytest.approx(strat_stats['max_drawdown_account']) == 1.399999e-08
|
||||||
assert strat_stats['drawdown_start'] == '2017-11-14 22:10:00'
|
assert strat_stats['drawdown_start'] == '2017-11-14 22:10:00'
|
||||||
assert strat_stats['drawdown_end'] == '2017-11-14 22:43:00'
|
assert strat_stats['drawdown_end'] == '2017-11-14 22:43:00'
|
||||||
assert strat_stats['drawdown_end_ts'] == 1510699380000
|
assert strat_stats['drawdown_end_ts'] == 1510699380000
|
||||||
@ -165,7 +165,7 @@ def test_generate_backtest_stats(default_conf, testdatadir, tmpdir):
|
|||||||
filename1 = Path(tmpdir / last_fn)
|
filename1 = Path(tmpdir / last_fn)
|
||||||
assert filename1.is_file()
|
assert filename1.is_file()
|
||||||
content = filename1.read_text()
|
content = filename1.read_text()
|
||||||
assert 'max_drawdown' in content
|
assert 'max_drawdown_account' in content
|
||||||
assert 'strategy' in content
|
assert 'strategy' in content
|
||||||
assert 'pairlist' in content
|
assert 'pairlist' in content
|
||||||
|
|
||||||
|
@ -343,7 +343,7 @@ def test_generate_profit_graph(testdatadir):
|
|||||||
|
|
||||||
profit = find_trace_in_fig_data(figure.data, "Profit")
|
profit = find_trace_in_fig_data(figure.data, "Profit")
|
||||||
assert isinstance(profit, go.Scatter)
|
assert isinstance(profit, go.Scatter)
|
||||||
drawdown = find_trace_in_fig_data(figure.data, "Max drawdown 10.45%")
|
drawdown = find_trace_in_fig_data(figure.data, "Max drawdown 35.69%")
|
||||||
assert isinstance(drawdown, go.Scatter)
|
assert isinstance(drawdown, go.Scatter)
|
||||||
parallel = find_trace_in_fig_data(figure.data, "Parallel trades")
|
parallel = find_trace_in_fig_data(figure.data, "Parallel trades")
|
||||||
assert isinstance(parallel, go.Scatter)
|
assert isinstance(parallel, go.Scatter)
|
||||||
|
Loading…
Reference in New Issue
Block a user