/** * One row of the strategy's order and fill ledger, and the fold of one frame * into it, `stdlib.md` 17.7 and 17.8. * * **A row is appended when the order is sent and is never rewritten by * guesswork.** Every change to it comes from a frame the destination sent, so * the sequence that produced a position can be replayed rather than inferred. * * **The fold is where double counting happens**, which is why it is written * once, here, and why every step of it is the numbered step `stdlib.md` 17.8 * gives. Frames repeat, cross in flight and arrive after the order they are * about has ended, and an engine that added each frame's quantity to a running * total would report a position the strategy never held. Taking the greatest * cumulative quantity instead makes a repeat cost nothing and makes a terminal * frame that overtook a partial one carry the whole remainder in one piece. * * **A row also carries what its order takes out of the position**, because that * is the only record of it that outlives the bar the order was sent on. A * position moves when a fill settles, so an order that has been sent and not * answered has moved nothing and is invisible to every position figure; the row * is where it is visible. `closable.ts` reads the reduction back, and what makes * the reading work is that a row already says whether the order is still going: * an order neither terminal nor fully filled is one the destination still has. */ import type { Identity, OrderFrame, OrderSide, OrderType } from './intent.js'; /** * What one order claimed of the part it reduces, in units, when it was sent. * * Absent on an order that adds to a position, and absent on one whose quantity * the engine cannot count in units, which are two different facts with the same * consequence: neither subtracts anything from what is left to close. * * **`claimed` is a claim and not a size**, and the name says so because reading * it as a size was a defect twice over. On an order the engine sized itself the * two are the same number; on one it could not read they are not, and there it * is the whole of what was left to close at the moment the order left, which is * the reading `stdlib.md` 17.1 makes for an order it cannot count. That number * belongs to one question, how much of a part is already spoken for, and it * answers it whatever unit the order itself was written in. The order's own * size, where the engine can read it, is `units` on the row beside this. */ export interface Reduction { /** The tag the close named, or absent where it reduces the leg as a whole. */ readonly part: string | null; /** The units of that part this order claimed when it was sent. */ readonly claimed: number; /** * Whether `claimed` is the order's own quantity or the most it could have * been. False on an order whose quantity the engine cannot read in units, * where `claimed` is the whole of what was left to close when it was sent. */ readonly counted: boolean; } /** * The words a row's status may take, `stdlib.md` 17.7. * * `placed` is the engine's own and no host may send it: it means an intent has * left and nothing has come back, and a destination cannot report a state it * has never described. */ export type OrderStatus = 'placed' | 'working' | 'triggerPending' | 'filled' | 'cancelled' | 'rejected' | 'expired'; export declare function isTerminal(status: OrderStatus): boolean; /** The word a host sent, or nothing when it is not one of the seven. */ export declare function statusFrom(word: string): OrderStatus | undefined; /** A row of the ledger, `stdlib.md` 17.7. */ export interface LedgerRow { readonly intentId: number; /** The destination's own reference, recorded, shown, and never parsed. */ orderRef: string; readonly tag: string; readonly leg: string; readonly positionRef: number; /** The contract actually sent, which the host may restate in a frame. */ instrument: Identity; /** The product actually sent, after the host's own translation. */ product: string; readonly side: OrderSide; readonly qty: number; readonly type: OrderType; readonly price: number | null; readonly trigger: number | null; status: OrderStatus; filledQty: number; avgFillPrice: number | null; rejection: string; readonly placedAt: number | null; updatedAt: number | null; /** * What this order claimed of the position, absent where it claimed nothing. * * Fixed when the order was sent and never rewritten, because it is a record * of what the engine measured at that moment and not a running total. What * moves is `filledQty` beside it, and the two together say how much of the * claim the destination still has. */ readonly reduces: Reduction | null; /** * This order's own quantity in units, absent where the engine cannot read it. * * A quantity the script stated is in the declaration's own unit * (`host-interface.md` 7.1) and a quantity the engine worked out is in units, * and the two are told apart per order rather than per run: a reverse under a * declaration counting in lots opens its replacement at a size the engine * worked out, in units, on the same bar as orders that are not. * * `reduces` says what an order takes out of a position and this says what it * puts into one, which is the half `holdings.ts` needs to see a position * being opened before any of it has settled. */ readonly units: number | null; } /** * How much of this row's claim on the part it reduces is still outstanding. * * **An order that has been sent and not answered is the whole of what this * file exists to make visible.** A position is folded from settled fills and * from nothing else (17.8), so such an order has moved no position figure: the * leg still reads what it held before it was sent, and a second reducing order * measured against the leg alone sends the position a second time. * * Four facts decide the number, and each of them is a case a strategy meets: * * - **A partial fill of an order the engine counted.** Three sold with one * filled leaves two working, because the one that filled has already moved * the leg and only the other two are still to come. * - **A partial fill of one it could not count**, where the claim stands whole * until the order ends. The claim is in units and the fill is in whatever * the destination answered a quantity in lots, cash or an equity percent * with, and subtracting one from the other is the drift this file is written * to make impossible: a destination answering more than the claim released * room that was never there and the next close sent the position again. * - **An order that has ended.** A rejection, a cancellation and an expiry all * put the row at a terminal status, and nothing more is coming from any of * them, so the claim is released and the strategy may close again. That is * the script's way out of a destination that refused its close. * - **An order that adds**, which claims nothing and holds nothing: an entry * that has not settled is not a position, and counting it would let a close * be sent for units that may never exist. * * **This answers one question and one reader.** What is left to close is a * question about a part of the leg, and `closable.ts` is where it is asked. * Which side a position reference is on is a different question, asked of one * reference against what has settled on it, and `holdings.ts` reads the order's * own size for it rather than this. */ export declare function workingUnits(row: LedgerRow): number; /** Why a frame was refused, `stdlib.md` 17.14. */ export type FrameRefusal = 'unknownIntent' | 'fillWithNoPrice'; /** * What one frame did. * * A refused frame changes nothing and is recorded, because a frame naming an * order this strategy never placed is a fact about the host rather than about * the strategy, and one reporting a fill with no price cannot be marked against * anything. */ export interface FrameOutcome { readonly intentId: number; readonly refused: FrameRefusal | undefined; readonly changed: boolean; /** Units this frame added, which is what settles against a position. */ readonly delta: number; readonly price: number | null; /** Whether the delta arrived after the row had already ended, 17.11. */ readonly afterTerminal: boolean; } /** * Folds one frame into one row, `stdlib.md` 17.8 steps 2 to 5 and 7. * * Step 1 is the ledger's, because locating a row is a question about the whole * ledger rather than about any one row, and step 6 is the position book's: this * function says how many units settled and at what price, and where they settle * is `positions.ts`. */ export declare function foldFrame(row: LedgerRow, frame: OrderFrame): FrameOutcome; //# sourceMappingURL=row.d.ts.map