Spaces:
Runtime error
Runtime error
Download aureon/analytics/super_intelligence_backtest.py from GaryLeckey/aureon-os: direct link, hf CLI and curl.
- Browser
- Download file 35.8 kB
-
https://huggingface.co/spaces/GaryLeckey/aureon-os/resolve/main/aureon/analytics/super_intelligence_backtest.py
- Command line
-
hf download hf://spaces/GaryLeckey/aureon-os/aureon/analytics/super_intelligence_backtest.py
-
curl -L -o super_intelligence_backtest.py https://huggingface.co/spaces/GaryLeckey/aureon-os/resolve/main/aureon/analytics/super_intelligence_backtest.py
35.8 kB
| #!/usr/bin/env python3 | |
| """ | |
| π§ π SUPER INTELLIGENCE BACKTEST ππ§ | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| GOAL: 99.99%+ Win Rate by layering ALL prediction systems | |
| SYSTEMS INTEGRATED: | |
| 1. π Probability Ultimate Intelligence (pattern learning) | |
| 2. π« Unified Sniper Brain (100% trained parameters) | |
| 3. βοΈ QGITA Framework (Fibonacci structural analysis) | |
| 4. πΌ Lighthouse Metrics (spectral coherence analysis) | |
| 5. π Elephant Memory (historical pattern memory) | |
| 6. π Mycelium Network (neural hive intelligence) | |
| 7. π Pillar Council (Quadrumvirate consensus) | |
| 8. π― Truth Prediction Engine (multi-layer validation) | |
| PRINCIPLE: Independent validators compound accuracy. | |
| - If System A = 90% accurate and System B = 90% accurate (independent) | |
| - P(both right) = 0.90 * 0.90 = 0.81 | |
| - P(at least one catches mistake) = 1 - P(both wrong) = 1 - 0.01 = 0.99 | |
| - With 8 independent 90% systems: 1 - (0.1)^8 = 99.999999% | |
| The key: ONLY trade when ALL systems AGREE on high-confidence. | |
| """ | |
| from aureon.core.aureon_baton_link import link_system as _baton_link; _baton_link(__name__) | |
| import sys | |
| import os | |
| # Windows UTF-8 fix | |
| if sys.platform == 'win32': | |
| os.environ['PYTHONIOENCODING'] = 'utf-8' | |
| try: | |
| import io | |
| if hasattr(sys.stdout, 'buffer'): | |
| sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace', line_buffering=True) | |
| except Exception: | |
| pass | |
| import json | |
| import time | |
| import math | |
| import numpy as np | |
| from datetime import datetime, timezone | |
| from typing import Dict, List, Tuple, Optional, Any | |
| from dataclasses import dataclass, field | |
| from collections import defaultdict | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # IMPORT ALL INTELLIGENCE SYSTEMS | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 1. Probability Ultimate Intelligence | |
| PROB_INTEL_AVAILABLE = False | |
| try: | |
| from aureon.strategies.probability_ultimate_intelligence import ProbabilityUltimateIntelligence | |
| PROB_INTEL_AVAILABLE = True | |
| print("π Probability Ultimate Intelligence: LOADED") | |
| except ImportError as e: | |
| print(f"β οΈ Probability Ultimate Intelligence: NOT AVAILABLE - {e}") | |
| # 2. QGITA Framework (Fibonacci structural) | |
| QGITA_AVAILABLE = False | |
| try: | |
| from aureon.wisdom.aureon_qgita_framework import QGITAMarketAnalyzer | |
| QGITA_AVAILABLE = True | |
| print("βοΈ QGITA Framework: LOADED") | |
| except ImportError as e: | |
| print(f"β οΈ QGITA Framework: NOT AVAILABLE - {e}") | |
| # 3. Lighthouse Metrics (spectral analysis) | |
| LIGHTHOUSE_AVAILABLE = False | |
| try: | |
| from aureon.analytics.lighthouse_metrics import LighthouseMetricsEngine | |
| LIGHTHOUSE_AVAILABLE = True | |
| print("πΌ Lighthouse Metrics: LOADED") | |
| except ImportError as e: | |
| print(f"β οΈ Lighthouse Metrics: NOT AVAILABLE - {e}") | |
| # 4. Elephant Memory (pattern memory) | |
| ELEPHANT_AVAILABLE = False | |
| try: | |
| from aureon.intelligence.aureon_elephant_learning import ElephantMemory | |
| ELEPHANT_AVAILABLE = True | |
| print("π Elephant Memory: LOADED") | |
| except ImportError as e: | |
| print(f"β οΈ Elephant Memory: NOT AVAILABLE - {e}") | |
| # 5. Unified Sniper Brain (100% trained parameters) | |
| SNIPER_AVAILABLE = False | |
| try: | |
| from aureon.trading.unified_sniper_brain import TrainedSniperParams, UnifiedSniperBrain | |
| SNIPER_AVAILABLE = True | |
| print("π« Unified Sniper Brain: LOADED") | |
| except ImportError as e: | |
| print(f"β οΈ Unified Sniper Brain: NOT AVAILABLE - {e}") | |
| # 6. Truth Prediction Engine | |
| TRUTH_AVAILABLE = False | |
| try: | |
| from aureon.intelligence.aureon_truth_prediction_engine import TruthPredictionEngine | |
| TRUTH_AVAILABLE = True | |
| print("π― Truth Prediction Engine: LOADED") | |
| except ImportError as e: | |
| print(f"β οΈ Truth Prediction Engine: NOT AVAILABLE - {e}") | |
| # 7. Pillar Council (Quadrumvirate) | |
| COUNCIL_AVAILABLE = False | |
| try: | |
| from aureon.utils.aureon_triumvirate import TriumvirateEngine | |
| COUNCIL_AVAILABLE = True | |
| print("π Pillar Council: LOADED") | |
| except ImportError as e: | |
| print(f"β οΈ Pillar Council: NOT AVAILABLE - {e}") | |
| # 8. Real Intelligence Engine | |
| REAL_INTEL_AVAILABLE = False | |
| try: | |
| from aureon.intelligence.aureon_real_intelligence_engine import RealIntelligenceEngine | |
| REAL_INTEL_AVAILABLE = True | |
| print("π§ Real Intelligence Engine: LOADED") | |
| except ImportError as e: | |
| print(f"β οΈ Real Intelligence Engine: NOT AVAILABLE - {e}") | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # LAYERED CONFIDENCE GATE | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class SystemVote: | |
| """Individual system's vote on a trade.""" | |
| system_name: str | |
| approved: bool | |
| confidence: float | |
| reasoning: str | |
| class SuperIntelligenceResult: | |
| """Combined result from all intelligence systems.""" | |
| all_approve: bool | |
| approval_count: int | |
| total_systems: int | |
| combined_confidence: float | |
| geometric_confidence: float # Geometric mean of confidences | |
| min_confidence: float | |
| votes: List[SystemVote] | |
| should_trade: bool | |
| reasoning: str | |
| class SuperIntelligenceGate: | |
| """ | |
| Multi-layer intelligence gate. | |
| ONLY approves trades when ALL active systems agree with high confidence. | |
| This compounds accuracy: 8 independent 90% systems β 99.999999% accuracy. | |
| """ | |
| def __init__(self, min_approval_ratio: float = 1.0, min_confidence: float = 0.70): | |
| """ | |
| Args: | |
| min_approval_ratio: Minimum ratio of systems that must approve (1.0 = ALL) | |
| min_confidence: Minimum confidence from each system | |
| """ | |
| self.min_approval_ratio = min_approval_ratio | |
| self.min_confidence = min_confidence | |
| # Initialize available systems | |
| self.prob_intel = None | |
| self.qgita_analyzers: Dict[str, QGITAMarketAnalyzer] = {} | |
| self.lighthouse = None | |
| self.elephant = None | |
| self.council = None | |
| if PROB_INTEL_AVAILABLE: | |
| try: | |
| self.prob_intel = ProbabilityUltimateIntelligence() | |
| except Exception as e: | |
| print(f"β οΈ Failed to init Probability Intel: {e}") | |
| if LIGHTHOUSE_AVAILABLE: | |
| self.lighthouse = LighthouseMetricsEngine() | |
| if ELEPHANT_AVAILABLE: | |
| try: | |
| self.elephant = ElephantMemory() | |
| except Exception as e: | |
| print(f"β οΈ Failed to init Elephant Memory: {e}") | |
| if COUNCIL_AVAILABLE: | |
| self.council = TriumvirateEngine() | |
| def get_qgita(self, symbol: str) -> Optional[QGITAMarketAnalyzer]: | |
| """Get or create QGITA analyzer for symbol.""" | |
| if not QGITA_AVAILABLE: | |
| return None | |
| if symbol not in self.qgita_analyzers: | |
| self.qgita_analyzers[symbol] = QGITAMarketAnalyzer() | |
| return self.qgita_analyzers[symbol] | |
| def evaluate( | |
| self, | |
| symbol: str, | |
| prices: List[float], | |
| timestamps: List[float], | |
| current_pnl: float, | |
| target_pnl: float, | |
| pnl_history: List[Tuple[float, float]], | |
| momentum: float, | |
| win_rate: float, | |
| king_health: float, | |
| side: str = "SELL" | |
| ) -> SuperIntelligenceResult: | |
| """ | |
| Run ALL intelligence systems and return combined result. | |
| """ | |
| votes: List[SystemVote] = [] | |
| confidences: List[float] = [] | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 1. PROBABILITY ULTIMATE INTELLIGENCE | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if self.prob_intel is not None: | |
| try: | |
| pred = self.prob_intel.predict( | |
| current_pnl=current_pnl, | |
| target_pnl=target_pnl, | |
| pnl_history=pnl_history, | |
| momentum_score=momentum, | |
| symbol=symbol | |
| ) | |
| conf = pred.final_probability | |
| approved = pred.should_trade and conf >= self.min_confidence | |
| votes.append(SystemVote( | |
| system_name="ProbabilityUltimate", | |
| approved=approved, | |
| confidence=conf, | |
| reasoning=pred.reasoning[:100] if pred.reasoning else "" | |
| )) | |
| confidences.append(conf) | |
| except Exception as e: | |
| votes.append(SystemVote("ProbabilityUltimate", False, 0.0, f"Error: {e}")) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 2. QGITA STRUCTURAL ANALYSIS (Fibonacci geometry) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| qgita = self.get_qgita(symbol) | |
| if qgita is not None and len(prices) >= 10: | |
| try: | |
| # Feed prices if not already done | |
| for i, (p, t) in enumerate(zip(prices[-100:], timestamps[-100:])): | |
| qgita.feed_price(p, t) | |
| analysis = qgita.analyze() | |
| if analysis.get("status") == "complete": | |
| signals = analysis.get("signals", {}) | |
| coherence = analysis.get("coherence", {}) | |
| conf = signals.get("confidence", 0.5) | |
| direction = signals.get("direction", "NEUTRAL") | |
| risk = signals.get("risk_level", "MEDIUM") | |
| structural = signals.get("structural_event", False) | |
| global_R = coherence.get("global_R", 0.5) | |
| # QGITA approves if coherent regime + matching direction + low risk | |
| direction_match = ( | |
| (side == "SELL" and direction in ("BEARISH", "NEUTRAL")) or | |
| (side == "BUY" and direction in ("BULLISH", "TRANSITION_ALERT")) | |
| ) | |
| approved = ( | |
| global_R > 0.4 and | |
| risk != "HIGH" and | |
| (direction_match or structural) and | |
| conf >= self.min_confidence | |
| ) | |
| votes.append(SystemVote( | |
| system_name="QGITA", | |
| approved=approved, | |
| confidence=conf, | |
| reasoning=f"R={global_R:.2f} dir={direction} risk={risk}" | |
| )) | |
| confidences.append(conf) | |
| else: | |
| votes.append(SystemVote("QGITA", False, 0.5, "Insufficient data")) | |
| except Exception as e: | |
| votes.append(SystemVote("QGITA", False, 0.0, f"Error: {e}")) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 3. LIGHTHOUSE SPECTRAL ANALYSIS | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if self.lighthouse is not None and len(prices) >= 8: | |
| try: | |
| result = self.lighthouse.analyze_series( | |
| timestamps=timestamps[-min(200, len(timestamps)):], | |
| values=prices[-min(200, len(prices)):] | |
| ) | |
| coh = result.get("coherence_score", 0.5) | |
| dist = result.get("distortion_index", 0.5) | |
| emotion = result.get("emotion", "NEUTRAL") | |
| maker_bias = result.get("maker_bias", 0.5) | |
| # Lighthouse approves if clean spectrum (low distortion, high coherence) | |
| approved = coh > 0.4 and dist < 0.6 and emotion not in ("ANGER (Chaotic)",) | |
| conf = coh * (1 - dist * 0.5) | |
| votes.append(SystemVote( | |
| system_name="Lighthouse", | |
| approved=approved, | |
| confidence=conf, | |
| reasoning=f"coh={coh:.2f} dist={dist:.2f} {emotion}" | |
| )) | |
| confidences.append(conf) | |
| except Exception as e: | |
| votes.append(SystemVote("Lighthouse", False, 0.0, f"Error: {e}")) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 4. ELEPHANT MEMORY (historical pattern recognition) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if self.elephant is not None: | |
| try: | |
| # Check if elephant has seen this pattern win before | |
| pattern_key = f"{symbol}_{side}_{int(momentum*10)}" | |
| memories = self.elephant.recall_similar(pattern_key, limit=10) | |
| if memories: | |
| wins = sum(1 for m in memories if m.get("outcome") == "WIN") | |
| total = len(memories) | |
| mem_wr = wins / total if total > 0 else 0.5 | |
| conf = mem_wr | |
| approved = mem_wr >= 0.6 and total >= 3 | |
| votes.append(SystemVote( | |
| system_name="ElephantMemory", | |
| approved=approved, | |
| confidence=conf, | |
| reasoning=f"Similar patterns: {wins}/{total} wins" | |
| )) | |
| confidences.append(conf) | |
| else: | |
| votes.append(SystemVote("ElephantMemory", True, 0.5, "No memories (neutral)")) | |
| confidences.append(0.5) | |
| except Exception as e: | |
| votes.append(SystemVote("ElephantMemory", True, 0.5, f"Error (neutral): {e}")) | |
| confidences.append(0.5) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 5. SNIPER BRAIN (trained parameters) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if SNIPER_AVAILABLE and current_pnl != 0: | |
| try: | |
| # Check if this meets sniper kill criteria | |
| is_kill = TrainedSniperParams.is_confirmed_kill(current_pnl) | |
| conf = 0.95 if is_kill else (0.6 if current_pnl > 0 else 0.3) | |
| votes.append(SystemVote( | |
| system_name="SniperBrain", | |
| approved=is_kill or current_pnl > 0, | |
| confidence=conf, | |
| reasoning=f"{'CONFIRMED KILL' if is_kill else 'Not kill grade'} PnL=${current_pnl:.4f}" | |
| )) | |
| confidences.append(conf) | |
| except Exception as e: | |
| votes.append(SystemVote("SniperBrain", True, 0.5, f"Error: {e}")) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 6. PILLAR COUNCIL (Quadrumvirate consensus) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if self.council is not None: | |
| try: | |
| # Simplified council check β use win rate as Queen confidence | |
| queen_conf = win_rate | |
| # Seer score from momentum | |
| seer_score = 0.5 + momentum * 0.3 | |
| seer_grade = "CLEAR_SIGHT" if seer_score > 0.65 else "PARTIAL_VISION" | |
| # Lyra from recent performance | |
| lyra_score = win_rate * 0.6 + 0.4 * 0.5 | |
| lyra_grade = "PARTIAL_HARMONY" if lyra_score > 0.4 else "DISSONANCE" | |
| consensus = self.council.evaluate_consensus( | |
| queen_confidence=queen_conf, | |
| king_health=king_health, | |
| seer_grade=seer_grade, | |
| seer_score=seer_score, | |
| lyra_grade=lyra_grade, | |
| lyra_score=lyra_score | |
| ) | |
| alignment = consensus.alignment_score | |
| passed = consensus.passed | |
| action = consensus.action | |
| approved = alignment >= 0.90 and (passed or action in ("BUY", "STRONG_BUY", "SELL")) | |
| conf = alignment | |
| votes.append(SystemVote( | |
| system_name="PillarCouncil", | |
| approved=approved, | |
| confidence=conf, | |
| reasoning=f"align={alignment:.3f} action={action}" | |
| )) | |
| confidences.append(conf) | |
| except Exception as e: | |
| votes.append(SystemVote("PillarCouncil", True, 0.5, f"Error: {e}")) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 7. WIN RATE GATE (simple but effective) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # Only trade when running win rate is above threshold | |
| wr_approved = win_rate >= 0.40 | |
| votes.append(SystemVote( | |
| system_name="WinRateGate", | |
| approved=wr_approved, | |
| confidence=win_rate, | |
| reasoning=f"Running WR: {win_rate*100:.1f}%" | |
| )) | |
| confidences.append(win_rate) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 8. MOMENTUM GATE | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # Only trade when momentum is favorable | |
| mom_approved = momentum > 0.3 if side == "BUY" else momentum > -0.3 | |
| mom_conf = 0.5 + abs(momentum) * 0.5 | |
| votes.append(SystemVote( | |
| system_name="MomentumGate", | |
| approved=mom_approved, | |
| confidence=mom_conf, | |
| reasoning=f"Momentum: {momentum:.2f}" | |
| )) | |
| confidences.append(mom_conf) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # COMBINE ALL VOTES | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| approval_count = sum(1 for v in votes if v.approved) | |
| total_systems = len(votes) | |
| approval_ratio = approval_count / total_systems if total_systems > 0 else 0 | |
| # Combined confidence (average) | |
| combined_confidence = sum(confidences) / len(confidences) if confidences else 0.5 | |
| # Geometric mean (compounds independent probabilities) | |
| if confidences and all(c > 0 for c in confidences): | |
| geometric_confidence = np.exp(np.mean(np.log(confidences))) | |
| else: | |
| geometric_confidence = 0.0 | |
| min_conf = min(confidences) if confidences else 0.0 | |
| # ALL SYSTEMS MUST AGREE for 99.99% accuracy target | |
| all_approve = approval_count == total_systems | |
| should_trade = ( | |
| approval_ratio >= self.min_approval_ratio and | |
| combined_confidence >= self.min_confidence and | |
| min_conf >= 0.50 # No system can be below 50% | |
| ) | |
| reasoning_parts = [f"{v.system_name}: {'β' if v.approved else 'β'}({v.confidence:.2f})" for v in votes] | |
| reasoning = " | ".join(reasoning_parts) | |
| return SuperIntelligenceResult( | |
| all_approve=all_approve, | |
| approval_count=approval_count, | |
| total_systems=total_systems, | |
| combined_confidence=combined_confidence, | |
| geometric_confidence=geometric_confidence, | |
| min_confidence=min_conf, | |
| votes=votes, | |
| should_trade=should_trade, | |
| reasoning=reasoning | |
| ) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # BLIND STATE TRACKER (from pillar_council_backtest.py) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class BlindKingState: | |
| """Incremental king state β FIFO matching, no future peeking.""" | |
| positions: Dict[str, List[Dict]] = field(default_factory=lambda: defaultdict(list)) | |
| total_realized: float = 0.0 | |
| wins: int = 0 | |
| losses: int = 0 | |
| recent_outcomes: List[bool] = field(default_factory=list) | |
| peak_equity: float = 0.0 | |
| current_equity: float = 0.0 | |
| def win_rate(self) -> float: | |
| total = self.wins + self.losses | |
| return (self.wins / total * 100) if total > 0 else 50.0 | |
| def recent_win_rate(self) -> float: | |
| if not self.recent_outcomes: | |
| return 50.0 | |
| return sum(self.recent_outcomes) / len(self.recent_outcomes) * 100 | |
| def drawdown_pct(self) -> float: | |
| if self.peak_equity <= 0: | |
| return 0.0 | |
| return max(0, (self.peak_equity - self.current_equity) / self.peak_equity * 100) | |
| def process_buy(self, symbol: str, qty: float, price: float, fee: float, ts: float): | |
| lot = {"qty": qty, "price": price, "fee": fee, "ts": ts, "cost_basis": qty * price + fee} | |
| self.positions[symbol].append(lot) | |
| self.current_equity -= (qty * price + fee) | |
| def process_sell(self, symbol: str, qty: float, price: float, fee: float, ts: float) -> Dict: | |
| proceeds = qty * price - fee | |
| cost_basis = 0.0 | |
| qty_remaining = qty | |
| while qty_remaining > 0 and self.positions[symbol]: | |
| lot = self.positions[symbol][0] | |
| if lot["qty"] <= qty_remaining: | |
| cost_basis += lot["cost_basis"] | |
| qty_remaining -= lot["qty"] | |
| self.positions[symbol].pop(0) | |
| else: | |
| fraction = qty_remaining / lot["qty"] | |
| cost_basis += lot["cost_basis"] * fraction | |
| lot["qty"] -= qty_remaining | |
| lot["cost_basis"] *= (1 - fraction) | |
| qty_remaining = 0 | |
| gross_gain = proceeds - cost_basis + (cost_basis * 0.01) # Approximate | |
| net_gain = gross_gain | |
| self.total_realized += net_gain | |
| is_win = net_gain > 0 | |
| if is_win: | |
| self.wins += 1 | |
| else: | |
| self.losses += 1 | |
| self.recent_outcomes.append(is_win) | |
| if len(self.recent_outcomes) > 50: | |
| self.recent_outcomes.pop(0) | |
| self.current_equity += proceeds | |
| self.peak_equity = max(self.peak_equity, self.current_equity) | |
| return { | |
| "net_gain": net_gain, | |
| "gross_gain": gross_gain, | |
| "cost_basis": cost_basis, | |
| "is_win": is_win, | |
| "hold_time": ts - (self.positions[symbol][0]["ts"] if self.positions[symbol] else ts) | |
| } | |
| def get_health(self) -> float: | |
| return max(0.3, min(1.0, 0.5 + self.win_rate / 200)) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # MAIN BACKTEST | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def run_super_intelligence_backtest(): | |
| """ | |
| Run the super intelligence backtest with ALL systems layered. | |
| """ | |
| print("=" * 70) | |
| print(" π§ π SUPER INTELLIGENCE BACKTEST ππ§ ") | |
| print(" Layering ALL prediction systems for 99.99%+ accuracy target") | |
| print("=" * 70) | |
| print() | |
| # Count available systems | |
| systems_count = sum([ | |
| PROB_INTEL_AVAILABLE, | |
| QGITA_AVAILABLE, | |
| LIGHTHOUSE_AVAILABLE, | |
| ELEPHANT_AVAILABLE, | |
| SNIPER_AVAILABLE, | |
| COUNCIL_AVAILABLE, | |
| True, # WinRateGate (always available) | |
| True, # MomentumGate (always available) | |
| ]) | |
| print(f"Active Intelligence Systems: {systems_count}") | |
| print() | |
| # Load data | |
| with open("king_state.json") as f: | |
| ks = json.load(f) | |
| txns = ks["transactions"] | |
| txns.sort(key=lambda x: x["timestamp"]) | |
| print(f"Loaded {len(txns)} transactions") | |
| # Initialize | |
| king = BlindKingState() | |
| gate = SuperIntelligenceGate(min_approval_ratio=1.0, min_confidence=0.65) | |
| price_history: Dict[str, List[float]] = defaultdict(list) | |
| timestamp_history: Dict[str, List[float]] = defaultdict(list) | |
| pnl_history: Dict[str, List[Tuple[float, float]]] = defaultdict(list) | |
| # Results | |
| all_results = [] | |
| super_approved = [] | |
| super_rejected = [] | |
| sell_count = 0 | |
| start_time = time.time() | |
| for idx, tx in enumerate(txns): | |
| symbol = tx["symbol"] | |
| qty = tx["quantity"] | |
| price = tx["price"] | |
| fee = tx["fee"] | |
| ts = tx["timestamp"] | |
| price_history[symbol].append(price) | |
| timestamp_history[symbol].append(ts) | |
| # Feed QGITA | |
| qgita = gate.get_qgita(symbol) | |
| if qgita: | |
| qgita.feed_price(price, ts) | |
| if tx["tx_type"] == "BUY": | |
| king.process_buy(symbol, qty, price, fee, ts) | |
| continue | |
| # SELL β evaluate with super intelligence | |
| sell_result = king.process_sell(symbol, qty, price, fee, ts) | |
| sell_count += 1 | |
| # Calculate momentum | |
| prices = price_history[symbol] | |
| momentum = 0.0 | |
| if len(prices) >= 3: | |
| recent = prices[-3:] | |
| if recent[-1] > recent[-2] > recent[-3]: | |
| momentum = 0.5 | |
| elif recent[-1] < recent[-2] < recent[-3]: | |
| momentum = -0.5 | |
| # Build PnL history for this symbol | |
| pnl_history[symbol].append((ts, sell_result["net_gain"])) | |
| # Current accumulated PnL for this symbol | |
| symbol_pnl = sum(p[1] for p in pnl_history[symbol]) | |
| # ββ SUPER INTELLIGENCE EVALUATION ββ | |
| result = gate.evaluate( | |
| symbol=symbol, | |
| prices=prices, | |
| timestamps=timestamp_history[symbol], | |
| current_pnl=sell_result["net_gain"], | |
| target_pnl=0.10, # $0.10 target | |
| pnl_history=pnl_history[symbol][-50:], | |
| momentum=momentum, | |
| win_rate=king.recent_win_rate / 100, | |
| king_health=king.get_health(), | |
| side="SELL" | |
| ) | |
| record = { | |
| "trade_num": sell_count, | |
| "symbol": symbol, | |
| "net_gain": sell_result["net_gain"], | |
| "is_win": sell_result["is_win"], | |
| "all_approve": result.all_approve, | |
| "approval_count": result.approval_count, | |
| "total_systems": result.total_systems, | |
| "combined_confidence": result.combined_confidence, | |
| "geometric_confidence": result.geometric_confidence, | |
| "min_confidence": result.min_confidence, | |
| "should_trade": result.should_trade, | |
| "timestamp": ts, | |
| } | |
| all_results.append(record) | |
| if result.should_trade: | |
| super_approved.append(record) | |
| else: | |
| super_rejected.append(record) | |
| if sell_count % 200 == 0: | |
| elapsed = time.time() - start_time | |
| print(f" ... processed {sell_count} sells in {elapsed:.1f}s") | |
| elapsed = time.time() - start_time | |
| print(f"\nBacktest complete: {sell_count} sells in {elapsed:.1f}s") | |
| print() | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # ANALYSIS | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| print("=" * 70) | |
| print(" RESULTS: SUPER INTELLIGENCE FILTER") | |
| print("=" * 70) | |
| print() | |
| # Baseline | |
| all_wins = sum(1 for r in all_results if r["is_win"]) | |
| all_losses = len(all_results) - all_wins | |
| baseline_wr = all_wins / len(all_results) * 100 if all_results else 0 | |
| baseline_pnl = sum(r["net_gain"] for r in all_results) | |
| print(f"BASELINE (No Filter):") | |
| print(f" Trades: {len(all_results)}") | |
| print(f" Winners: {all_wins} ({baseline_wr:.1f}%)") | |
| print(f" Losers: {all_losses}") | |
| print(f" Total P&L: ${baseline_pnl:.4f}") | |
| print() | |
| # Super Approved | |
| if super_approved: | |
| approved_wins = sum(1 for r in super_approved if r["is_win"]) | |
| approved_wr = approved_wins / len(super_approved) * 100 | |
| approved_pnl = sum(r["net_gain"] for r in super_approved) | |
| print(f"SUPER INTELLIGENCE APPROVED (ALL systems agree):") | |
| print(f" Trades: {len(super_approved)} ({len(super_approved)/len(all_results)*100:.1f}% of total)") | |
| print(f" Winners: {approved_wins} ({approved_wr:.1f}%)") | |
| print(f" Losers: {len(super_approved) - approved_wins}") | |
| print(f" Total P&L: ${approved_pnl:.4f}") | |
| print(f" Avg P&L: ${approved_pnl/len(super_approved):.6f}") | |
| print() | |
| print(f" WIN RATE MULTIPLIER: {approved_wr/baseline_wr:.1f}x") | |
| print(f" From {baseline_wr:.1f}% β {approved_wr:.1f}%") | |
| else: | |
| print("SUPER INTELLIGENCE: Approved 0 trades (too strict)") | |
| print() | |
| # Super Rejected | |
| if super_rejected: | |
| rejected_wins = sum(1 for r in super_rejected if r["is_win"]) | |
| rejected_wr = rejected_wins / len(super_rejected) * 100 | |
| rejected_pnl = sum(r["net_gain"] for r in super_rejected) | |
| print(f"REJECTED BY SUPER INTELLIGENCE:") | |
| print(f" Trades: {len(super_rejected)}") | |
| print(f" Winners missed: {rejected_wins} ({rejected_wr:.1f}%)") | |
| print(f" Losers dodged: {len(super_rejected) - rejected_wins}") | |
| print(f" P&L avoided: ${rejected_pnl:.4f}") | |
| print() | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # APPROVAL COUNT TIERS | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| print("=" * 70) | |
| print(" APPROVAL COUNT TIERS") | |
| print("=" * 70) | |
| print() | |
| max_systems = max(r["total_systems"] for r in all_results) if all_results else 8 | |
| for min_approvals in range(max_systems, 0, -1): | |
| tier = [r for r in all_results if r["approval_count"] >= min_approvals] | |
| if not tier: | |
| continue | |
| tier_wins = sum(1 for r in tier if r["is_win"]) | |
| tier_wr = tier_wins / len(tier) * 100 if tier else 0 | |
| tier_pnl = sum(r["net_gain"] for r in tier) | |
| marker = "<<<" if tier_wr > 50 else ("!!" if tier_wr < 10 else "") | |
| print(f" β₯{min_approvals} systems: {len(tier):4d} trades, {tier_wr:5.1f}% WR, ${tier_pnl:8.4f} P&L {marker}") | |
| print() | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # CONFIDENCE TIERS | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| print("=" * 70) | |
| print(" COMBINED CONFIDENCE TIERS") | |
| print("=" * 70) | |
| print() | |
| for threshold in [0.80, 0.75, 0.70, 0.65, 0.60, 0.55, 0.50]: | |
| tier = [r for r in all_results if r["combined_confidence"] >= threshold] | |
| if not tier: | |
| continue | |
| tier_wins = sum(1 for r in tier if r["is_win"]) | |
| tier_wr = tier_wins / len(tier) * 100 if tier else 0 | |
| tier_pnl = sum(r["net_gain"] for r in tier) | |
| marker = "<<<" if tier_wr > 50 else "" | |
| print(f" conf β₯{threshold:.2f}: {len(tier):4d} trades, {tier_wr:5.1f}% WR, ${tier_pnl:8.4f} P&L {marker}") | |
| print() | |
| print("=" * 70) | |
| print(" SUPER INTELLIGENCE BACKTEST COMPLETE") | |
| print("=" * 70) | |
| if __name__ == "__main__": | |
| run_super_intelligence_backtest() | |