/** * What a trade is, which is the definition every statistic is counted over. * * **A trade is one position reference, from the fill that first takes it away * from zero to the fill that returns it to zero.** Nothing else is invented, * because the engine already mints a reference per position, no order crosses * zero, and every fill names the reference it settled however late it arrives. * * Everything awkward falls out of that rather than needing a rule of its own. A * pyramided entry is more entry fills on one trade. A partial close is an exit * fill that does not close the trade. A flip is two references and therefore * two trades, which is what a reversing order already sends. A reference still * holding something at the last bar is an open trade: it is in the list with * `isOpen` true, it is counted in equity, and it is counted in no win rate. * * The definition is written in the specification before it is written here, * because a rule decided in code is a rule two engines decide differently. * * ## What the fold reads, and what it refuses to work out for itself * * A fill carries the signed size of its reference either side of the * settlement, so the fold reads what the position book did rather than * recomputing it from quantities and sides. That is the whole of how a partial * close, a pyramided entry and a reversal tell themselves apart: the move from * one size to the other says how much of the fill closed what was held and how * much of it opened something, and nothing else has to be inferred. * * It also settles the one case the definition does not cover on its face. A * destination that fills more than the order asked takes a reference through * zero rather than to it, and the position book reads the remainder as a * position in the other direction opened at that fill's price. So the fold does * the same: the fill closes the trade that was held and opens a second one on * the same reference, at its own price and on its own bar. A trade that ran * from a size to the other side of zero would report a quantity nothing was * ever entered at. * * **Reducing a position does not move its average**, which is the position * book's rule read here rather than a second one: a reducing fill adds to the * exits and touches neither the entry quantity nor the entry cost. Every figure * measured from the entry, the entry price a reader compares against and the * excursion at each bar close, is measured from the price the units were * actually bought at. * * **A charge lands whole on one trade and is never split.** It was rounded once * for the fill that incurred it, and splitting it between the trade a crossing * fill closed and the trade it opened would round it again and put a residue * somewhere. So it is attributed to the trade the fill closed where it closed * one, and to the trade it opened otherwise, which makes the charges of the * trades add up to the charges of the fills exactly rather than nearly. * * **Gross profit is over the units that have left.** For a closed trade that is * every unit it entered, which is the formula the report is specified with. For * one still open it is what its exits have realised so far, which is a figure * that is true rather than a zero standing in for money the run has already * made. */ import type { BarMark, Contract, Money, RecordedFill } from './shapes.js'; /** One round trip on one position reference. */ export interface Trade { /** 1-based, in the order the trade opened. */ readonly index: number; readonly positionRef: number; readonly side: 'long' | 'short'; readonly openedOnBar: number; readonly openedAt: number | null; readonly closedOnBar: number | null; readonly closedAt: number | null; readonly barsHeld: number | null; /** Total units entered. */ readonly units: number; /** Quantity weighted over the entry fills. */ readonly entryPrice: number; /** Quantity weighted over the exit fills. */ readonly exitPrice: number | null; readonly entries: number; readonly exits: number; readonly grossProfit: Money; readonly charges: Money; readonly netProfit: Money; /** * Excursion at bar closes while open. Favourable is zero or better, adverse * zero or worse, and both are zero for a trade no bar closed on. */ readonly maxFavourable: Money; readonly maxAdverse: Money; readonly isOpen: boolean; } /** * The round trips a run's fills make up, in the order they opened. * * `charges[index]` is the money `fills[index]` was charged, rounded once by * whoever computed it, so the two travel as one thing and nothing here rounds * anything a second time. A caller with no cost model supplies no charges at * all and every trade's charges are zero. * * The fills are read in `seq` order whatever order they are handed in, because * `seq` is the order the engine folded them and a report that depended on the * order a caller happened to be holding them in would not be reproducible. The * marks are read in bar order for the same reason, and a bar is marked after * every fill up to it has been folded, because a fill happens during its bar * and the close comes after. */ export declare function tradesOf(fills: readonly RecordedFill[], charges: readonly Money[], marks: readonly BarMark[], contract: Contract): readonly Trade[]; /** * How much of a move from one size to another closed what was held. * * Exported to the module and not through its door: `markers.ts` asks the same * question of the same fills, and the rule for what a move closed and what it * opened is one rule. Two folds may read a fill differently and produce a * marker for an entry the trade list calls an exit, so they read it here. * * A move to the other side of zero closed all of it, which is the case a * destination that overfilled produces and the one this has to get right. */ export declare function closedBy(before: number, after: number): number; /** And how much of it opened something, which is the rest of the same move. */ export declare function openedBy(before: number, after: number): number; //# sourceMappingURL=trades.d.ts.map