File size: 4,793 Bytes
81811e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80dd6a1
81811e0
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import streamlit as st
import yfinance as yf
import tweepy
import requests
from googleapiclient.discovery import build
from transformers import pipeline
import numpy as np
import daal4py as d4p

# Twitter API setup
def twitter_api_setup():
    consumer_key = 'YOUR_TWITTER_API_KEY'
    consumer_secret = 'YOUR_TWITTER_API_SECRET'
    access_token = 'YOUR_TWITTER_ACCESS_TOKEN'
    access_token_secret = 'YOUR_TWITTER_ACCESS_SECRET'
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    return api

# YouTube API setup
def youtube_api_setup():
    api_key = 'YOUR_YOUTUBE_API_KEY'
    youtube = build('youtube', 'v3', developerKey=api_key)
    return youtube

# Fetch Twitter sentiment
def fetch_twitter_sentiment(symbol, api, sentiment_model):
    tweets = api.search(q=symbol, lang='en', count=100, tweet_mode='extended')
    tweet_texts = [tweet.full_text for tweet in tweets]
    sentiments = sentiment_model(tweet_texts)
    sentiment_scores = [s['label'] for s in sentiments]
    positive = sentiment_scores.count('POSITIVE')
    negative = sentiment_scores.count('NEGATIVE')
    return positive, negative

# Fetch YouTube sentiment
def fetch_youtube_sentiment(symbol, youtube, sentiment_model):
    search_response = youtube.search().list(q=symbol, part='snippet', maxResults=10).execute()
    video_ids = [item['id']['videoId'] for item in search_response['items'] if 'videoId' in item['id']]
    
    comments = []
    for video_id in video_ids:
        comment_response = youtube.commentThreads().list(part='snippet', videoId=video_id, maxResults=50).execute()
        for comment in comment_response['items']:
            comment_text = comment['snippet']['topLevelComment']['snippet']['textOriginal']
            comments.append(comment_text)
    
    sentiments = sentiment_model(comments)
    sentiment_scores = [s['label'] for s in sentiments]
    positive = sentiment_scores.count('POSITIVE')
    negative = sentiment_scores.count('NEGATIVE')
    return positive, negative

# Moving Average (technical analysis)
def calculate_moving_average(stock_data, window_size):
    moving_avg_algo = d4p.moving_average(window_size)
    result = moving_avg_algo.compute(stock_data['Close'].to_numpy())
    return result

# Fetch stock data using Yahoo Finance
def fetch_stock_data(symbol):
    stock = yf.Ticker(symbol)
    stock_data = stock.history(period="1y")
    return stock_data

# Main app
def main():
    st.title("Stock Analysis App")
    
    # Input for stock symbol
    stock_symbol = st.text_input("Enter Stock Symbol (e.g., AAPL, TSLA):", "AAPL")
    
    if st.button("Analyze"):
        # Fetch stock data
        stock_data = fetch_stock_data(stock_symbol)
        
        # Display stock data overview
        st.subheader(f"Stock Overview - {stock_symbol}")
        st.write(stock_data.tail())

        # Sentiment analysis
        sentiment_model = pipeline("sentiment-analysis")
        
        # Twitter Sentiment
        st.subheader("Twitter Sentiment Analysis")
        api = twitter_api_setup()
        positive_twitter, negative_twitter = fetch_twitter_sentiment(stock_symbol, api, sentiment_model)
        st.write(f"Positive Tweets: {positive_twitter}, Negative Tweets: {negative_twitter}")

        # YouTube Sentiment
        st.subheader("YouTube Sentiment Analysis")
        youtube = youtube_api_setup()
        positive_youtube, negative_youtube = fetch_youtube_sentiment(stock_symbol, youtube, sentiment_model)
        st.write(f"Positive Comments: {positive_youtube}, Negative Comments: {negative_youtube}")
        
        # Technical analysis (Moving Average)
        st.subheader("Technical Analysis (Moving Average)")
        window_size = st.slider("Select Moving Average Window Size:", 5, 100, 20)
        moving_avg = calculate_moving_average(stock_data, window_size)
        st.line_chart(moving_avg)

        # Fundamental analysis
        st.subheader("Fundamental Analysis")
        st.write("Market Cap:", stock_data['Close'].iloc[-1] * stock_data['Volume'].mean())
        st.write("Price-to-Earnings Ratio (P/E):", stock_data['Close'].iloc[-1] / (stock_data['Close'].mean()))
        
        # Recommendation based on sentiment analysis
        st.subheader("Stock Recommendation")
        total_positive = positive_twitter + positive_youtube
        total_negative = negative_twitter + negative_youtube
        if total_positive > total_negative:
            st.write(f"Recommendation: *BUY* {stock_symbol}")
        elif total_negative > total_positive:
            st.write(f"Recommendation: *SELL* {stock_symbol}")
        else:
            st.write(f"Recommendation: *HOLD* {stock_symbol}")

if __name__ == "__main__":
    main()