All data fetching functions in Backtest Kit automatically read the current execution context timestamp from Node.jsDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/backtest-kit/backtest-kit-docs/llms.txt
Use this file to discover all available pages before exploring further.
AsyncLocalStorage. This means you never pass a when or timestamp argument — the framework injects it automatically based on the current backtest tick or live clock. In backtest mode this is the simulated candle time; in live mode it is new Date(). The result is that look-ahead bias is structurally impossible: no function can return data from the future relative to the current execution context.
All candle timestamp boundaries are UTC-based. For intervals like
4h, boundaries fall at 00:00, 04:00, 08:00, 12:00, 16:00, 20:00 UTC. If your local timezone is not a multiple of the interval, timestamps may appear uneven when printed in local time — use .toUTCString() or .toISOString() to inspect the actual alignment.getCandles(symbol, interval, limit)
Returnslimit fully-closed candles ending before the current execution context time. The pending (currently forming) candle at the aligned boundary is always excluded — it has incomplete OHLCV data and would distort indicators.
Trading pair symbol passed verbatim to the exchange adapter’s
getCandles function.Candle interval. Supported values:
1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M.Number of closed candles to return. The adapter must return exactly this many candles.
Promise<ICandleData[]> — array of limit candles, ordered oldest to newest. Each candle has: timestamp (open time in ms), open, high, low, close, volume.
Timestamp alignment math
The engine alignswhen down to the interval boundary and excludes the pending candle:
- The first candle’s
timestampequalssinceexactly - Exactly
limitcandles are returned - Candle timestamps are sequential:
since + i * stepMsfori = 0 .. limit-1
getNextCandles(symbol, interval, limit)
Returnslimit candles starting from the current execution context time (forward-looking). The first candle starts at alignedWhen — the current aligned boundary — and subsequent candles go forward in time.
Trading pair symbol.
Candle interval.
Number of forward-looking candles to return.
Promise<ICandleData[]> — limit candles ordered oldest to newest, starting from the current aligned boundary (inclusive).
getRawCandles(symbol, interval, limit?, sDate?, eDate?)
Flexible candle retrieval with multiple parameter combinations. All combinations respect look-ahead bias protection — theeDate or computed end is clamped to alignedWhen.
Trading pair symbol.
Candle interval.
Number of candles to return. When omitted with both
sDate and eDate, the limit is inferred from the date range.Start date (inclusive). When provided with
limit, returns limit candles starting from align(sDate).End date (exclusive). When provided with
limit, returns limit candles ending before align(eDate).Promise<ICandleData[]>
getOrderBook(symbol, depth?)
Returns an order book snapshot aligned to theCC_ORDER_BOOK_TIME_OFFSET_MINUTES boundary. The from/to window passed to the exchange adapter is exactly one offsetMinutes period wide.
Trading pair symbol.
Number of order book levels to return. Defaults to
CC_ORDER_BOOK_MAX_DEPTH_LEVELS from config.Promise<IOrderBookData> — { symbol, bids: IBidData[], asks: IBidData[] }. Each IBidData has price: string and quantity: string.
Requires: getOrderBook must be defined in the active exchange schema (addExchangeSchema). If not defined, this call throws a runtime error.
getAggregatedTrades(symbol, limit?)
Returns aggregated trade records from the exchange, aligned to the 1-minute boundary. Withoutlimit, returns one full window of CC_AGGREGATED_TRADES_MAX_MINUTES duration. With limit, paginates backwards until at least limit trades are collected, then slices to the most recent limit.
Trading pair symbol.
Maximum number of trades to return. If the most recent window has fewer trades than
limit, the function paginates backwards automatically.Promise<IAggregatedTradeData[]> — each entry has price, quantity, timestamp, isBuyerMaker: boolean, and id.
Requires: getAggregatedTrades must be defined in the active exchange schema. Compatible with the volume-anomaly and garch packages which accept the same from/to time range format.
getAveragePrice(symbol)
Returns the VWAP (Volume Weighted Average Price) calculated from the lastCC_AVG_PRICE_CANDLES_COUNT one-minute candles (default: 5). This is the same price used internally by the engine for all entry and exit decisions.
Promise<number> — VWAP. Falls back to the arithmetic mean of closing prices if volume data is unavailable.
Position Data Helpers
These functions read the state of the currently active position for the given symbol. All returnnull if no position is active.
getPositionPnlPercent(symbol)
Current unrealized PnL as a percentage, accounting for slippage, fees, DCA entries, and partial closes.
Promise<number | null>
getPositionPnlCost(symbol)
Current unrealized PnL in dollars (quote currency). Accounts for cost basis after partial closes.
Returns: Promise<number | null>
getPositionEntries(symbol)
Returns the list of all DCA entry levels for the current position.
Promise<Array<{ price: number; cost: number }> | null>
getPositionEntryOverlap(symbol, price, ladder?)
Returns true if price falls within a tolerance band around any existing DCA entry level. Use this to avoid stacking entries too close together.
Trading pair symbol.
Candidate price to check against existing entries.
Upper tolerance band as a percentage (0–100). Defaults to the framework’s overlap detection threshold.
Lower tolerance band as a percentage (0–100).
Promise<boolean> — true if overlap detected.
getPositionEffectivePrice(symbol)
Returns the current harmonic-mean (VWAP) entry price across all DCA entries, adjusted for any partial closes. This is the reference price used for PnL calculations.
Returns: Promise<number | null>
getPositionInvestedCount(symbol)
Returns the number of DCA entries made so far. A value of 1 means only the initial entry; each subsequent commitAverageBuy increments this.
Returns: Promise<number | null>
getPositionInvestedCost(symbol)
Returns the total dollar cost basis of the current position (sum of all accepted DCA entries minus proportional cost from partial closes).
Returns: Promise<number | null>
getPositionHighestProfitPrice(symbol)
Returns the highest price reached by the position since it was opened (for longs) or the lowest price (for shorts). Used for trailing stop calculations.
Returns: Promise<number | null>
getPositionMaxDrawdownPrice(symbol)
Returns the price at which the position experienced its maximum drawdown.
Returns: Promise<number | null>
getPositionActiveMinutes(symbol)
Returns how many minutes the position has been open.
Returns: Promise<number | null>
getPositionPartials(symbol)
Returns the history of partial profit and loss closes for the current position.
Returns: Promise<Array<{ type: 'profit' | 'loss'; percentToClose: number; price: number; cost: number; totalEntries: number }> | null>
getPendingSignal(symbol)
Returns the current active signal row, or null if no position is open.
Returns: Promise<ISignalRow | null>
getScheduledSignal(symbol)
Returns the current scheduled (pending entry at limit price) signal, or null if none.
Returns: Promise<IScheduledSignalRow | null>
getBreakeven(symbol, currentPrice)
Returns true if currentPrice has cleared the breakeven threshold (entry price + slippage + fees). Use to gate commitBreakeven calls.
Returns: Promise<boolean>