netflypsb commited on
Commit
32617e2
·
verified ·
1 Parent(s): b2b662d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("Netflyp Stock and Crypto Analysis Tool")
35
+ st.markdown("""
36
+ Welcome to Netflyp Stock and Crypto Analysis Tool. This version includes RSI, MACD, Bollinger Bands, and volume indicators.
37
+
38
+ **Features**:
39
+ - View stock and crypto 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 or crypto symbol (as they are in yahoo finance) in the sidebar or choose from sample tickers below.
46
+ 2. Select your desired time period.
47
+ 3. Click the 'Analyze' button to load and display the data.
48
+ """)
49
+
50
+ # User Inputs
51
+ sidebar = st.sidebar
52
+ symbol = sidebar.text_input("Enter stock or crypto symbol", "AAPL")
53
+ period = sidebar.selectbox("Select period", ["1mo", "3mo", "6mo", "1y", "2y", "5y", "10y", "ytd", "max"])
54
+
55
+ # Sample Inputs
56
+ sample_tickers = ["NVDA", "BTC-USD", "5258.KL", "5209.KL", "5235SS.KL"]
57
+ ticker_buttons = st.radio("Sample tickers:", sample_tickers)
58
+
59
+ # Set ticker from sample when clicked
60
+ if ticker_buttons:
61
+ symbol = ticker_buttons
62
+ sidebar.text_input("Enter stock symbol", value=symbol, key="1")
63
+
64
+ # Button to trigger analysis
65
+ if st.button('Analyze') or st.sidebar.button('Analyze Sidebar'):
66
+ # Fetch stock data
67
+ data = yf.download(symbol, period=period)
68
+
69
+ # Calculate technical indicators
70
+ data['MA20'] = data['Close'].rolling(window=20).mean()
71
+ data['MA50'] = data['Close'].rolling(window=50).mean()
72
+ data['MA200'] = data['Close'].rolling(window=200).mean()
73
+ data['RSI'] = compute_rsi(data)
74
+ data['MACD'], data['Signal'] = compute_macd(data)
75
+ data['Upper_Band'], data['Lower_Band'] = compute_bollinger(data)
76
+
77
+ # Plot setup
78
+ fig = make_subplots(rows=3, cols=1, shared_xaxes=True, vertical_spacing=0.02,
79
+ subplot_titles=('Stock Price and Moving Averages', 'Volume and Bollinger Bands', 'RSI and MACD'))
80
+
81
+ # Add traces
82
+ fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Close Price'), row=1, col=1)
83
+ fig.add_trace(go.Scatter(x=data.index, y=data['MA20'], name='20-Period MA', line=dict(color='green')), row=1, col=1)
84
+ fig.add_trace(go.Scatter(x=data.index, y=data['MA50'], name='50-Period MA', line=dict(color='blue')), row=1, col=1)
85
+ fig.add_trace(go.Scatter(x=data.index, y=data['MA200'], name='200-Period MA', line=dict(color='red')), row=1, col=1)
86
+ fig.add_trace(go.Bar(x=data.index, y=data['Volume'], name='Volume'), row=2, col=1)
87
+ fig.add_trace(go.Scatter(x=data.index, y=data['Upper_Band'], name='Upper Bollinger Band', line=dict(color='purple')), row=2, col=1)
88
+ fig.add_trace(go.Scatter(x=data.index, y=data['Lower_Band'], name='Lower Bollinger Band', line=dict(color='purple')), row=2, col=1)
89
+ fig.add_trace(go.Scatter(x=data.index, y=data['RSI'], name='RSI', line=dict(color='orange')), row=3, col=1)
90
+ fig.add_trace(go.Scatter(x=data.index, y=data['MACD'], name='MACD', line=dict(color='pink')), row=3, col=1)
91
+ fig.add_trace(go.Scatter(x=data.index, y=data['Signal'], name='MACD Signal', line=dict(color='cyan')), row=3, col=1)
92
+
93
+ # Layout adjustments
94
+ fig.update_layout(height=1200, width=800, showlegend=True)
95
+ fig.update_yaxes(title_text="Price", row=1, col=1)
96
+ fig.update_yaxes(title_text="Volume", row=2, col=1)
97
+ fig.update_yaxes(title_text="Index Value", row=3, col=1)
98
+
99
+ # Display the chart with a download button
100
+ st.plotly_chart(fig)
101
+ fig.write_html("stock_analysis_chart.html")
102
+ st.markdown("[Download Chart](stock_analysis_chart.html)")
103
+