File size: 12,811 Bytes
da06ca2
 
 
de7857d
da06ca2
 
 
de7857d
 
 
da06ca2
 
 
 
 
 
 
 
 
 
 
 
2b65b2d
da06ca2
de7857d
 
 
da06ca2
de7857d
 
 
 
 
 
da06ca2
 
de7857d
68dc3cf
de7857d
68dc3cf
de7857d
68dc3cf
 
de7857d
 
68dc3cf
 
de7857d
68dc3cf
 
de7857d
 
68dc3cf
 
de7857d
68dc3cf
 
de7857d
 
68dc3cf
 
de7857d
68dc3cf
 
de7857d
 
68dc3cf
 
de7857d
68dc3cf
 
de7857d
 
68dc3cf
 
da06ca2
de7857d
 
 
2b65b2d
 
 
de7857d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da06ca2
de7857d
 
 
 
 
 
 
 
 
 
 
da06ca2
 
de7857d
da06ca2
de7857d
 
 
 
 
 
 
 
 
da06ca2
 
de7857d
68dc3cf
 
de7857d
 
 
da06ca2
de7857d
 
68dc3cf
da06ca2
de7857d
 
 
 
 
 
 
 
 
da06ca2
de7857d
da06ca2
de7857d
 
 
 
 
68dc3cf
de7857d
da06ca2
68dc3cf
 
 
de7857d
68dc3cf
 
de7857d
 
 
 
 
68dc3cf
de7857d
68dc3cf
de7857d
68dc3cf
de7857d
 
68dc3cf
de7857d
68dc3cf
 
 
 
de7857d
68dc3cf
da06ca2
68dc3cf
de7857d
68dc3cf
 
 
de7857d
68dc3cf
de7857d
 
 
 
 
 
 
 
 
 
 
 
68dc3cf
da06ca2
de7857d
 
 
 
 
 
 
 
 
68dc3cf
de7857d
68dc3cf
 
de7857d
da06ca2
68dc3cf
de7857d
da06ca2
de7857d
 
da06ca2
 
68dc3cf
 
 
 
 
 
 
 
de7857d
 
da06ca2
 
 
bdcb77f
 
 
2b65b2d
bdcb77f
 
 
 
 
2b65b2d
 
 
 
 
bdcb77f
 
 
da06ca2
de7857d
da06ca2
de7857d
da06ca2
 
bdcb77f
 
da06ca2
68dc3cf
da06ca2
 
 
68dc3cf
 
da06ca2
68dc3cf
 
 
 
 
 
da06ca2
de7857d
da06ca2
 
2b65b2d
da06ca2
 
2b65b2d
 
 
 
 
 
 
 
 
 
 
 
 
 
de7857d
2b65b2d
da06ca2
de7857d
 
2b65b2d
 
da06ca2
de7857d
 
 
da06ca2
 
 
 
 
2b65b2d
 
 
68dc3cf
2b65b2d
da06ca2
 
 
68dc3cf
 
de7857d
 
da06ca2
68dc3cf
 
de7857d
 
da06ca2
68dc3cf
 
 
 
de7857d
68dc3cf
da06ca2
2b65b2d
 
 
 
 
 
 
 
de7857d
 
 
da06ca2
2b65b2d
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
# ============================================
# АВТО-УСТАНОВКА ПАКЕТОВ
# ============================================
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 24 v2.2 — ECOSYSTEM AGGREGATOR PRO (АВТО-ОТПРАВКА)
# ============================================
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

logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger("Space24_EcosystemAggregator")

# ================= КОНФИГУРАЦИЯ =================
SYMBOLS = ["XAU/USD", "ETH/USD", "SOL/USD"]

ECOSYSTEM_SPACES = {
    "space_19_sol": {
        "url": "https://tomirisai80-tomirisanal.hf.space",
        "weight": 0.25,
        "symbols": ["SOL/USD"],
        "description": "SOL Master",
        "cache_ttl": 300
    },
    "space_20_l2_defi": {
        "url": "https://tomirisai80-tomirisanal2.hf.space",
        "weight": 0.25,
        "symbols": ["ETH/USD"],
        "description": "L2 & DeFi Pulse",
        "cache_ttl": 300
    },
    "space_21_gold_macro": {
        "url": "https://tomirisai80-tomirisanal3.hf.space",
        "weight": 0.25,
        "symbols": ["XAU/USD"],
        "description": "Gold Macro & Flow",
        "cache_ttl": 600
    },
    "space_22_sentiment": {
        "url": "https://tomirisai80-tomirisanal4.hf.space",
        "weight": 0.15,
        "symbols": ["XAU/USD", "ETH/USD", "SOL/USD"],
        "description": "Cross-Asset Sentiment",
        "cache_ttl": 300
    },
    "space_23_anomaly": {
        "url": "https://tomirisai80-tomirisai5.hf.space",
        "weight": 0.10,
        "symbols": ["ETH/USD", "SOL/USD"],
        "description": "On-Chain Anomaly",
        "cache_ttl": 60
    }
}

HUB_URL = os.getenv("SPACE17_URL", "https://tomiris-ai-name5-5.hf.space")
SPACE31_URL = os.getenv("SPACE31_URL", "https://nuxotetotmailsvoboden-tomiris-perf.hf.space")

# Интервал авто-отправки
AUTO_SEND_INTERVAL = int(os.getenv("AUTO_SEND_INTERVAL", "300"))

# ================= HTTP КЛИЕНТ =================
http_client = httpx.AsyncClient(timeout=15.0)

# ================= ГЛОБАЛЬНЫЙ КЭШ И BREAKER =================
cache_store = {}
cache_times = {}
CIRCUIT_BREAKER = {}

def breaker_open(name: str) -> bool:
    info = CIRCUIT_BREAKER.get(name)
    if not info: return False
    if info["fails"] < 3: return False
    if time.time() - info["last_fail"] > 600:
        CIRCUIT_BREAKER[name] = {"fails": 0, "last_fail": 0}
        return False
    return True

def breaker_record(name: str, success: bool):
    info = CIRCUIT_BREAKER.get(name, {"fails": 0, "last_fail": 0})
    if success:
        info["fails"] = 0
    else:
        info["fails"] += 1
        info["last_fail"] = time.time()
    CIRCUIT_BREAKER[name] = info

# ================= ДИНАМИЧЕСКИЕ ВЕСА =================
async def fetch_dynamic_weights() -> Dict[str, float]:
    if SPACE31_URL:
        try:
            r = await http_client.get(f"{SPACE31_URL}/weights", timeout=8)
            if r.status_code == 200:
                data = r.json()
                if data:
                    return {k: v.get("weight", 0.1) for k, v in data.items() if k in ECOSYSTEM_SPACES}
        except:
            pass
    try:
        r = await http_client.get(f"{HUB_URL}/metrics", timeout=8)
        if r.status_code == 200:
            metrics = r.json()
            weights = {}
            for m in metrics:
                name = m.get("space_name", "")
                if name in ECOSYSTEM_SPACES:
                    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
    return {name: cfg["weight"] for name, cfg in ECOSYSTEM_SPACES.items()}

# ================= ОПРОС ОДНОГО SPACE =================
async def fetch_space_signal(name: str, config: Dict, symbol: str) -> Optional[Dict]:
    if breaker_open(name):
        return None
    cache_key = f"{name}_{symbol}"
    ttl = config.get("cache_ttl", 60)
    now = time.time()
    if cache_key in cache_store and (now - cache_times.get(cache_key, 0)) < ttl:
        return cache_store[cache_key]

    try:
        r = await http_client.get(f"{config['url']}/consilium?symbol={symbol}", timeout=10)
        if r.status_code == 200:
            data = r.json()
            breaker_record(name, True)
            cache_store[cache_key] = data
            cache_times[cache_key] = time.time()
            return data
    except:
        breaker_record(name, False)
    return None

# ================= АГРЕГАЦИЯ =================
async def aggregate_ecosystem(symbol: str) -> Dict[str, Any]:
    dynamic_weights = await fetch_dynamic_weights()
    tasks = []
    space_names = []
    for name, cfg in ECOSYSTEM_SPACES.items():
        if symbol in cfg["symbols"]:
            tasks.append(fetch_space_signal(name, cfg, symbol))
            space_names.append(name)

    results = await asyncio.gather(*tasks)

    signals = {}
    errors = []
    for name, res in zip(space_names, results):
        if res and isinstance(res, dict):
            signals[name] = res
        else:
            errors.append(name)

    total_weight = 0.0
    long_votes = 0.0
    short_votes = 0.0
    wait_votes = 0.0
    details = {}

    for name, data in signals.items():
        weight = dynamic_weights.get(name, ECOSYSTEM_SPACES[name]["weight"])
        sig = data.get("signal", {})
        direction = sig.get("direction", "WAIT")
        confidence = sig.get("confidence", 0)

        if direction == "LONG":
            long_votes += weight * confidence
        elif direction == "SHORT":
            short_votes += weight * confidence
        else:
            wait_votes += weight * confidence
        total_weight += weight
        details[name] = {
            "direction": direction,
            "confidence": confidence,
            "weight": weight,
            "status": "active",
            "description": ECOSYSTEM_SPACES[name]["description"]
        }

    for name in errors:
        weight = dynamic_weights.get(name, ECOSYSTEM_SPACES[name]["weight"])
        details[name] = {
            "direction": "ERROR",
            "confidence": 0,
            "weight": weight,
            "status": "error",
            "description": ECOSYSTEM_SPACES[name]["description"]
        }

    if total_weight == 0:
        return {
            "ecosystem_score": 50.0,
            "direction": "WAIT",
            "confidence": 0.0,
            "active_spaces": 0,
            "error_spaces": len(errors),
            "signals_detail": details,
            "votes": {"LONG": 0.0, "SHORT": 0.0, "WAIT": 0.0}
        }

    bias = (long_votes - short_votes) / total_weight
    ecosystem_score = 50.0 + bias * 50.0
    ecosystem_score = max(0, min(100, ecosystem_score))

    wait_ratio = wait_votes / total_weight
    if wait_ratio > 0.6:
        direction = "WAIT"
        confidence = wait_ratio
    elif long_votes > short_votes * 1.3:
        direction = "LONG"
        confidence = min(0.9, long_votes / total_weight)
    elif short_votes > long_votes * 1.3:
        direction = "SHORT"
        confidence = min(0.9, short_votes / total_weight)
    else:
        direction = "WAIT"
        confidence = max(long_votes, short_votes) / total_weight

    active_ratio = len(signals) / len(space_names) if space_names else 0
    confidence *= 0.5 + 0.5 * active_ratio

    return {
        "ecosystem_score": round(ecosystem_score, 2),
        "direction": direction,
        "confidence": round(confidence, 4),
        "active_spaces": len(signals),
        "error_spaces": len(errors),
        "signals_detail": details,
        "votes": {
            "LONG": round(long_votes, 4),
            "SHORT": round(short_votes, 4),
            "WAIT": round(wait_votes, 4)
        }
    }

# ================= ОТПРАВКА В HUB =================
async def send_signal_to_hub(symbol: str, direction: str, confidence: float):
    try:
        resp = await http_client.post(f"{HUB_URL}/signal", json={
            "space": "space_24_eco_agg",
            "symbol": symbol,
            "direction": direction,
            "confidence": confidence,
            "raw": json.dumps({"source": "space_24_eco_agg"})
        }, timeout=10)
        if resp.status_code == 200:
            logger.info(f"📤 {symbol}: {direction} conf={confidence:.3f} отправлен в Hub")
        else:
            logger.warning(f"Hub вернул {resp.status_code}")
    except Exception as e:
        logger.error(f"Ошибка отправки в Hub: {e}")

# ================= ГЛАВНЫЙ СИГНАЛ =================
async def get_ecosystem_aggregate(symbol: str = "XAU/USD") -> Dict[str, Any]:
    start = time.time()
    agg = await aggregate_ecosystem(symbol)
    latency = int((time.time() - start) * 1000)

    await send_signal_to_hub(symbol, agg['direction'], agg['confidence'])

    result = {
        "space": "space_24_ecosystem_aggregator",
        "timestamp": int(time.time()),
        "symbol": symbol,
        "signal": {
            "direction": agg['direction'],
            "confidence": agg['confidence']
        },
        "ecosystem_analysis": {
            "score": agg['ecosystem_score'],
            "active_spaces": agg['active_spaces'],
            "error_spaces": agg['error_spaces'],
            "votes": agg['votes'],
            "details": agg['signals_detail']
        },
        "latency_ms": latency
    }

    logger.info(f"🌐 Ecosystem Agg: {symbol} {agg['direction']} conf={agg['confidence']:.3f}")
    return result

# ================= АВТО-ОТПРАВКА =================
async def auto_send_loop():
    logger.info(f"🔄 Авто-отправка Ecosystem Aggregator запущена (интервал {AUTO_SEND_INTERVAL}с)")
    await asyncio.sleep(30)
    while True:
        try:
            for symbol in SYMBOLS:
                await get_ecosystem_aggregate(symbol)
                await asyncio.sleep(1)
            logger.info("✅ Ecosystem Aggregator авто-отправка завершена")
        except Exception as e:
            logger.error(f"Ошибка авто-отправки: {e}")
        await asyncio.sleep(AUTO_SEND_INTERVAL)

# ================= FASTAPI =================
app = FastAPI(title="Tomiris Space 24 v2.2 — Ecosystem Aggregator Pro (Auto-Hub)")

@app.on_event("startup")
async def startup():
    asyncio.create_task(auto_send_loop())
    logger.info("🚀 Space 24 v2.2 запущен с авто-отправкой в Hub")

@app.on_event("shutdown")
async def shutdown():
    await http_client.aclose()

@app.get("/health")
async def health():
    return {
        "status": "operational",
        "version": "2.2",
        "hub_url": HUB_URL,
        "auto_send_interval": AUTO_SEND_INTERVAL,
        "symbols": SYMBOLS,
        "ecosystem_spaces": list(ECOSYSTEM_SPACES.keys())
    }

@app.get("/consilium")
async def consilium(symbol: str = Query("XAU/USD")):
    if symbol not in SYMBOLS:
        return {"error": "Invalid symbol"}
    return await get_ecosystem_aggregate(symbol)

@app.get("/breakdown/{symbol}")
async def breakdown(symbol: str):
    agg = await aggregate_ecosystem(symbol)
    return {"symbol": symbol, "aggregate": agg, "spaces_detail": agg.get('signals_detail', {})}

@app.get("/all")
async def all_signals():
    results = {}
    for sym in SYMBOLS:
        results[sym] = await aggregate_ecosystem(sym)
    return results

@app.get("/send_now")
async def send_now():
    results = {}
    for symbol in SYMBOLS:
        analysis = await get_ecosystem_aggregate(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 24 v2.2 — ECOSYSTEM AGGREGATOR PRO (АВТО-ОТПРАВКА) ЗАПУЩЕН!")