Skip to content

Dev#14

Open
cloudQuant wants to merge 158 commits into
developmentfrom
dev
Open

Dev#14
cloudQuant wants to merge 158 commits into
developmentfrom
dev

Conversation

@cloudQuant
Copy link
Copy Markdown
Owner

Backtrader 去元编程项目 - Pull Request

📋 PR 概述

基本信息

  • PR类型: [ ] 元编程移除 [ ] 性能优化 [ ] Bug修复 [ ] 文档更新 [ ] 测试增强
  • 影响范围: [ ] 核心系统 [ ] 单个模块 [ ] 测试代码 [ ] 文档 [ ] 工具
  • 破坏性变更: [ ] 是 [ ] 否
  • 关联Issue: #[Issue编号]

变更摘要

修改范围

  • backtrader/[模块名].py: [变更描述]
  • tests/[测试文件].py: [变更描述]

🎯 元编程移除验证 ⭐⭐⭐⭐⭐

移除的元编程模式

  • 移除的元类:

    • MetaStrategy - [替代方案描述]
    • MetaIndicator - [替代方案描述]
    • MetaLineSeries - [替代方案描述]
    • MetaParams - [替代方案描述]
    • MetaSingleton - [替代方案描述]
    • 其他: [具体元类名] - [替代方案描述]
  • 移除的动态类创建:

    • type() 动态创建 - [替代方案描述]
    • setattr 动态属性 - [替代方案描述]
    • 其他: [具体模式] - [替代方案描述]

替代方案实现

  • 使用的替代技术:

    • 描述符 (Descriptors)
    • 混入类 (Mixins)
    • 组合模式 (Composition)
    • __init_subclass__ 方法
    • 依赖注入
    • 其他: [具体技术]
  • 实现完整性验证:

    • 所有原有功能已被替代实现
    • 边界条件处理完善
    • 错误处理机制保持一致

工具验证

# 运行元编程检测工具
python tools/metaclass_detector.py

# 检查结果:
# - 元类使用点数量: [之前] → [现在]
# - 动态创建点数量: [之前] → [现在]
# - 验证结果: [ ] 通过 [ ] 未通过

🔄 API兼容性验证 ⭐⭐⭐⭐

公共接口检查

  • 方法签名保持不变:

    • 所有公共方法签名未变化
    • 参数名称和默认值保持一致
    • 返回值类型保持兼容
  • 属性访问保持一致:

    • 关键属性 (如 lines, params) 访问方式不变
    • 属性值类型和格式保持一致
    • 动态属性访问行为保持相同

行为兼容性

  • 核心功能验证:

    • 策略执行行为一致
    • 指标计算结果一致
    • 数据访问行为一致
    • 参数处理行为一致
  • 异常处理验证:

    • 异常类型保持一致
    • 错误信息保持有用性
    • 边界条件处理一致

兼容性测试结果

# 运行兼容性测试框架
python tools/compatibility_tester.py

# 测试结果:
# - API兼容性: [通过率]%
# - 行为兼容性: [通过率]%
# - 性能对比: [变化百分比]%
# - 总体评估: [ ] PASS [ ] FAIL

⚡ 性能影响评估 ⭐⭐⭐

性能基准测试

  • 执行性能对比:

    • 策略执行时间: [变化] ([百分比]%)
    • 指标计算时间: [变化] ([百分比]%)
    • 数据访问时间: [变化] ([百分比]%)
    • 启动时间: [变化] ([百分比]%)
  • 内存使用分析:

    • 内存占用: [变化] ([百分比]%)
    • 无内存泄漏验证
    • 垃圾回收影响评估

性能测试命令

# 运行性能基准测试
python tools/performance_benchmark.py --before [git-ref] --after HEAD

# 性能目标:
# - 执行时间变化: ≤ 10%
# - 内存使用变化: ≤ 20%
# - 启动时间变化: ≤ 30%

关键路径优化

  • 热点代码路径检查:
    • 数据访问路径优化
    • 指标计算路径优化
    • 策略执行路径优化

🧪 测试覆盖验证 ⭐⭐⭐

测试覆盖率

# 运行测试覆盖率检查
python -m pytest tests/ --cov=backtrader --cov-report=term-missing

# 覆盖率要求:
# - 新增代码覆盖率: ≥ 85%
# - 修改代码覆盖率: ≥ 90%
# - 关键路径覆盖率: ≥ 95%
  • 测试类型覆盖:
    • 单元测试: [数量] 个
    • 集成测试: [数量] 个
    • 回归测试: [数量] 个
    • 性能测试: [数量] 个

新增测试用例

  • 功能测试:

    • 测试用例1: [描述]
    • 测试用例2: [描述]
  • 边界条件测试:

    • 测试用例1: [描述]
    • 测试用例2: [描述]
  • 异常情况测试:

    • 测试用例1: [描述]
    • 测试用例2: [描述]

📊 代码质量检查 ⭐⭐⭐

静态分析结果

# 代码格式检查
black --check backtrader/

# 静态分析
pylint backtrader/ --score=yes

# 类型检查
mypy backtrader/
  • 代码规范检查:
    • Black 格式化: [ ] 通过 [ ] 未通过
    • Pylint 评分: [分数]/10 (要求 ≥ 8.0)
    • MyPy 类型检查: [ ] 通过 [ ] 有警告

代码质量评估

  • 可读性和维护性:

    • 函数复杂度适中 (≤ 10)
    • 类设计合理 (≤ 500行)
    • 命名清晰一致
    • 注释和文档充分
  • 架构设计:

    • 单一职责原则
    • 开闭原则
    • 依赖倒置原则
    • 接口隔离原则

📚 文档更新 ⭐⭐

文档变更

  • API文档:

    • 新增API文档
    • 修改API文档
    • 废弃API说明
  • 用户文档:

    • 使用指南更新
    • 迁移指南更新
    • 示例代码更新
  • 开发文档:

    • 架构文档更新
    • 贡献指南更新
    • 故障排除指南更新

文档质量

  • 内容质量:
    • 信息准确完整
    • 示例代码可执行
    • 链接和引用有效

🔍 回归测试验证

完整测试套件

# 运行完整的回归测试
python -m pytest tests/ -v --timeout=300

# 测试结果统计:
# - 总测试数: [数量]
# - 通过数: [数量]
# - 失败数: [数量]
# - 跳过数: [数量]
# - 通过率: [百分比]%
  • 核心功能测试:

    • 策略系统测试: [ ] 通过
    • 指标系统测试: [ ] 通过
    • 数据系统测试: [ ] 通过
    • 经纪商系统测试: [ ] 通过
  • 集成测试:

    • 端到端测试: [ ] 通过
    • 多组件协作测试: [ ] 通过
    • 用户场景测试: [ ] 通过

已知问题

  • 当前已知问题:
    • 问题1: [描述] - [状态]
    • 问题2: [描述] - [状态]

📝 变更详情

主要变更

  1. [变更类别]: [详细描述]

    • 文件: [文件路径]
    • 变更: [具体变更内容]
    • 原因: [变更原因]
  2. [变更类别]: [详细描述]

    • 文件: [文件路径]
    • 变更: [具体变更内容]
    • 原因: [变更原因]

技术实现细节

设计决策

🚨 风险评估

潜在风险

  • 高风险:

    • 风险1: [描述] - [缓解措施]
    • 风险2: [描述] - [缓解措施]
  • 中等风险:

    • 风险1: [描述] - [缓解措施]
    • 风险2: [描述] - [缓解措施]

回滚计划

  • 回滚方案:
    • 回滚步骤已准备
    • 回滚测试已验证
    • 数据备份已完成

✅ 审查检查清单

自检完成项

  • 元编程完全移除
  • 兼容性测试通过
  • 性能测试通过
  • 代码质量检查通过
  • 测试覆盖率达标
  • 文档已更新
  • 工具验证通过

待审查项

  • 代码架构设计
  • 实现方案合理性
  • 边界条件处理
  • 错误处理机制
  • 性能优化机会
  • 安全性考虑

📞 联系信息

PR提交者: [姓名]
联系方式: [邮箱/Slack]
提交时间: [日期时间]
预计审查时间: [时间估算]


🔧 审查者使用指南

审查步骤

  1. 克隆分支: git checkout [branch-name]
  2. 运行自动化检查: 按照上述命令执行各项检查
  3. 手动代码审查: 重点关注元编程移除和兼容性
  4. 测试验证: 运行完整测试套件
  5. 文档审查: 检查文档更新的准确性

审查标准

  • 必须通过: 元编程移除、API兼容性、性能要求
  • 建议改进: 代码质量、文档完整性、测试覆盖
  • 可选优化: 性能优化、架构改进

审查反馈模板

请使用代码审查规范中的反馈模板进行审查反馈。


模板版本: 1.0
最后更新: 2025年05月30日

修改内容:
- 重命名并优化测试文件
- 实现8个 SimNow 环境
- 添加 mock 数据支持
- 修复 CTP 连接和测试
- 添加类型存根依赖: types-python-dateutil, types-PyMySQL
- 修复 writer.py: collections.abc 导入、类型注解、Optional 检查
- 修复 bokeh/utils/helpers.py: datanames 类型注解
- 配置 pyproject.toml: 添加 per-module mypy overrides,减少框架模式的误报

Made-with: Cursor
cloudQuant and others added 16 commits March 12, 2026 15:13
# Conflicts:
#	backtrader/stores/btapistore.py
- 标记Phase 0仓库卫生治理为已完成
- 更新风险点A-E,附精确行号和代码审计证据
- 纠正Store两套继承体系的错误描述
- 新增TestStrategy硬编码类名检查等新发现
- 更新Phase 1-4任务拆分和验收标准
- 更新文件映射、里程碑、任务单
- 新增附录A:代码审计证据摘要
…llback (T02-T05)

T02: Add 16 regression tests for strategy instantiation chain
T03: Remove hardcoded 'TestStrategy' class name check in __init__
T04: Fix _create_strategy_safely to pass kwargs through instead of {}
T05: Remove silent fallback that caught init exceptions and retried

All 746 tests pass, 0 regressions.
…T06-T08)

T06: Narrow channel mode 'except Exception: continue' to 'except StrategySkipError'
T07: Replace hardcoded ~/cerebro_runnext_error.log with logger.exception()
T08: Remove dead code: _rs/_rp unused vars, my_num debug counter,
     unreachable code in btapistore.py:231-234, commented debug prints

All 746 tests pass, 0 regressions.
T09: Fix LiveStoreBase.start() to accept optional data/broker params,
     matching actual usage in BtApiStore/btapifeed/btapibroker.
     Add 7 Store contract tests verifying ABC enforcement,
     incomplete subclass rejection, and BtApiStore compliance.

All 753 tests pass, 0 regressions.
R1: strategy.py:160-164 - params creation failure now raises TypeError
    instead of silently creating empty ParamsInstance
R2: cerebro.py dt0<1 - clarified as valid sentinel guard, added
    logger.warning for visibility
R3: while True loop - documented as intentional (_last() needs extra round)
R4: newqcheck - documented mixed live/historical qcheck override logic
R5: store.py - added docstring documenting two Store hierarchies
    (legacy Store vs LiveStoreBase) and migration guidance
R6: _runnext - added phase docstring, cleaned commented-out plotfillers
    and debug print; deeper method extraction deferred (tight coupling)

All 753 tests pass, 0 regressions.
- All Phase headers (0-4) marked ✅
- All milestones M1-M6 marked completed
- All 16 task tickets (T01-T10, R1-R6) marked completed with evidence
- Appendix A audit findings updated with fix status per item
- Test coverage section updated: 730 → 753 tests (+23 new)
- Fixed outdated LiveStoreBase.start() description
- Added completion date header
cloudQuant and others added 30 commits March 19, 2026 19:39
Source code fixes:
- dateintern: fix get_last_timeframe_timestamp infinite loop when time_diff<=0, optimize O(n)->O(1)
- dateintern: fix tzparse crash (AttributeError) on unknown timezone strings
- autodict: fix AutoDict._close() broken (private attrs stored as dict keys instead of __dict__)
- autodict: fix AutoOrderedDict copy-paste bugs in __imul__/__idiv__/__itruediv__ (returned + instead of *,//,/)
- autodict: add key info to KeyError in AutoDict/AutoOrderedDict.__missing__
- autodict: remove dead code (if False branches) in AutoDict
- position: remove dead code in Position.__init__ (upopened/upclosed overwritten by set())
- mathsupport: add is_finite_real utility function
- analyzers: centralize is_finite_real usage in sharpe/drawdown/leverage
- strategy: fix mutable default args in _notify ([] -> None)
- feed: add null check in CSVDataBase.preload for self.f
- errors: improve ModuleImportError/FromModuleImportError

New tests (87): test_quality_improvements_v2.py covering all fixes
Verified: 1355 passed, 1 skipped (pytest tests -n 30)
…it, fix DotDict/Position/Writer

Code quality improvements:
- functions.py: Fix List.__contains__ to use identity check ('is') instead of
  hash comparison, matching the original design intent and avoiding hash collisions
- trade.py: Add __repr__ for readable debugging output
- order.py: Add __repr__ to OrderExecutionBit for readable debugging output
- utils/autodict.py: Fix DotDict.__getattr__ to raise AttributeError for dunder
  attributes instead of calling non-existent super().__getattr__
- position.py: Fix Position.set() upopened=size when opening from zero;
  add __repr__ and __eq__ methods
- writer.py: Add context manager support and safer stop() for WriterFile

All 1415 tests pass (pytest tests -n 30).
…ontracts

- AutoDict/AutoOrderedDict: __getattr__ now raises AttributeError with key info
  instead of bare AttributeError, improving debuggability and hasattr() behavior
- Position: add explicit __hash__ = None (mutable objects must not be hashable)
- Trade.__repr__: bounds-safe status name lookup prevents IndexError/TypeError
- mathsupport: guard variance() and standarddev() against empty sequences
- CommInfo subclasses: guard get_margin() against None margin parameter
  (ComminfoDC, ComminfoFuturesPercent, ComminfoFuturesFixed, ComminfoFundingRate)
- Add 32 new unit tests covering all improvements
…ive guards

- Order: add __hash__ based on immutable ref (enables use in sets/dicts)
- Order.alive(): use frozenset for O(1) status membership lookup
- Order.getstatusname()/getordername(): bounds-safe with try/except
- SQN: explicit zero-stddev guard before division (avoids ZeroDivisionError)
- Writer.stop(): narrow except to OSError instead of bare Exception
- BuySell.next(): narrow except to (IndexError, AttributeError, TypeError)
- Envelope: narrow except to (AttributeError, StopIteration, TypeError, KeyError)
- HurstExponent: narrow except to (ValueError, TypeError, FloatingPointError)
- Add 32 new unit tests covering all improvements
- trade.py: getdataname/open_datetime/close_datetime safe when data is None
- position.py: clone() now preserves datetime/adjbase/updt state
- mathsupport.py: average() handles negative denominator edge case (empty list + bessel)
- stochastic.py: division by zero guard in Stochastic.next()
- store.py: put_notification/get_notifications safe when notifs is None
- Added 34 new tests covering all improvements (test_quality_improvements_v6.py)

All 1513 tests pass (1479 original + 34 new), 1 skipped.
…pleteness

- timer.py: Fix mutable default arguments for weekdays/monthdays parameters
  (changed from [] to None to prevent shared state across instances)
- percents_sizer.py: Add NaN guard for close_price in _getsizing
  (float('nan') is truthy, so 'not close_price' didn't catch it)
- comminfo.py: Guard against None interest in _creditrate calculation
- position.py: Make clone() copy all state fields (upopened, upclosed, price_orig)
- Add 23 new unit tests covering all fixes (tests/unit/test_quality_fixes.py)

All 1536 tests pass (pytest tests -n 30), 1 skipped.
Add HFT broker/comparison modules, maker-taker commission handling, MixBroker/TickBroker support, examples/tests/docs, queue market making alignment, and NumPy 2.0 plot compatibility.
- Improve trade logger observer with better edge case handling
- Update position modes for dual side trading support
- Enhance BtApiStore with dual side capabilities
- Add dual side trading examples: basic, hedge, and vs_net modes
- Update CTP example configurations across all broker types
- Add comprehensive test coverage for trade logger and CTP examples
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