/** * The destination a backtest runs against: intents in, frames out. * * **This is a host, not part of the engine**, and the division is the one thing * to keep straight while reading it. `stdlib.md` 17.1 puts slippage and * commission on the destination: the engine folds the price it is told and * never adjusts one, so the price worsening below is a venue giving a worse * fill, which is what a venue does, and not an engine moving a number, which is * what the invariant forbids. Everything here could be a broker. * * **A frame is cumulative and reaches the engine between bars.** Every frame * restates the whole life of one order from its beginning, so a repeat costs * nothing, and none of them is handed over during an execution: they are * answered here and collected at the boundary, which is where * `host-interface.md` 7.4 puts the intake. * * **When a fill is known is not when it happened.** A market order decided at * the close of a bar and filled at the next bar's open is known at that open, * so its frame is delivered before that bar executes and the strategy holds the * position for the whole of it. An order that rested and traded somewhere * inside a bar is only known to have traded once that bar is complete, so its * frame is delivered before the bar after it. Both are the truthful reading of * what a venue could have told anybody at the time, and the difference between * them is why they are not folded together. * * **A bracket is answered with nothing at all**, exactly as a venue with no * order to report would answer. A bracket reaches a destination as a protective * instruction attached to a tag and the engine appends no row for it, so there * is nothing for a frame to be about: a fill reported against one would name an * intent no row holds and the fold would refuse it. The row a bracket wants is * decision 55 and it is not in this release, so a stop cannot fill here and a * page that says it can is ahead of the engine. * * **A destination that behaves badly does it on a schedule, stated before the * run and never by chance.** Every case harvested before this one ran against a * venue that filled every order whole and on time, so two engines were proved * to agree about the half of a day that costs nobody anything. The other half * is a partial fill, an order arriving in pieces, a rejection, a cancellation, * an expiry and a fill that turns up after the order has ended, and * `SimulatorOptions.fill` carries the schedule saying which order each of those * happens to and at which boundary. There is no random number here and there * will not be one: `stdlib.md` 8.2 keeps a script that answers differently on a * second run out of a conformance suite, and a venue rolling a die would make * every case it wrote unreproducible in the same breath. **An order the * schedule names is answered by the schedule and by nothing else**, so a run * stating none is the run the cases before this one were harvested from, to the * bit, which is what lets this exist at all. */ import type { Contract } from '../accounting/index.js'; import type { OrderFrame, OrderIntent, RoutedEffect } from '../engine/index.js'; import type { RecordedBar } from './record.js'; import type { FillPolicy } from './settings.js'; /** * What this destination does to one order, at one boundary. * * Four words reaching every status of `stdlib.md` 17.7 a host may send. `fill` * carries the cumulative quantity, so one word covers the acknowledgement * before anything has traded, a fill of part of the order and a fill of the * whole of it: `working` and `filled` are that quantity read against the * order's own rather than two instructions. The other three are the three ways * an order ends carrying less than it asked for, and an engine never handed * one has never been asked what it does with the quantity still working. */ export type VenueDoes = 'fill' | 'reject' | 'cancel' | 'expire'; /** One act of the schedule: what the destination does, to which order, when. */ export interface VenueAct { /** * The nth order this destination took, counting from one. * * Orders and not intents: a bracket is never taken and a cancellation is * answered without being held, so neither is counted and neither can be named * here. An ordinal for the reason `conformance.md` section 3 gives a case's * own frames: nobody writing a schedule knows the id an engine will mint. */ readonly order: number; /** * Boundaries after the one the order arrived at, `0` being that boundary. * * Counted from the order rather than stated as a bar index, because the bar * a strategy decides an order on moves the moment anything else about the run * does, and a schedule in bar indices is one nobody can read back. */ readonly afterBars: number; readonly does: VenueDoes; /** * The cumulative quantity a fill reports, in units, or absent for all of it. * * Cumulative because a frame is (`stdlib.md` 17.8): `0` is the * acknowledgement a venue sends before anything has traded, a number below * the order's own is a partial fill, and one at or above it completes the * order. A quantity below what this venue has already reported is a stale * frame, which a venue really sends and the fold has to swallow, so it is * stated here rather than refused. */ readonly units?: number | null; /** The destination's own text, which the ledger records against the row. */ readonly text?: string; } /** * The fill policy, and what this destination does to the orders it names. * * The schedule sits on the policy because it is the same kind of fact: how this * destination decides a fill, chosen before the first bar and carried in the * record beside the policy's own version, so a stored run replays as it ran. */ export interface VenuePolicy extends FillPolicy { readonly schedule?: readonly VenueAct[]; } /** What the venue prices against and how it decides a fill. */ export interface SimulatorOptions { readonly bars: readonly RecordedBar[]; readonly contract: Contract; readonly fill: VenuePolicy; /** Adverse always, in ticks, applied to a market fill and to a stop. */ readonly slippageTicks: number; /** The declaration's own fill rule: where a market order is priced. */ readonly fillOn: string; /** * The unit the declaration's quantity is stated in, `language.md` 13.3. * * Here because converting it is the destination's job and a fill is counted * in units. Only the units this destination can arrive at reach it: the rest * are refused before the first bar by `checkSettings`. */ readonly qtyType: string; } /** * A venue, for one run. * * It holds the orders it was handed and nothing else: no position, no money and * no view of what the strategy is doing, because a destination has none of * those about somebody else's strategy. */ export declare class Simulator { private readonly options; private readonly orders; /** The intents handed over, in the order they were, whatever became of them. */ readonly intents: OrderIntent[]; /** Brackets, held so that a run can say how many it was handed and answered. */ readonly brackets: OrderIntent[]; private queued; private answered; private refs; private seq; constructor(options: SimulatorOptions); /** Step 9: what the strategy decided reaches the venue. */ route(effect: RoutedEffect, barIndex: number): void; /** * The frames a boundary hands over: everything this venue can say now. * * The orders that were already resting are decided against this bar first and * the orders this bar sent are answered after, which is the order the venue * learned of them in. */ framesFor(barIndex: number): readonly OrderFrame[]; /** How many orders this venue is still holding, which a run reports. */ get working(): number; /** An order the strategy sent, which this venue now holds. */ private accept; /** * A cancellation, which is about the orders a tag names and not about itself. * * Every live order carrying the tag is withdrawn and answered, which is what * releases the position they were claiming, and the cancellation is then * answered as an order of its own, because the engine appended a row for it * and a row nothing ever answers stays at `placed` for the life of the run. */ private withdraw; /** * The first thing a venue says about an order it has taken. * * A working frame before any fill, because 7.4 asks for a frame on every * change of status and a script waiting for a working order to clear waits * blind without one. A market order is then filled in the same breath where * the declaration prices it at this bar's close, and at the next bar's open * where it does not. */ private open; /** One resting order against one bar. */ private decide; /** * The whole of an order, filled at one price. * * **The quantity is converted into units here, because that is what a fill * is counted in and what every money figure is folded over.** A quantity * travels in the declaration's own unit (`host-interface.md` 7.1) and the * destination is the party that converts it. This filled the number verbatim, * so a strategy sizing in lots on a lot of sixty five traded one sixty fifth * of what it asked for and the whole report was out by that factor. The units * this destination cannot arrive at are refused before the first bar rather * than guessed at here, so by this point the unit is one of two. */ private complete; /** * The acts due at this boundary, in the order the schedule stated them. * * Due is counted from the bar that sent the order, so an act names a moment * in the life of its own order rather than a bar of the run. Two acts due * together are answered as written, which is how a schedule states a * cancellation and the fill that raced it. * * Liveness is not consulted. An order that has ended can still be spoken * about, because the frame that arrives after it ended is the whole reason * this is here: `stdlib.md` 17.8's fill after a terminal status, which a * venue sends whenever a cancel races a fill and which an engine refusing it * loses, leaving a position the strategy cannot see. */ private perform; /** * A fill the schedule stated, at this bar's close and worsened like any other. * * **The average is this venue's own, over the cumulative quantity**, the * figure `stdlib.md` 17.8 step 3 says the row takes whole. A venue reporting * the last piece's price and calling it an average hands the engine a number * that is not one, and the engine may not work its own out from two of them, * so the lie settles into the ledger and into every trade folded from it. * * A stated quantity at or below what this venue has already reported adds * nothing and moves nothing: that is a repeated or a stale frame, which a real * destination sends and the fold has to swallow. It carries the average this * venue holds now rather than then, because it keeps no history of its own * averages and the fold ignores the price of a frame adding no quantity. A * bar with no close prices nothing, so an act due at one says nothing rather * than raising a quantity with no price against it, which step 3 refuses. */ private report; /** * The order's quantity in units, which is what a fill is counted in. * * The conversion `complete` did inline, wanted in two places the moment a * schedule can fill an order in pieces: the whole a piece is measured * against has to be the number the fill path would have reported. */ private unitsOf; /** * A price worsened by the slippage the run was carried out under. * * **Adverse always: a buy pays more and a sell receives less.** The sign is * taken as written rather than as a magnitude, because a slippage below zero * is refused before the first bar and taking its magnitude here would be this * function quietly covering for a hole in that refusal. It went uncovered for * a while: `scheduleProblem` has always refused a negative rate, and * `checkSettings` asked it only about a schedule the host supplied, so a * declaration stating a slippage of minus one improved both sides of every * fill and the backtest paid the strategy to trade. * * A slippage in ticks with no tick size to measure a tick in charges nothing. * The comment here once said that was refused before the first bar and it was * not; it still is not, because an instrument with no tick is a fact a host * may legitimately not hold, and a run that charges no slippage is a study * before slippage rather than a wrong answer. What has changed is that this * says so instead of claiming a refusal that never existed. */ private worsen; /** One frame, cumulative, in the order this venue spoke. */ private say; } //# sourceMappingURL=simulate.d.ts.map