feat: add event-driven Python backtester#5
Draft
Giuseppe84-code wants to merge 3 commits into
Draft
Conversation
Six-module backtester (data, strategy, engine, metrics, plot, run) built with only pandas/numpy/yfinance/matplotlib — no external backtesting frameworks. Key design guarantees: - Signal on bar t executes at OPEN of bar t+1 (no look-ahead bias) - Round-trip transaction costs: configurable commission + slippage - Equity curve = cash + shares × close (consistent MTM) - Trade log PnL invariant: sum == equity_gain (enforced by tests) Includes SmaCrossover(fast, slow) as the reference strategy and a README explaining how to subclass Strategy to add new ones. https://claude.ai/code/session_017BPoCB77D5qjiMr1z5EaNB
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Excludes __pycache__, .cache/ (parquet downloads), and generated *.png files from version control. https://claude.ai/code/session_017BPoCB77D5qjiMr1z5EaNB
Single-shot, cron-friendly forward tester that imports the EXACT strategy code (strategy.py) and metrics (metrics.py) from the backtester — no logic is reimplemented, so forward results compare apples-to-apples. - broker_paper.py: PaperBroker simulated account. Same cost model as engine.py (0.1% commission + 0.05% slippage per side), same position/PnL accounting and trade-log shape. Full state serialized to state.json. - runner.py: one decision per invocation. Loads state, fetches latest bars via yfinance, gets signal from the imported strategy, executes at the latest real close, appends to log.csv, saves state, prints one-line status. Look-ahead-free spots are commented (signal on closed bars; execution at the latest observed price only). - report.py: rebuilds the equity curve from log.csv and runs the backtester's compute_metrics over it for direct backtest-vs-forward comparison. - FORWARD_TEST.md: cron/launchd scheduling and how to reset (rm state.json). - test_forward.py: verifies state persistence across 351 simulated runs, exact cost-model match vs. backtester, and save/load round-trip integrity. state.json and log.csv are gitignored (per-machine runtime state). https://claude.ai/code/session_017BPoCB77D5qjiMr1z5EaNB
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.
$(cat <<'EOF'
Summary
backtester/using onlypandas,numpy,yfinance, andmatplotlibdata,strategy,engine,metrics,plot,run— each ≤ 200 linestest_synthetic.pythat verifies all correctness invariants without network accessKey design decisions
No look-ahead bias (verified by test)
Signal on bar
tis produced from data throughclose[t], but executed atopen[t+1]:The no-look-ahead test confirms entry is always on bar 1, never bar 0.
Transaction costs
open × (1 + commission + slippage)open × (1 − commission − slippage)Defaults: 0.10% commission + 0.05% slippage per side.
Equity invariant
sum(trade_log PnL) == equity.iloc[-1] − initial_capital— tested explicitly.Test plan
cd backtester && pip install -r requirements.txtpython test_synthetic.py— all 5 tests passpython run.py— runs end-to-end once yfinance has network access (blocked in this sandbox)README.mdfor instructions on adding a new strategy via subclassinghttps://claude.ai/code/session_017BPoCB77D5qjiMr1z5EaNB
EOF
)
Generated by Claude Code