Skip to content

Implement self-improvement learning loop with trade analysis and adaptive config tuning#42

Open
Raynergy-svg wants to merge 2 commits intomainfrom
claude/autonomous-self-improvement-prd-qiePI
Open

Implement self-improvement learning loop with trade analysis and adaptive config tuning#42
Raynergy-svg wants to merge 2 commits intomainfrom
claude/autonomous-self-improvement-prd-qiePI

Conversation

@Raynergy-svg
Copy link
Owner

Summary

This PR implements a complete self-improvement learning loop for the Buddy FX trading bot, enabling automatic trade outcome analysis, pattern extraction, rule promotion, and adaptive configuration tuning. The system closes the feedback loop: trades → learnings → rules → config adjustments → better trades.

Key Changes

Core Learning Infrastructure

  • StateEngine (src/scanner/automation/state_engine.py): Manages cross-session state persistence in .claude/state.json, enabling continuity between trading sessions with portfolio snapshots and improvement focus tracking.

  • LearningEngine (src/scanner/automation/learning_engine.py): Analyzes closed trades and extracts actionable patterns:

    • analyze_trade(): Examines trade outcomes against 5 rules (SL too tight, TP too fast, uncertainty warnings, high consensus, model disagreement)
    • check_promotions(): Identifies patterns with 3+ observations and promotes them to trading rules
    • deep_analyze_loss(): Uses local 3B LLM (Qwen2.5) for detailed analysis of significant losses (>$100)
    • audit() / consolidate(): Manages file growth and archives promoted learnings
  • ConfigTuner (src/scanner/automation/config_tuner.py): Reads promoted rules and applies bounded config adjustments:

    • Parses rule patterns and maps them to config fields
    • Enforces safety bounds to prevent runaway tuning (e.g., atr_sl_multiplier: 0.5-2.0)
    • Tracks applied rules to avoid duplicate adjustments

Observation & Metrics

  • ObservationLog (src/scanner/automation/observation_log.py): Captures interesting market patterns during scans (regime changes, agent disagreement, near-misses) even when no trade is taken, building a pattern library for future analysis.

  • ImprovementTracker (src/scanner/automation/improvement_tracker.py): Records session-level metrics (win rate, P/L, learnings added, rules promoted) to improvement_log.jsonl and computes rolling trends to measure whether the learning system itself is improving.

CLI & Integration

  • learn_commands.py (cli/learn_commands.py): New buddy learn CLI command with subcommands:

    • --analyze: Run trade outcome analysis on journal
    • --promote: Check for and promote qualifying patterns
    • --consolidate: Archive and manage file growth
    • --report: Display improvement metrics
    • --status: Show current state and recent learnings
  • continuous.py integration: Learning loop runs automatically in _run_smart_loop() after each RL sync, analyzing newly closed trades and applying config tuning before the next scan cycle.

Configuration & Engine Updates

  • engine.py: Per-pair adaptive SL/TP multipliers (US-006) read from pair_sl_tp_config.json, allowing pair-specific learning overrides.
  • config.py: Added enable_llm_trade_analysis flag to control deep LLM analysis for significant losses.
  • argparser.py: Added learn command with --analyze, --promote, --consolidate, --report, --status flags.
  • main.py: Dispatch handler for learn subcommand.

Notable Implementation Details

  1. Pattern Extraction: Learning entries are formatted as [date] category | pattern_key → action, enabling regex-based pattern counting and promotion when 3+ observations match.

  2. Rule Promotion: Patterns are automatically promoted to .claude/rules/trading.md with specific config change recommendations (e.g., "Increase atr_sl_multiplier by 0.1 for EURUSD").

  3. Safety Bounds: All config adjustments are clamped to predefined bounds to prevent extreme tuning that could destabilize trading.

  4. LLM Integration: Deep loss analysis uses llm_providers.select_buddy_provider_name() to prefer local 3B model (no API tokens required), with graceful fallback if unavailable.

  5. **Cross-Session Continu

https://claude.ai/code/session_01AsBRYCkTiUN4K4WXMSXVkb

claude added 2 commits March 18, 2026 03:20
Complete self-improvement PRD implementation:
- StateEngine: cross-session state persistence (.claude/state.json)
- LearningEngine: trade outcome analysis, rule promotion, LLM deep analysis, consolidation
- ConfigTuner: promoted rules -> adaptive config changes with bounds
- ImprovementTracker: session-level meta-learning metrics (JSONL)
- ObservationLog: market pattern capture from non-trade scans
- Per-pair SL/TP: adaptive multipliers from trade outcomes in engine.py
- CLI: `buddy learn` command with --analyze/--promote/--consolidate/--report/--status
- Integration: full learning loop wired into ContinuousScanner._run_smart_loop()

Architecture: scan -> trade -> close -> analyze -> learn -> promote -> tune -> scan

https://claude.ai/code/session_01AsBRYCkTiUN4K4WXMSXVkb
Replace self_refine.buddy_self_improve() (which uses OpenAI API) with
llm_providers.llm_call() + select_buddy_provider_name() which prefers
the local Buddy Planner 3B (Qwen2.5-3B-Instruct with LoRA adapter).
No API tokens consumed for post-trade loss analysis.

https://claude.ai/code/session_01AsBRYCkTiUN4K4WXMSXVkb
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