Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add modified dietz returns #48

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified frontend/tests/e2e/__image_snapshots__/report_corp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified frontend/tests/e2e/__image_snapshots__/report_corpeur.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified frontend/tests/e2e/__image_snapshots__/report_gold.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified frontend/tests/e2e/__image_snapshots__/report_overview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified frontend/tests/e2e/__image_snapshots__/report_vht.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2,636 changes: 2,350 additions & 286 deletions frontend/tests/e2e/__snapshots__/reports.test.js.snap

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = [
]
dependencies = [
"fava>=1.26",
"beangrow>=1.0.0",
"beangrow @ git+https://github.com/beancount/beangrow",
"protobuf<3.21",
]
readme = "README.md"
Expand Down
109 changes: 74 additions & 35 deletions src/fava_portfolio_returns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@
import beangrow.config as configlib # type: ignore
from beangrow.investments import CashFlow # type: ignore
import beangrow.returns as returnslib # type: ignore
from beangrow.reports import ( # type: ignore
Table,
compute_returns_table,
get_calendar_intervals,
get_cumulative_intervals,
get_accounts_table,
)
from beangrow import reports # type: ignore
from fava.ext import FavaExtensionBase
from fava.helpers import FavaAPIError
from fava.context import g
Expand Down Expand Up @@ -187,6 +181,14 @@ def calculate_group_performance(
irr = returnslib.compute_returns(
truncated_cash_flows, pricer, target_currency, end_date
)
try:
mdr = returnslib.compute_returns(
truncated_cash_flows, pricer, target_currency, end_date, dietz=True
)
except TypeError as ex:
raise FavaAPIError(
"You are using an incompatible version of beangrow, please regenerate your lockfile and venv to install a supported version of beangrow."
) from ex

return {
"units": units_balance,
Expand All @@ -196,6 +198,7 @@ def calculate_group_performance(
"returns": returns,
"returns_pct": returns_pct,
"irr": irr.total,
"mdr": mdr.total,
}

def overview(self):
Expand Down Expand Up @@ -268,12 +271,7 @@ def create_plots(
amt = -float(flow.amount.number)
if remaining_days > 0:
gflow = amt * (rate ** np.arange(0, remaining_days))
gamounts[-remaining_days:] = np.add(
gamounts[-remaining_days:],
gflow,
out=gamounts[-remaining_days:],
casting="unsafe",
)
gamounts[-remaining_days:] += gflow
amounts[-remaining_days:] += amt
else:
gamounts[-1] += amt
Expand Down Expand Up @@ -327,9 +325,9 @@ def create_plots(
)

return {
"cashflows": cashflows_plot,
"cumvalue": cumvalue_plot,
"pnl": pnl_plot,
"cumvalue": cumvalue_plot,
"cashflows": cashflows_plot,
"min_date": dates[0] if dates else None,
"max_date": dates[-1] if dates else None,
}
Expand All @@ -345,45 +343,86 @@ def generate_report(
if not target_currency:
target_currency = self.get_target_currency(account_data)

# cash flows
# compute cash flows
cash_flows = returnslib.truncate_and_merge_cash_flows(
pricer, account_data, start_date, end_date
)
returns = returnslib.compute_returns(
cash_flows, pricer, target_currency, end_date
)
transactions = data.sorted(
[txn for ad in account_data for txn in ad.transactions]
)

# cumulative value plot
plots = self.create_plots(
pricer, target_currency, cash_flows, transactions, returns.total
# IRR
irr_total = returnslib.compute_returns(
cash_flows, pricer, target_currency, end_date
)
irr_total_returns_tbl = reports.Table(
["Total Returns", "Ex-Div", "Div"],
[[irr_total.total, irr_total.exdiv, irr_total.div]],
)
irr_annualized_returns_tbl = reports.compute_returns_table(
pricer,
target_currency,
account_data,
reports.get_calendar_intervals(end_date),
header_text="Annualized Returns",
)
irr_cumulative_returns_tbl = reports.compute_returns_table(
pricer,
target_currency,
account_data,
reports.get_cumulative_intervals(end_date),
header_text="Cumulative Returns",
)

# returns
total_returns_tbl = Table(
["Total", "Ex-Div", "Div"], [[returns.total, returns.exdiv, returns.div]]
# Modified Dietz Returns
mdr_total = returnslib.compute_returns(
cash_flows, pricer, target_currency, end_date, dietz=True
)
calendar_returns_tbl = compute_returns_table(
pricer, target_currency, account_data, get_calendar_intervals(end_date)
mdr_total_returns_tbl = reports.Table(
["Total Returns", "Ex-Div", "Div"],
[[mdr_total.total, mdr_total.exdiv, mdr_total.div]],
)
cumulative_returns_tbl = compute_returns_table(
pricer, target_currency, account_data, get_cumulative_intervals(end_date)
mdr_annualized_returns_tbl = reports.compute_returns_table(
pricer,
target_currency,
account_data,
reports.get_calendar_intervals(end_date),
header_text="Annualized Returns",
dietz=True,
)
mdr_cumulative_returns_tbl = reports.compute_returns_table(
pricer,
target_currency,
account_data,
reports.get_cumulative_intervals(end_date),
header_text="Cumulative Returns",
dietz=True,
)

# accounts
accounts_tbl = get_accounts_table(account_data)
# PnL, cumulative value and cashflow charts
plots = self.create_plots(
pricer, target_currency, cash_flows, transactions, irr_total.total
)

# cash flows
# accounts and cashflow tables
accounts_tbl = reports.get_accounts_table(account_data)
cashflows_tbl = investments.cash_flows_to_table(cash_flows)

return {
"target_currency": target_currency,
"plots": plots,
"total_returns": total_returns_tbl,
"calendar_returns": calendar_returns_tbl,
"cumulative_returns": cumulative_returns_tbl,
"returns": {
"irr": {
"total": irr_total_returns_tbl,
"annualized": irr_annualized_returns_tbl,
"cumulative": irr_cumulative_returns_tbl,
},
"mdr": {
"total": mdr_total_returns_tbl,
"annualized": mdr_annualized_returns_tbl,
"cumulative": mdr_cumulative_returns_tbl,
},
},
"accounts": accounts_tbl,
"cashflows": cashflows_tbl,
}
Expand Down
6 changes: 5 additions & 1 deletion src/fava_portfolio_returns/templates/_overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ <h2>Investments</h2>
<th data-sort="num">Market Value</th>
<th data-sort="num">Returns</th>
<th data-sort="num">Yield</th>
<th data-sort="num">IRR</th>
<th data-sort="num" title="Internal Rate of Return">IRR</th>
<th data-sort="num" title="Modified Dietz Return">MDR</th>
</tr>
</thead>
<tbody>
Expand All @@ -56,6 +57,9 @@ <h2>Investments</h2>
<td class="num {% if group.irr >= 0 %}positive{% else %}negative{% endif %}" data-sort-value="{{ group.irr }}">
<span class="num">{{ "%.2f" | format(group.irr * 100) }} %</span>
</td>
<td class="num {% if group.mdr >= 0 %}positive{% else %}negative{% endif %}" data-sort-value="{{ group.mdr }}">
<span class="num">{{ "%.2f" | format(group.mdr * 100) }} %</span>
</td>
</tr>
{% endfor %}
</tbody>
Expand Down
24 changes: 15 additions & 9 deletions src/fava_portfolio_returns/templates/_report.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

{% set report = extension.report(group) %}
<script id="favaPortfolioReturnsReportData" type="application/json">
{{ {"target_currency": report["target_currency"], "plots": report["plots"]}|tojson }}
{{ {"target_currency": report.target_currency, "plots": report.plots}|tojson }}
</script>

<h2>Portfolio Returns: {{ group }}</h2>
Expand All @@ -50,24 +50,30 @@ <h2>Portfolio Returns: {{ group }}</h2>
<div class="chart"></div>
</div>

<h3>Internal Rate of Returns</h3>
{{ returns_table(report["total_returns"]) }}
{{ returns_table(report["calendar_returns"]) }}
{{ returns_table(report["cumulative_returns"]) }}
<h3>Internal Rate of Return (IRR)</h3>
{{ returns_table(report.returns.irr.total) }}
{{ returns_table(report.returns.irr.annualized) }}
{{ returns_table(report.returns.irr.cumulative) }}

<br>
<h3>Modified Dietz Return (MDR)</h3>
{{ returns_table(report.returns.mdr.total) }}
{{ returns_table(report.returns.mdr.annualized) }}
{{ returns_table(report.returns.mdr.cumulative) }}

<br>
<h3>Accounts</h3>
<p>Cost Currency: {{ report["target_currency"] }}</p>
<p>Cost Currency: {{ report.target_currency }}</p>
<table>
<thead>
<tr>
{% for header in report["accounts"].columns.to_list() %}
{% for header in report.accounts.columns.to_list() %}
<th>{{ header }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in report["accounts"].itertuples() %}
{% for row in report.accounts.itertuples() %}
<tr>
<td>{{ row.Investment }}</td>
<td>{{ row.Description }}</td>
Expand All @@ -90,7 +96,7 @@ <h3>Cash Flows</h3>
</tr>
</thead>
<tbody>
{% for row in report["cashflows"].itertuples() %}
{% for row in report.cashflows.itertuples() %}
<tr>
<td>{{ row.date }}</td>
<td class="num">{{ row.amount | format_currency(row.currency) }} {{ row.currency }}</td>
Expand Down
Loading