Post-trade feedback loop system: shaped-reward logging, diagnostics, and self-heal. Use when investigating trade outcomes, checking system health, or understanding automated corrective actions.
Instrucciones de origen · Vista previa de solo lectura
name
feedback_loop
description
Post-trade feedback loop system: shaped-reward logging, diagnostics, and self-heal. Use when investigating trade outcomes, checking system health, or understanding automated corrective actions.
user-invocable
false
Post-Trade Feedback Loop
Automated post-trade analysis pipeline that logs shaped rewards, runs health diagnostics, and applies corrective actions without human intervention.
Loop Diagram
Trade Close (OANDA)
|
v
PostTradeLoop.run(trade_result)
|
+---> [1] Idempotency check (trade_id dedup)
|
+---> [2] Load journal entry from trade_journal_rl.json
|
+---> [3] Compute shaped reward
|
+---> [4] Append to logs/trade_feedback.jsonl
|
+---> [5] Append learning if |reward| > 2R
|
+---> [6] PostTradeDiagnostics.run()
| |
| v
| Structured health report
| {status: HEALTHY|DEGRADED|CRITICAL}
| |
| v (if not HEALTHY)
+---> [7] SelfHeal.apply(diag_result)
|
v
Decision log + corrective actions
When It Fires
Per trade close: Hooked into close_trade_oanda() in src/scanner/execution.py. Every confirmed OANDA trade close triggers PostTradeLoop.run().
Per cycle: The orchestrator dispatches feedback processing each scan cycle via run_cycle().
Manual: Via MCP tools (trigger_rl_update, trigger_diagnostics, ) or CLI ().
trigger_self_heal
python main.py feedback-status
What It Does NOT Do
The feedback loop does NOT update agent weights. Weight updates remain on the existing batch path in execution.sync_closed_trades_rl(). This is a hard scope rule (Parity Q5 compliance). The loop only logs shaped rewards and triggers diagnostics/self-heal.
When a CRITICAL issue is detected or any self-heal handler raises an exception, the system enters degraded mode.
Marker file: trained_data/self_heal_degraded.flag (JSON with timestamp, reason, source).
Effect: The next scan cycle reads this flag and runs in conservative fallback (reduced position sizing, tighter gates).
Clearing: Delete the flag file manually or let a subsequent healthy diagnostics cycle clear it.
# Check if degraded mode is activecat trained_data/self_heal_degraded.flag
# Clear degraded mode manuallyrm trained_data/self_heal_degraded.flag
Manual Triggers
Via MCP Tools
trigger_diagnostics # Run the 5-check diagnostics suite
trigger_self_heal # Run diagnostics + apply corrective actions
trigger_rl_update(trade_id) # Run full feedback loop for one trade
Via CLI
python main.py feedback-status # Show current feedback loop state
Log Files
File
Format
Contents
logs/trade_feedback.jsonl
JSONL
One record per processed trade: shaped_reward, pnl_pips, slippage, mae, regime, source
logs/system_decisions.jsonl
JSONL
One record per self-heal action: trigger, issue, action_taken, files_modified, degraded_mode
logs/unhandled_errors.jsonl
JSONL
Safety-net errors from post_trade_loop or self_heal modules
.Codex/learnings.md
Markdown
Significant outcomes (>= 2R) appended as dated bullet entries
Idempotency
The feedback loop deduplicates by trade_id. Before processing, PostTradeLoop._already_processed() scans the last 500 lines of logs/trade_feedback.jsonl for a matching trade_id string. If found, the trade is skipped with {"status": "skipped", "reason": "already_processed"}.
This means you can safely call PostTradeLoop.run() multiple times for the same trade without producing duplicate records.
Escalation Criteria
Alert a human when:
CRITICAL status on two consecutive cycles -- the self-heal actions are not resolving the underlying issue.
Repeated self-heal failures -- multiple "success": false entries in logs/system_decisions.jsonl for the same action type.
Degraded mode persisting > 1 hour -- check the timestamp in trained_data/self_heal_degraded.flag. If it is older than 1 hour and still present, manual investigation is required.
Key Source Files
src/scanner/feedback/post_trade_loop.py -- PostTradeLoop.run() entry point
src/scanner/feedback/diagnostics.py -- PostTradeDiagnostics.run() health checks