For agentic workers: REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (
- [ ]) syntax for tracking.
Goal: Extract one shared sampled-report shell, migrate BBMR and BBSB onto it without behavior change, and add three new sampled-report strategies with explicit CLI parameters.
Architecture: Keep each strategy’s segment loop local to its own module. Extract only the duplicated sampled-report shell into one shared module that owns sampling, aggregate metrics, plotting, HTML rendering, and file writing. Each new strategy then adds only its own segment runner, thin wrapper, tests, and explicit CLI flags.
Tech Stack: Python 3.11+, pandas, bokeh, existing OKX public candle client, existing CLI test harness, plain HTML/CSS/JS
/home/lxy/okx-codex-trader/okx_codex_trader/sampled_report.py/home/lxy/okx-codex-trader/okx_codex_trader/bbmr_report.py/home/lxy/okx-codex-trader/okx_codex_trader/bbsb_report.py/home/lxy/okx-codex-trader/okx_codex_trader/donchian_report.py/home/lxy/okx-codex-trader/okx_codex_trader/rsi2_report.py/home/lxy/okx-codex-trader/okx_codex_trader/ema_pullback_report.py/home/lxy/okx-codex-trader/okx_codex_trader/cli.py/home/lxy/okx-codex-trader/tests/test_sampled_report.py/home/lxy/okx-codex-trader/tests/test_bbmr_report.py/home/lxy/okx-codex-trader/tests/test_bbsb_report.py/home/lxy/okx-codex-trader/tests/test_cli.py/home/lxy/okx-codex-trader/tests/test_donchian_report.py/home/lxy/okx-codex-trader/tests/test_rsi2_report.py/home/lxy/okx-codex-trader/tests/test_ema_pullback_report.pyFiles:
/home/lxy/okx-codex-trader/tests/test_sampled_report.pyCreate: /home/lxy/okx-codex-trader/okx_codex_trader/sampled_report.py
[ ] Step 1: Write the failing shared-shell tests
def test_sample_segments_is_deterministic():
...
def test_sample_segments_rejects_invalid_sampling_result():
...
def test_generate_sampled_report_aggregates_metrics():
...
def test_render_sampled_report_includes_strategy_params():
...
def test_build_segment_plot_embeds_entry_exit_markers():
...
Run: cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest tests/test_sampled_report.py -q
Expected: FAIL on missing shared-shell behavior, not import/setup failure
@dataclass(frozen=True)
class SampledSegment: ...
@dataclass(frozen=True)
class SegmentResult: ...
def sample_segments(...): ...
def trade_equity(...): ...
def mark_to_market(...): ...
def build_segment_plot(...): ...
def render_sampled_report(...): ...
def generate_sampled_report(...): ...
Run: cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest tests/test_sampled_report.py -q
Expected: PASS
Files:
/home/lxy/okx-codex-trader/tests/test_bbmr_report.pyModify: /home/lxy/okx-codex-trader/okx_codex_trader/bbmr_report.py
[ ] Step 1: Add one failing wrapper-contract test
def test_generate_bbmr_sampled_report_uses_shared_shell(monkeypatch, tmp_path):
...
Run: cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest tests/test_bbmr_report.py -q
Expected: FAIL on the new wrapper contract test while current behavior tests still define the expected contract
def generate_bbmr_sampled_report(...):
return generate_sampled_report(
...,
strategy_label="BBMR",
strategy_description="...",
strategy_params={...},
run_segment=run_bbmr_segment,
)
Run: cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest tests/test_bbmr_report.py -q
Expected: PASS
Files:
/home/lxy/okx-codex-trader/tests/test_bbsb_report.pyModify: /home/lxy/okx-codex-trader/okx_codex_trader/bbsb_report.py
[ ] Step 1: Add one failing wrapper-contract test
def test_generate_bbsb_sampled_report_uses_shared_shell(monkeypatch, tmp_path):
...
Run: cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest tests/test_bbsb_report.py -q
Expected: FAIL on the new wrapper contract test while current behavior tests still define the expected contract
def generate_bbsb_sampled_report(...):
return generate_sampled_report(
...,
strategy_label="BBSB",
strategy_description="...",
strategy_params={...},
run_segment=run_bbsb_segment,
)
Run: cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest tests/test_bbsb_report.py -q
Expected: PASS
Files:
Modify: /home/lxy/okx-codex-trader/tests/test_cli.py
[ ] Step 1: Add explicit unchanged-summary assertions
def test_backtest_bbmr_report_summary_shape_is_unchanged(): ...
def test_backtest_bbsb_report_summary_shape_is_unchanged(): ...
Run: cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest tests/test_cli.py -k "bbmr or bbsb" -q
Expected: PASS
Files:
/home/lxy/okx-codex-trader/tests/test_donchian_report.py/home/lxy/okx-codex-trader/okx_codex_trader/donchian_report.py/home/lxy/okx-codex-trader/tests/test_cli.pyModify: /home/lxy/okx-codex-trader/okx_codex_trader/cli.py
[ ] Step 1: Write the failing Donchian strategy and CLI tests
def test_run_donchian_segment_produces_long_trade(): ...
def test_run_donchian_segment_produces_short_trade(): ...
def test_run_donchian_segment_stop_loss_takes_precedence(): ...
def test_run_donchian_segment_does_not_generate_entry_from_final_bar(): ...
def test_run_donchian_segment_marks_open_position_to_market(): ...
def test_backtest_donchian_report_dispatches_generator(): ...
Run: cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest tests/test_donchian_report.py tests/test_cli.py -k donchian -q
Expected: FAIL on missing Donchian behavior and missing CLI dispatch, not import/setup failure
@dataclass(frozen=True)
class DonchianConfig:
entry_window: int = 20
exit_window: int = 10
stop_loss_pct: float = 0.01
def run_donchian_segment(...): ...
def generate_donchian_sampled_report(...): ...
def _add_sampled_report_parser(...): ...
SAMPLED_REPORT_COMMANDS = {
"backtest-donchian-report": {
"generator": generate_donchian_sampled_report,
"parser_args": (...),
"strategy_params": (...),
},
}
Run: cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest tests/test_donchian_report.py tests/test_cli.py -k donchian -q
Expected: PASS
Files:
/home/lxy/okx-codex-trader/tests/test_rsi2_report.py/home/lxy/okx-codex-trader/okx_codex_trader/rsi2_report.py/home/lxy/okx-codex-trader/tests/test_cli.pyModify: /home/lxy/okx-codex-trader/okx_codex_trader/cli.py
[ ] Step 1: Write the failing RSI2 strategy and CLI tests
def test_run_rsi2_segment_produces_long_trade(): ...
def test_run_rsi2_segment_produces_short_trade(): ...
def test_run_rsi2_segment_exit_priority_is_correct(): ...
def test_run_rsi2_segment_does_not_generate_entry_from_final_bar(): ...
def test_run_rsi2_segment_marks_open_position_to_market(): ...
def test_backtest_rsi2_report_dispatches_generator(): ...
Run: cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest tests/test_rsi2_report.py tests/test_cli.py -k rsi2 -q
Expected: FAIL on missing RSI2 behavior and missing CLI dispatch, not import/setup failure
@dataclass(frozen=True)
class RSI2Config:
trend_sma: int = 50
rsi_length: int = 2
rsi_long_threshold: float = 10.0
rsi_short_threshold: float = 90.0
exit_rsi: float = 50.0
def run_rsi2_segment(...): ...
def generate_rsi2_sampled_report(...): ...
Run: cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest tests/test_rsi2_report.py tests/test_cli.py -k rsi2 -q
Expected: PASS
Files:
/home/lxy/okx-codex-trader/tests/test_ema_pullback_report.py/home/lxy/okx-codex-trader/okx_codex_trader/ema_pullback_report.py/home/lxy/okx-codex-trader/tests/test_cli.pyModify: /home/lxy/okx-codex-trader/okx_codex_trader/cli.py
[ ] Step 1: Write the failing EMA pullback strategy and CLI tests
def test_run_ema_pullback_segment_produces_long_trade(): ...
def test_run_ema_pullback_segment_produces_short_trade(): ...
def test_run_ema_pullback_segment_stop_priority_is_correct(): ...
def test_run_ema_pullback_segment_does_not_generate_entry_from_final_bar(): ...
def test_run_ema_pullback_segment_marks_open_position_to_market(): ...
def test_backtest_ema_pullback_report_dispatches_generator(): ...
Run: cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest tests/test_ema_pullback_report.py tests/test_cli.py -k ema_pullback -q
Expected: FAIL on missing EMA pullback behavior and missing CLI dispatch, not import/setup failure
@dataclass(frozen=True)
class EMAPullbackConfig:
fast_ema: int = 20
slow_ema: int = 50
stop_buffer_pct: float = 0.005
def run_ema_pullback_segment(...): ...
def generate_ema_pullback_sampled_report(...): ...
Run: cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest tests/test_ema_pullback_report.py tests/test_cli.py -k ema_pullback -q
Expected: PASS
Files:
None
[ ] Step 1: Run focused and full verification
Run:
cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest tests/test_sampled_report.py -q
cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest tests/test_bbmr_report.py tests/test_bbsb_report.py -q
cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest tests/test_donchian_report.py tests/test_rsi2_report.py tests/test_ema_pullback_report.py -q
cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest tests/test_cli.py -k "bbmr or bbsb or donchian or rsi2 or ema_pullback" -q
cd /home/lxy/okx-codex-trader && .venv/bin/python -m pytest -q
Expected:
BBMR/BBSB suites stay greenfull suite stays green
[ ] Step 2: Generate one real report per new strategy
Run:
cd /home/lxy/okx-codex-trader && .venv/bin/python -m okx_codex_trader.cli backtest-donchian-report --symbol BTC-USDT-SWAP --bar 3m --history-limit 5000 --leverage 2 --segments 8 --window-size 300 --output-file donchian-sampled-report.html
cd /home/lxy/okx-codex-trader && .venv/bin/python -m okx_codex_trader.cli backtest-rsi2-report --symbol BTC-USDT-SWAP --bar 3m --history-limit 5000 --leverage 2 --segments 8 --window-size 300 --output-file rsi2-sampled-report.html
cd /home/lxy/okx-codex-trader && .venv/bin/python -m okx_codex_trader.cli backtest-ema-pullback-report --symbol BTC-USDT-SWAP --bar 3m --history-limit 5000 --leverage 2 --segments 8 --window-size 300 --output-file ema-pullback-sampled-report.html
Expected:
0report_file, segment_count, window_size, aggregate_trade_count, average_return