File size: 1,564 Bytes
728ef2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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)