netflypsb commited on
Commit
5657c7a
·
verified ·
1 Parent(s): c6d4325

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Required imports
2
+ import yfinance as yf
3
+ import pandas as pd
4
+ import numpy as np
5
+ from scipy.signal import find_peaks
6
+ import plotly.graph_objects as go
7
+ import plotly.express as px
8
+ import streamlit as st
9
+ from plotly.subplots import make_subplots
10
+
11
+ # Define functions for technical indicators
12
+ def compute_macd(data, slow=26, fast=12, smooth=9):
13
+ exp1 = data['Close'].ewm(span=fast, adjust=False).mean()
14
+ exp2 = data['Close'].ewm(span=slow, adjust=False).mean()
15
+ macd = exp1 - exp2
16
+ signal = macd.ewm(span=smooth, adjust=False).mean()
17
+ return macd, signal
18
+
19
+ def compute_rsi(data, window=14):
20
+ delta = data['Close'].diff()
21
+ gain = (delta.where(delta > 0, 0)).rolling(window=window).mean()
22
+ loss = (-delta.where(delta < 0, 0)).rolling(window=window).mean()
23
+ rs = gain / loss
24
+ return 100 - (100 / (1 + rs))
25
+
26
+ def compute_bollinger(data, window=20, num_std=2):
27
+ rolling_mean = data['Close'].rolling(window=window).mean()
28
+ rolling_std = data['Close'].rolling(window=window).std()
29
+ upper_band = rolling_mean + (rolling_std * num_std)
30
+ lower_band = rolling_mean - (rolling_std * num_std)
31
+ return upper_band, lower_band
32
+
33
+ # Streamlit UI - Introduction and How to Use the App
34
+ st.title("Advanced Stock Analysis Tool")
35
+ st.markdown("""
36
+ Welcome to the advanced stock analysis application designed for both beginner and seasoned traders. This version includes RSI, MACD, Bollinger Bands, and volume indicators, along with the original features.
37
+
38
+ **Features**:
39
+ - View stock price movements and indicators over time.
40
+ - Analyze technical indicators such as RSI, MACD, and Bollinger Bands.
41
+ - Identify and visualize MA crossovers which are significant for trading strategies.
42
+ - Download charts for offline analysis.
43
+
44
+ **Instructions**:
45
+ 1. Enter a stock symbol in the sidebar.
46
+ 2. Select your desired time period.
47
+ 3. Explore the indicators and make informed trading decisions.
48
+ """)
49
+
50
+ # User Inputs
51
+ sidebar = st.sidebar
52
+ symbol = sidebar.text_input("Enter stock symbol", "AAPL")
53
+ period = sidebar.selectbox("Select period", ["1mo", "3mo", "6mo", "1y", "2y", "5y", "10y", "ytd", "max"])
54
+
55
+ # Fetch stock data
56
+ data = yf.download(symbol, period=period)
57
+
58
+ # Calculate technical indicators
59
+ data['MA20'] = data['Close'].rolling(window=20).mean()
60
+ data['MA50'] = data['Close'].rolling(window=50).mean()
61
+ data['MA200'] = data['Close'].rolling(window=200).mean()
62
+ data['RSI'] = compute_rsi(data)
63
+ data['MACD'], data['Signal'] = compute_macd(data)
64
+ data['Upper_Band'], data['Lower_Band'] = compute_bollinger(data)
65
+
66
+ # Detect MA crossovers
67
+ cross_above = (data['MA20'].shift(14) < data['MA50'].shift(14)) & (data['MA20'] > data['MA50'])
68
+ cross_below = (data['MA20'].shift(14) > data['MA50'].shift(14)) & (data['MA20'] < data['MA50'])
69
+
70
+ # Plot setup
71
+ fig = make_subplots(rows=3, cols=1, shared_xaxes=True, vertical_spacing=0.02,
72
+ subplot_titles=('Stock Price and Moving Averages', 'Volume and Bollinger Bands', 'RSI and MACD'))
73
+
74
+ # Add traces for price and moving averages
75
+ fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Close Price'), row=1, col=1)
76
+ fig.add_trace(go.Scatter(x=data.index, y=data['MA20'], name='20-Period MA', line=dict(color='green')), row=1, col=1)
77
+ fig.add_trace(go.Scatter(x=data.index, y=data['MA50'], name='50-Period MA', line=dict(color='blue')), row=1, col=1)
78
+ fig.add_trace(go.Scatter(x=data.index, y=data['MA200'], name='200-Period MA', line=dict(color='red')), row=1, col=1)
79
+ fig.add_trace(go.Scatter(x=data.index[cross_above], y=data['MA20'][cross_above], mode='markers', marker=dict(color='green', size=10), name='Golden Cross'), row=1, col=1)
80
+ fig.add_trace(go.Scatter(x=data.index[cross_below], y=data['MA20'][cross_below], mode='markers', marker=dict(color='red', size=10), name='Death Cross'), row=1, col=1)
81
+
82
+ # Add traces for Bollinger Bands and volume
83
+ fig.add_trace(go.Bar(x=data.index, y=data['Volume'], name='Volume'), row=2, col=1)
84
+ fig.add_trace(go.Scatter(x=data.index, y=data['Upper_Band'], name='Upper Bollinger Band', line=dict(color='purple')), row=2, col=1)
85
+ fig.add_trace(go.Scatter(x=data.index, y=data['Lower_Band'], name='Lower Bollinger Band', line=dict(color='purple')), row=2, col=1)
86
+
87
+ # Add traces for RSI and MACD
88
+ fig.add_trace(go.Scatter(x=data.index, y=data['RSI'], name='RSI', line=dict(color='orange')), row=3, col=1)
89
+ fig.add_trace(go.Scatter(x=data.index, y=data['MACD'], name='MACD', line=dict(color='pink')), row=3, col=1)
90
+ fig.add_trace(go.Scatter(x=data.index, y=data['Signal'], name='MACD Signal', line=dict(color='cyan')), row=3, col=1)
91
+
92
+ # Layout adjustments
93
+ fig.update_layout(height=1200, width=800, showlegend=True)
94
+ fig.update_yaxes(title_text="Price", row=1, col=1)
95
+ fig.update_yaxes(title_text="Volume", row=2, col=1)
96
+ fig.update_yaxes(title_text="Index Value", row=3, col=1)
97
+
98
+ # Display the chart with a download button
99
+ st.plotly_chart(fig)
100
+ fig.write_html("stock_analysis_chart.html")
101
+ st.markdown("[Download Chart](stock_analysis_chart.html)")
102
+
103
+ # The code provides a detailed analysis and visual representation of the stock data with added technical indicators, fulfilling the advanced requirements of both beginner and seasoned traders.
104
+