fix(analytics): TWR не цепляет остаток после полного вывода из портфеля
День, выводящий всё, кроме 1 % стоимости (EMPTIED_SHARE), не участвует в цепочке: остаток в пару рублей превращал комиссию или округление в −60 % за день, и несколько таких опустошений обнуляли всю историю.
This commit is contained in:
@@ -68,6 +68,11 @@ PERIODS: tuple[str, ...] = ("1m", "3m", "6m", "ytd", "1y", "3y", "all")
|
||||
#: Periods reported even when the data is younger than their nominal length.
|
||||
ELASTIC_PERIODS = frozenset({"ytd", "all"})
|
||||
|
||||
EMPTIED_SHARE = Decimal("0.1")
|
||||
"""A day that withdraws all but this share of the previous value has emptied the portfolio: what
|
||||
is left is a residue of rounding, fees and dust, and its daily change says nothing about the
|
||||
market. See `twr`."""
|
||||
|
||||
MIN_XIRR_DAYS = 30
|
||||
"""Below this, annualising a per-instrument return turns a day of noise into a percent a
|
||||
year. A paper bought yesterday gets no XIRR rather than a triple-digit one."""
|
||||
@@ -178,8 +183,16 @@ def twr(points: Sequence[Point], *, opening_value: Decimal, opening_missing: int
|
||||
|
||||
One case is still skipped rather than adjusted: a day where the NUMBER of unvalued
|
||||
positions changed. The value then jumps by a whole position that neither gained nor lost
|
||||
anything, and no flow describes it. `TwrResult.days_skipped` reports how many such days
|
||||
a period contains, because a TWR with holes has to say so.
|
||||
anything, and no flow describes it.
|
||||
|
||||
A day that withdraws all but 10 % of what the portfolio was worth (`EMPTIED_SHARE`) is not
|
||||
chained either: the base left is dust, and the fee, rounding or unrecorded small item that eats
|
||||
a share of it reads as -60 %. Chained, a few such emptyings over the years multiply the whole
|
||||
history to -99.9 %. The day is neither used nor skipped — like a zero base, there was
|
||||
nothing invested to earn a return on — and the chain carries on from the next day.
|
||||
|
||||
`TwrResult.days_skipped` reports how many days of the first kind a period contains, because
|
||||
a TWR with holes has to say so.
|
||||
"""
|
||||
factor = ONE
|
||||
previous = opening_value
|
||||
@@ -189,6 +202,8 @@ def twr(points: Sequence[Point], *, opening_value: Decimal, opening_missing: int
|
||||
base = previous + point.flow + point.unvalued_flow
|
||||
if point.missing != previous_missing:
|
||||
skipped += 1
|
||||
elif (point.flow + point.unvalued_flow) < ZERO and base < previous * EMPTIED_SHARE:
|
||||
pass
|
||||
elif base > ZERO:
|
||||
factor *= point.value / base
|
||||
used += 1
|
||||
|
||||
Reference in New Issue
Block a user