Skip to content

feat: add event-driven Python backtester#5

Draft
Giuseppe84-code wants to merge 3 commits into
mainfrom
claude/funny-darwin-2N7RB
Draft

feat: add event-driven Python backtester#5
Giuseppe84-code wants to merge 3 commits into
mainfrom
claude/funny-darwin-2N7RB

Conversation

@Giuseppe84-code

Copy link
Copy Markdown
Owner

$(cat <<'EOF'

Summary

  • Adds a complete, from-scratch event-driven backtester in backtester/ using only pandas, numpy, yfinance, and matplotlib
  • Six focused modules: data, strategy, engine, metrics, plot, run — each ≤ 200 lines
  • Includes test_synthetic.py that verifies all correctness invariants without network access

Key design decisions

No look-ahead bias (verified by test)

Signal on bar t is produced from data through close[t], but executed at open[t+1]:

pending_signal = signals.iloc[i - 1]   # yesterday's signal
open_px        = df.iloc[i]["open"]     # today's open  ← execution price

The no-look-ahead test confirms entry is always on bar 1, never bar 0.

Transaction costs

Action Effective fill
Buy (long entry / short cover) open × (1 + commission + slippage)
Sell (long exit / short entry) 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.txt
  • python test_synthetic.py — all 5 tests pass
  • python run.py — runs end-to-end once yfinance has network access (blocked in this sandbox)
  • Read README.md for instructions on adding a new strategy via subclassing

https://claude.ai/code/session_017BPoCB77D5qjiMr1z5EaNB
EOF
)


Generated by Claude Code

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
@vercel

vercel Bot commented Jun 4, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
logistics-control-tower Ready Ready Preview, Comment Jun 4, 2026 1:55pm

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants