import streamlit as st import yfinance as yf import pandas as pd import cufflinks as cf import datetime import plotly.graph_objects as go # App title st.markdown(''' # Sovrenn Market Sentiment Indicator App Shown are the stock price data for the selected company! **Credits** - App built by SRL ''') st.write('---') # Sidebar st.sidebar.subheader('Query parameters') start_date = st.sidebar.date_input("Start date", datetime.date(2023,9, 20)) #start_date = start_date - datetime.timedelta(days=1) end_date = start_date + datetime.timedelta(days=14) # User input for the stock ticker symbol tickerSymbol = st.sidebar.text_input('Enter Stock Ticker Symbol') if tickerSymbol: tickerData = yf.Ticker(tickerSymbol) # Get ticker data tickerDf = tickerData.history(period='1d', start=start_date, end=end_date) # Get the historical prices for this ticker string_name = tickerData.info.get('longName', 'Company Name Not Available') st.header('**%s**' % string_name) # Try to get the business summary, handle KeyError if not available try: string_summary = tickerData.info['longBusinessSummary'] st.info(string_summary) except KeyError: st.warning("Business summary not available for this company.") # Ticker data st.header('**Ticker data**') st.write(tickerDf) # Create a candlestick chart and volume bar chart fig_candlestick = go.Figure(data=[go.Candlestick(x=tickerDf.index, open=tickerDf['Open'], high=tickerDf['High'], low=tickerDf['Low'], close=tickerDf['Close'])]) fig_volume = go.Figure(data=[go.Bar(x=tickerDf.index, y=tickerDf['Volume'])]) st.header('**Candlestick Chart**') st.plotly_chart(fig_candlestick) st.header('**Volume Bar Chart**') st.plotly_chart(fig_volume) st.write(start_date) tickerDf = pd.DataFrame(tickerDf).reset_index() # st.write(tickerDf) #date = datetime.date(start_date) date_str = start_date.strftime("%Y-%m-%d") st.write(date_str) df = tickerDf[tickerDf["Date"]==date_str] st.write(df) if (df["Close"][0] > df["Open"][0] ): st.write("NSE has uptrend on " +date_str ) if (df["Close"][0] < df["Open"][0] ): st.write(" NSE has downdtrend on " +date_str ) else: st.warning("Please enter a valid Stock Ticker Symbol.") Begindatestring = start_date #Begindatestring = datetime.strptime(Begindatestring, "%Y-%m-%d").date() val = 39448 + int(delta.days) url = 'https://economictimes.indiatimes.com/archivelist/year-'+str(Begindatestring.year)+',month-'+str(Begindatestring.month)+',starttime-'+str(val)+'.cms' # Replace with your URL response = requests.get(url) if response.status_code == 200: html_text = response.text soup = BeautifulSoup(html_text, "lxml") else: st.write(f"Failed to fetch the page. Status code: {response.status_code}") jobs = soup.find_all("li") headlines = [] for job in jobs: try: target_element = job.find("a") target_element.text headlines.append(target_element.text) except: continue index = [idx for idx, s in enumerate(headlines) if s=='Most Read' ][0] del headlines[index:] news = pd.DataFrame({"News": headlines}) news.insert(0, 'Date', Begindatestring) #st.dataframe(df[0:1]) news = news.drop_duplicates() news = news.dropna(how='any') news = news.reset_index(drop=True) import pandas as pd import numpy as np from transformers import pipeline import torch from transformers import AutoTokenizer, AutoModelForSequenceClassification device = torch.device("cuda" if torch.cuda.is_available() else "cpu") tokenizer = AutoTokenizer.from_pretrained("nickmuchi/sec-bert-finetuned-finance-classification") model = AutoModelForSequenceClassification.from_pretrained("nickmuchi/sec-bert-finetuned-finance-classification") nlp = pipeline("text-classification", model=model, tokenizer=tokenizer, device=device) length = len(news[ 'News'].to_list()) news_list = news[ 'News'].to_list() df = pd.DataFrame() for i in range (0, length): results = nlp(news_list[i]) df.loc[i, "News"] = news_list[i] df.loc[i , 'label'] = results[0]["label"] df.loc[i , 'score'] = results[0]["score"] #st.dataframe(df) # Filter the DataFrame to get rows with "neutral" sentiment bullish_rows = df[df['label'] == 'bullish'] # Calculate the sum of the 'Score' column for "neutral" rows bullish_score_sum = bullish_rows['score'].sum() num_bullish_rows = len(bullish_rows) # Calculate the average score for "neutral" sentiment average_score_for_bullish = bullish_score_sum / num_bullish_rows # Filter the DataFrame to get rows with "neutral" sentiment bearish_rows = df[df['label'] == 'bearish'] # Calculate the sum of the 'Score' column for "neutral" rows bearish_score_sum = bearish_rows['score'].sum() # Cabearishlculate the number of "neutral" rows num_bearish_rows = len(bearish_rows) # Calculate the average score for "neutral" sentiment average_score_for_bearish = bearish_score_sum / num_bearish_rows if(average_score_for_bearish > average_score_for_bullish): st.write("Stock will go down") if(average_score_for_bearish < average_score_for_bullish): st.write("Stock will go up")