A strategy inDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/backtest-kit/backtest-monorepo-parallel/llms.txt
Use this file to discover all available pages before exploring further.
backtest-kit is a named unit of trading logic registered at startup time through addStrategySchema. Once registered, the engine resolves it by name when Backtest.background or Live.background is called, invoking getSignal for every new position entry and routing lifecycle events to any attached listeners. Strategy files live under ./content/ and are loaded by the CLI at runtime — they are never bundled into @pro/* packages, so they can be edited and swapped without rebuilding the monorepo.
addStrategySchema
Registers a named strategy for use by the parallel runner.Parameters
Unique identifier for this strategy. Must match the
strategyName passed to Backtest.background or Live.background. In the apr_2026 example this is "apr_2026_strategy".Async function called by the engine when it needs a new position signal for a given symbol. Receives the symbol string, the current backtest timestamp as a
Date, and the close price of the current candle.Event listeners
Strategy files attach listeners for the two main lifecycle events: active-position ticks and errors. Both listeners are registered at module evaluation time and persist for the lifetime of the process.listenActivePing
Called on every candle tick while a position is active for the symbol. The handler receives a context object with the current market state and is the primary place to implement ladder-averaging, profit-target checks, and trailing-stop logic.Async callback invoked on every active candle tick.
listenError
Called when an unhandled error occurs inside the strategy runtime.Helper functions
These functions frombacktest-kit are used inside listenActivePing handlers to implement ladder-averaging and position management.
getPositionEntries(symbol)
getPositionEntries(symbol)
Returns the array of currently open position entries for the given symbol. Use
.length to count ladder steps.getPositionEntryOverlap(symbol, currentPrice, { upperPercent, lowerPercent })
getPositionEntryOverlap(symbol, currentPrice, { upperPercent, lowerPercent })
Returns
true if a new entry at currentPrice would fall within the percentage band of an existing entry. Prevents stacking duplicate entries at the same price level.commitAverageBuy(symbol, cost)
commitAverageBuy(symbol, cost)
Adds a new ladder step to the open position at the current price. The engine averages the entry price and adjusts the overall stop-loss accordingly.
getPositionPnlPercent(symbol)
getPositionPnlPercent(symbol)
Returns the current unrealized PnL as a percentage (e.g.,
3.0 for +3%). Used to trigger target-profit exits.commitClosePending(symbol, { id, note })
commitClosePending(symbol, { id, note })
Closes all pending position entries for the symbol and records a note. The
note field is a markdown string stored in the position record for review in @backtest-kit/ui.Log.info / Log.debug
Log.info / Log.debug
Structured JSONL logging used throughout strategy files.
Log.info records operational events; Log.debug records diagnostic data such as error details.Full example: apr_2026.strategy.ts
The reference strategy implements a ladder-averaging long strategy with a hard stop-loss, a configurable maximum number of steps, and a target-profit exit. TwolistenActivePing handlers run on every active tick: one adds a new ladder step if conditions are met, and the other checks for the profit target and closes the position.