File size: 5,888 Bytes
2dd7bee
99561e6
 
 
f2f8150
7d98092
870fb2b
 
 
a5f31f0
4fbfe72
 
 
062e3be
 
 
78198ac
ed60e56
072101a
a5f31f0
 
 
fb2d7e5
dc87a8c
acecbbf
a5f31f0
 
 
acecbbf
a5f31f0
acecbbf
a5f31f0
 
 
 
4fbfe72
a5f31f0
 
 
 
 
 
 
 
 
072101a
acecbbf
 
072101a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
acecbbf
072101a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19e5e9a
072101a
19e5e9a
072101a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19e5e9a
062e3be
 
 
 
 
 
a5f31f0
 
 
 
acecbbf
 
a5f31f0
 
 
acecbbf
 
a5f31f0
 
acecbbf
062e3be
acecbbf
a5f31f0
acecbbf
a5f31f0
 
acecbbf
a5f31f0
 
acecbbf
 
a5f31f0
 
 
 
4fbfe72
acecbbf
 
a5f31f0
acecbbf
a5f31f0
acecbbf
a5f31f0
acecbbf
4fbfe72
acecbbf
a5f31f0
acecbbf
a5f31f0
acecbbf
a5f31f0
acecbbf
a5f31f0
acecbbf
 
a5f31f0
 
 
 
acecbbf
 
ed60e56
a5f31f0
 
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import streamlit as st
import yfinance as yf
import pandas as pd
import cufflinks as cf
import datetime
import plotly.graph_objects as go
from bs4 import BeautifulSoup
import requests
import os
from datetime import date, timedelta
from transformers import pipeline
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from contextlib import redirect_stdout
from io import StringIO



def scrapper (start_date):
    d0 = start_date
    d1 = datetime.date(2008, 1, 1)
    delta = d0 - d1

    #st.write(delta)

    Begindatestring = start_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:
        gg=0
    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
    return headlines


# 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:
    try:
        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 )
    except:
        st.write("Stock Data of this Index on the selected Date is not available \n")







    
    Begindatestring = start_date
    # Create a dummy stream to suppress output
    dummy_stream = StringIO()

    with redirect_stdout(dummy_stream):
        headlines = scrapper(start_date)
    
    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)


    news = news.drop_duplicates()
    news = news.dropna(how='any')
    news = news.reset_index(drop=True)


    tokenizer = AutoTokenizer.from_pretrained("nickmuchi/sec-bert-finetuned-finance-classification")
    model = AutoModelForSequenceClassification.from_pretrained("nickmuchi/sec-bert-finetuned-finance-classification")

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    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"]
        if(i%100 ==0): st.write("Articles Processed Number "+ str(i))


    bullish_rows = df[df['label'] == 'bullish']

    bullish_score_sum = bullish_rows['score'].sum()

    num_bullish_rows = len(bullish_rows)

    average_score_for_bullish = bullish_score_sum / num_bullish_rows

    bearish_rows = df[df['label'] == 'bearish']

    bearish_score_sum = bearish_rows['score'].sum()

    num_bearish_rows = len(bearish_rows)

    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")



else:
    st.warning("Please enter a valid Stock Ticker Symbol.")