Skip to content

Commit

Permalink
Adds unrealized_pnl to EvalMetrics.
Browse files Browse the repository at this point in the history
  • Loading branch information
edtechre committed Mar 12, 2023
1 parent 153340c commit 3a72c11
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/pybroker/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ class EvalMetrics:
end_market_value: Ending market value of the
:class:`pybroker.portfolio.Portfolio`.
total_pnl: Total realized profit and loss (PnL).
unrealized_pnl: Total unrealized profit and loss (PnL).
total_return_pct: Total realized return measured in percentage.
total_profit: Total realized profit.
total_loss: Total realized loss.
Expand Down Expand Up @@ -657,6 +658,7 @@ class EvalMetrics:
initial_market_value: float = field(default=0)
end_market_value: float = field(default=0)
total_pnl: float = field(default=0)
unrealized_pnl: float = field(default=0)
total_return_pct: float = field(default=0)
total_profit: float = field(default=0)
total_loss: float = field(default=0)
Expand Down Expand Up @@ -851,6 +853,7 @@ def _calc_eval_metrics(
total_profit = 0.0
total_loss = 0.0
total_pnl = 0.0
unrealized_pnl = 0.0
max_wins = 0
max_losses = 0
if len(pnls):
Expand All @@ -876,6 +879,7 @@ def _calc_eval_metrics(
total_return_pct = (
(total_pnl + market_values[0]) / market_values[0] - 1
) * 100
unrealized_pnl = market_values[-1] - market_values[0] - total_pnl
return EvalMetrics(
trade_count=len(pnls),
initial_market_value=market_values[0],
Expand Down Expand Up @@ -904,6 +908,7 @@ def _calc_eval_metrics(
total_profit=total_profit,
total_loss=total_loss,
total_pnl=total_pnl,
unrealized_pnl=unrealized_pnl,
total_return_pct=total_return_pct,
total_fees=total_fees,
sharpe=sharpe,
Expand Down
6 changes: 6 additions & 0 deletions tests/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,12 @@ def test_evaluate(
assert metrics.initial_market_value == 500000
assert metrics.end_market_value == 693111.87
assert metrics.total_pnl == 165740.2
assert (
metrics.unrealized_pnl
== metrics.end_market_value
- metrics.initial_market_value
- metrics.total_pnl
)
assert metrics.total_return_pct == 33.14804
assert metrics.total_profit == 403511.07999999996
assert metrics.total_loss == -237770.88
Expand Down

0 comments on commit 3a72c11

Please sign in to comment.