Skip to content

Commit 1ca594b

Browse files
committed
chore: show "total" profit if necessary
1 parent e66611c commit 1ca594b

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

freqtrade/rpc/rpc.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def _rpc_trade_status(self, trade_ids: list[int] | None = None) -> list[dict[str
287287

288288
def _rpc_status_table(
289289
self, stake_currency: str, fiat_display_currency: str
290-
) -> tuple[list, list, float]:
290+
) -> tuple[list, list, float, float]:
291291
"""
292292
:return: list of trades, list of columns, sum of fiat profit
293293
"""
@@ -297,17 +297,27 @@ def _rpc_status_table(
297297

298298
trades_list = []
299299
fiat_profit_sum = nan
300+
fiat_total_profit_sum = nan
300301
for trade in self._rpc_trade_status():
301302
# Format profit as a string with the right sign
302303
profit = f"{trade['profit_ratio']:.2%}"
303304
fiat_profit = trade.get("profit_fiat", None)
304305
if fiat_profit is None or isnan(fiat_profit):
305-
fiat_profit: float = trade.get("profit_abs", 0.0)
306+
fiat_profit = trade.get("profit_abs", 0.0)
306307
if not isnan(fiat_profit):
307308
profit += f" ({fiat_profit:.2f})"
308309
fiat_profit_sum = (
309310
fiat_profit if isnan(fiat_profit_sum) else fiat_profit_sum + fiat_profit
310311
)
312+
total_profit = trade.get("total_profit_fiat", None)
313+
if total_profit is None or isnan(total_profit):
314+
total_profit = trade.get("total_profit_abs", 0.0)
315+
if not isnan(total_profit):
316+
fiat_total_profit_sum = (
317+
total_profit
318+
if isnan(fiat_total_profit_sum)
319+
else fiat_total_profit_sum + total_profit
320+
)
311321

312322
# Format the active order side symbols
313323
active_order_side = ""
@@ -352,7 +362,7 @@ def _rpc_status_table(
352362
if self._config.get("position_adjustment_enable", False):
353363
columns.append("# Entries")
354364

355-
return trades_list, columns, fiat_profit_sum
365+
return trades_list, columns, fiat_profit_sum, fiat_total_profit_sum
356366

357367
def _rpc_timeunit_profit(
358368
self,

freqtrade/rpc/telegram.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -858,11 +858,14 @@ async def _status_table(self, update: Update, context: CallbackContext) -> None:
858858
:return: None
859859
"""
860860
fiat_currency = self._config.get("fiat_display_currency", "")
861-
statlist, head, fiat_profit_sum = self._rpc._rpc_status_table(
861+
statlist, head, fiat_profit_sum, fiat_total_profit_sum = self._rpc._rpc_status_table(
862862
self._config["stake_currency"], fiat_currency
863863
)
864864

865865
show_total = not isnan(fiat_profit_sum) and len(statlist) > 1
866+
show_total_realized = (
867+
not isnan(fiat_total_profit_sum) and len(statlist) > 1 and fiat_profit_sum
868+
) != fiat_total_profit_sum
866869
max_trades_per_msg = 50
867870
"""
868871
Calculate the number of messages of 50 trades per message
@@ -875,12 +878,22 @@ async def _status_table(self, update: Update, context: CallbackContext) -> None:
875878
if show_total and i == messages_count - 1:
876879
# append total line
877880
trades.append(["Total", "", "", f"{fiat_profit_sum:.2f} {fiat_currency}"])
881+
if show_total_realized:
882+
trades.append(
883+
[
884+
"Total",
885+
"(incl. realized Profits)",
886+
"",
887+
f"{fiat_total_profit_sum:.2f} {fiat_currency}",
888+
]
889+
)
878890

879891
message = tabulate(trades, headers=head, tablefmt="simple")
880892
if show_total and i == messages_count - 1:
881893
# insert separators line between Total
882894
lines = message.split("\n")
883-
message = "\n".join(lines[:-1] + [lines[1]] + [lines[-1]])
895+
offset = 2 if show_total_realized else 1
896+
message = "\n".join(lines[:-offset] + [lines[1]] + lines[-offset:])
884897
await self._send_msg(
885898
f"<pre>{message}</pre>",
886899
parse_mode=ParseMode.HTML,
@@ -1287,7 +1300,7 @@ async def _force_exit(self, update: Update, context: CallbackContext) -> None:
12871300
else:
12881301
fiat_currency = self._config.get("fiat_display_currency", "")
12891302
try:
1290-
statlist, _, _ = self._rpc._rpc_status_table(
1303+
statlist, _, _, _ = self._rpc._rpc_status_table(
12911304
self._config["stake_currency"], fiat_currency
12921305
)
12931306
except RPCException:

tests/rpc/test_rpc.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,23 @@ def test_rpc_status_table(default_conf, ticker, fee, mocker) -> None:
251251
mocker.patch(f"{EXMS}._dry_is_price_crossed", return_value=False)
252252
freqtradebot.enter_positions()
253253

254-
result, headers, fiat_profit_sum = rpc._rpc_status_table(default_conf["stake_currency"], "USD")
254+
result, headers, fiat_profit_sum, total_sum = rpc._rpc_status_table(
255+
default_conf["stake_currency"], "USD"
256+
)
255257
assert "Since" in headers
256258
assert "Pair" in headers
257259
assert "now" == result[0][2]
258260
assert "ETH/BTC" in result[0][1]
259261
assert "0.00% (0.00)" == result[0][3]
260262
assert "0.00" == f"{fiat_profit_sum:.2f}"
263+
assert "0.00" == f"{total_sum:.2f}"
261264

262265
mocker.patch(f"{EXMS}._dry_is_price_crossed", return_value=True)
263266
freqtradebot.process()
264267

265-
result, headers, fiat_profit_sum = rpc._rpc_status_table(default_conf["stake_currency"], "USD")
268+
result, headers, fiat_profit_sum, total_sum = rpc._rpc_status_table(
269+
default_conf["stake_currency"], "USD"
270+
)
266271
assert "Since" in headers
267272
assert "Pair" in headers
268273
assert "now" == result[0][2]
@@ -273,18 +278,23 @@ def test_rpc_status_table(default_conf, ticker, fee, mocker) -> None:
273278
# Test with fiat convert
274279
rpc._config["fiat_display_currency"] = "USD"
275280
rpc._fiat_converter = CryptoToFiatConverter({})
276-
result, headers, fiat_profit_sum = rpc._rpc_status_table(default_conf["stake_currency"], "USD")
281+
result, headers, fiat_profit_sum, total_sum = rpc._rpc_status_table(
282+
default_conf["stake_currency"], "USD"
283+
)
277284
assert "Since" in headers
278285
assert "Pair" in headers
279286
assert len(result[0]) == 4
280287
assert "now" == result[0][2]
281288
assert "ETH/BTC" in result[0][1]
282289
assert "-0.41% (-0.06)" == result[0][3]
283290
assert "-0.06" == f"{fiat_profit_sum:.2f}"
291+
assert "-0.06" == f"{total_sum:.2f}"
284292

285293
rpc._config["position_adjustment_enable"] = True
286294
rpc._config["max_entry_position_adjustment"] = 3
287-
result, headers, fiat_profit_sum = rpc._rpc_status_table(default_conf["stake_currency"], "USD")
295+
result, headers, fiat_profit_sum, total_sum = rpc._rpc_status_table(
296+
default_conf["stake_currency"], "USD"
297+
)
288298
assert "# Entries" in headers
289299
assert len(result[0]) == 5
290300
# 4th column should be 1/4 - as 1 order filled (a total of 4 is possible)
@@ -294,7 +304,9 @@ def test_rpc_status_table(default_conf, ticker, fee, mocker) -> None:
294304
mocker.patch(
295305
f"{EXMS}.get_rate", MagicMock(side_effect=ExchangeError("Pair 'ETH/BTC' not available"))
296306
)
297-
result, headers, fiat_profit_sum = rpc._rpc_status_table(default_conf["stake_currency"], "USD")
307+
result, headers, fiat_profit_sum, total_sum = rpc._rpc_status_table(
308+
default_conf["stake_currency"], "USD"
309+
)
298310
assert "now" == result[0][2]
299311
assert "ETH/BTC" in result[0][1]
300312
assert "nan%" == result[0][3]

0 commit comments

Comments
 (0)