Spaces:
Sleeping
Sleeping
Download app.py from 404chain/Signal_bot: direct link, hf CLI and curl.
- Browser
- Download file 8.33 kB
-
https://huggingface.co/spaces/404chain/Signal_bot/resolve/main/app.py
- Command line
-
hf download hf://spaces/404chain/Signal_bot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/404chain/Signal_bot/resolve/main/app.py
8.33 kB
| import gradio as gr | |
| import pandas as pd | |
| import numpy as np | |
| import plotly.graph_objects as go | |
| import yfinance as yf | |
| from datetime import datetime | |
| from ta_engine import compute_ta | |
| from regime_engine import detect_market_regime | |
| from risk_engine import calculate_risk | |
| from ai_engine import ask_ai | |
| # ========================================================= | |
| # ASSET MAP | |
| # ========================================================= | |
| ASSET_MAP = { | |
| # CRYPTO | |
| "btc": "BTC-USD", | |
| "bitcoin": "BTC-USD", | |
| "eth": "ETH-USD", | |
| "ethereum": "ETH-USD", | |
| "sol": "SOL-USD", | |
| "solana": "SOL-USD", | |
| # GOLD | |
| "gold": "GC=F", | |
| "xauusd": "GC=F", | |
| # FOREX | |
| "eurusd": "EURUSD=X", | |
| "gbpusd": "GBPUSD=X", | |
| "usdjpy": "JPY=X", | |
| # INDEX | |
| "nas100": "^NDX", | |
| "nasdaq": "^NDX", | |
| "spx": "^GSPC", | |
| "sp500": "^GSPC" | |
| } | |
| # ========================================================= | |
| # FETCH MARKET DATA | |
| # ========================================================= | |
| def get_market_data(symbol): | |
| symbol = symbol.lower().strip() | |
| ticker = ASSET_MAP.get(symbol, "BTC-USD") | |
| df = yf.download( | |
| ticker, | |
| period="7d", | |
| interval="1h", | |
| auto_adjust=True | |
| ) | |
| if df.empty: | |
| raise Exception("No market data found") | |
| # ========================================= | |
| # FIX MULTI INDEX COLUMNS | |
| # ========================================= | |
| if isinstance(df.columns, pd.MultiIndex): | |
| df.columns = df.columns.get_level_values(0) | |
| # ========================================= | |
| # LOWERCASE | |
| # ========================================= | |
| df.columns = [str(col).lower() for col in df.columns] | |
| # ========================================= | |
| # DEBUG | |
| # ========================================= | |
| print("DATA COLUMNS:", df.columns.tolist()) | |
| required = ["open", "high", "low", "close"] | |
| for col in required: | |
| if col not in df.columns: | |
| raise Exception(f"Missing column: {col}") | |
| return df, ticker | |
| # ========================================================= | |
| # BUILD CHART | |
| # ========================================================= | |
| def build_chart(df): | |
| fig = go.Figure() | |
| fig.add_trace( | |
| go.Candlestick( | |
| x=df.index, | |
| open=df["open"], | |
| high=df["high"], | |
| low=df["low"], | |
| close=df["close"], | |
| name="PRICE" | |
| ) | |
| ) | |
| fig.update_layout( | |
| template="plotly_dark", | |
| paper_bgcolor="#050505", | |
| plot_bgcolor="#050505", | |
| font_color="#39ff14", | |
| height=520, | |
| margin=dict( | |
| l=20, | |
| r=20, | |
| t=30, | |
| b=20 | |
| ), | |
| xaxis_rangeslider_visible=False | |
| ) | |
| return fig | |
| # ========================================================= | |
| # ANALYZE | |
| # ========================================================= | |
| def analyze(user_input): | |
| try: | |
| if not user_input: | |
| raise Exception("Input kosong") | |
| symbol = user_input.lower().strip() | |
| df, ticker = get_market_data(symbol) | |
| # ===================================== | |
| # TA ENGINE | |
| # ===================================== | |
| ta = compute_ta(df) | |
| regime = detect_market_regime(ta) | |
| risk = calculate_risk(ta) | |
| ai = ask_ai( | |
| ta, | |
| ticker, | |
| user_input, | |
| regime | |
| ) | |
| chart = build_chart(df) | |
| latest = df.iloc[-1] | |
| # ===================================== | |
| # MARKET PANEL | |
| # ===================================== | |
| volume_value = latest["volume"] if "volume" in df.columns else 0 | |
| market = f""" | |
| LIVE MARKET TERMINAL | |
| SYMBOL : {ticker} | |
| PRICE : {latest['close']:.2f} | |
| HIGH : {latest['high']:.2f} | |
| LOW : {latest['low']:.2f} | |
| VOLUME : {volume_value:.2f} | |
| UPDATED : {datetime.now().strftime('%H:%M:%S')} | |
| """ | |
| # ===================================== | |
| # SIGNAL PANEL | |
| # ===================================== | |
| signal = f""" | |
| SIGNAL ENGINE | |
| MARKET REGIME : {regime.get('regime', 'UNKNOWN')} | |
| TREND : {ta.get('trend', 'UNKNOWN')} | |
| CONFIDENCE : {ta.get('confidence', 0)}% | |
| EMA20 : {ta.get('ema20', 0)} | |
| EMA50 : {ta.get('ema50', 0)} | |
| EMA100 : {ta.get('ema100', 0)} | |
| RSI : {ta.get('rsi', 0)} | |
| ENTRY : {risk.get('entry', 0)} | |
| STOP LOSS : {risk.get('stop_loss', 0)} | |
| TAKE PROFIT : {risk.get('take_profit', 0)} | |
| """ | |
| return market, chart, signal, ai | |
| except Exception as e: | |
| return ( | |
| f"❌ ERROR : {str(e)}", | |
| None, | |
| "ERROR", | |
| str(e) | |
| ) | |
| # ========================================================= | |
| # CSS | |
| # ========================================================= | |
| css = """ | |
| body{ | |
| background:#050505; | |
| color:#39ff14; | |
| font-family:Arial; | |
| } | |
| .gradio-container{ | |
| background:#050505 !important; | |
| } | |
| .panel{ | |
| background:rgba(0,0,0,0.45); | |
| border:1px solid rgba(57,255,20,0.25); | |
| border-radius:18px; | |
| padding:14px; | |
| backdrop-filter:blur(12px); | |
| box-shadow:0 0 15px rgba(57,255,20,0.08); | |
| } | |
| textarea,input{ | |
| background:#111 !important; | |
| color:#39ff14 !important; | |
| border:1px solid #39ff14 !important; | |
| border-radius:12px !important; | |
| } | |
| button{ | |
| background:#39ff14 !important; | |
| color:black !important; | |
| font-weight:bold !important; | |
| border:none !important; | |
| border-radius:12px !important; | |
| height:50px; | |
| } | |
| button:hover{ | |
| filter:brightness(1.1); | |
| } | |
| """ | |
| # ========================================================= | |
| # UI | |
| # ========================================================= | |
| with gr.Blocks( | |
| theme=gr.themes.Base(), | |
| css=css | |
| ) as demo: | |
| gr.HTML(""" | |
| <h1 style='color:#39ff14'> | |
| CRYPTOINTELLECT TERMINAL | |
| </h1> | |
| <p style='color:#00ff99'> | |
| Multi Asset AI Trading System | |
| </p> | |
| """) | |
| with gr.Row(): | |
| # ================================================= | |
| # LEFT PANEL | |
| # ================================================= | |
| with gr.Column(scale=1): | |
| market_box = gr.Textbox( | |
| label="LIVE MARKET", | |
| lines=14, | |
| elem_classes="panel" | |
| ) | |
| left_panel = gr.Markdown( | |
| """ | |
| ### 🌍 MARKET INTEL | |
| 🟢 Fear & Greed : 72 | |
| 🟢 BTC Dominance : 54% | |
| 🟢 Gold Momentum : Bullish | |
| 🟢 Open Interest : Rising | |
| 🟢 Volatility : Medium | |
| """, | |
| elem_classes="panel" | |
| ) | |
| # ================================================= | |
| # CENTER PANEL | |
| # ================================================= | |
| with gr.Column(scale=2): | |
| chart_output = gr.Plot( | |
| elem_classes="panel" | |
| ) | |
| ai_output = gr.Markdown( | |
| elem_classes="panel" | |
| ) | |
| # ================================================= | |
| # RIGHT PANEL | |
| # ================================================= | |
| with gr.Column(scale=1): | |
| signal_box = gr.Textbox( | |
| label="SIGNAL ENGINE", | |
| lines=14, | |
| elem_classes="panel" | |
| ) | |
| news_panel = gr.Markdown( | |
| """ | |
| ### 📰 MARKET NEWS | |
| • BTC ETF inflow meningkat | |
| • Gold masih bullish | |
| • Whale accumulation terdeteksi | |
| • ETH ecosystem menguat | |
| • FED masih hawkish | |
| """, | |
| elem_classes="panel" | |
| ) | |
| # ===================================================== | |
| # INPUT | |
| # ===================================================== | |
| user_input = gr.Textbox( | |
| label="AI TRADING TERMINAL", | |
| placeholder="btc / eth / sol / gold / xauusd / eurusd", | |
| lines=2, | |
| elem_classes="panel" | |
| ) | |
| # ===================================================== | |
| # BUTTON | |
| # ===================================================== | |
| submit_btn = gr.Button( | |
| "EXECUTE ANALYSIS" | |
| ) | |
| # ===================================================== | |
| # ACTION | |
| # ===================================================== | |
| submit_btn.click( | |
| fn=analyze, | |
| inputs=user_input, | |
| outputs=[ | |
| market_box, | |
| chart_output, | |
| signal_box, | |
| ai_output | |
| ] | |
| ) | |
| # ========================================================= | |
| # LAUNCH | |
| # ========================================================= | |
| demo.launch() |