Spaces:
Paused
Paused
File size: 15,255 Bytes
bde3cd6 38c207b bde3cd6 38c207b bde3cd6 38c207b 6170d9c 38c207b 395feb0 38c207b 395feb0 0c864d2 38c207b 395feb0 38c207b 395feb0 38c207b 395feb0 6170d9c 38c207b 7f9cdd7 bde3cd6 38c207b 395feb0 bde3cd6 38c207b 7f9cdd7 38c207b bde3cd6 38c207b 395feb0 6170d9c 38c207b bde3cd6 6170d9c bde3cd6 38c207b 395feb0 6170d9c 38c207b 6170d9c bde3cd6 38c207b bde3cd6 6170d9c 38c207b 395feb0 38c207b 395feb0 bde3cd6 38c207b 6170d9c 395feb0 38c207b 6170d9c 38c207b 6170d9c 38c207b 6170d9c 38c207b 6170d9c 38c207b 6170d9c 38c207b 6170d9c 38c207b 6170d9c 38c207b 6170d9c 38c207b 7f9cdd7 38c207b 7f9cdd7 38c207b 7f9cdd7 38c207b 7f9cdd7 38c207b 7f9cdd7 38c207b 6170d9c 7f9cdd7 bde3cd6 38c207b 6170d9c 38c207b 6170d9c 38c207b 6170d9c 38c207b 6170d9c 38c207b 6170d9c 38c207b 6170d9c bde3cd6 395feb0 6170d9c bde3cd6 395feb0 bde3cd6 6170d9c bde3cd6 7f9cdd7 395feb0 6170d9c bde3cd6 395feb0 38c207b 6170d9c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | # ============================================
# АВТО-УСТАНОВКА ПАКЕТОВ
# ============================================
import subprocess, sys, importlib
REQUIRED_PACKAGES = {
'numpy': 'numpy',
'httpx': 'httpx',
'fastapi': 'fastapi',
'uvicorn': 'uvicorn',
'requests': 'requests'
}
for module_name, pip_name in REQUIRED_PACKAGES.items():
try:
importlib.import_module(module_name)
except ImportError:
print(f"📦 Устанавливаю {pip_name}...")
subprocess.check_call([sys.executable, "-m", "pip", "install", pip_name])
print(f"✅ {pip_name} установлен!")
# ============================================
# 👑 TOMIRIS SPACE 30 v3.1 — META-ENSEMBLE AI (АВТО-ОТПРАВКА В HUB)
# ============================================
import os, time, json, logging, asyncio
from typing import Dict, Any, List, Optional
from datetime import datetime, timezone
from collections import deque
import numpy as np
import httpx
from fastapi import FastAPI, Query, HTTPException
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger("Space30_MetaEnsemble")
# ================= КОНФИГУРАЦИЯ =================
SYMBOLS = ["XAU/USD", "ETH/USD", "SOL/USD"]
SPACE31_URL = os.getenv("SPACE31_URL", "").rstrip("/") # Performance Engine
SPACE17_URL = os.getenv("SPACE17_URL", "https://tomiris-ai-name5-5.hf.space").rstrip("/")
# Интервал авто-отправки (секунды)
AUTO_SEND_INTERVAL = int(os.getenv("AUTO_SEND_INTERVAL", "300")) # 5 минут
# Группы аналитиков (без Space 18 – рекурсия исключена)
ANALYST_GROUPS = {
"technical": {
"space_1_xau_master": "https://nuxotetotmailsvoboden-tomiris.hf.space",
"space_2_eth_master": "https://nuxotetotmailsvoboden-tomiris-falcon-ai.hf.space",
"space_3_mtf": "https://nuxotetotmailsvoboden-tomiris-smollm-ai.hf.space",
"space_4_patterns": "https://nuxotetotmailsvoboden-tomiris-llama-ai.hf.space",
"space_6_arbitrage": "https://nuxotetotmailsvoboden-tomiris-agents.hf.space",
"space_16_quant": "https://tomiris-ai-name4-4.hf.space",
"space_19_sol": "https://tomirisai80-tomirisanal.hf.space",
"space_25_correlation": "https://tomirisg25-tomirisgold1.hf.space",
},
"macro": {
"space_7_fred": "https://nuxotetotnicksvoboden-name-1.hf.space",
"space_11_behavioral": "https://nuxotetotnicksvoboden-name5.hf.space",
"space_12_macro_agg": "https://nuxotetotnicksvoboden-name6.hf.space",
"space_21_gold_macro": "https://tomirisai80-tomirisanal3.hf.space",
"space_24_eco_agg": "https://tomirisai80-tomirisanal6.hf.space",
"space_27_seasonality": "https://tomirisg25-tomirisgold6-1.hf.space",
"space_29_macro_surprise": "https://tomirisg25-tomirisgold5.hf.space",
},
"onchain": {
"space_9_onchain": "https://nuxotetotnicksvoboden-name3.hf.space",
"space_20_l2_defi": "https://tomirisai80-tomirisanal2.hf.space",
},
"risk": {
"space_5_risk": "https://nuxotetotmailsvoboden-tomiris-mistral.hf.space",
"space_14_portfolio": "https://tomiris-ai-name2-2.hf.space",
"space_23_anomaly": "https://tomirisai80-tomirisai5.hf.space",
"space_26_options": "https://tomirisg25-tomirisgold2.hf.space",
"space_28_regime": "https://tomirisg25-tomirisgold4.hf.space",
},
"sentiment": {
"space_8_news": "https://nuxotetotnicksvoboden-name2.hf.space",
"space_10_whales": "https://nuxotetotnicksvoboden-name4.hf.space",
"space_22_sentiment": "https://tomirisai80-tomirisanal4.hf.space",
},
"ai": {
"space_13_qwen": "https://tomiris-ai-name1-1.hf.space",
},
"analysis": {
"space_15_backtest": "https://tomiris-ai-name3-3.hf.space",
},
}
# ================= HTTP КЛИЕНТ =================
http_client = httpx.AsyncClient(timeout=12.0)
# ================= ГЛОБАЛЬНЫЙ КЭШ =================
cache_store = {}
cache_times = {}
# История решений
HISTORY_FILE = "meta_ensemble_history.json"
if os.path.exists(HISTORY_FILE):
try:
with open(HISTORY_FILE) as f:
DECISION_HISTORY = deque(json.load(f), maxlen=500)
except:
DECISION_HISTORY = deque(maxlen=500)
else:
DECISION_HISTORY = deque(maxlen=500)
def save_history():
with open(HISTORY_FILE, 'w') as f:
json.dump(list(DECISION_HISTORY), f)
# ================= ПОЛУЧЕНИЕ ГОЛОСА =================
async def fetch_vote(name: str, url: str, symbol: str) -> Dict:
cache_key = f"{name}_{symbol}"
now = time.time()
if cache_key in cache_store and (now - cache_times.get(cache_key, 0)) < 30:
return cache_store[cache_key]
try:
r = await http_client.get(f"{url}/consilium?symbol={symbol}")
if r.status_code == 200:
data = r.json()
signal = data.get("signal", {})
direction = signal.get("direction", "WAIT")
confidence = signal.get("confidence", 0.0)
result = {
"direction": direction,
"confidence": confidence,
"active": True
}
cache_store[cache_key] = result
cache_times[cache_key] = now
return result
except:
pass
return {
"direction": "WAIT",
"confidence": 0.0,
"active": False
}
# ================= ДИНАМИЧЕСКИЕ ВЕСА =================
async def get_dynamic_weights() -> Dict[str, float]:
# Пробуем Space 31 (Performance Engine)
if SPACE31_URL:
try:
r = await http_client.get(f"{SPACE31_URL}/weights")
if r.status_code == 200:
data = r.json()
return {k: v["weight"] for k, v in data.items()}
except:
pass
# Fallback на метрики Space 17 (Data Hub)
try:
r = await http_client.get(f"{SPACE17_URL}/metrics")
if r.status_code == 200:
metrics = r.json()
weights = {}
for m in metrics:
name = m.get("space_name", "")
acc = m.get("accuracy", 0.5)
pf = m.get("profit_factor", 1.0)
weights[name] = acc * min(pf, 3.0) / 3.0
return weights
except:
pass
# Равные веса — fallback
all_names = [name for group in ANALYST_GROUPS.values() for name in group]
return {name: 1.0 for name in all_names}
# ================= АГРЕГАЦИЯ С ГРУППОВЫМ ГОЛОСОВАНИЕМ =================
async def aggregate_meta_ensemble(symbol: str) -> Dict:
dyn_weights = await get_dynamic_weights()
# Собираем все голоса параллельно
all_names = []
tasks = []
for group, members in ANALYST_GROUPS.items():
for name, url in members.items():
all_names.append(name)
tasks.append(fetch_vote(name, url, symbol))
results = await asyncio.gather(*tasks)
votes = dict(zip(all_names, results))
# Групповые голоса
group_votes = {}
for group, members in ANALYST_GROUPS.items():
long_score = 0.0
short_score = 0.0
wait_score = 0.0
total_weight = 0.0
active_count = 0
for name in members:
vote = votes.get(name, {"active": False})
if not vote["active"]:
continue
w = dyn_weights.get(name, 0.5)
conf = vote["confidence"]
if vote["direction"] == "LONG":
long_score += w * conf
elif vote["direction"] == "SHORT":
short_score += w * conf
else:
wait_score += w * conf
total_weight += w
active_count += 1
if total_weight > 0:
group_votes[group] = {
"LONG": round(long_score / total_weight, 4),
"SHORT": round(short_score / total_weight, 4),
"WAIT": round(wait_score / total_weight, 4),
"active": active_count
}
else:
group_votes[group] = {
"LONG": 0.0,
"SHORT": 0.0,
"WAIT": 0.0,
"active": 0
}
# Общее голосование с учётом группового консенсуса
long_total = sum(gv["LONG"] for gv in group_votes.values())
short_total = sum(gv["SHORT"] for gv in group_votes.values())
wait_total = sum(gv["WAIT"] for gv in group_votes.values())
total_weight = long_total + short_total + wait_total
if total_weight == 0:
return {
"direction": "WAIT",
"confidence": 0.0,
"active_spaces": 0,
"reason": "Нет голосов"
}
long_pct = long_total / total_weight
short_pct = short_total / total_weight
wait_pct = wait_total / total_weight
# WAIT доминирует → WAIT
if wait_pct > 0.6:
direction = "WAIT"
confidence = wait_pct
elif long_pct > short_pct * 1.2 and long_pct > 0.15:
direction = "LONG"
confidence = min(0.9, long_pct)
elif short_pct > long_pct * 1.2 and short_pct > 0.15:
direction = "SHORT"
confidence = min(0.9, short_pct)
else:
direction = "WAIT"
confidence = max(long_pct, short_pct)
# Консенсус и конфликт
consensus_strength = "STRONG" if confidence > 0.7 else "MODERATE" if confidence > 0.4 else "WEAK"
conflict_detected = (long_pct > 0.2 and short_pct > 0.2)
# Health Score
alive = sum(1 for v in votes.values() if v["active"])
total = len(all_names)
health_score = alive / total if total else 0
result = {
"direction": direction,
"confidence": round(confidence, 4),
"active_spaces": alive,
"total_spaces": total,
"health_score": round(health_score, 2),
"consensus_strength": consensus_strength,
"conflict_detected": conflict_detected,
"group_votes": group_votes,
"votes_summary": {
"LONG": round(long_pct, 4),
"SHORT": round(short_pct, 4),
"WAIT": round(wait_pct, 4)
},
}
# Сохраняем в историю
DECISION_HISTORY.append({
"timestamp": datetime.now(timezone.utc).isoformat(),
"symbol": symbol,
"direction": direction,
"confidence": confidence,
"health": health_score
})
save_history()
return result
# ================= ОТПРАВКА В HUB =================
async def send_signal_to_hub(symbol: str, direction: str, confidence: float):
"""Отправка агрегированного сигнала в Space 17 (Data Hub)."""
try:
resp = await http_client.post(f"{SPACE17_URL}/signal", json={
"space": "space_30_meta_ensemble",
"symbol": symbol,
"direction": direction,
"confidence": confidence,
"raw": json.dumps({"source": "space_30_meta_ensemble"})
}, timeout=10)
if resp.status_code == 200:
logger.info(f"📤 {symbol}: {direction} conf={confidence:.3f} отправлен в Hub")
else:
logger.warning(f"Hub вернул {resp.status_code}: {resp.text[:100]}")
except Exception as e:
logger.error(f"Ошибка отправки в Hub: {e}")
# ================= ГЛАВНЫЙ СИГНАЛ =================
async def get_meta_ensemble_signal(symbol: str = "XAU/USD") -> Dict:
start = time.time()
meta = await aggregate_meta_ensemble(symbol)
latency = int((time.time() - start) * 1000)
# Отправка агрегированного сигнала в Hub
await send_signal_to_hub(symbol, meta['direction'], meta['confidence'])
result = {
"space": "space_30_meta_ensemble",
"timestamp": int(time.time()),
"symbol": symbol,
"signal": {
"direction": meta["direction"],
"confidence": meta["confidence"]
},
"meta": meta,
"latency_ms": latency
}
logger.info(f"🧠 Meta-Ensemble {symbol}: {meta['direction']} conf={meta['confidence']:.3f} "
f"health={meta['health_score']:.0%} active={meta['active_spaces']}")
return result
# ================= АВТО-ОТПРАВКА ПО ТАЙМЕРУ =================
async def auto_send_loop():
"""🔥 Фоновая задача: каждые N секунд собирает голоса и шлёт агрегированный сигнал в Hub."""
logger.info(f"🔄 Авто-отправка Meta-Ensemble запущена (интервал {AUTO_SEND_INTERVAL}с)")
# Первый запуск через 30 секунд после старта
await asyncio.sleep(30)
while True:
try:
logger.info("🧠 Meta-Ensemble авто-анализ...")
for symbol in SYMBOLS:
await get_meta_ensemble_signal(symbol)
await asyncio.sleep(3) # Пауза между символами — много запросов
logger.info("✅ Meta-Ensemble авто-отправка завершена")
except Exception as e:
logger.error(f"Ошибка в авто-отправке: {e}")
await asyncio.sleep(AUTO_SEND_INTERVAL)
# ================= FASTAPI =================
app = FastAPI(title="Tomiris Meta-Ensemble v3.1 Ultimate Consensus (Auto-Hub)")
@app.on_event("startup")
async def startup():
# Запускаем фоновую авто-отправку
asyncio.create_task(auto_send_loop())
logger.info("🚀 Space 30 v3.1 запущен с авто-отправкой в Hub")
@app.on_event("shutdown")
async def shutdown():
await http_client.aclose()
@app.get("/health")
async def health():
return {
"status": "alive",
"version": "3.1",
"hub_url": SPACE17_URL,
"auto_send_interval": AUTO_SEND_INTERVAL,
"space31_connected": bool(SPACE31_URL)
}
@app.get("/consilium")
async def consilium(symbol: str = Query("XAU/USD")):
if symbol not in SYMBOLS:
raise HTTPException(status_code=400, detail="Invalid symbol")
return await get_meta_ensemble_signal(symbol)
@app.get("/send_now")
async def send_now():
"""Ручной триггер отправки всех символов."""
results = {}
for symbol in SYMBOLS:
analysis = await get_meta_ensemble_signal(symbol)
results[symbol] = analysis.get("signal", {}).get("direction", "WAIT")
return {"status": "sent", "results": results}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)
print("🚀 SPACE 30 v3.1 — META-ENSEMBLE AI (АВТО-ОТПРАВКА В HUB) ЗАПУЩЕН!") |