Spaces:
Paused

Node_Oracle / main.py
Ngwese175's picture
Create main.py
d8d034f verified
Raw History Blame Contribute Delete
3.72 kB
import yfinance as yf
import pandas as pd
import numpy as np
import asyncio
import httpx
from fastapi import FastAPI
app = FastAPI()
# --- CONFIG ---
TELEGRAM_TOKEN = "8498558381:AAGJ_6AWzlc3e4z00VZgurc-jzc8SXtueSU"
TELEGRAM_CHAT_ID = "5947661538"
ASSETS = {"Gold": "GC=F", "BTC": "BTC-USD", "Euro": "EURUSD=X", "Pound": "GBPUSD=X"}
async def send_telegram_msg(text: str):
url = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage"
async with httpx.AsyncClient() as client:
try: await client.post(url, json={"chat_id": TELEGRAM_CHAT_ID, "text": text, "parse_mode": "Markdown"})
except: pass
def get_oracle_verdict(ticker):
try:
df = yf.download(ticker, period="1d", interval="1m", progress=False)
if len(df) < 60: return None
# 1. Trend (Past Hour Slope)
y = df['Close'].tail(60).values
x = np.arange(len(y))
slope = np.polyfit(x, y, 1)[0]
trend_dir = "UP" if slope > 0 else "DOWN"
# 2. RSI Velocity (vs last 6 candles)
delta = df['Close'].diff()
gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean()
df['rsi'] = 100 - (100 / (1 + (gain / loss)))
rsi_now = df['rsi'].iloc[-1]
rsi_6_avg = df['rsi'].tail(6).mean()
rsi_vel_dir = "ACCELERATING" if rsi_now > rsi_6_avg else "DECELERATING"
# 3. Standard Deviation (Volatility)
std_dev = df['Close'].tail(60).std()
# 4. Momentum Direction & Magnitude
roc = ((df['Close'].iloc[-1] - df['Close'].iloc[-10]) / df['Close'].iloc[-10]) * 100
mom_dir = "BULLISH" if roc > 0 else "BEARISH"
mom_mag = abs(roc)
# 5. Displacement (Mean Reversion)
mean_p = df['Close'].tail(60).mean()
curr_p = df['Close'].iloc[-1]
disp_mag = ((curr_p - mean_p) / mean_p) * 100
disp_dir = "ABOVE" if disp_mag > 0 else "BELOW"
# --- THE INTERSECTION (PROBABLE DIRECTION) ---
# Logic: If Trend, RSI Velocity, and Momentum align, that is the path.
# If Displacement is extreme, it suggests a Reversal.
prob_dir = "STALL/SIDEWAYS"
if trend_dir == "UP" and rsi_vel_dir == "ACCELERATING" and mom_dir == "BULLISH":
prob_dir = "Strong BULL Continuation"
elif trend_dir == "DOWN" and rsi_vel_dir == "DECELERATING" and mom_dir == "BEARISH":
prob_dir = "Strong BEAR Continuation"
elif abs(disp_mag) > 0.08: # Extreme stretch
prob_dir = "REVERSAL Expected"
return {
"trend": trend_dir,
"rsi_v": rsi_vel_dir,
"std": round(std_dev, 6),
"mom": f"{mom_dir} ({round(mom_mag, 4)})",
"disp": f"{round(disp_mag, 3)}% {disp_dir}",
"verdict": prob_dir
}
except: return None
async def oracle_loop():
while True:
lines = []
for name, ticker in ASSETS.items():
o = get_oracle_verdict(ticker)
if o:
msg = (f"🔮 *{name} ORACLE*\n"
f"• Trend (1h): {o['trend']}\n"
f"• RSI Vel (6c): {o['rsi_v']}\n"
f"• StdDev: {o['std']}\n"
f"• Momentum: {o['mom']}\n"
f"• Displacement: {o['disp']}\n"
f"📍 *PROBABLE DIRECTION:* `{o['verdict']}`\n---")
lines.append(msg)
if lines:
await send_telegram_msg("\n".join(lines))
await asyncio.sleep(300) # Oracle runs every 5 mins for stability
@app.on_event("startup")
async def startup(): asyncio.create_task(oracle_loop())