Skip to content

fix: round 2 backtest engine correctness and performance fixes#37

Merged
AlanFokCo merged 2 commits into
mainfrom
copilot/easyquant-optimization-round-two
May 28, 2026
Merged

fix: round 2 backtest engine correctness and performance fixes#37
AlanFokCo merged 2 commits into
mainfrom
copilot/easyquant-optimization-round-two

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 28, 2026

Second optimization pass on the EasyQuant backtest framework, targeting correctness bugs in order execution and attribution, plus memory/performance improvements.

Bug fixes

  • ORDER_TARGET partial fill logic (engine.py): Remaining order after partial fill now keeps the original target_amount. The engine already computes delta = target - current_position on the next bar, so subtracting filled shares caused systematic undershoot.
# Before (wrong): target 1000, filled 300 → remaining target = 700
# Next bar: delta = 700 - 300 = 400 (misses 300 shares)
remaining_req["target_amount"] = target - filled

# After (correct): target 1000, filled 300 → remaining target = 1000  
# Next bar: delta = 1000 - 300 = 700 (correct)
remaining_req["target_amount"] = order_req["target_amount"]
  • ORDER_TIMEOUT crashes attribution (attribution.py): _calc_trade_win_rate() and _calc_profit_loss_ratio() now skip non-BUY/SELL entries. ORDER_TIMEOUT log entries lack price/amount keys, causing KeyError.

  • NaN propagation in excess metrics (attribution.py): _calc_excess_metrics() applies fillna(0) on benchmark returns before subtraction, preventing NaN from poisoning all downstream excess return metrics.

Performance / memory

  • Unbounded _file_locks dict (data_cache.py): Capped at 1024 entries with eviction of unlocked entries. Previously grew monotonically in long-running services.

  • _to_tv_date() simplification (report.py): Collapsed 5 isinstance checks to 1 — datetime.date covers datetime.datetime and pd.Timestamp via inheritance.

  • _bare_code() helper (engine.py): Extracted the repeated .replace(".XSHG","").replace(".XSHE","") pattern (6 call sites) into a single function.

Tests

17 new tests in test_round2_fixes.py covering all fixes. 391 total pass (374 + 17).

Copilot AI added 2 commits May 28, 2026 08:15
…filter, NaN benchmark, bounded locks, _bare_code helper, _to_tv_date simplification
Copilot AI changed the title fix: EasyQuant 第二轮优化 — ORDER_TARGET partial fill, ORDER_TIMEOUT filter, NaN benchmark, bounded locks, _bare_code, _to_tv_date fix: round 2 backtest engine correctness and performance fixes May 28, 2026
Copilot AI requested a review from AlanFokCo May 28, 2026 08:18
@AlanFokCo AlanFokCo marked this pull request as ready for review May 28, 2026 08:42
@AlanFokCo AlanFokCo merged commit 3aef776 into main May 28, 2026
4 checks passed
@AlanFokCo AlanFokCo deleted the copilot/easyquant-optimization-round-two branch May 28, 2026 08:42
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