|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
def youtube_api_setup(): |
|
|
api_key = 'YOUR_YOUTUBE_API_KEY' |
|
|
youtube = build('youtube', 'v3', developerKey=api_key) |
|
|
return youtube |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
def fetch_stock_data(symbol): |
|
|
stock = yf.Ticker(symbol) |
|
|
stock_data = stock.history(period="1y") |
|
|
return stock_data |
|
|
|
|
|
|
|
|
def main(): |
|
|
st.title("Stock Analysis App") |
|
|
|
|
|
|
|
|
stock_symbol = st.text_input("Enter Stock Symbol (e.g., AAPL, TSLA):", "AAPL") |
|
|
|
|
|
if st.button("Analyze"): |
|
|
|
|
|
stock_data = fetch_stock_data(stock_symbol) |
|
|
|
|
|
|
|
|
st.subheader(f"Stock Overview - {stock_symbol}") |
|
|
st.write(stock_data.tail()) |
|
|
|
|
|
|
|
|
sentiment_model = pipeline("sentiment-analysis") |
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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())) |
|
|
|
|
|
|
|
|
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() |