Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,7 +21,7 @@ for module_name, pip_name in REQUIRED_PACKAGES.items():
|
|
| 21 |
print(f"✅ {pip_name} установлен!")
|
| 22 |
|
| 23 |
# ============================================
|
| 24 |
-
# 👑 TOMIRIS SPACE 21 v3.
|
| 25 |
# ============================================
|
| 26 |
import os, time, json, logging, asyncio
|
| 27 |
from typing import Dict, Any, List, Optional
|
|
@@ -42,8 +42,12 @@ TWELVE_DATA_KEY = os.getenv("TWELVE_DATA_KEY", "f33e660d8c1945d19e3c4ded72a2875e
|
|
| 42 |
FRED_KEY = os.getenv("FRED_KEY", "faa11c8e2e4beee08c5b966e8b63a513")
|
| 43 |
NEWSAPI_KEY = os.getenv("NEWSAPI_KEY", "948c7816beea47baa23b054592472d0e")
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
GOLD_ETFS = ["GLD", "IAU", "GDX"]
|
| 46 |
CACHE_TTL = {"fred": 3600, "etf_aum": 600, "cot": 86400, "gpr": 900}
|
|
|
|
| 47 |
|
| 48 |
# ================= HTTP КЛИЕНТ =================
|
| 49 |
http_client = httpx.AsyncClient(timeout=15.0)
|
|
@@ -51,14 +55,17 @@ http_client = httpx.AsyncClient(timeout=15.0)
|
|
| 51 |
# ================= ОТПРАВКА В HUB =================
|
| 52 |
async def send_signal_to_hub(symbol: str, direction: str, confidence: float):
|
| 53 |
try:
|
| 54 |
-
await http_client.post(f"{HUB_URL}/signal", json={
|
| 55 |
"space": "space_21_gold_macro",
|
| 56 |
"symbol": symbol,
|
| 57 |
"direction": direction,
|
| 58 |
"confidence": confidence,
|
| 59 |
"raw": json.dumps({"source": "space_21_gold_macro"})
|
| 60 |
-
})
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
logger.error(f"Ошибка отправки в Hub: {e}")
|
| 64 |
|
|
@@ -270,7 +277,6 @@ async def get_gold_macro_signal() -> Dict[str, Any]:
|
|
| 270 |
analysis = await analyze_gold_macro()
|
| 271 |
latency = int((time.time() - start) * 1000)
|
| 272 |
|
| 273 |
-
# Отправка в Hub
|
| 274 |
await send_signal_to_hub(SYMBOL, analysis['direction'], analysis['confidence'])
|
| 275 |
|
| 276 |
result = {
|
|
@@ -289,15 +295,28 @@ async def get_gold_macro_signal() -> Dict[str, Any]:
|
|
| 289 |
"latency_ms": latency
|
| 290 |
}
|
| 291 |
|
| 292 |
-
logger.info(f"🥇 Gold Macro: {analysis['direction']} | Score={analysis['macro_score']}
|
| 293 |
return result
|
| 294 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
# ================= FASTAPI =================
|
| 296 |
-
app = FastAPI(title="Tomiris Space 21 v3.
|
| 297 |
|
| 298 |
@app.on_event("startup")
|
| 299 |
async def startup():
|
| 300 |
-
|
|
|
|
| 301 |
|
| 302 |
@app.on_event("shutdown")
|
| 303 |
async def shutdown():
|
|
@@ -305,7 +324,13 @@ async def shutdown():
|
|
| 305 |
|
| 306 |
@app.get("/health")
|
| 307 |
async def health():
|
| 308 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
@app.get("/consilium")
|
| 311 |
async def consilium():
|
|
@@ -335,8 +360,12 @@ async def gpr():
|
|
| 335 |
async def full():
|
| 336 |
return await analyze_gold_macro()
|
| 337 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
if __name__ == "__main__":
|
| 339 |
import uvicorn
|
| 340 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 341 |
|
| 342 |
-
print("🚀 SPACE 21 v3.
|
|
|
|
| 21 |
print(f"✅ {pip_name} установлен!")
|
| 22 |
|
| 23 |
# ============================================
|
| 24 |
+
# 👑 TOMIRIS SPACE 21 v3.2 — GOLD MACRO & FLOW ENGINE (АВТО-ОТПРАВКА)
|
| 25 |
# ============================================
|
| 26 |
import os, time, json, logging, asyncio
|
| 27 |
from typing import Dict, Any, List, Optional
|
|
|
|
| 42 |
FRED_KEY = os.getenv("FRED_KEY", "faa11c8e2e4beee08c5b966e8b63a513")
|
| 43 |
NEWSAPI_KEY = os.getenv("NEWSAPI_KEY", "948c7816beea47baa23b054592472d0e")
|
| 44 |
|
| 45 |
+
# Интервал авто-отправки
|
| 46 |
+
AUTO_SEND_INTERVAL = int(os.getenv("AUTO_SEND_INTERVAL", "300"))
|
| 47 |
+
|
| 48 |
GOLD_ETFS = ["GLD", "IAU", "GDX"]
|
| 49 |
CACHE_TTL = {"fred": 3600, "etf_aum": 600, "cot": 86400, "gpr": 900}
|
| 50 |
+
CACHE: Dict[str, Any] = {}
|
| 51 |
|
| 52 |
# ================= HTTP КЛИЕНТ =================
|
| 53 |
http_client = httpx.AsyncClient(timeout=15.0)
|
|
|
|
| 55 |
# ================= ОТПРАВКА В HUB =================
|
| 56 |
async def send_signal_to_hub(symbol: str, direction: str, confidence: float):
|
| 57 |
try:
|
| 58 |
+
resp = await http_client.post(f"{HUB_URL}/signal", json={
|
| 59 |
"space": "space_21_gold_macro",
|
| 60 |
"symbol": symbol,
|
| 61 |
"direction": direction,
|
| 62 |
"confidence": confidence,
|
| 63 |
"raw": json.dumps({"source": "space_21_gold_macro"})
|
| 64 |
+
}, timeout=10)
|
| 65 |
+
if resp.status_code == 200:
|
| 66 |
+
logger.info(f"📤 {symbol}: {direction} conf={confidence:.3f} отправлен в Hub")
|
| 67 |
+
else:
|
| 68 |
+
logger.warning(f"Hub вернул {resp.status_code}")
|
| 69 |
except Exception as e:
|
| 70 |
logger.error(f"Ошибка отправки в Hub: {e}")
|
| 71 |
|
|
|
|
| 277 |
analysis = await analyze_gold_macro()
|
| 278 |
latency = int((time.time() - start) * 1000)
|
| 279 |
|
|
|
|
| 280 |
await send_signal_to_hub(SYMBOL, analysis['direction'], analysis['confidence'])
|
| 281 |
|
| 282 |
result = {
|
|
|
|
| 295 |
"latency_ms": latency
|
| 296 |
}
|
| 297 |
|
| 298 |
+
logger.info(f"🥇 Gold Macro: {analysis['direction']} | Score={analysis['macro_score']}")
|
| 299 |
return result
|
| 300 |
|
| 301 |
+
# ================= АВТО-ОТПРАВКА =================
|
| 302 |
+
async def auto_send_loop():
|
| 303 |
+
logger.info(f"🔄 Авто-отправка Gold Macro запущена (интервал {AUTO_SEND_INTERVAL}с)")
|
| 304 |
+
await asyncio.sleep(30)
|
| 305 |
+
while True:
|
| 306 |
+
try:
|
| 307 |
+
await get_gold_macro_signal()
|
| 308 |
+
logger.info("✅ Gold Macro авто-отправка завершена")
|
| 309 |
+
except Exception as e:
|
| 310 |
+
logger.error(f"Ошибка авто-отправки: {e}")
|
| 311 |
+
await asyncio.sleep(AUTO_SEND_INTERVAL)
|
| 312 |
+
|
| 313 |
# ================= FASTAPI =================
|
| 314 |
+
app = FastAPI(title="Tomiris Space 21 v3.2 — Gold Macro & Flow Engine (Auto-Hub)")
|
| 315 |
|
| 316 |
@app.on_event("startup")
|
| 317 |
async def startup():
|
| 318 |
+
asyncio.create_task(auto_send_loop())
|
| 319 |
+
logger.info("🚀 Space 21 v3.2 запущен с авто-отправкой в Hub")
|
| 320 |
|
| 321 |
@app.on_event("shutdown")
|
| 322 |
async def shutdown():
|
|
|
|
| 324 |
|
| 325 |
@app.get("/health")
|
| 326 |
async def health():
|
| 327 |
+
return {
|
| 328 |
+
"status": "operational",
|
| 329 |
+
"version": "3.2",
|
| 330 |
+
"hub_url": HUB_URL,
|
| 331 |
+
"auto_send_interval": AUTO_SEND_INTERVAL,
|
| 332 |
+
"async": True
|
| 333 |
+
}
|
| 334 |
|
| 335 |
@app.get("/consilium")
|
| 336 |
async def consilium():
|
|
|
|
| 360 |
async def full():
|
| 361 |
return await analyze_gold_macro()
|
| 362 |
|
| 363 |
+
@app.get("/send_now")
|
| 364 |
+
async def send_now():
|
| 365 |
+
return await get_gold_macro_signal()
|
| 366 |
+
|
| 367 |
if __name__ == "__main__":
|
| 368 |
import uvicorn
|
| 369 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 370 |
|
| 371 |
+
print("🚀 SPACE 21 v3.2 — GOLD MACRO & FLOW ENGINE (АВТО-ОТПРАВКА) ЗАПУЩЕН!")
|