fix: anchor hedge contribution bars at zero
This commit is contained in:
@@ -33,19 +33,8 @@ def _cost_benefit_options(metrics: dict) -> dict:
|
||||
|
||||
def _waterfall_options(metrics: dict) -> dict:
|
||||
steps = metrics["waterfall_steps"]
|
||||
running = 0.0
|
||||
base: list[float] = []
|
||||
values: list[dict[str, object]] = []
|
||||
for index, (label, amount) in enumerate(steps):
|
||||
if index == 0:
|
||||
base.append(0)
|
||||
running = amount
|
||||
elif index == len(steps) - 1:
|
||||
base.append(0)
|
||||
else:
|
||||
base.append(running)
|
||||
running += amount
|
||||
|
||||
for label, amount in steps:
|
||||
color = "#0ea5e9" if label == "Net equity" else ("#22c55e" if amount >= 0 else "#ef4444")
|
||||
values.append({"value": amount, "itemStyle": {"color": color}})
|
||||
|
||||
@@ -56,14 +45,6 @@ def _waterfall_options(metrics: dict) -> dict:
|
||||
"series": [
|
||||
{
|
||||
"type": "bar",
|
||||
"stack": "total",
|
||||
"data": base,
|
||||
"itemStyle": {"color": "rgba(0,0,0,0)"},
|
||||
"tooltip": {"show": False},
|
||||
},
|
||||
{
|
||||
"type": "bar",
|
||||
"stack": "total",
|
||||
"data": values,
|
||||
},
|
||||
],
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from app.pages.common import strategy_metrics
|
||||
from app.pages.hedge import _waterfall_options
|
||||
|
||||
|
||||
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),
|
||||
("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"
|
||||
|
||||
Reference in New Issue
Block a user