Repository navigation
examples: honest backtest protocol on getCandles data - #351
Merged
Merged
Conversation
Add examples/honest-backtest, a self-contained RSI divergence + EMA 200 trend filter backtest (BINANCE:BTCUSDT 4H by default, all parameters configurable) built only on getCandles: - candles fetched once, validated and frozen (forming bar dropped, gaps and SHA-256 recorded, saved for offline replay with --snapshot) - warm-up bars never traded, chronological 70/30 IS/OOS split, no position across the split - pivots confirmed N bars later, signal on the confirmation close, entry at the next bar open - fee and adverse slippage on both sides; same-bar stop and target counts as a loss; gaps through a stop fill at the open - interchangeable exits: fixed 1.5:1 and 2:1, ATR 1.5x stop with 2R target, ATR trailing stop updated on closed bars only - seeded random-entry baseline per exit and split with percentile and p5/p50/p95, compact metrics table and trades CSV Deterministic unit tests on synthetic candles cover pivot timing, next-open entry, same-bar fills, costs, the split, the trailing stop and baseline reproducibility. Link the example from examples/README.md, docs/data-api.md and llms.txt.
Split runTrade into small helpers (openTrade, barExit, gapExit, intrabarExit, closedTrade, reachedStop, reachedTarget) to reduce its complexity. Behaviour is unchanged: fills, exit reasons, costs and the trailing stop update follow the same rules and order as before.
Mathieu2301
marked this pull request as ready for review
October 23, 2026 01:46
Mathieu2301
added a commit
that referenced
this pull request
Oct 23, 2026
New entry point @mathieuc/tradingview/backtest built from examples/honest-backtest (#351): the caller writes only the entry rule; the module freezes candles, rejects look-ahead (LOOK_AHEAD), applies costs, pessimistic fills, the IS/OOS split, four exits and a seeded random baseline, and returns a JSON report. Adds chartData() and backtestMany(). Version 4.1.0-beta.2.
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.
Summary
Adds
examples/honest-backtest/, a self-contained reference for testing a strategy on v4 data, so that people and coding agents can copy it. It uses onlygetCandlesandtimeframeSeconds(root export). It adds no dependency, needs no account and places no orders. It makes no claim of profitability: the README says that a negative or inconclusive result is a valid outcome.The strategy is a regular RSI(14) divergence with an EMA(200) trend filter. It runs on BINANCE:BTCUSDT 4H by default, and every parameter is a CLI option (
--help).--snapshotreplay; warns if history is shorter than 2 tradable yearsfreezeSnapshot,index.jssplitSegments,simulatefindPivots,findSignalsfillPrice,tradeReturnrunTradefixedExit,atrExit,atrTrailingExitrandomBaseline,percentileRankcomputeMetrics,formatTabletradesToCsvDocs:
examples/honest-backtest/README.md, a row and a section inexamples/README.md, a short section indocs/data-api.md, and one line inllms.txt.honest-backtest-output/is git-ignored.Tests
tests/unit/honest-backtest.test.tshas 15 deterministic tests on synthetic candles, with no network. They cover:To check that the tests catch real bugs, I made 7 deliberate code mutations: a backdated signal, entry at the signal close, a trailing look-ahead, a missing exit fee, the same-bar rule counted as a win, a gap filled at the stop, and a position allowed across the split. Each one made at least one test fail.
npm run typecheck: passnpm run lint: passnpm test: 18 files, 191 passed, 2 skipped (unchanged)npm run test:bun: 190 pass, 3 skip, 0 failnpm run check(including build and package smoke under Node and Bun): passThe engine's tests import
examples/honest-backtest/engine.jsthroughengine.d.ts, sotsconfig.jsonis unchanged.Real run (TradingView public data, no account)
node examples/honest-backtest/index.jsran on 2026-10-23 at 01:40 UTC. Replaying the saved snapshot with Bun gave identical output.The result is inconclusive. Each segment has 6 to 10 trades, which is far too few to conclude anything, and the out-of-sample percentiles are spread between 34 and 86 depending on the exit.
Limits and open points
--minYears 3) may be cut short by the server; the run prints a warning when that happens.--snapshotto reproduce a result exactly.