Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Display news articles
|
81 |
+
news_api_key = 'YOUR_NEWS_API_KEY'
|
82 |
+
if submit_button:
|
83 |
+
try:
|
84 |
+
url = f'https://newsapi.org/v2/everything?q={stock_symbol}&language=en&apiKey={news_api_key}'
|
85 |
+
response = requests.get(url)
|
86 |
+
data = response.json()
|
87 |
+
articles = data['articles']
|
88 |
+
|
89 |
+
if articles:
|
90 |
+
st.subheader('News')
|
91 |
+
for article in articles:
|
92 |
+
st.markdown(f"## {article['title']}")
|
93 |
+
st.markdown(article['description'])
|
94 |
+
st.markdown(f"[Read More]({article['url']})")
|
95 |
+
else:
|
96 |
+
st.warning('No news articles found for the given stock symbol.')
|
97 |
+
|
98 |
+
except Exception as e:
|
99 |
+
st.error(f'Error: {str(e)}')
|