samarthv commited on
Commit
6de7b08
·
1 Parent(s): b2aa9c4

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import yfinance as yf
4
+ import plotly.graph_objs as go
5
+ from datetime import datetime, timedelta
6
+ from newsapi import NewsApiClient
7
+
8
+ # Define the News API client
9
+ newsapi = NewsApiClient(api_key='30f892ddb31d43709e5a7a77833f824a')
10
+
11
+ # Define the layout using Streamlit components
12
+ st.title('Stonks20.com')
13
+ stock_symbol = st.text_input('Enter the stock symbol:', 'AAPL')
14
+ date_range = st.date_input('Select the dates:', [(datetime.today() - timedelta(days=365)).date(), datetime.today().date()])
15
+ submit_button = st.button('Submit')
16
+
17
+ # Fetch stock data and update the graphs
18
+ if submit_button:
19
+ try:
20
+ start_date, end_date = date_range
21
+ stock_data = yf.download(stock_symbol, start=start_date, end=end_date)
22
+
23
+ # Create a Candlestick chart
24
+ candlestick_fig = go.Figure(data=[go.Candlestick(x=stock_data.index,
25
+ open=stock_data['Open'],
26
+ high=stock_data['High'],
27
+ low=stock_data['Low'],
28
+ close=stock_data['Close'])])
29
+ candlestick_fig.update_layout(
30
+ title=f'{stock_symbol} Stock Price',
31
+ xaxis_title='Date',
32
+ yaxis_title='Price',
33
+ autosize=True
34
+ )
35
+ st.plotly_chart(candlestick_fig)
36
+
37
+ # Create a Volume chart
38
+ volume_fig = go.Figure(data=[go.Bar(x=stock_data.index,
39
+ y=stock_data['Volume'])])
40
+ volume_fig.update_layout(
41
+ title='Volume',
42
+ xaxis_title='Date',
43
+ yaxis_title='Volume',
44
+ autosize=True
45
+ )
46
+ st.plotly_chart(volume_fig)
47
+
48
+ # Create a Moving Average chart
49
+ moving_average_fig = go.Figure(data=[
50
+ go.Scatter(x=stock_data.index, y=stock_data['Close'], name='Price'),
51
+ go.Scatter(x=stock_data.index, y=stock_data['Close'].rolling(window=50).mean(), name='50-day MA'),
52
+ go.Scatter(x=stock_data.index, y=stock_data['Close'].rolling(window=200).mean(), name='200-day MA')
53
+ ])
54
+ moving_average_fig.update_layout(
55
+ title='Moving Averages',
56
+ xaxis_title='Date',
57
+ yaxis_title='Price',
58
+ autosize=True
59
+ )
60
+ st.plotly_chart(moving_average_fig)
61
+
62
+ # Create an RSI chart
63
+ delta = stock_data['Close'].diff()
64
+ gain = delta.mask(delta < 0, 0)
65
+ loss = -delta.mask(delta > 0, 0)
66
+ avg_gain = gain.rolling(window=14).mean()
67
+ avg_loss = loss.rolling(window=14).mean()
68
+ rs = avg_gain / avg_loss
69
+ rsi = 100 - (100 / (1 + rs))
70
+
71
+ rsi_fig = go.Figure(data=[go.Scatter(x=stock_data.index, y=rsi, name='RSI', line=dict(color='blue'))])
72
+ rsi_fig.update_layout(
73
+ title='Relative Strength Index (RSI)',
74
+ xaxis_title='Date',
75
+ yaxis_title='RSI',
76
+ autosize=True
77
+ )
78
+ st.plotly_chart(rsi_fig)
79
+
80
+ except Exception as e:
81
+ st.error(f'Error: {str(e)}')
82
+
83
+ # Display news articles
84
+ news_articles = newsapi.get_everything(q=stock_symbol, language='en', sort_by='publishedAt')['articles']
85
+ if news_articles:
86
+ st.subheader('News')
87
+ for article in news_articles:
88
+ st.markdown(f"## {article['title']}")
89
+ st.markdown(article['description'])
90
+ st.markdown(f"[Read More]({article['url']})")
91
+ else:
92
+ st.warning('No news articles found for the given stock symbol.')