Backtest Kit treats position management as a first-class citizen of the framework rather than an afterthought. Beyond simple entry and exit, the platform supports full DCA ladders with harmonic-mean cost tracking, partial closes at any percentage, trailing take-profit and stop-loss levels that update on every tick, and automatic breakeven protection. All of these operations maintain mathematically accurate cost-basis accounting across the entire position lifespan.Documentation 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.
Dollar Cost Averaging (DCA)
DCA in Backtest Kit is designed to lower the effective entry price of an existing position, not to pyramid into winners. The engine uses the harmonic mean of all accepted entry prices to compute the effective open price (priceOpen) after each DCA fill.
Why Harmonic Mean
When buying fixed-dollar amounts at different prices, the harmonic mean gives the true average cost per unit — the same result you get by dividing total dollars invested by total units acquired. Arithmetic mean would overstate the effective entry price.Averaging-Down Guard
By default,commitAverageBuy only accepts a new entry when the current price is strictly below the current effective entry price. Attempting to DCA at a price above the effective entry is silently rejected — no error is thrown, and the strategy continues.
After each partial close, the cost basis and effective entry price are recalculated from the remaining position. This means the DCA acceptance threshold shifts after every partial, so a subsequent DCA call is evaluated against the updated effective price, not the original one.
Entry Overlap Detection
Before committing a DCA entry, you can check whether the current price falls too close to an existing DCA entry to avoid clustering entries in the same zone:getPositionEntryOverlap returns true if currentPrice falls within the overlap band of any existing DCA entry, letting you space entries across a meaningful price range.
Partial Profit and Loss Closes
Partial closes let you lock in gains or reduce exposure incrementally without fully exiting the position. Both functions accept a percentage of the current position size to close.priceOpen (effective entry) is recalculated from the new cost basis and remaining coin count, and this updated value determines whether the next commitAverageBuy call will be accepted.
Cost Basis Walkthrough
Consider a long position with three DCA entries followed by a partial profit close:$210 cost basis carries forward. All subsequent DCA acceptance checks and PnL calculations reference this updated basis.
Trailing Stop and Take Profit
Trailing exits move automatically with price on every active tick, ensuring gains are protected as the market moves favorably.- Trailing stop-loss moves upward (for longs) as price rises, locking in a floor below the current price at a fixed percentage distance. It never moves in the unfavorable direction.
- Trailing take-profit adjusts upward as price rises, capturing additional upside beyond the original target without requiring manual intervention.
Breakeven Protection
Breakeven protection automatically moves the stop-loss to the position’s entry price once profit crosses a configured threshold. This converts a winning trade into a risk-free position — price can retrace all the way to entry without triggering a loss.commitBreakeven is called internally and the stop-loss is set to priceOpen. The strategy’s onBreakeven callback fires at this point if one is registered. After breakeven, trailing stop behavior continues from the new floor.
Breakeven and trailing stop can be combined. Once breakeven triggers and the stop moves to entry, the trailing stop takes over from that new floor — it will continue moving upward with price but will never drop below the breakeven level.
Moonbag and Bracket Helpers
For common signal construction patterns, Backtest Kit provides convenience helpers on thePosition class that compute take-profit and stop-loss prices from percentage inputs. Both helpers spread directly into the signal object returned from getSignal.
Position.moonbag computes a take-profit and stop-loss from a single percentStopLoss value, automatically adjusting direction for long and short positions:
Position.bracket computes both take-profit and stop-loss from explicit percentage distances, giving full control over the reward-to-risk ratio:
{ priceOpen, priceTakeProfit, priceStopLoss } computed from currentPrice and the given percentages. They are signal-constructor utilities — they define where the position opens and closes, not what happens after it is open.