feat(trading-strategies): assessAtrMultiples — sweep to choose the ATR multiple#1132
Open
bennycode wants to merge 1 commit into
Open
feat(trading-strategies): assessAtrMultiples — sweep to choose the ATR multiple#1132bennycode wants to merge 1 commit into
bennycode wants to merge 1 commit into
Conversation
236bd85 to
970954a
Compare
Sweeps a range of ATR multiples over an instrument's candles and reports how each would have behaved (held / protective / whipsawed) plus the static whippy/balanced/ loose band, the resulting trail %, exit, final value, and edge vs buy & hold — so the trail width is chosen from evidence instead of guessed. On the STX window the knee sits between 2.5x (whipsawed) and 3x (held), and the empirical outcome corrects the static band (2-2.5x classify as 'balanced' but whipsaw here).
970954a to
911b346
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an evidence-based helper to choose an ATR trailing-stop multiple by backtesting a sweep of multiples against a window of candles, reporting both the static “band” (whippy/balanced/loose) and the empirical outcome (held/protective/whipsawed) plus edge vs buy & hold.
Changes:
- Introduces
assessAtrMultiples()to sweep ATR multiples, warmAtrTrailStrategyfrom history, backtest across candles, and report outcome/edge metrics. - Adds Vitest coverage exercising sweep shape, a real STX regression outcome (2× whipsawed vs 3× held), value comparison, and empty-input guard.
- Exports the new API and types from the package entrypoint.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/trading-strategies/src/strategy-atr-trail/assessAtrMultiples.ts | New sweep/backtest utility producing per-multiple outcome + performance metrics. |
| packages/trading-strategies/src/strategy-atr-trail/assessAtrMultiples.test.ts | Unit tests validating the sweep report and STX regression outcomes. |
| packages/trading-strategies/src/index.ts | Publicly exports assessAtrMultiples and its related types. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+93
to
+96
| let outcome: AtrMultipleOutcome = 'held'; | ||
| if (exitPrice !== null) { | ||
| outcome = lastClose > exitPrice ? 'whipsawed' : 'protective'; | ||
| } |
Comment on lines
+70
to
+72
| const lastClose = parseFloat(candles[candles.length - 1].close); | ||
| const buyHoldValue = parseFloat(baseBalance) * lastClose; | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #1131 (it imports
AtrTrailStrategy). Retargets tomainonce #1131 merges.Problem
We had helpers to classify a multiple (
classifyAtrMultiple→ whippy/balanced/loose) and convert units (atrMultipleToPercent), but nothing to choose the multiple from an instrument's actual behavior.What this adds
assessAtrMultiples({history, candles, multiples?})sweeps a range of ATR multiples and, for each, warmsAtrTrailStrategyfromhistory, trails it overcandles, and reports:band— static whippy/balanced/looseoutcome— empirical:held/protective(exited then kept falling) /whipsawed(exited then recovered)trailDownPct,exitPrice/exitDate,finalValue,buyHoldValue,edgeVsHoldPctSo you pick the width from evidence instead of a guess.
On the real STX window (5 shares, buy & hold ≈ 4628)
The knee is between 2.5× and 3×. Note the empirical outcome corrects the static band: 2× and 2.5× classify as "balanced" but actually whipsawed on this instrument — which is the whole point of measuring instead of trusting a generic threshold.
Scope (MVP)
Outcome + edge vs buy & hold. Deliberately does not pull the richer suitability metrics (Kaufman efficiency ratio, max drawdown, full verdict) from the old
assessTrailSuitability— easy to add later if wanted.4 tests covering the sweep shape, the 2×-whipsaw / 3×-held outcomes, the edge comparison, and the empty-input guard.