import type { Diagnostic } from '../diagnostics/index.js'; import type { Contract, Money, RecordedFill } from './shapes.js'; /** * What a line's rate is measured against. * * Four bases cover every stack the documentation describes without naming a * market: a fraction of turnover, money per unit, money per fill, and a * fraction of the lines named before this one, which is the charge on a charge. */ export type ChargeBase = 'turnover' | 'units' | 'order' | 'charges'; /** * Which side of the trade a line applies to. * * A transaction tax levied on one side is expressed exactly rather than smeared * across both fills at half the rate, which is what a reader doing it by hand * has to do and what makes their figure disagree with their broker's. */ export type ChargeSide = 'buy' | 'sell' | 'both'; /** One line of a schedule, applied to one fill. */ export interface ChargeLine { /** The platform's own word. */ readonly name: string; readonly base: ChargeBase; readonly side: ChargeSide; /** * Fraction for turnover and charges, money per unit for units, money for * order. */ readonly rate: number; /** Floor per application. */ readonly min: Money | null; /** Cap per application. */ readonly max: Money | null; /** Base 'charges' only: names of lines declared before this one. */ readonly of: readonly string[]; } /** The whole cost model a run was carried out under. */ export interface ChargeSchedule { readonly currency: string; readonly digits: number; readonly slippageTicks: number; /** Applied in order, and order is part of the result. */ readonly lines: readonly ChargeLine[]; /** * Where the schedule came from. * * A schedule derived from the declaration is derived again on replay rather * than stored, because what the program states is stored once, as the * program. A supplied one is the host's choice and travels with the record. */ readonly source: 'declaration' | 'supplied'; } /** What one fill was charged, line by line and in total. */ export interface ChargeBreakdown { readonly lines: readonly { readonly name: string; readonly amount: Money; }[]; readonly total: Money; } /** * What one fill cost, line by line and in total. * * **The lines are the arithmetic and the total is the money.** Each line's * amount is computed in binary64 and left unrounded, and the per-fill total is * rounded once, half to even, to the contract's digits. Rounding each line * would round once per line, and two engines rounding in two places disagree in * the last bit, which is a failed conformance comparison months later on * somebody else's engine. So a reader adding the lines up by hand may land a * fraction of the last digit away from the total, and that is the honest way * round: the total is the figure the report accumulates. * * **Half to even, and not the language's own rounding.** `round()` in the * language is halves away from zero, because a price a trader reads should * agree with what they would write down (`stdlib.md` 8.1). Money folded over * thousands of fills is a different question: away from zero biases every exact * half upward, and half a unit of the last digit per fill is a bias that grows * with the length of the backtest. Two rules, two reasons, both written down. * * **A line that does not apply to this side is not in the breakdown at all.** A * name beside a zero reads as a charge that was levied and came to nothing, * which is not what happened, and a later line levied on that name is levied on * nothing, which is exactly what a tax on one side of the trade does. */ export declare function chargeFor(schedule: ChargeSchedule, fill: RecordedFill, contract: Contract): ChargeBreakdown; /** * The declaration's own cost model, as the one schedule this module evaluates. * * **The declaration is not a second cost engine.** Its three commission * spellings are a schedule of one line: a flat fee is a line charged per fill, * a per unit fee is a line charged per unit, and a percentage is a line charged * on turnover. A second evaluator for the declaration would be the same money * computed two ways, and the day the two disagreed the report and the * platform's own cost panel would both be defensible. * * **A commission of zero is no line at all**, rather than a line charging * nothing. A zero line would put a name in every breakdown, and it would make a * declaration that states no commission indistinguishable from one that states * a commission, which is the distinction a run has to make before it accepts a * schedule from the host as well. * * **A flat fee is charged per fill**, which is the one place `language.md` 13.3 * lets a reasonable person read the words two ways: per order, or per completed * round trip. A charge is attributed to the fill that incurred it everywhere in * this module, because that is what attributes it to a trade, so a round trip of * two fills is charged twice. * * The currency and the digit count are parameters because neither is the * declaration's to state: the declaration's currency is a label and is often * left blank, and money rounding is a fact about the contract. A default here * would be this module inventing a rounding rule for somebody else's market. * * `commissionType`'s value set is declared once, in `check/declaration.ts`, and * is not restated here: what is below is a mapping from each spelling to the * line it means. A program reaching this has been checked, so a fourth spelling * cannot arrive, and the spelling that falls through is the declaration's own * default rather than a shape this module made up. */ export declare function scheduleFromDeclaration(commission: number, commissionType: string, slippage: number, currency: string, digits: number): ChargeSchedule; /** * Why this schedule cannot be carried out, or null. * * **Asked before the first bar, and answered once.** Everything here is a fact * about the schedule rather than about any fill, so a run that would produce a * number nobody can explain is refused while nothing has been computed and the * cost of correcting it is one run. The contract is optional because a schedule * is checkable on its own: what it adds is the three questions that need both, * which are the currency the money is in, the digits it is rounded to, and the * tick a slippage in ticks is measured in. * * The first problem found is the one reported. A list of everything wrong with * a schedule reads as a worse schedule than it is, and it is corrected one line * at a time regardless. */ export declare function scheduleProblem(schedule: ChargeSchedule, contract?: Contract | null): Diagnostic | null; //# sourceMappingURL=charges.d.ts.map