Spaces:
Runtime error
Runtime error
| import requests | |
| import pandas as pd | |
| def fetch_crypto_data(symbol): | |
| """Fetch crypto market data from Binance.""" | |
| url = f"https://api.binance.com/api/v3/klines" | |
| params = {"symbol": symbol, "interval": "1h", "limit": 100} | |
| response = requests.get(url, params=params) | |
| if response.status_code == 200: | |
| data = response.json() | |
| df = pd.DataFrame(data, columns=["timestamp", "open", "high", "low", "close", "volume"]) | |
| df["close"] = df["close"].astype(float) | |
| return df.dropna() | |
| else: | |
| raise Exception("Error fetching crypto data.") | |
| def fetch_stock_data(symbol): | |
| """Fetch stock market data from Alpha Vantage.""" | |
| url = f"https://www.alphavantage.co/query" | |
| params = {"function": "TIME_SERIES_INTRADAY", "symbol": symbol, "interval": "60min", | |
| "apikey": ALPHA_VANTAGE_API_KEY} | |
| response = requests.get(url, params=params) | |
| if response.status_code == 200: | |
| data = response.json()["Time Series (60min)"] | |
| df = pd.DataFrame(data).T.astype(float).reset_index() | |
| df.columns = ["timestamp", "open", "high", "low", "close", "volume"] | |
| return df.dropna() | |
| else: | |
| raise Exception("Error fetching stock data.") | |
| def fetch_sentiment_data(keyword): | |
| """Analyze sentiment from social media.""" | |
| tweets = [ | |
| f"{keyword} is going to moon!", | |
| f"I hate {keyword}, it's trash!", | |
| f"{keyword} is amazing!" | |
| ] | |
| sentiments = [TextBlob(tweet).sentiment.polarity for tweet in tweets] | |
| return sum(sentiments) / len(sentiments) | |