fix: anchor hedge contribution bars at zero

This commit is contained in:
Bu5hm4nn
2026-03-24 19:34:41 +01:00
parent 98ecfb735e
commit 021ce7dd99
2 changed files with 12 additions and 20 deletions

View File

@@ -33,19 +33,8 @@ def _cost_benefit_options(metrics: dict) -> dict:
def _waterfall_options(metrics: dict) -> dict: def _waterfall_options(metrics: dict) -> dict:
steps = metrics["waterfall_steps"] steps = metrics["waterfall_steps"]
running = 0.0
base: list[float] = []
values: list[dict[str, object]] = [] values: list[dict[str, object]] = []
for index, (label, amount) in enumerate(steps): for label, amount in steps:
if index == 0:
base.append(0)
running = amount
elif index == len(steps) - 1:
base.append(0)
else:
base.append(running)
running += amount
color = "#0ea5e9" if label == "Net equity" else ("#22c55e" if amount >= 0 else "#ef4444") color = "#0ea5e9" if label == "Net equity" else ("#22c55e" if amount >= 0 else "#ef4444")
values.append({"value": amount, "itemStyle": {"color": color}}) values.append({"value": amount, "itemStyle": {"color": color}})
@@ -56,14 +45,6 @@ def _waterfall_options(metrics: dict) -> dict:
"series": [ "series": [
{ {
"type": "bar", "type": "bar",
"stack": "total",
"data": base,
"itemStyle": {"color": "rgba(0,0,0,0)"},
"tooltip": {"show": False},
},
{
"type": "bar",
"stack": "total",
"data": values, "data": values,
}, },
], ],

View File

@@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
from app.pages.common import strategy_metrics from app.pages.common import strategy_metrics
from app.pages.hedge import _waterfall_options
def test_protective_put_atm_minus_20pct_improves_equity() -> None: def test_protective_put_atm_minus_20pct_improves_equity() -> None:
@@ -18,3 +19,13 @@ def test_protective_put_atm_minus_20pct_improves_equity() -> None:
("Hedge cost", -6_250.0), ("Hedge cost", -6_250.0),
("Net equity", 58_750.0), ("Net equity", 58_750.0),
] ]
def test_hedge_waterfall_uses_zero_based_contribution_bars() -> None:
options = _waterfall_options(strategy_metrics("protective_put_atm", -20))
assert len(options["series"]) == 1
assert options["series"][0]["type"] == "bar"
values = options["series"][0]["data"]
assert values[2]["value"] == 38_000.0
assert values[2]["itemStyle"]["color"] == "#22c55e"