samarthv commited on
Commit
7b9eb2f
1 Parent(s): 32e0506

Upload app.py

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