Spaces:
Sleeping
Sleeping
import requests | |
import yfinance as yf | |
import pandas_ta as ta | |
import cryptocompare | |
from tradingview_ta import TA_Handler, Interval | |
import gradio as gr | |
def get_coin_gecko_price(): | |
try: | |
response = requests.get("https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd") | |
data = response.json() | |
return data['bitcoin']['usd'] | |
except Exception as e: | |
return f"Error fetching CoinGecko data: {str(e)}" | |
def get_yfinance_price(): | |
try: | |
btc_data = yf.Ticker("BTC-USD") | |
current_price = btc_data.history(period="1d")['Close'][0] | |
return current_price | |
except Exception as e: | |
return f"Error fetching Yahoo Finance data: {str(e)}" | |
def get_cryptocompare_price(): | |
try: | |
cryptocompare_price = cryptocompare.get_price('BTC', currency='USD') | |
return cryptocompare_price['BTC']['USD'] | |
except Exception as e: | |
return f"Error fetching CryptoCompare data: {str(e)}" | |
def get_tradingview_analysis(): | |
try: | |
handler = TA_Handler( | |
symbol="BTCUSD", | |
screener="crypto", | |
exchange="BINANCE", | |
interval=Interval.INTERVAL_1_HOUR | |
) | |
analysis = handler.get_analysis() | |
return analysis.summary["RECOMMENDATION"] | |
except Exception as e: | |
return f"Error fetching TradingView data: {str(e)}" | |
def calculate_technical_indicators(): | |
try: | |
btc_data = yf.download("BTC-USD", period="30d", interval="1h") | |
close_prices = btc_data['Close'] | |
ma_50 = ta.sma(close_prices, length=50) | |
ma_200 = ta.sma(close_prices, length=200) | |
rsi = ta.rsi(close_prices, length=14) | |
macd = ta.macd(close_prices) | |
return { | |
"MA 50": ma_50.iloc[-1], | |
"MA 200": ma_200.iloc[-1], | |
"RSI": rsi.iloc[-1], | |
"MACD": macd['MACD_12_26_9'].iloc[-1], | |
"Signal Line": macd['MACDs_12_26_9'].iloc[-1] | |
} | |
except Exception as e: | |
return f"Error calculating technical indicators: {str(e)}" | |
def analyze_btc(prompt): | |
prompt_lower = prompt.lower() | |
if "price" in prompt_lower: | |
price_yf = get_yfinance_price() | |
price_cg = get_coin_gecko_price() | |
price_cryptocompare = get_cryptocompare_price() | |
return (f"BTC Price (Yahoo Finance): ${price_yf:.2f}\n" | |
f"BTC Price (CoinGecko): ${price_cg:.2f}\n" | |
f"BTC Price (CryptoCompare): ${price_cryptocompare:.2f}") | |
elif "rsi" in prompt_lower or "macd" in prompt_lower or "moving average" in prompt_lower or "ma" in prompt_lower: | |
indicators = calculate_technical_indicators() | |
return (f"RSI: {indicators['RSI']:.2f}\n" | |
f"MACD: {indicators['MACD']:.2f}, Signal Line: {indicators['Signal Line']:.2f}\n" | |
f"MA 50: {indicators['MA 50']:.2f}, MA 200: {indicators['MA 200']:.2f}") | |
elif "trend" in prompt_lower: | |
trend_recommendation = get_tradingview_analysis() | |
return f"BTC Trend (TradingView): {trend_recommendation}" | |
else: | |
return "I'm not sure what you're asking. Try asking about price, RSI, MACD, or trend." | |
interface = gr.Interface( | |
fn=analyze_btc, | |
inputs="text", | |
outputs="text", | |
title="BTC Real-Time Analyzer with Multiple Data Sources", | |
description="Ask questions about BTC's real-time data (from CoinGecko, Yahoo Finance, CryptoCompare, TradingView)" | |
) | |
interface.launch() | |