Functional Weave
Code in Python

invest.dividend-yield Unreviewed

Trailing and forward dividend yield of a share or fund, in basis points, from the price and the dividends per unit.

1.0.1 · published 2026-10-03 by charlie · Anterra

Pinned by 19 tests, run in TypeScript, Python and Rust.

Unreviewed. This capability’s implementations agree in every language and pass its published test vectors, which were worked out from the official sources cited. But no qualified tax adviser has yet checked those vectors, or confirmed that the capability covers the cases it claims. Treat it as a draft. Do not use it for real people, money or decisions without your own expert review. Once a qualified reviewer signs off, this notice is replaced with their name, qualification and the date. Each new version needs fresh sign-off.

Not professional advice. This capability calculates investment figures from published rules. It is a software component for developers, not financial advice. Rules change and every rate here has an effective date. Check that the dates cover your case. Verify results against the official sources listed in its README, and have a tax adviser review how you use it, before anyone relies on the output. Provided “as is” under its licence, without warranty.

What it does

A share's or fund's dividend yield two ways, in basis points (10000 = 100%):

- **Trailing** (historic): the dividends actually paid in the last twelve months, added up, over today's price. It is what happened, and it includes any special dividend, which is why it can overstate what an investor will receive next year. - **Forward** (indicated): the latest declared regular dividend, times the number of regular payments a year, over today's price. After a dividend cut it falls at once, while the trailing yield takes a year to catch up.

For example

  • dividend_yield(£2.50, £0.05, £0.06, £0.05, £0.06, £0.06, 4) → trailing annual £0.22, forward annual £0.24, trailing basis points 8.8%, forward basis points 9.6% quarterly payer at 250p: 22p trailing is 8.8%, 6p a quarter forward is 9.6%
  • dividend_yield(£10.00, £0.10, £0.10, £0.10, £0.10, £0.05, 4) → trailing annual £0.40, forward annual £0.20, trailing basis points 4%, forward basis points 2% after a dividend cut the forward yield halves while the trailing yield has not moved
  • dividend_yield(£20.00, £0.30, £0.50, £0.30, £0.30, 2) → trailing annual £1.10, forward annual £0.60, trailing basis points 5.5%, forward basis points 3% a special dividend lifts the trailing yield only

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

def dividend_yield(price: Money, trailing_dividends: Sequence[Money], forward_dividend: Money, payments_per_year: int) -> DividendYield
priceMoneythe price of one share, or of a lot (100 shares) when dividends have fractions of a penny
trailing_dividendsMoney[]every dividend per share (or per lot) paid in the last twelve months, specials included
forward_dividendMoneythe latest declared regular dividend per share (or per lot); 0 if none
payments_per_yearinthow many regular dividends a year: 1, 2, 4, 12; 1 to 52
returnsDividendYield

The type it declares, generated into your project

@dataclass(frozen=True)
class DividendYield:
    """Both yields, and the annual amounts they were computed from."""

    #: the trailing dividends added up
    trailing_annual: Money
    #: forwardDividend x paymentsPerYear
    forward_annual: Money
    #: trailingAnnual / price, half-up; 350 = 3.5%
    trailing_basis_points: int
    #: forwardAnnual / price, half-up
    forward_basis_points: int

Your code names it in one line, in the file that uses it

from fune.invest.dividend_yield import dividend_yield  # invest.dividend-yield@^1
impl/python.py · 40 lines · open · raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from typing import Sequence

from .invest_dividend_yield_types import DividendYield
from .money_amount import Money, money  ← from money.amount ^1.0.0 · built alongside by fune


def _is_int(value: object) -> bool:
    return isinstance(value, int) and not isinstance(value, bool)


def _basis_points(amount: int, price: int) -> int:
    """amount x 10000 / price, half-up; both 0 or more, price > 0."""
    q, r = divmod(amount * 10000, price)
    return q + 1 if 2 * r >= price else q


def dividend_yield(
    price: Money, trailing_dividends: Sequence[Money], forward_dividend: Money, payments_per_year: int
) -> DividendYield:
    """Trailing yield from the dividends paid in the last twelve months, and
    forward yield from the latest regular dividend annualised, both over the
    current price."""
    currency = price.currency
    if not _is_int(price.minor) or price.minor <= 0:
        raise ValueError("price must be whole minor units greater than zero; received %s" % (price.minor,))
    if not _is_int(payments_per_year) or payments_per_year < 1 or payments_per_year > 52:
        raise ValueError("paymentsPerYear must be a whole number from 1 to 52; received %s" % (payments_per_year,))
    for dividend in list(trailing_dividends) + [forward_dividend]:
        if dividend.currency != currency:
            raise ValueError("currency mismatch: %s and %s" % (currency, dividend.currency))
        if not _is_int(dividend.minor) or dividend.minor < 0:
            raise ValueError("dividends must be whole minor units, 0 or more; received %s" % (dividend.minor,))
    trailing = sum(d.minor for d in trailing_dividends)
    forward = forward_dividend.minor * payments_per_year
    return DividendYield(
        trailing_annual=money(trailing, currency),
        forward_annual=money(forward, currency),
        trailing_basis_points=_basis_points(trailing, price.minor),
        forward_basis_points=_basis_points(forward, price.minor),
    )

Install

fune build

With that line in your source, in a Python project (language python in fune.project), fune build resolves it and its 1 dependency, pins them in fune.lock, downloads only the Python package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:

fune add invest.dividend-yield
Download for Python invest.dividend-yield-1.0.1-python.fune · 17,074 bytes sha256 ca525f6e2231be501c4ff100dca5712b56cd51ed471f2ea9f7c808cdcd724e56

The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./invest.dividend-yield-1.0.1-python.fune, or fetch it from a terminal with fune pull invest.dividend-yield@1.0.1:python.

The whole function, every language, is one file too: invest.dividend-yield-1.0.1.fune, 22,396 bytes, sha256 f1cd722e5b8f802d33186c5cc93f3723a0300ae96aaea140bad42be8b9f5fcac. It installs into a project of any language.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

# fune: before invest.dividend-yield

after — your function gets the result and the arguments, and returns the final result.

# fune: after invest.dividend-yield

replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.

# fune: replace money.amount in invest.dividend-yield

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show invest.dividend-yield --steps.

# fune: step invest.dividend-yield after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.

CaseArgumentsExpected
quarterly payer at 250p: 22p trailing is 8.8%, 6p a quarter forward is 9.6% £2.50, £0.05, £0.06, £0.05, £0.06, £0.06, 4 → trailing annual £0.22, forward annual £0.24, trailing basis points 8.8%, forward basis points 9.6%
after a dividend cut the forward yield halves while the trailing yield has not moved £10.00, £0.10, £0.10, £0.10, £0.10, £0.05, 4 → trailing annual £0.40, forward annual £0.20, trailing basis points 4%, forward basis points 2%
a special dividend lifts the trailing yield only £20.00, £0.30, £0.50, £0.30, £0.30, 2 → trailing annual £1.10, forward annual £0.60, trailing basis points 5.5%, forward basis points 3%
fractions of a penny: 11.8p twice a year on 1,234.56p, given per 100 shares £1,234.56, £11.80, £11.80, £11.80, 2 → trailing annual £23.60, forward annual £23.60, trailing basis points 1.91%, forward basis points 1.91%
a third of a basis point rounds down £0.03, £0.01, £0.01, 1 → trailing annual £0.01, forward annual £0.01, trailing basis points 33.33%, forward basis points 33.33%
half a basis point rounds up £200.00, £0.01, £0.01, 1 → trailing annual £0.01, forward annual £0.01, trailing basis points 0.01%, forward basis points 0.01%
a quarter of a basis point rounds down to zero £400.00, £0.01, £0.00, 1 → trailing annual £0.01, forward annual £0.00, trailing basis points 0%, forward basis points 0%
no dividends in the last year and none declared £5.00, , £0.00, 4 → trailing annual £0.00, forward annual £0.00, trailing basis points 0%, forward basis points 0%
monthly payer in dollars $25.00, $0.10, $0.10, $0.10, $0.10, $0.10, $0.10, $0.10, $0.10, $0.10, $0.10, $0.10, $0.10, $0.11, 12 → trailing annual $1.20, forward annual $1.32, trailing basis points 4.8%, forward basis points 5.28%
a yield above 100% is reported, not capped £1.00, £1.50, £1.50, 1 → trailing annual £1.50, forward annual £1.50, trailing basis points 150%, forward basis points 150%
Show the other 9 tests
CaseArgumentsExpected
a price of zero is an error £0.00, £0.01, £0.01, 1 → error: price must be whole minor units greater than zero
a negative price is an error -£1.00, £0.01, £0.01, 1 → error: price must be whole minor units greater than zero
a fractional price is an error £99..5, £0.01, £0.01, 1 → error: price must be whole minor units greater than zero
a negative dividend is an error £1.00, -£0.01, £0.01, 1 → error: dividends must be whole minor units, 0 or more
a fractional dividend is an error: give amounts per lot instead £1.00, £11..8, £0.01, 1 → error: dividends must be whole minor units, 0 or more
a dividend in another currency is an error £1.00, $0.01, £0.01, 1 → error: currency mismatch: GBP and USD
zero payments a year is an error £1.00, £0.01, £0.01, 0 → error: paymentsPerYear must be a whole number from 1 to 52
53 payments a year is an error £1.00, £0.01, £0.01, 53 → error: paymentsPerYear must be a whole number from 1 to 52
fractional payments a year is an error £1.00, £0.01, £0.01, 2.5 → error: paymentsPerYear must be a whole number from 1 to 52

More from the author

Both are rounded half-up to a whole basis point, from exact integer arithmetic.

## Pence and fractions of a penny

`Money` holds whole minor units, but UK dividends are often declared in fractions of a penny (11.8p). A yield is a ratio, so give the price and the dividends for a lot instead: per 100 shares, 11.8p is 1180 and a price of 1,234.56p is 123456. The yields are the same; the annual amounts are per lot.

## What it does not do

It does not decide which dividends fall in the last twelve months (pass the ones that do, by ex-dividend or payment date as your policy says), convert currencies, or gross up for tax credits. Price and dividends must be in one currency.

## Errors

A price of 0 or less, a negative or fractional dividend, `paymentsPerYear` outside 1-52, and mixed currencies.

## Before you rely on this

**Not professional advice.** This capability calculates investment figures from published rules. It is a software component for developers, not financial advice. Rules change and every rate here has an effective date. Check that the dates cover your case. Verify results against the official sources listed above, and have a tax adviser review how you use it, before anyone relies on the output. Provided "as is" under its licence, without warranty.

**Unreviewed.** This capability's implementations agree in every language and pass its published test vectors, which were worked out from the official sources cited. But no qualified tax adviser has yet checked those vectors, or confirmed that the capability covers the cases it claims. Treat it as a draft. Do not use it for real people, money or decisions without your own expert review. Once a qualified reviewer signs off, this notice is replaced with their name, qualification and the date. Each new version needs fresh sign-off.

1.0.1 marks it unreviewed. The code and the tests are unchanged.

Files

PathBytes
README.md2,458
impl/python.py1,906
impl/rust.rs3,242
impl/typescript.ts1,891
vectors.json8,523