Commit functions are the only way to mutate position state in Backtest Kit. They are transactional: in live mode, every commit function fires the correspondingDocumentation 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.
IBroker adapter method before the internal state changes. If the exchange rejects the order, the network fails, or the fill times out, the adapter throws, the mutation is skipped, and Backtest Kit retries automatically on the next tick. In backtest mode, commits update the in-memory simulation state directly.
In backtest mode without a broker adapter registered, commit functions skip the exchange interaction and update the simulation state directly. To enable broker integration in live mode, call
Broker.useBrokerAdapter(...) and Broker.enable() at startup.commitAverageBuy(symbol, cost?)
Adds a new DCA (Dollar Cost Averaging) entry to the current position. By default, the entry is only accepted if the current VWAP is strictly below the effective average entry price of the existing position — this prevents averaging up and enforces downward DCA discipline. The acceptance threshold is configurable viasetConfig.
When accepted, the new entry is appended to the position’s _entry array, the harmonic-mean entry price is recalculated, and the stop-loss and take-profit levels on the exchange are updated to reflect the full combined position.
The trading pair symbol. Must match the symbol of the currently active position.
Dollar amount for this DCA entry. Defaults to the
cost value from the original signal if not provided.listenActivePing (or onActivePing) to build DCA ladders. Use getPositionEntryOverlap to prevent stacking entries at similar price levels and getPositionInvestedCount to cap the total number of entries.
commitPartialProfit(symbol, percentToClose)
Closes a percentage of the current position at the current VWAP as a profit-taking action. The closed portion’s cost basis is removed proportionally from the total, and the remaining position continues to be monitored with the same TP/SL levels (adjusted on the exchange for the reduced quantity).The trading pair symbol.
Percentage of the current position to close (1–100). For example,
50 closes half the position.commitPartialProfit(symbol, 30), the remaining cost basis is 70% of what it was. The effective entry price is unchanged, but the weight of this position in subsequent PnL calculations is reduced proportionally.
commitPartialLoss(symbol, percentToClose)
Closes a percentage of the current position at the current VWAP as a loss-cutting action. Identical mechanics tocommitPartialProfit but intended for reducing exposure before the stop-loss level is hit.
The trading pair symbol.
Percentage of the current position to close (1–100).
commitClosePending(symbol, options?)
Manually closes the active position at the current VWAP, regardless of the TP/SL levels. This is a clean close — it does not mark the signal as a stop-loss or take-profit hit. ThecloseReason on the resulting closed event will be 'close_pending'.
This commit does not interrupt strategy execution. The strategy continues running after the close and can generate new signals on subsequent ticks.
The trading pair symbol.
Optional identifier for this close action. Appears in the
closeId field of the resulting IStrategyTickResultClosed event.Optional human-readable annotation for the close reason. Appears in reports and notification payloads.
commitBreakeven(symbol)
Moves the stop-loss to the effective entry price (accounting for fee and slippage buffer so a breakeven close is still profitable net of costs). This is a one-time operation per signal — callingcommitBreakeven multiple times on the same signal is a no-op after the first call.
The engine calculates the breakeven threshold automatically using CC_PERCENT_FEE and CC_PERCENT_SLIPPAGE. Calling commitBreakeven before the price has cleared this threshold is silently ignored.
The trading pair symbol.
commitTrailingStop(symbol, percentShift, currentPrice)
Adjusts the trailing stop-loss by a percentage shift relative to the original stop-loss distance (not the current trailing value). This avoids compounding rounding errors when chaining multiple trailing adjustments. The engine enforces ratchet absorption: for long positions, the adjusted stop-loss is only applied if it is higher than the current stop-loss (only tightening allowed). For short positions, only a lower stop-loss is accepted. Use a negativepercentShift to bring the stop closer to entry; use a positive value to widen it.
To set an absolute stop-loss price directly, use commitTrailingStopCost(symbol, newStopLossPrice) instead.
The trading pair symbol.
Percentage adjustment relative to the original stop-loss distance. Negative values move the stop closer to entry (tightening); positive values widen it. The ratchet ensures the result is only applied if it improves the stop level.
Current market price, used to compute the adjusted stop-loss level.
commitTrailingTake(symbol, percentShift, currentPrice)
Adjusts the trailing take-profit by a percentage shift relative to the original take-profit distance. Always calculates from the original TP, not the current trailing value, to prevent error accumulation. The engine enforces ratchet absorption: for long positions, only a lower take-profit is accepted (tightening the target). For short positions, only a higher take-profit is accepted. Use a negativepercentShift to bring TP closer to entry (more conservative); positive to move it further away.
To set an absolute take-profit price directly, use commitTrailingTakeCost(symbol, newTakeProfitPrice) instead.
The trading pair symbol.
Percentage adjustment relative to the original take-profit distance. Negative values move TP closer to entry (more conservative); positive values move it further away. Ratchet semantics apply.
Current market price, used to compute the adjusted take-profit level.
Dollar-Amount Variants
For convenience,commitPartialProfit and commitPartialLoss have dollar-amount variants that compute the correct percentage automatically:
commitTrailingStopCost / commitTrailingTakeCost when you have a concrete target price. Use commitTrailingStop / commitTrailingTake when you want to shift relative to the original stop/TP distance.