Properly include fees in total profit calculation

This commit is contained in:
Matthias 2022-07-22 07:07:59 +02:00
parent 9382c4bd75
commit 4e8680a67f
3 changed files with 8 additions and 8 deletions

View File

@ -837,7 +837,7 @@ class LocalTrade():
current_amount = 0.0 current_amount = 0.0
current_stake = 0.0 current_stake = 0.0
total_stake = 0.0 # Total stake after all buy orders (does not subtract!) total_stake = 0.0 # Total stake after all buy orders (does not subtract!)
avg_price = None avg_price = 0.0
close_profit = 0.0 close_profit = 0.0
close_profit_abs = 0.0 close_profit_abs = 0.0
@ -855,8 +855,6 @@ class LocalTrade():
price = avg_price if is_exit else tmp_price price = avg_price if is_exit else tmp_price
current_stake += price * tmp_amount * side current_stake += price * tmp_amount * side
total_stake = total_stake + (price * tmp_amount) if not is_exit else total_stake
if current_amount > 0: if current_amount > 0:
avg_price = current_stake / current_amount avg_price = current_stake / current_amount
@ -870,6 +868,8 @@ class LocalTrade():
exit_rate, amount=exit_amount, open_rate=avg_price) exit_rate, amount=exit_amount, open_rate=avg_price)
if current_amount <= 0: if current_amount <= 0:
profit = close_profit_abs profit = close_profit_abs
else:
total_stake = total_stake + self._calc_open_trade_value(tmp_amount, price)
if close_profit: if close_profit:
self.close_profit = close_profit self.close_profit = close_profit

View File

@ -3673,7 +3673,7 @@ def test_execute_trade_exit_market_order(
) )
assert not trade.is_open assert not trade.is_open
assert trade.close_profit == profit_ratio assert pytest.approx(trade.close_profit) == profit_ratio
assert rpc_mock.call_count == 4 assert rpc_mock.call_count == 4
last_msg = rpc_mock.call_args_list[-2][0][0] last_msg = rpc_mock.call_args_list[-2][0][0]
@ -5752,7 +5752,7 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None:
(('buy', 100, 15), (200.0, 12.5, 2500.0, 0.0, None, None)), (('buy', 100, 15), (200.0, 12.5, 2500.0, 0.0, None, None)),
(('sell', 50, 12), (150.0, 12.5, 1875.0, -28.0625, -28.0625, -0.044788)), (('sell', 50, 12), (150.0, 12.5, 1875.0, -28.0625, -28.0625, -0.044788)),
(('sell', 100, 20), (50.0, 12.5, 625.0, 713.8125, 741.875, 0.59201995)), (('sell', 100, 20), (50.0, 12.5, 625.0, 713.8125, 741.875, 0.59201995)),
(('sell', 50, 5), (50.0, 12.5, 625.0, 336.625, 336.625, 0.13465)), # final profit (sum) (('sell', 50, 5), (50.0, 12.5, 625.0, 336.625, 336.625, 0.1343142)), # final profit (sum)
), ),
( (
(('buy', 100, 3), (100.0, 3.0, 300.0, 0.0, None, None)), (('buy', 100, 3), (100.0, 3.0, 300.0, 0.0, None, None)),
@ -5760,7 +5760,7 @@ def test_position_adjust2(mocker, default_conf_usdt, fee) -> None:
(('sell', 100, 11), (100.0, 5.0, 500.0, 596.0, 596.0, 1.189027)), (('sell', 100, 11), (100.0, 5.0, 500.0, 596.0, 596.0, 1.189027)),
(('buy', 150, 15), (250.0, 11.0, 2750.0, 596.0, 596.0, 1.189027)), (('buy', 150, 15), (250.0, 11.0, 2750.0, 596.0, 596.0, 1.189027)),
(('sell', 100, 19), (150.0, 11.0, 1650.0, 1388.5, 792.5, 0.7186579)), (('sell', 100, 19), (150.0, 11.0, 1650.0, 1388.5, 792.5, 0.7186579)),
(('sell', 150, 23), (150.0, 11.0, 1650.0, 3175.75, 3175.75, 0.977153)), # final profit (('sell', 150, 23), (150.0, 11.0, 1650.0, 3175.75, 3175.75, 0.9747170)), # final profit
) )
]) ])
def test_position_adjust3(mocker, default_conf_usdt, fee, data) -> None: def test_position_adjust3(mocker, default_conf_usdt, fee, data) -> None:

View File

@ -2797,7 +2797,7 @@ def test_order_to_ccxt(limit_buy_order_open):
(('sell', 50, 5), (50.0, 12.5, 625.0, 713.8125, -377.1875, -0.60199501)), (('sell', 50, 5), (50.0, 12.5, 625.0, 713.8125, -377.1875, -0.60199501)),
], ],
'end_profit': 336.625, 'end_profit': 336.625,
'end_profit_ratio': 0.13465, 'end_profit_ratio': 0.1343142,
'fee': 0.0025, 'fee': 0.0025,
}, },
{ {
@ -2810,7 +2810,7 @@ def test_order_to_ccxt(limit_buy_order_open):
(('sell', 150, 23), (150.0, 11.0, 1650.0, 1388.5, 1787.25, 1.08048062)), (('sell', 150, 23), (150.0, 11.0, 1650.0, 1388.5, 1787.25, 1.08048062)),
], ],
'end_profit': 3175.75, 'end_profit': 3175.75,
'end_profit_ratio': 0.977153, 'end_profit_ratio': 0.9747170,
'fee': 0.0025, 'fee': 0.0025,
}, },
{ {