Spaces:
Running
Running
File size: 4,809 Bytes
32617e2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# Required imports
import yfinance as yf
import pandas as pd
import numpy as np
from scipy.signal import find_peaks
import plotly.graph_objects as go
import plotly.express as px
import streamlit as st
from plotly.subplots import make_subplots
# Define functions for technical indicators
def compute_macd(data, slow=26, fast=12, smooth=9):
exp1 = data['Close'].ewm(span=fast, adjust=False).mean()
exp2 = data['Close'].ewm(span=slow, adjust=False).mean()
macd = exp1 - exp2
signal = macd.ewm(span=smooth, adjust=False).mean()
return macd, signal
def compute_rsi(data, window=14):
delta = data['Close'].diff()
gain = (delta.where(delta > 0, 0)).rolling(window=window).mean()
loss = (-delta.where(delta < 0, 0)).rolling(window=window).mean()
rs = gain / loss
return 100 - (100 / (1 + rs))
def compute_bollinger(data, window=20, num_std=2):
rolling_mean = data['Close'].rolling(window=window).mean()
rolling_std = data['Close'].rolling(window=window).std()
upper_band = rolling_mean + (rolling_std * num_std)
lower_band = rolling_mean - (rolling_std * num_std)
return upper_band, lower_band
# Streamlit UI - Introduction and How to Use the App
st.title("Netflyp Stock and Crypto Analysis Tool")
st.markdown("""
Welcome to Netflyp Stock and Crypto Analysis Tool. This version includes RSI, MACD, Bollinger Bands, and volume indicators.
**Features**:
- View stock and crypto price movements and indicators over time.
- Analyze technical indicators such as RSI, MACD, and Bollinger Bands.
- Identify and visualize MA crossovers which are significant for trading strategies.
- Download charts for offline analysis.
**Instructions**:
1. Enter a stock or crypto symbol (as they are in yahoo finance) in the sidebar or choose from sample tickers below.
2. Select your desired time period.
3. Click the 'Analyze' button to load and display the data.
""")
# User Inputs
sidebar = st.sidebar
symbol = sidebar.text_input("Enter stock or crypto symbol", "AAPL")
period = sidebar.selectbox("Select period", ["1mo", "3mo", "6mo", "1y", "2y", "5y", "10y", "ytd", "max"])
# Sample Inputs
sample_tickers = ["NVDA", "BTC-USD", "5258.KL", "5209.KL", "5235SS.KL"]
ticker_buttons = st.radio("Sample tickers:", sample_tickers)
# Set ticker from sample when clicked
if ticker_buttons:
symbol = ticker_buttons
sidebar.text_input("Enter stock symbol", value=symbol, key="1")
# Button to trigger analysis
if st.button('Analyze') or st.sidebar.button('Analyze Sidebar'):
# Fetch stock data
data = yf.download(symbol, period=period)
# Calculate technical indicators
data['MA20'] = data['Close'].rolling(window=20).mean()
data['MA50'] = data['Close'].rolling(window=50).mean()
data['MA200'] = data['Close'].rolling(window=200).mean()
data['RSI'] = compute_rsi(data)
data['MACD'], data['Signal'] = compute_macd(data)
data['Upper_Band'], data['Lower_Band'] = compute_bollinger(data)
# Plot setup
fig = make_subplots(rows=3, cols=1, shared_xaxes=True, vertical_spacing=0.02,
subplot_titles=('Stock Price and Moving Averages', 'Volume and Bollinger Bands', 'RSI and MACD'))
# Add traces
fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Close Price'), row=1, col=1)
fig.add_trace(go.Scatter(x=data.index, y=data['MA20'], name='20-Period MA', line=dict(color='green')), row=1, col=1)
fig.add_trace(go.Scatter(x=data.index, y=data['MA50'], name='50-Period MA', line=dict(color='blue')), row=1, col=1)
fig.add_trace(go.Scatter(x=data.index, y=data['MA200'], name='200-Period MA', line=dict(color='red')), row=1, col=1)
fig.add_trace(go.Bar(x=data.index, y=data['Volume'], name='Volume'), row=2, col=1)
fig.add_trace(go.Scatter(x=data.index, y=data['Upper_Band'], name='Upper Bollinger Band', line=dict(color='purple')), row=2, col=1)
fig.add_trace(go.Scatter(x=data.index, y=data['Lower_Band'], name='Lower Bollinger Band', line=dict(color='purple')), row=2, col=1)
fig.add_trace(go.Scatter(x=data.index, y=data['RSI'], name='RSI', line=dict(color='orange')), row=3, col=1)
fig.add_trace(go.Scatter(x=data.index, y=data['MACD'], name='MACD', line=dict(color='pink')), row=3, col=1)
fig.add_trace(go.Scatter(x=data.index, y=data['Signal'], name='MACD Signal', line=dict(color='cyan')), row=3, col=1)
# Layout adjustments
fig.update_layout(height=1200, width=800, showlegend=True)
fig.update_yaxes(title_text="Price", row=1, col=1)
fig.update_yaxes(title_text="Volume", row=2, col=1)
fig.update_yaxes(title_text="Index Value", row=3, col=1)
# Display the chart with a download button
st.plotly_chart(fig)
fig.write_html("stock_analysis_chart.html")
st.markdown("[Download Chart](stock_analysis_chart.html)")
|