Merge branch 'develop' into feat_readjust_entry

This commit is contained in:
eSeR1805
2022-05-04 21:43:41 +03:00
15 changed files with 314 additions and 139 deletions

View File

@@ -376,3 +376,38 @@ 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')
@pytest.mark.parametrize('profits,relative,highd,lowd,result,result_rel', [
([0.0, -500.0, 500.0, 10000.0, -1000.0], False, 3, 4, 1000.0, 0.090909),
([0.0, -500.0, 500.0, 10000.0, -1000.0], True, 0, 1, 500.0, 0.5),
])
def test_calculate_max_drawdown_abs(profits, relative, highd, lowd, result, result_rel):
"""
Test case from issue https://github.com/freqtrade/freqtrade/issues/6655
[1000, 500, 1000, 11000, 10000] # absolute results
[1000, 50%, 0%, 0%, ~9%] # Relative drawdowns
"""
init_date = Arrow(2020, 1, 1)
dates = [init_date.shift(days=i) for i in range(len(profits))]
df = DataFrame(zip(profits, dates), columns=['profit_abs', 'open_date'])
# sort by profit and reset index
df = df.sort_values('profit_abs').reset_index(drop=True)
df1 = df.copy()
drawdown, hdate, ldate, hval, lval, drawdown_rel = calculate_max_drawdown(
df, date_col='open_date', starting_balance=1000, relative=relative)
# Ensure df has not been altered.
assert df.equals(df1)
assert isinstance(drawdown, float)
assert isinstance(drawdown_rel, float)
assert hdate == init_date.shift(days=highd)
assert ldate == init_date.shift(days=lowd)
# High must be before low
assert hdate < ldate
# High value must be higher than low value
assert hval > lval
assert drawdown == result
assert pytest.approx(drawdown_rel) == result_rel

View File

@@ -4165,7 +4165,10 @@ def test__order_contracts_to_amount(
'cost': 60.0,
'filled': None,
'remaining': 30.0,
'fee': 0.06,
'fee': {
'currency': 'USDT',
'cost': 0.06,
},
'fees': [{
'currency': 'USDT',
'cost': 0.06,
@@ -4192,7 +4195,10 @@ def test__order_contracts_to_amount(
'cost': 80.0,
'filled': None,
'remaining': 40.0,
'fee': 0.08,
'fee': {
'currency': 'USDT',
'cost': 0.08,
},
'fees': [{
'currency': 'USDT',
'cost': 0.08,
@@ -4226,12 +4232,18 @@ def test__order_contracts_to_amount(
'info': {},
},
]
order1_bef = orders[0]
order2_bef = orders[1]
order1 = exchange._order_contracts_to_amount(deepcopy(order1_bef))
order2 = exchange._order_contracts_to_amount(deepcopy(order2_bef))
assert order1['amount'] == order1_bef['amount'] * contract_size
assert order1['cost'] == order1_bef['cost'] * contract_size
order1 = exchange._order_contracts_to_amount(orders[0])
order2 = exchange._order_contracts_to_amount(orders[1])
assert order2['amount'] == order2_bef['amount'] * contract_size
assert order2['cost'] == order2_bef['cost'] * contract_size
# Don't fail
exchange._order_contracts_to_amount(orders[2])
assert order1['amount'] == 30.0 * contract_size
assert order2['amount'] == 40.0 * contract_size
@pytest.mark.parametrize('pair,contract_size,trading_mode', [

View File

@@ -85,6 +85,7 @@ def test_loss_calculation_has_limited_profit(hyperopt_conf, hyperopt_results) ->
"SharpeHyperOptLoss",
"SharpeHyperOptLossDaily",
"MaxDrawDownHyperOptLoss",
"MaxDrawDownRelativeHyperOptLoss",
"CalmarHyperOptLoss",
"ProfitDrawDownHyperOptLoss",

View File

@@ -332,7 +332,13 @@ def test_generate_profit_graph(testdatadir):
trades = trades[trades['pair'].isin(pairs)]
fig = generate_profit_graph(pairs, data, trades, timeframe="5m", stake_currency='BTC')
fig = generate_profit_graph(
pairs,
data,
trades,
timeframe="5m",
stake_currency='BTC',
starting_balance=0)
assert isinstance(fig, go.Figure)
assert fig.layout.title.text == "Freqtrade Profit plot"
@@ -341,7 +347,7 @@ def test_generate_profit_graph(testdatadir):
assert fig.layout.yaxis3.title.text == "Profit BTC"
figure = fig.layout.figure
assert len(figure.data) == 7
assert len(figure.data) == 8
avgclose = find_trace_in_fig_data(figure.data, "Avg close price")
assert isinstance(avgclose, go.Scatter)
@@ -356,6 +362,9 @@ def test_generate_profit_graph(testdatadir):
underwater = find_trace_in_fig_data(figure.data, "Underwater Plot")
assert isinstance(underwater, go.Scatter)
underwater_relative = find_trace_in_fig_data(figure.data, "Underwater Plot (%)")
assert isinstance(underwater_relative, go.Scatter)
for pair in pairs:
profit_pair = find_trace_in_fig_data(figure.data, f"Profit {pair}")
assert isinstance(profit_pair, go.Scatter)
@@ -363,7 +372,7 @@ def test_generate_profit_graph(testdatadir):
with pytest.raises(OperationalException, match=r"No trades found.*"):
# Pair cannot be empty - so it's an empty dataframe.
generate_profit_graph(pairs, data, trades.loc[trades['pair'].isnull()], timeframe="5m",
stake_currency='BTC')
stake_currency='BTC', starting_balance=0)
def test_start_plot_dataframe(mocker):