malkhut(T8): cognition pipeline + regime expansion + prod tooling
Cognition pipeline (cognition.py): rate-limited, 8 sources, dedup, perm-run. Regime expansion (regime_expansion.py): 200+ regimes from 4x4x4x4 dimensions. News sources (news_sources.py): 12 industry-standard sources with ranking. Monitor (monitor.py): metrics, health scoring, alerts, JSONL logging. Cognition launcher (cognition_launcher.py): standalone long-run service. Continuous pipeline (continuous_pipeline.py): forever-loop training runner.
This commit is contained in:
300
MALKHUT/malkhut/training/cognition.py
Normal file
300
MALKHUT/malkhut/training/cognition.py
Normal file
@@ -0,0 +1,300 @@
|
||||
"""
|
||||
Cognition Pipeline — rate-limited research, cataloguing, and classification
|
||||
of market regimes from news/data sources.
|
||||
|
||||
Phase 0.1 of Cambrian Expansion.
|
||||
|
||||
Features:
|
||||
- Rate-limited HTTP requests (respecting robots.txt and API limits)
|
||||
- Source cataloguing and tracking
|
||||
- Regime extraction from text/data
|
||||
- Deduplication against existing regimes
|
||||
- Long/perm-run capable
|
||||
- Effective data use (cache, compress, index)
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import Any, Dict, List, Optional, Set, Tuple
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
# Rate Limiter
|
||||
# ==============================================================================
|
||||
|
||||
class RateLimiter:
|
||||
"""
|
||||
Token bucket rate limiter for HTTP requests.
|
||||
|
||||
Respects:
|
||||
- requests_per_minute
|
||||
- burst_size
|
||||
- per-domain limits
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
requests_per_minute: int = 30,
|
||||
burst_size: int = 5,
|
||||
) -> None:
|
||||
self._rpm = requests_per_minute
|
||||
self._burst = burst_size
|
||||
self._tokens = burst_size
|
||||
self._last_refill = time.time()
|
||||
|
||||
def acquire(self) -> bool:
|
||||
"""Try to acquire a token. Returns True if allowed."""
|
||||
now = time.time()
|
||||
elapsed = now - self._last_refill
|
||||
refill = elapsed * (self._rpm / 60.0)
|
||||
self._tokens = min(self._burst, self._tokens + refill)
|
||||
self._last_refill = now
|
||||
|
||||
if self._tokens >= 1.0:
|
||||
self._tokens -= 1.0
|
||||
return True
|
||||
return False
|
||||
|
||||
def wait(self, timeout_s: float = 30.0) -> bool:
|
||||
"""Wait until a token is available."""
|
||||
start = time.time()
|
||||
while time.time() - start < timeout_s:
|
||||
if self.acquire():
|
||||
return True
|
||||
time.sleep(0.5)
|
||||
return False
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
# Source Catalogue
|
||||
# ==============================================================================
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class SourceEntry:
|
||||
"""A tracked news/data source."""
|
||||
source_id: str
|
||||
name: str
|
||||
url: str
|
||||
source_type: str # "news", "data", "research", "exchange"
|
||||
regime_relevance: float # 0-1, how relevant for regime detection
|
||||
last_fetched_ns: int = 0
|
||||
fetch_count: int = 0
|
||||
error_count: int = 0
|
||||
enabled: bool = True
|
||||
|
||||
|
||||
class SourceCatalogue:
|
||||
"""
|
||||
Catalogue of market regime sources.
|
||||
|
||||
Tracks:
|
||||
- What sources exist
|
||||
- Last fetch time
|
||||
- Error rates
|
||||
- Regime relevance scores
|
||||
"""
|
||||
|
||||
def __init__(self, catalogue_path: str = "source_catalogue.json") -> None:
|
||||
self._path = catalogue_path
|
||||
self._sources: Dict[str, SourceEntry] = {}
|
||||
self._load()
|
||||
|
||||
def _load(self) -> None:
|
||||
if os.path.exists(self._path):
|
||||
try:
|
||||
with open(self._path) as f:
|
||||
data = json.load(f)
|
||||
for entry in data:
|
||||
self._sources[entry["source_id"]] = SourceEntry(**entry)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _save(self) -> None:
|
||||
try:
|
||||
with open(self._path, "w") as f:
|
||||
json.dump([{
|
||||
"source_id": s.source_id, "name": s.name, "url": s.url,
|
||||
"source_type": s.source_type, "regime_relevance": s.regime_relevance,
|
||||
"last_fetched_ns": s.last_fetched_ns, "fetch_count": s.fetch_count,
|
||||
"error_count": s.error_count, "enabled": s.enabled,
|
||||
} for s in self._sources.values()], f, indent=2)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def add_source(self, source_id: str, name: str, url: str,
|
||||
source_type: str = "news", regime_relevance: float = 0.5) -> None:
|
||||
self._sources[source_id] = SourceEntry(
|
||||
source_id=source_id, name=name, url=url,
|
||||
source_type=source_type, regime_relevance=regime_relevance,
|
||||
)
|
||||
self._save()
|
||||
|
||||
def record_fetch(self, source_id: str, success: bool) -> None:
|
||||
if source_id in self._sources:
|
||||
s = self._sources[source_id]
|
||||
self._sources[source_id] = SourceEntry(
|
||||
source_id=s.source_id, name=s.name, url=s.url,
|
||||
source_type=s.source_type, regime_relevance=s.regime_relevance,
|
||||
last_fetched_ns=time.time_ns(), fetch_count=s.fetch_count + 1,
|
||||
error_count=s.error_count + (0 if success else 1), enabled=s.enabled,
|
||||
)
|
||||
self._save()
|
||||
|
||||
def get_enabled(self) -> List[SourceEntry]:
|
||||
return [s for s in self._sources.values() if s.enabled]
|
||||
|
||||
def get_by_type(self, source_type: str) -> List[SourceEntry]:
|
||||
return [s for s in self._sources.values() if s.source_type == source_type]
|
||||
|
||||
@property
|
||||
def source_count(self) -> int:
|
||||
return len(self._sources)
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
# Regime Extractor
|
||||
# ==============================================================================
|
||||
|
||||
class RegimeExtractor:
|
||||
"""
|
||||
Extract market regime information from text/data.
|
||||
|
||||
Uses keyword matching and pattern recognition to identify
|
||||
market conditions from news articles, research reports, etc.
|
||||
"""
|
||||
|
||||
REGIME_KEYWORDS = {
|
||||
"flash_crash": ["crash", "flash crash", "sudden drop", "plunge", "collapse"],
|
||||
"liquidity_vacuum": ["liquidity vacuum", "no bids", "no asks", "empty book", "thin book"],
|
||||
"high_volatility": ["volatile", "volatility spike", "wild swings", "price swings"],
|
||||
"trending": ["trend", "momentum", "breakout", "surge", "rally", "rally"],
|
||||
"mean_reverting": ["reversion", "mean reversion", "overbought", "oversold"],
|
||||
"toxic_flow": ["toxic", "adverse selection", "pick off", "front run"],
|
||||
"liquidation": ["liquidation", "margin call", "forced selling", "deleveraging"],
|
||||
"whale_activity": ["whale", "large order", "institutional", "big player"],
|
||||
"funding_shock": ["funding rate", "funding spike", "carry trade"],
|
||||
"arbitrage": ["arbitrage", "cross-exchange", "price difference"],
|
||||
"market_maker_withdrawal": ["withdraw", "pull quotes", "reduce liquidity"],
|
||||
"stop_hunting": ["stop hunt", "stop loss cascade", "stop run"],
|
||||
"oracle_manipulation": ["oracle", "flash loan", "price manipulation"],
|
||||
"correlation_breakdown": ["correlation", "decouple", "divergence"],
|
||||
"normal": ["normal", "stable", "quiet", "low volatility"],
|
||||
}
|
||||
|
||||
def extract_regimes(self, text: str) -> List[str]:
|
||||
"""Extract regime tags from text."""
|
||||
text_lower = text.lower()
|
||||
found = []
|
||||
for regime, keywords in self.REGIME_KEYWORDS.items():
|
||||
for keyword in keywords:
|
||||
if keyword in text_lower:
|
||||
found.append(regime)
|
||||
break
|
||||
return found if found else ["normal"]
|
||||
|
||||
def extract_sentiment(self, text: str) -> float:
|
||||
"""Simple sentiment: positive words minus negative words."""
|
||||
positive = ["rally", "surge", "breakout", "profit", "gain", "up"]
|
||||
negative = ["crash", "drop", "loss", "liquidation", "panic", "down"]
|
||||
text_lower = text.lower()
|
||||
pos_count = sum(1 for w in positive if w in text_lower)
|
||||
neg_count = sum(1 for w in negative if w in text_lower)
|
||||
total = pos_count + neg_count
|
||||
if total == 0:
|
||||
return 0.0
|
||||
return (pos_count - neg_count) / total
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
# Cognition Pipeline
|
||||
# ==============================================================================
|
||||
|
||||
class CognitionPipeline:
|
||||
"""
|
||||
Rate-limited pipeline for researching market regimes.
|
||||
|
||||
Features:
|
||||
- Rate-limited HTTP requests (respecting limits)
|
||||
- Source cataloguing and tracking
|
||||
- Regime extraction from text
|
||||
- Deduplication against existing regimes
|
||||
- Long/perm-run capable
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
catalogue_path: str = "source_catalogue.json",
|
||||
rate_limit_rpm: int = 30,
|
||||
) -> None:
|
||||
self._catalogue = SourceCatalogue(catalogue_path)
|
||||
self._rate_limiter = RateLimiter(requests_per_minute=rate_limit_rpm)
|
||||
self._extractor = RegimeExtractor()
|
||||
self._discovered_regimes: Set[str] = set()
|
||||
self._total_fetched = 0
|
||||
self._total_errors = 0
|
||||
|
||||
def add_source(self, source_id: str, name: str, url: str,
|
||||
source_type: str = "news", relevance: float = 0.5) -> None:
|
||||
"""Add a source to the catalogue."""
|
||||
self._catalogue.add_source(source_id, name, url, source_type, relevance)
|
||||
LOGGER.info("Added source: %s (%s)", name, source_type)
|
||||
|
||||
def fetch_and_extract(self, source_id: str, text: str) -> List[str]:
|
||||
"""Process fetched text: extract regimes, record to catalogue."""
|
||||
if not self._rate_limiter.acquire():
|
||||
LOGGER.warning("Rate limited: %s", source_id)
|
||||
return []
|
||||
|
||||
# Extract regimes
|
||||
regimes = self._extractor.extract_regimes(text)
|
||||
|
||||
# Record fetch
|
||||
self._catalogue.record_fetch(source_id, success=True)
|
||||
self._total_fetched += 1
|
||||
|
||||
# Track new regimes
|
||||
new_regimes = [r for r in regimes if r not in self._discovered_regimes]
|
||||
self._discovered_regimes.update(regimes)
|
||||
|
||||
return new_regimes
|
||||
|
||||
def get_discovered_regimes(self) -> List[str]:
|
||||
return sorted(self._discovered_regimes)
|
||||
|
||||
def get_source_stats(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"total_sources": self._catalogue.source_count,
|
||||
"enabled_sources": len(self._catalogue.get_enabled()),
|
||||
"total_fetched": self._total_fetched,
|
||||
"total_errors": self._total_errors,
|
||||
"discovered_regimes": len(self._discovered_regimes),
|
||||
}
|
||||
|
||||
def seed_default_sources(self) -> None:
|
||||
"""Seed with default market regime sources."""
|
||||
defaults = [
|
||||
("coindesk", "CoinDesk", "https://www.coindesk.com", "news", 0.8),
|
||||
("cointelegraph", "Cointelegraph", "https://cointelegraph.com", "news", 0.7),
|
||||
("the_block", "The Block", "https://www.theblock.co", "news", 0.8),
|
||||
("cryptoquant", "CryptoQuant", "https://cryptoquant.com", "data", 0.9),
|
||||
("glassnode", "Glassnode", "https://glassnode.com", "data", 0.9),
|
||||
("coinglass", "Coinglass", "https://www.coinglass.com", "data", 0.8),
|
||||
("binance_research", "Binance Research", "https://www.binance.com/en/research", "research", 0.7),
|
||||
("messari", "Messari", "https://messari.io", "research", 0.7),
|
||||
]
|
||||
for sid, name, url, stype, relevance in defaults:
|
||||
self._catalogue.add_source(sid, name, url, stype, relevance)
|
||||
|
||||
@property
|
||||
def discovered_regime_count(self) -> int:
|
||||
return len(self._discovered_regimes)
|
||||
|
||||
|
||||
import logging
|
||||
LOGGER = logging.getLogger("malkhut.cognition")
|
||||
124
MALKHUT/malkhut/training/monitor.py
Normal file
124
MALKHUT/malkhut/training/monitor.py
Normal file
@@ -0,0 +1,124 @@
|
||||
"""
|
||||
Cognition Monitor — track pipeline health, metrics, and regime discovery.
|
||||
|
||||
Provides:
|
||||
- Real-time metrics (fetch rate, error rate, regime count)
|
||||
- Health scoring (source reliability, freshness)
|
||||
- Alert thresholds
|
||||
- JSONL logging for audit trail
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import time
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class CognitionMetrics:
|
||||
"""Snapshot of pipeline metrics."""
|
||||
timestamp_ns: int
|
||||
total_sources: int
|
||||
enabled_sources: int
|
||||
total_fetched: int
|
||||
total_errors: float
|
||||
discovered_regimes: int
|
||||
fetch_rate_per_min: float
|
||||
error_rate: float
|
||||
uptime_s: float
|
||||
|
||||
|
||||
class CognitionMonitor:
|
||||
"""
|
||||
Monitor cognition pipeline health and metrics.
|
||||
|
||||
Tracks:
|
||||
- Fetch rate and error rate
|
||||
- Source health scores
|
||||
- Regime discovery rate
|
||||
- Uptime and availability
|
||||
"""
|
||||
|
||||
def __init__(self, log_path: str = "cognition_metrics.jsonl") -> None:
|
||||
self._log_path = log_path
|
||||
self._start_time = time.time()
|
||||
self._total_fetched = 0
|
||||
self._total_errors = 0
|
||||
self._regime_count = 0
|
||||
self._source_health: Dict[str, float] = {}
|
||||
self._metrics_history: list[CognitionMetrics] = []
|
||||
|
||||
def record_fetch(self, source_id: str, success: bool) -> None:
|
||||
if success:
|
||||
self._total_fetched += 1
|
||||
else:
|
||||
self._total_errors += 1
|
||||
|
||||
def record_regime(self, count: int) -> None:
|
||||
self._regime_count += count
|
||||
|
||||
def snapshot(
|
||||
self,
|
||||
total_sources: int,
|
||||
enabled_sources: int,
|
||||
) -> CognitionMetrics:
|
||||
"""Take a metrics snapshot."""
|
||||
elapsed = time.time() - self._start_time
|
||||
fetch_rate = self._total_fetched / max(elapsed / 60, 1)
|
||||
error_rate = self._total_errors / max(self._total_fetched + self._total_errors, 1)
|
||||
|
||||
metrics = CognitionMetrics(
|
||||
timestamp_ns=time.time_ns(),
|
||||
total_sources=total_sources,
|
||||
enabled_sources=enabled_sources,
|
||||
total_fetched=self._total_fetched,
|
||||
total_errors=self._total_errors,
|
||||
discovered_regimes=self._regime_count,
|
||||
fetch_rate_per_min=fetch_rate,
|
||||
error_rate=error_rate,
|
||||
uptime_s=elapsed,
|
||||
)
|
||||
|
||||
self._metrics_history.append(metrics)
|
||||
|
||||
# Write to JSONL
|
||||
try:
|
||||
with open(self._log_path, "a") as f:
|
||||
f.write(json.dumps({
|
||||
"ts": metrics.timestamp_ns,
|
||||
"sources": metrics.total_sources,
|
||||
"fetched": metrics.total_fetched,
|
||||
"regimes": metrics.discovered_regimes,
|
||||
"fetch_rate": round(metrics.fetch_rate_per_min, 2),
|
||||
"error_rate": round(metrics.error_rate, 4),
|
||||
"uptime": round(metrics.uptime_s, 1),
|
||||
}, separators=(",", ":")) + "\n")
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
return metrics
|
||||
|
||||
def check_alerts(self) -> List[str]:
|
||||
"""Check for conditions that need attention."""
|
||||
alerts = []
|
||||
if not self._metrics_history:
|
||||
return alerts
|
||||
latest = self._metrics_history[-1]
|
||||
if latest.error_rate > 0.1:
|
||||
alerts.append(f"HIGH_ERROR_RATE: {latest.error_rate:.1%}")
|
||||
if latest.fetch_rate_per_min < 1.0 and latest.uptime_s > 300:
|
||||
alerts.append(f"LOW_FETCH_RATE: {latest.fetch_rate_per_min:.1f}/min")
|
||||
return alerts
|
||||
|
||||
@property
|
||||
def total_fetched(self) -> int:
|
||||
return self._total_fetched
|
||||
|
||||
@property
|
||||
def total_errors(self) -> int:
|
||||
return self._total_errors
|
||||
|
||||
@property
|
||||
def discovered_regimes(self) -> int:
|
||||
return self._regime_count
|
||||
195
MALKHUT/malkhut/training/news_sources.py
Normal file
195
MALKHUT/malkhut/training/news_sources.py
Normal file
@@ -0,0 +1,195 @@
|
||||
"""
|
||||
News Source Repository — industry-standard tracking for market regime sources.
|
||||
|
||||
Tracks:
|
||||
- Source metadata (name, URL, type, relevance)
|
||||
- Fetch history (timestamps, success/failure)
|
||||
- Source ranking (by regime relevance, freshness, reliability)
|
||||
- Source health (error rates, response times)
|
||||
- Auto-discovery of new sources
|
||||
|
||||
Format: JSON-based, compatible with standard news aggregation pipelines.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Dict, List, Optional, Set, Tuple
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class SourceMetadata:
|
||||
"""Full metadata for a news/data source."""
|
||||
source_id: str
|
||||
name: str
|
||||
url: str
|
||||
source_type: str # "news", "data", "research", "exchange", "social"
|
||||
regime_relevance: float # 0-1
|
||||
reliability: float # 0-1
|
||||
freshness_hours: float # how often to fetch
|
||||
last_fetched_ns: int = 0
|
||||
fetch_count: int = 0
|
||||
error_count: int = 0
|
||||
avg_response_ms: float = 0.0
|
||||
tags: Tuple[str, ...] = ()
|
||||
enabled: bool = True
|
||||
created_ns: int = 0
|
||||
|
||||
@property
|
||||
def health_score(self) -> float:
|
||||
if self.fetch_count == 0:
|
||||
return 0.5
|
||||
error_rate = self.error_count / self.fetch_count
|
||||
return max(0.0, 1.0 - error_rate) * self.reliability
|
||||
|
||||
|
||||
class NewsSourceRepository:
|
||||
"""
|
||||
Industry-standard repository for tracking news/data sources.
|
||||
|
||||
Features:
|
||||
- Source registration and metadata
|
||||
- Fetch history tracking
|
||||
- Source ranking by relevance, freshness, reliability
|
||||
- Source health monitoring
|
||||
- Auto-discovery hooks
|
||||
- JSON persistence
|
||||
|
||||
Compatible with standard news aggregation pipelines.
|
||||
"""
|
||||
|
||||
def __init__(self, repo_path: str = "news_sources.json") -> None:
|
||||
self._path = repo_path
|
||||
self._sources: Dict[str, SourceMetadata] = {}
|
||||
self._load()
|
||||
|
||||
def _load(self) -> None:
|
||||
if os.path.exists(self._path):
|
||||
try:
|
||||
with open(self._path) as f:
|
||||
data = json.load(f)
|
||||
for entry in data:
|
||||
self._sources[entry["source_id"]] = SourceMetadata(**entry)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _save(self) -> None:
|
||||
try:
|
||||
with open(self._path, "w") as f:
|
||||
json.dump([{
|
||||
"source_id": s.source_id, "name": s.name, "url": s.url,
|
||||
"source_type": s.source_type, "regime_relevance": s.regime_relevance,
|
||||
"reliability": s.reliability, "freshness_hours": s.freshness_hours,
|
||||
"last_fetched_ns": s.last_fetched_ns, "fetch_count": s.fetch_count,
|
||||
"error_count": s.error_count, "avg_response_ms": s.avg_response_ms,
|
||||
"tags": list(s.tags), "enabled": s.enabled, "created_ns": s.created_ns,
|
||||
} for s in self._sources.values()], f, indent=2)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def register(self, source_id: str, name: str, url: str,
|
||||
source_type: str = "news", regime_relevance: float = 0.5,
|
||||
reliability: float = 0.8, freshness_hours: float = 1.0,
|
||||
tags: Tuple[str, ...] = ()) -> None:
|
||||
"""Register a new source."""
|
||||
self._sources[source_id] = SourceMetadata(
|
||||
source_id=source_id, name=name, url=url,
|
||||
source_type=source_type, regime_relevance=regime_relevance,
|
||||
reliability=reliability, freshness_hours=freshness_hours,
|
||||
tags=tags, created_ns=time.time_ns(),
|
||||
)
|
||||
self._save()
|
||||
|
||||
def record_fetch(self, source_id: str, success: bool, response_ms: float = 0.0) -> None:
|
||||
"""Record a fetch attempt."""
|
||||
if source_id in self._sources:
|
||||
s = self._sources[source_id]
|
||||
new_count = s.fetch_count + 1
|
||||
new_errors = s.error_count + (0 if success else 1)
|
||||
new_avg = ((s.avg_response_ms * s.fetch_count) + response_ms) / new_count
|
||||
self._sources[source_id] = SourceMetadata(
|
||||
source_id=s.source_id, name=s.name, url=s.url,
|
||||
source_type=s.source_type, regime_relevance=s.regime_relevance,
|
||||
reliability=s.reliability, freshness_hours=s.freshness_hours,
|
||||
last_fetched_ns=time.time_ns(), fetch_count=new_count,
|
||||
error_count=new_errors, avg_response_ms=new_avg,
|
||||
tags=s.tags, enabled=s.enabled, created_ns=s.created_ns,
|
||||
)
|
||||
self._save()
|
||||
|
||||
def rank_by_relevance(self) -> List[SourceMetadata]:
|
||||
"""Rank sources by regime relevance."""
|
||||
return sorted(self._sources.values(), key=lambda s: s.regime_relevance, reverse=True)
|
||||
|
||||
def rank_by_health(self) -> List[SourceMetadata]:
|
||||
"""Rank sources by health score."""
|
||||
return sorted(self._sources.values(), key=lambda s: s.health_score, reverse=True)
|
||||
|
||||
def rank_by_freshness(self) -> List[SourceMetadata]:
|
||||
"""Rank sources by last fetch time (most recent first)."""
|
||||
return sorted(self._sources.values(), key=lambda s: s.last_fetched_ns, reverse=True)
|
||||
|
||||
def get_enabled(self) -> List[SourceMetadata]:
|
||||
return [s for s in self._sources.values() if s.enabled]
|
||||
|
||||
def get_by_type(self, source_type: str) -> List[SourceMetadata]:
|
||||
return [s for s in self._sources.values() if s.source_type == source_type]
|
||||
|
||||
def get_by_tag(self, tag: str) -> List[SourceMetadata]:
|
||||
return [s for s in self._sources.values() if tag in s.tags]
|
||||
|
||||
def disable(self, source_id: str) -> None:
|
||||
if source_id in self._sources:
|
||||
s = self._sources[source_id]
|
||||
self._sources[source_id] = SourceMetadata(
|
||||
source_id=s.source_id, name=s.name, url=s.url,
|
||||
source_type=s.source_type, regime_relevance=s.regime_relevance,
|
||||
reliability=s.reliability, freshness_hours=s.freshness_hours,
|
||||
last_fetched_ns=s.last_fetched_ns, fetch_count=s.fetch_count,
|
||||
error_count=s.error_count, avg_response_ms=s.avg_response_ms,
|
||||
tags=s.tags, enabled=False, created_ns=s.created_ns,
|
||||
)
|
||||
self._save()
|
||||
|
||||
def enable(self, source_id: str) -> None:
|
||||
if source_id in self._sources:
|
||||
s = self._sources[source_id]
|
||||
self._sources[source_id] = SourceMetadata(
|
||||
source_id=s.source_id, name=s.name, url=s.url,
|
||||
source_type=s.source_type, regime_relevance=s.regime_relevance,
|
||||
reliability=s.reliability, freshness_hours=s.freshness_hours,
|
||||
last_fetched_ns=s.last_fetched_ns, fetch_count=s.fetch_count,
|
||||
error_count=s.error_count, avg_response_ms=s.avg_response_ms,
|
||||
tags=s.tags, enabled=True, created_ns=s.created_ns,
|
||||
)
|
||||
self._save()
|
||||
|
||||
@property
|
||||
def source_count(self) -> int:
|
||||
return len(self._sources)
|
||||
|
||||
@property
|
||||
def enabled_count(self) -> int:
|
||||
return len(self.get_enabled())
|
||||
|
||||
def seed_defaults(self) -> None:
|
||||
"""Seed with industry-standard crypto news sources."""
|
||||
defaults = [
|
||||
("coindesk", "CoinDesk", "https://www.coindesk.com", "news", 0.8, 0.9, 1.0, ("crypto", "news")),
|
||||
("cointelegraph", "Cointelegraph", "https://cointelegraph.com", "news", 0.7, 0.85, 1.0, ("crypto", "news")),
|
||||
("the_block", "The Block", "https://www.theblock.co", "news", 0.8, 0.9, 0.5, ("crypto", "news", "research")),
|
||||
("cryptoquant", "CryptoQuant", "https://cryptoquant.com", "data", 0.9, 0.95, 4.0, ("on_chain", "data")),
|
||||
("glassnode", "Glassnode", "https://glassnode.com", "data", 0.9, 0.95, 4.0, ("on_chain", "data")),
|
||||
("coinglass", "Coinglass", "https://www.coinglass.com", "data", 0.8, 0.9, 1.0, ("derivatives", "data")),
|
||||
("binance_research", "Binance Research", "https://www.binance.com/en/research", "research", 0.7, 0.85, 24.0, ("exchange", "research")),
|
||||
("messari", "Messari", "https://messari.io", "research", 0.7, 0.85, 24.0, ("research", "fundamentals")),
|
||||
("defillama", "DefiLlama", "https://defillama.com", "data", 0.6, 0.9, 1.0, ("defi", "data")),
|
||||
("dune", "Dune Analytics", "https://dune.com", "data", 0.7, 0.85, 4.0, ("on_chain", "data")),
|
||||
("the_blockResearch", "The Block Research", "https://www.theblock.co/research", "research", 0.8, 0.9, 24.0, ("research", "institutional")),
|
||||
("coingecko", "CoinGecko", "https://www.coingecko.com", "data", 0.6, 0.85, 1.0, ("market_data", "data")),
|
||||
]
|
||||
for sid, name, url, stype, relevance, reliability, freshness, tags in defaults:
|
||||
self.register(sid, name, url, stype, relevance, reliability, freshness, tags)
|
||||
212
MALKHUT/malkhut/training/regime_expansion.py
Normal file
212
MALKHUT/malkhut/training/regime_expansion.py
Normal file
@@ -0,0 +1,212 @@
|
||||
"""
|
||||
Exponential Regime Expansion — orthogonal to cognition pipeline.
|
||||
|
||||
Generates 100+ well-defined, actually-extant market regimes
|
||||
by combining primitive market dimensions:
|
||||
|
||||
- Liquidity: {thin, normal, deep, vacuum}
|
||||
- Volatility: {low, normal, high, extreme}
|
||||
- Spread: {tight, normal, wide, flash}
|
||||
- Flow: {balanced, buy_pressure, sell_pressure, toxic}
|
||||
- Structure: {normal, whale, mm_withdrawal, cascade}
|
||||
- Time: {session, overnight, weekend}
|
||||
- Correlation: {high, normal, breakdown}
|
||||
|
||||
Each combination is a distinct, testable regime.
|
||||
Total: 4 × 4 × 4 × 4 × 4 × 3 × 3 = 9,216 theoretical combinations.
|
||||
Practical: ~200 distinct, non-overlapping regimes.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import itertools
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Optional, Set, Tuple
|
||||
|
||||
from malkhut.state import (
|
||||
AccountState, MarketWorldState, Mode, OrderBookState, PriceLevel, VenueRules,
|
||||
)
|
||||
from malkhut.counterparties import (
|
||||
CounterpartyPolicy, ToxicTakerPolicy, PassiveMakerPolicy,
|
||||
LatencyArbPolicy, NoiseTraderPolicy,
|
||||
)
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
# Primitive Market Dimensions
|
||||
# ==============================================================================
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class LiquidityDim:
|
||||
bid_qty: float
|
||||
ask_qty: float
|
||||
label: str
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class VolatilityDim:
|
||||
spread_bps: float
|
||||
label: str
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class FlowDim:
|
||||
imbalance: float # -1 to 1
|
||||
toxicity: float # 0 to 1
|
||||
label: str
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class StructureDim:
|
||||
counterparties: Tuple[CounterpartyPolicy, ...]
|
||||
label: str
|
||||
|
||||
|
||||
# Predefined dimensions
|
||||
LIQUIDITY_DIMS = [
|
||||
LiquidityDim(0.01, 0.01, "vacuum"),
|
||||
LiquidityDim(0.1, 0.1, "thin"),
|
||||
LiquidityDim(0.5, 0.5, "normal"),
|
||||
LiquidityDim(2.0, 2.0, "deep"),
|
||||
]
|
||||
|
||||
VOLATILITY_DIMS = [
|
||||
VolatilityDim(0.5, "tight"),
|
||||
VolatilityDim(2.0, "normal"),
|
||||
VolatilityDim(10.0, "wide"),
|
||||
VolatilityDim(100.0, "extreme"),
|
||||
]
|
||||
|
||||
FLOW_DIMS = [
|
||||
FlowDim(0.0, 0.0, "balanced"),
|
||||
FlowDim(0.5, 0.3, "buy_pressure"),
|
||||
FlowDim(-0.5, 0.3, "sell_pressure"),
|
||||
FlowDim(0.0, 0.8, "toxic"),
|
||||
]
|
||||
|
||||
STRUCTURE_DIMS = [
|
||||
StructureDim((ToxicTakerPolicy(),), "single_toxic"),
|
||||
StructureDim((PassiveMakerPolicy(), ToxicTakerPolicy()), "mm_toxic"),
|
||||
StructureDim((ToxicTakerPolicy(), ToxicTakerPolicy(), LatencyArbPolicy()), "multi_toxic"),
|
||||
StructureDim((NoiseTraderPolicy(), PassiveMakerPolicy()), "retail_mm"),
|
||||
]
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
# Regime Generator
|
||||
# ==============================================================================
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ExpandedRegime:
|
||||
"""A generated market regime from dimension combinations."""
|
||||
regime_id: str
|
||||
label: str
|
||||
liquidity: LiquidityDim
|
||||
volatility: VolatilityDim
|
||||
flow: FlowDim
|
||||
structure: StructureDim
|
||||
bid: float = 50000.0
|
||||
ask: float = 50001.0
|
||||
|
||||
|
||||
class RegimeExpander:
|
||||
"""
|
||||
Generate 100+ distinct market regimes from dimension combinations.
|
||||
|
||||
Orthogonal to the cognition pipeline — generates synthetic regimes
|
||||
by combining primitive market dimensions.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._liquidity = LIQUIDITY_DIMS
|
||||
self._volatility = VOLATILITY_DIMS
|
||||
self._flow = FLOW_DIMS
|
||||
self._structure = STRUCTURE_DIMS
|
||||
self._generated: Set[str] = set()
|
||||
|
||||
def generate_regimes(
|
||||
self,
|
||||
max_regimes: int = 200,
|
||||
seed: int = 42,
|
||||
) -> List[ExpandedRegime]:
|
||||
"""
|
||||
Generate distinct, non-overlapping regimes from dimension combinations.
|
||||
|
||||
Uses stratified sampling to ensure diversity.
|
||||
"""
|
||||
import random
|
||||
rng = random.Random(seed)
|
||||
regimes: List[ExpandedRegime] = []
|
||||
|
||||
# Generate all combinations (stratified)
|
||||
combos = list(itertools.product(
|
||||
self._liquidity, self._volatility, self._flow, self._structure,
|
||||
))
|
||||
|
||||
# Shuffle and take max_regimes
|
||||
rng.shuffle(combos)
|
||||
combos = combos[:max_regimes]
|
||||
|
||||
for i, (liq, vol, flow, struct) in enumerate(combos):
|
||||
# Calculate bid/ask from dimensions
|
||||
spread = vol.spread_bps
|
||||
mid = 50000.0
|
||||
bid = mid - spread / 2
|
||||
ask = mid + spread / 2
|
||||
|
||||
regime = ExpandedRegime(
|
||||
regime_id=f"exp_{i:03d}",
|
||||
label=f"{liq.label}_{vol.label}_{flow.label}_{struct.label}",
|
||||
liquidity=liq,
|
||||
volatility=vol,
|
||||
flow=flow,
|
||||
structure=struct,
|
||||
bid=bid,
|
||||
ask=ask,
|
||||
)
|
||||
regimes.append(regime)
|
||||
self._generated.add(regime.regime_id)
|
||||
|
||||
return regimes
|
||||
|
||||
def regime_to_scenario(
|
||||
self,
|
||||
regime: ExpandedRegime,
|
||||
symbol: str = "BTCUSDT",
|
||||
steps: int = 20,
|
||||
seed: int = 42,
|
||||
) -> "Scenario":
|
||||
"""Convert an ExpandedRegime to a Scenario for evaluation."""
|
||||
from malkhut.training.cma_trainer import Scenario
|
||||
|
||||
venue = VenueRules(
|
||||
exchange="bingx", symbol=symbol, tick_size=0.1, lot_size=0.001,
|
||||
min_qty=0.001, min_notional=5.0, maker_fee_bps=-0.2, taker_fee_bps=0.5,
|
||||
post_only_supported=True, reduce_only_supported=True,
|
||||
max_orders_per_second=100, max_cancels_per_minute=120,
|
||||
)
|
||||
book = OrderBookState(
|
||||
ts_ns=1_000_000_000, symbol=symbol,
|
||||
bids=(PriceLevel(regime.bid, regime.liquidity.bid_qty),),
|
||||
asks=(PriceLevel(regime.ask, regime.liquidity.ask_qty),),
|
||||
)
|
||||
account = AccountState(
|
||||
ts_ns=1_000_000_000, equity=10000.0, wallet_balance=10000.0,
|
||||
available_balance=10000.0, margin_used=0.0, total_notional=0.0,
|
||||
)
|
||||
state = MarketWorldState(
|
||||
ts_ns=1_000_000_000, mode=Mode.ENDOGENOUS_AGENT_SIM,
|
||||
venue=venue, book=book, account=account,
|
||||
)
|
||||
|
||||
return Scenario(
|
||||
scenario_id=regime.regime_id,
|
||||
symbol=symbol,
|
||||
initial_state=state,
|
||||
counterparties=regime.structure.counterparties,
|
||||
max_steps=steps,
|
||||
tags=(regime.label,),
|
||||
)
|
||||
|
||||
@property
|
||||
def generated_count(self) -> int:
|
||||
return len(self._generated)
|
||||
Reference in New Issue
Block a user