Backend v3: Telegram, Binance Balance, and Auto-Logging
Browse files4 unmodified lines56789101112131415161718191 unmodified line212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798994 unmodified linesfrom sklearn.preprocessing import MinMaxScalerfrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Dense, LSTM, Inputimport requestsimport osimport timeapp = FastAPI()# Kredensial SupabaseSUPA_URL = "https://nzpzddyjcthhzkyfrjpb.supabase.co"SUPA_KEY = os.getenv("SUPABASE_KEY")def save_to_supabase(payload):if not SUPA_KEY: return1 unmodified linetry: requests.post(f"{SUPA_URL}/rest/v1/signals", json=payload, headers=headers, timeout=5)except: passdef get_crypto_data(retries=3):"""Fungsi ambil data dengan mekanisme retry agar tahan banting"""for i in range(retries):try:df = yf.download('BTC-USD', period='60d', interval='1d', auto_adjust=True, progress=False)if not df.empty and len(df) > 30:return dfexcept Exception as e:print(f"Retry {i+1} failed: {e}")time.sleep(2)return Nonedef run_ai_logic():df = get_crypto_data()if df is None:raise ValueError("Data pasar tidak tersedia (Rate Limit Yahoo Finance)")# Handle multi-index columns dari yfinance baruif isinstance(df.columns, pd.MultiIndex):close_series = df['Close'].iloc[:, 0]else:close_series = df['Close']current_price = float(close_series.iloc[-1])# Preprocessingdata_df = close_series.to_frame()scaler = MinMaxScaler()scaled_data = scaler.fit_transform(data_df)lookback = 15X, y = [], []for i in range(lookback, len(scaled_data)):X.append(scaled_data[i-lookback:i, 0])y.append(scaled_data[i, 0])X = np.array(X).reshape(-1, lookback, 1)y = np.array(y)# Model Super Fastmodel = Sequential([Input(shape=(lookback, 1)),LSTM(16),Dense(1)])model.compile(optimizer='adam', loss='mse')model.fit(X, y, epochs=2, batch_size=32, verbose=0)# Predictlast_input = scaled_data[-lookback:].reshape(1, lookback, 1)pred_scaled = model.predict(last_input, verbose=0)final_pred = float(scaler.inverse_transform(pred_scaled).flatten()[0])return current_price, final_pred@app.get("/")def home():return {"status": "Chronos Online", "info": "Use /predict to run AI"}@app.get("/predict")def get_prediction():try:curr, pred = run_ai_logic()signal = "BUY" if pred > curr else "HOLD/SELL"payload = {"price": curr,"prediction": pred,"fng_index": 50, # Static if API fails"signal_type": signal}save_to_supabase(payload)return payloadexcept Exception as e:return {"error": "Backend Busy or Rate Limited", "detail": str(e)}4 unmodified lines5678910111213141516171819202122232425262728293031323334353637383940414243441 unmodified line464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293944 unmodified linesfrom sklearn.preprocessing import MinMaxScalerfrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Dense, LSTM, Inputfrom binance.client import Clientimport requestsimport osapp = FastAPI()# --- CONFIG & SECRETS ---SUPA_URL = "https://nzpzddyjcthhzkyfrjpb.supabase.co"SUPA_KEY = os.getenv("SUPABASE_KEY")TG_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")TG_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID")BIN_KEY = os.getenv("BINANCE_API_KEY")BIN_SEC = os.getenv("BINANCE_SECRET_KEY")# --- TOOLS ---def send_telegram(msg):if TG_TOKEN and TG_CHAT_ID:url = f"https://api.telegram.org/bot{TG_TOKEN}/sendMessage"try: requests.post(url, json={"chat_id": TG_CHAT_ID, "text": msg})except: passdef get_binance_balance():if not BIN_KEY or not BIN_SEC:return "Key Missing"try:client = Client(BIN_KEY, BIN_SEC)acc = client.get_account()balances = {item['asset']: item['free'] for item in acc['balances'] if float(item['free']) > 0}# Fokus ke USDT dan BTCusdt = balances.get('USDT', '0')btc = balances.get('BTC', '0')return f"💰 Wallet: {usdt} USDT | {btc} BTC"except:return "Binance Auth Failed"def save_to_supabase(payload):if not SUPA_KEY: return1 unmodified linetry: requests.post(f"{SUPA_URL}/rest/v1/signals", json=payload, headers=headers, timeout=5)except: pass# --- AI LOGIC ---def run_ai_logic():df = yf.download('BTC-USD', period='60d', interval='1d', auto_adjust=True, progress=False)if df.empty: raise ValueError("Yahoo Finance Limit")close_series = df['Close'].iloc[:, 0] if isinstance(df.columns, pd.MultiIndex) else df['Close']current_price = float(close_series.iloc[-1])scaler = MinMaxScaler()scaled_data = scaler.fit_transform(close_series.to_frame())lookback = 15X = np.array([scaled_data[i-lookback:i, 0] for i in range(lookback, len(scaled_data))])X = X.reshape(-1, lookback, 1)model = Sequential([Input(shape=(lookback, 1)), LSTM(16), Dense(1)])model.compile(optimizer='adam', loss='mse')model.fit(X, scaled_data[lookback:], epochs=2, verbose=0)pred_scaled = model.predict(scaled_data[-lookback:].reshape(1, lookback, 1), verbose=0)final_pred = float(scaler.inverse_transform(pred_scaled).flatten()[0])return current_price, final_pred# --- ENDPOINTS ---@app.get("/predict")def get_prediction():try:curr, pred = run_ai_logic()signal = "BUY 🟢" if pred > curr else "HOLD/SELL 🔴"wallet = get_binance_balance()payload = {"price": curr, "prediction": pred, "fng_index": 50, "signal_type": signal}save_to_supabase(payload)# LOG TO TELEGRAMmsg = f"🛡️ CHRONOS REPORT\n\nPrice: ${curr:,.2f}\nPred: ${pred:,.2f}\nSignal: {signal}\n\n{wallet}"send_telegram(msg)return {**payload, "wallet": wallet}except Exception as e:return {"error": str(e)}@app.get("/")def health():return {"status": "Chronos Online"}
app.py+52-57