Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import yfinance as yf
|
| 3 |
+
import pandas_ta as ta
|
| 4 |
+
import cryptocompare
|
| 5 |
+
from tradingview_ta import TA_Handler, Interval
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# Fetch BTC data from multiple sources
|
| 9 |
+
def get_coin_gecko_price():
|
| 10 |
+
response = requests.get("https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd")
|
| 11 |
+
data = response.json()
|
| 12 |
+
return data['bitcoin']['usd']
|
| 13 |
+
|
| 14 |
+
def get_yfinance_price():
|
| 15 |
+
btc_data = yf.Ticker("BTC-USD")
|
| 16 |
+
current_price = btc_data.history(period="1d")['Close'][0]
|
| 17 |
+
return current_price
|
| 18 |
+
|
| 19 |
+
def get_cryptocompare_price():
|
| 20 |
+
cryptocompare_price = cryptocompare.get_price('BTC', currency='USD')
|
| 21 |
+
return cryptocompare_price['BTC']['USD']
|
| 22 |
+
|
| 23 |
+
def get_tradingview_analysis():
|
| 24 |
+
handler = TA_Handler(
|
| 25 |
+
symbol="BTCUSD",
|
| 26 |
+
screener="crypto",
|
| 27 |
+
exchange="BINANCE",
|
| 28 |
+
interval=Interval.INTERVAL_1_HOUR
|
| 29 |
+
)
|
| 30 |
+
analysis = handler.get_analysis()
|
| 31 |
+
return analysis.summary["RECOMMENDATION"]
|
| 32 |
+
|
| 33 |
+
# Calculate technical indicators using pandas-ta
|
| 34 |
+
def calculate_technical_indicators():
|
| 35 |
+
btc_data = yf.download("BTC-USD", period="30d", interval="1h")
|
| 36 |
+
close_prices = btc_data['Close']
|
| 37 |
+
ma_50 = ta.sma(close_prices, length=50)
|
| 38 |
+
ma_200 = ta.sma(close_prices, length=200)
|
| 39 |
+
rsi = ta.rsi(close_prices, length=14)
|
| 40 |
+
macd = ta.macd(close_prices)
|
| 41 |
+
return {
|
| 42 |
+
"MA 50": ma_50.iloc[-1],
|
| 43 |
+
"MA 200": ma_200.iloc[-1],
|
| 44 |
+
"RSI": rsi.iloc[-1],
|
| 45 |
+
"MACD": macd['MACD_12_26_9'].iloc[-1],
|
| 46 |
+
"Signal Line": macd['MACDs_12_26_9'].iloc[-1]
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
# Gradio interface for custom prompts
|
| 50 |
+
def analyze_btc(prompt):
|
| 51 |
+
prompt_lower = prompt.lower()
|
| 52 |
+
|
| 53 |
+
if "price" in prompt_lower:
|
| 54 |
+
price_yf = get_yfinance_price()
|
| 55 |
+
price_cg = get_coin_gecko_price()
|
| 56 |
+
price_cryptocompare = get_cryptocompare_price()
|
| 57 |
+
return (f"BTC Price (Yahoo Finance): ${price_yf:.2f}\n"
|
| 58 |
+
f"BTC Price (CoinGecko): ${price_cg:.2f}\n"
|
| 59 |
+
f"BTC Price (CryptoCompare): ${price_cryptocompare:.2f}")
|
| 60 |
+
|
| 61 |
+
elif "rsi" in prompt_lower or "macd" in prompt_lower or "moving average" in prompt_lower or "ma" in prompt_lower:
|
| 62 |
+
indicators = calculate_technical_indicators()
|
| 63 |
+
return (f"RSI: {indicators['RSI']:.2f}\n"
|
| 64 |
+
f"MACD: {indicators['MACD']:.2f}, Signal Line: {indicators['Signal Line']:.2f}\n"
|
| 65 |
+
f"MA 50: {indicators['MA 50']:.2f}, MA 200: {indicators['MA 200']:.2f}")
|
| 66 |
+
|
| 67 |
+
elif "trend" in prompt_lower:
|
| 68 |
+
trend_recommendation = get_tradingview_analysis()
|
| 69 |
+
return f"BTC Trend (TradingView): {trend_recommendation}"
|
| 70 |
+
|
| 71 |
+
else:
|
| 72 |
+
return "I'm not sure what you're asking. Try asking about price, RSI, MACD, or trend."
|
| 73 |
+
|
| 74 |
+
# Gradio interface setup
|
| 75 |
+
interface = gr.Interface(
|
| 76 |
+
fn=analyze_btc,
|
| 77 |
+
inputs="text",
|
| 78 |
+
outputs="text",
|
| 79 |
+
title="BTC Real-Time Analyzer with Multiple Data Sources",
|
| 80 |
+
description="Ask questions about BTC's real-time data (from CoinGecko, Yahoo Finance, CryptoCompare, TradingView)"
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# Launch the Gradio app
|
| 84 |
+
interface.launch()
|