Functional Weave
Code in Rust

invest.dividend-yield

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

1.0.0 (not the latest) · published 2026-10-03 by charlie · Anterra

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

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.

pub fn dividend_yield(price: &Money, trailing_dividends: &[Money], forward_dividend: &Money, payments_per_year: i64) -> 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

/// Both yields, and the annual amounts they were computed from.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DividendYield {
    /// the trailing dividends added up
    pub trailing_annual: Money,
    /// forwardDividend x paymentsPerYear
    pub forward_annual: Money,
    /// trailingAnnual / price, half-up; 350 = 3.5%
    pub trailing_basis_points: i64,
    /// forwardAnnual / price, half-up
    pub forward_basis_points: i64,
}

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

fune!(invest.dividend-yield@^1);  // then call dividend_yield(…)
impl/rust.rs · 83 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.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
use super::money_amount::{money, money_from_value, money_to_value, Money};  ← from money.amount ^1.0.0 · built alongside by fune

/// amount x 10000 / price, half-up; both 0 or more, price > 0.
fn basis_points(amount: i64, price: i64) -> i64 {
    let n = amount as i128 * 10000;
    let d = price as i128;
    let mut q = n / d;
    if 2 * (n - q * d) >= d {
        q += 1;
    }
    q as i64
}

/// 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.
///
/// # Panics
/// Panics on a price of 0 or less, a negative dividend, paymentsPerYear
/// outside 1 to 52, or mixed currencies.
pub fn dividend_yield(price: &Money, trailing_dividends: &[Money], forward_dividend: &Money, payments_per_year: i64) -> DividendYield {
    let currency = price.currency.as_str();
    if price.minor <= 0 {
        panic!("price must be whole minor units greater than zero; received {}", price.minor);
    }
    if !(1..=52).contains(&payments_per_year) {
        panic!("paymentsPerYear must be a whole number from 1 to 52; received {}", payments_per_year);
    }
    for dividend in trailing_dividends.iter().chain(std::iter::once(forward_dividend)) {
        if dividend.currency != currency {
            panic!("currency mismatch: {} and {}", currency, dividend.currency);
        }
        if dividend.minor < 0 {
            panic!("dividends must be whole minor units, 0 or more; received {}", dividend.minor);
        }
    }
    let trailing: i64 = trailing_dividends.iter().map(|d| d.minor).sum();
    let forward = forward_dividend.minor * payments_per_year;
    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),
    }
}

pub fn dividend_yield_to_value(result: &DividendYield) -> Value {
    Value::obj(vec![
        ("trailingAnnual", money_to_value(&result.trailing_annual)),
        ("forwardAnnual", money_to_value(&result.forward_annual)),
        ("trailingBasisPoints", Value::Int(result.trailing_basis_points)),
        ("forwardBasisPoints", Value::Int(result.forward_basis_points)),
    ])
}

fn fraction(v: &Value) -> Option<f64> {
    match v {
        Value::Float(f) if f.fract() != 0.0 => Some(*f),
        _ => None,
    }
}

pub fn fune_vector(args: &[Value]) -> Value {
    if let Some(f) = fraction(args[0].get("minor")) {
        panic!("price must be whole minor units greater than zero; received {}", f);
    }
    for v in args[1].as_arr().iter().chain(std::iter::once(&args[2])) {
        if let Some(f) = fraction(v.get("minor")) {
            panic!("dividends must be whole minor units, 0 or more; received {}", f);
        }
    }
    if let Some(f) = fraction(&args[3]) {
        panic!("paymentsPerYear must be a whole number from 1 to 52; received {}", f);
    }
    let trailing: Vec<Money> = args[1].as_arr().iter().map(money_from_value).collect();
    dividend_yield_to_value(&dividend_yield(
        &money_from_value(&args[0]),
        &trailing,
        &money_from_value(&args[2]),
        args[3].as_i64(),
    ))
}

Install

fune build

With that line in your source, in a Rust project (language rust in fune.project), fune build resolves it and its 1 dependency, pins them in fune.lock, downloads only the Rust 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. A crate’s build.rs runs it before every compile. Or pin a range in fune.project and build in one step:

fune add invest.dividend-yield
Download for Rust invest.dividend-yield-1.0.0-rust.fune · 17,336 bytes sha256 db21650db38bf760ff57a1e321f847eeccec17adb1d7fece358e34c99ee10538

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

The whole function, every language, is one file too: invest.dividend-yield-1.0.0.fune, 21,277 bytes, sha256 5a1aeec65dc1f6366644681676843c7e932a0f097f7445bc929b2286cb331e8b. 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.

Files

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