import type { Closing } from './closable.js'; import type { Book } from './holdings.js'; import type { Identity, IntentBar, OrderIntent, OrderSide, OrderType } from './intent.js'; import type { Reduction } from './row.js'; /** * An order this run is about to send, before it is given an id. * * The intent's own shape less the four fields that are the same for every * order this run sends, so that the mapping below cannot state one of them * differently from one call to the next. */ export type Placement = Omit; /** One order a call sends, and what that order takes out of the leg. */ export interface MappedOrder { readonly placement: Placement; readonly reduces: Reduction | null; } /** An order that adds to a position, or whose size the engine cannot count. */ export declare function adding(placement: Placement): MappedOrder; /** The fields every placement states, so each case below states only its own. */ export declare const NOTHING: { readonly side: null; readonly qty: null; readonly qtyType: "units"; readonly type: null; readonly limit: null; readonly trigger: null; readonly target: null; readonly stop: null; readonly profit: null; readonly loss: null; }; /** What the mapping and the refusals read: the declaration, the leg and the ledger. */ export interface PlacingContext extends Closing, Book { readonly instrument: Identity; readonly product: string; /** The unit a quantity the script stated is counted in, `language.md` 13.3. */ readonly qtyType: string; /** The size an order that names none takes, from the declaration. */ readonly declaredQty: number; /** The instrument's tick size, absent where the host states none. */ readonly tickSize: number | null; /** Entries allowed in one direction before one is refused, `language.md` 13.3. */ readonly pyramiding: number; readonly bar: IntentBar; /** The average price of the open position, absent while flat. */ avgPrice(): number | null; /** A fresh position, for the replacement half of a flip. */ mint(): number; } /** What an entering order states, at one quantity and one position. */ interface Entry { readonly side: OrderSide; readonly limit: number | null; readonly trigger: number | null; readonly type: OrderType; readonly tag: string; } /** * The orders an entry sends, which is two where it crosses zero. * * **An instruction that would take a leg from long to short is sent as two * orders** (`stdlib.md` 17.1), one that closes the outgoing position and one * that opens the replacement, each carrying its own position reference. The * reason is not tidiness: a single order that crossed zero would leave a late * fill with no way to say which of the two positions it settled, and during a * flip a leg holds both at once. `sell(qty = abs(pos.size) + more)` is the * spelling the documentation teaches, and it is `order.reverse` with the * arithmetic written out. * * **The closing half is what is left to close, not what the leg holds.** An * order the destination has not answered has filled nothing, so a leg with a * close already going has nothing left for the outgoing half to take, whether * that close was sent on this bar or on one before it. * * **What it crosses is decided per position, not from the leg's net.** The net * is folded from settled fills, so on a silent destination it reads zero while * an entry is still going and an opposing entry is not seen as opposing * anything at all: `buy(qty = 6)` and then `sell(qty = 9)` a bar later put both * orders on reference 1, which opened six long and settled three short. The * question is about one position rather than about the leg, so it is asked of * one position (`holdings.ts`), and the answer is the same whether or not the * destination has answered yet. * * **An order that spans two positions is that many orders.** A leg holds more * than one position whenever an order that opposes it is outstanding, and each * of them is reduced by an order of its own, oldest first, for the same reason * the flip is two orders: a late fill has to be able to say which position it * settled. * * **The reference is minted in every unit; only the arithmetic waits.** The * split subtracts a position folded from filled quantities from a quantity the * script stated, and those are the same kind of number only in a declaration * counting in units: in lots, cash or an equity percent the engine cannot say * how much of the instruction closes and how much opens, and the lot size that * would tell it is the fact OS7005 is deferred on. Minting a reference needs * none of that arithmetic. So an order the engine cannot size against the leg * is still sent on a position of its own rather than on the outgoing one: * `buy(qty = 3)` then `sell(qty = 9)` under a declaration counting in lots took * reference 1 from seventy five units to minus one hundred and fifty, which is * the crossing 17.1 is unconditional about and the late fill with no owner the * split exists to prevent. What does not hold, and what has to exist before it * can, is written where a reader meets it: `stdlib.md` 17.1 and OS7005. */ export declare function entering(ctx: PlacingContext, entry: Entry, qty: number | null): readonly MappedOrder[]; /** * The orders a flattening call sends, which is one per position it reduces. * * A quantity the script stated is in the declaration's own unit and is passed * through as written. A quantity the engine worked out is in units, because a * filled quantity is what it was folded from. * * **It is divided across the positions holding it, oldest first.** A leg holds * more than one position whenever an order that opposes it is outstanding, and * this sized against the whole leg and attached the result to a single * reference with nothing asking whether that reference could absorb it: a leg * holding two positions was flattened by one order large enough to take the * first of them through zero and out the other side. Each position gets an * order of its own, bounded by what has settled on it and is not already * working against it, which is `closable.ts`'s rule read per position. * * **Only what has settled on a position**, never what is working into it. A * close chooses its own quantity, so a close counting an entry that has not * settled would send units that may never exist. That is the one place this * differs from an opposing entry, which is sent at the size the script wrote * whatever the leg holds, and whose only open question is where its units land. * * **A quantity the engine cannot read is one order.** It cannot be divided at * all, so it goes on the position it is closing, oldest first, and that * position is what it may take past zero: the one shape of 17.1 an engine does * not keep, named there and in OS7005 and waiting on the same lot size. Which * position that is, is not the question the two numbers above answer, and * `outgoingFor` is where it is asked: an order that is not choosing a quantity * is not held back by a position having no quantity left to give. * * `part` is what the order is counted against afterwards: the tag a close * named, or the leg as a whole. A stated quantity is counted only where the * declaration counts in units, for the reason `Reduction` gives. */ export declare function flattening(ctx: PlacingContext, units: number, side: OrderSide, qty: number | null, tag: string, part: string | null): readonly MappedOrder[]; export {}; //# sourceMappingURL=sizing.d.ts.map