Implement self-improvement learning loop with trade analysis and adaptive config tuning#42
Open
Raynergy-svg wants to merge 2 commits intomainfrom
Open
Implement self-improvement learning loop with trade analysis and adaptive config tuning#42Raynergy-svg wants to merge 2 commits intomainfrom
Raynergy-svg wants to merge 2 commits intomainfrom
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 rulesdeep_analyze_loss(): Uses local 3B LLM (Qwen2.5) for detailed analysis of significant losses (>$100)audit()/consolidate(): Manages file growth and archives promoted learningsConfigTuner (
src/scanner/automation/config_tuner.py): Reads promoted rules and applies bounded config adjustments:atr_sl_multiplier: 0.5-2.0)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) toimprovement_log.jsonland computes rolling trends to measure whether the learning system itself is improving.CLI & Integration
learn_commands.py (
cli/learn_commands.py): Newbuddy learnCLI 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 learningscontinuous.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
pair_sl_tp_config.json, allowing pair-specific learning overrides.enable_llm_trade_analysisflag to control deep LLM analysis for significant losses.learncommand with--analyze,--promote,--consolidate,--report,--statusflags.learnsubcommand.Notable Implementation Details
Pattern Extraction: Learning entries are formatted as
[date] category | pattern_key → action, enabling regex-based pattern counting and promotion when 3+ observations match.Rule Promotion: Patterns are automatically promoted to
.claude/rules/trading.mdwith specific config change recommendations (e.g., "Increase atr_sl_multiplier by 0.1 for EURUSD").Safety Bounds: All config adjustments are clamped to predefined bounds to prevent extreme tuning that could destabilize trading.
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.**Cross-Session Continu
https://claude.ai/code/session_01AsBRYCkTiUN4K4WXMSXVkb