Simplify profit calculation (removes unnecessary fallback)

This commit is contained in:
Matthias
2023-08-29 18:31:51 +02:00
parent f3187ddcbf
commit 23aa8dcd51
4 changed files with 8 additions and 5 deletions

View File

@@ -924,15 +924,14 @@ class LocalTrade:
short_close_zero = (self.is_short and close_trade_value == 0.0) short_close_zero = (self.is_short and close_trade_value == 0.0)
long_close_zero = (not self.is_short and open_trade_value == 0.0) long_close_zero = (not self.is_short and open_trade_value == 0.0)
leverage = self.leverage or 1.0
if (short_close_zero or long_close_zero): if (short_close_zero or long_close_zero):
return 0.0 return 0.0
else: else:
if self.is_short: if self.is_short:
profit_ratio = (1 - (close_trade_value / open_trade_value)) * leverage profit_ratio = (1 - (close_trade_value / open_trade_value)) * self.leverage
else: else:
profit_ratio = ((close_trade_value / open_trade_value) - 1) * leverage profit_ratio = ((close_trade_value / open_trade_value) - 1) * self.leverage
return float(f"{profit_ratio:.8f}") return float(f"{profit_ratio:.8f}")

View File

@@ -986,7 +986,7 @@ def test_api_performance(botclient, fee):
fee_close=fee.return_value, fee_close=fee.return_value,
fee_open=fee.return_value, fee_open=fee.return_value,
close_rate=0.265441, close_rate=0.265441,
leverage=1.0,
) )
trade.close_profit = trade.calc_profit_ratio(trade.close_rate) trade.close_profit = trade.calc_profit_ratio(trade.close_rate)
trade.close_profit_abs = trade.calc_profit(trade.close_rate) trade.close_profit_abs = trade.calc_profit(trade.close_rate)
@@ -1002,7 +1002,8 @@ def test_api_performance(botclient, fee):
is_open=False, is_open=False,
fee_close=fee.return_value, fee_close=fee.return_value,
fee_open=fee.return_value, fee_open=fee.return_value,
close_rate=0.391 close_rate=0.391,
leverage=1.0,
) )
trade.close_profit = trade.calc_profit_ratio(trade.close_rate) trade.close_profit = trade.calc_profit_ratio(trade.close_rate)
trade.close_profit_abs = trade.calc_profit(trade.close_rate) trade.close_profit_abs = trade.calc_profit(trade.close_rate)

View File

@@ -503,6 +503,7 @@ def test_custom_exit(default_conf, fee, caplog) -> None:
fee_close=fee.return_value, fee_close=fee.return_value,
exchange='binance', exchange='binance',
open_rate=1, open_rate=1,
leverage=1.0,
) )
now = dt_now() now = dt_now()
@@ -552,6 +553,7 @@ def test_should_sell(default_conf, fee) -> None:
fee_close=fee.return_value, fee_close=fee.return_value,
exchange='binance', exchange='binance',
open_rate=1, open_rate=1,
leverage=1.0,
) )
now = dt_now() now = dt_now()
res = strategy.should_exit(trade, 1, now, res = strategy.should_exit(trade, 1, now,

View File

@@ -3458,6 +3458,7 @@ def test_handle_cancel_exit_limit(mocker, default_conf_usdt, fee) -> None:
close_date=dt_now(), close_date=dt_now(),
exit_reason="sell_reason_whatever", exit_reason="sell_reason_whatever",
stake_amount=0.245441 * 2, stake_amount=0.245441 * 2,
leverage=1,
) )
trade.orders = [ trade.orders = [
Order( Order(