Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Required imports
|
| 2 |
+
import yfinance as yf
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from scipy.signal import find_peaks
|
| 5 |
+
import plotly.graph_objects as go
|
| 6 |
+
import streamlit as st
|
| 7 |
+
|
| 8 |
+
# Streamlit UI - Introduction and How to Use the App
|
| 9 |
+
st.markdown("""
|
| 10 |
+
# Stock Analysis Tool
|
| 11 |
+
|
| 12 |
+
Welcome to our stock analysis application, lovingly crafted to provide traders and investors with key insights into market trends, moving averages, and Fibonacci retracement levels.
|
| 13 |
+
|
| 14 |
+
### What It Does
|
| 15 |
+
This tool allows you to:
|
| 16 |
+
- View a stock's price movement over time.
|
| 17 |
+
- Analyze moving averages (20, 50, 200 periods) to identify trends.
|
| 18 |
+
- Utilize Fibonacci retracement levels to spot potential support and resistance areas.
|
| 19 |
+
|
| 20 |
+
### How to Use
|
| 21 |
+
1. Enter a stock symbol in the sidebar.
|
| 22 |
+
2. Choose your desired analysis period.
|
| 23 |
+
3. Review the plotted stock data and moving averages.
|
| 24 |
+
4. Use the Fibonacci levels to identify support and resistance areas.
|
| 25 |
+
|
| 26 |
+
**Pro Tip from a Seasoned Trader**: *Buy at support levels, and sell at resistance levels.* This strategy leverages the concept that prices tend to bounce off these key levels, offering opportunities for entry and exit.
|
| 27 |
+
|
| 28 |
+
Let's dive into the analysis!
|
| 29 |
+
""")
|
| 30 |
+
|
| 31 |
+
# User Inputs
|
| 32 |
+
sidebar = st.sidebar
|
| 33 |
+
symbol = sidebar.text_input("Enter stock symbol:", "AAPL")
|
| 34 |
+
period = sidebar.selectbox("Select period:", ["1mo", "3mo", "6mo", "1y", "2y", "5y", "10y", "ytd", "max"])
|
| 35 |
+
|
| 36 |
+
# Download stock data
|
| 37 |
+
data = yf.download(symbol, period=period)
|
| 38 |
+
|
| 39 |
+
# Calculate Moving Averages
|
| 40 |
+
data['MA50'] = data['Close'].rolling(window=50).mean()
|
| 41 |
+
data['MA200'] = data['Close'].rolling(window=200).mean()
|
| 42 |
+
data['MA20'] = data['Close'].rolling(window=20).mean()
|
| 43 |
+
|
| 44 |
+
# Detecting significant peaks and troughs
|
| 45 |
+
peaks, _ = find_peaks(data['Close'], prominence=1) # Adjust prominence as needed
|
| 46 |
+
troughs, _ = find_peaks(-data['Close'], prominence=1) # Finding troughs by inverting the data
|
| 47 |
+
|
| 48 |
+
# Plot setup
|
| 49 |
+
fig = go.Figure()
|
| 50 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Close Price', line=dict(color='black')))
|
| 51 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['MA50'], name='50-Period MA', line=dict(color='blue')))
|
| 52 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['MA200'], name='200-Period MA', line=dict(color='red')))
|
| 53 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['MA20'], name='20-Period MA', line=dict(color='green')))
|
| 54 |
+
|
| 55 |
+
# Handling Fibonacci Levels
|
| 56 |
+
if len(peaks) == 0 or len(troughs) == 0:
|
| 57 |
+
fig.add_annotation(xref='paper', yref='paper', x=0.5, y=0.5, text="No significant peaks or troughs detected for Fibonacci analysis", showarrow=False, font=dict(size=20, color="red"))
|
| 58 |
+
else:
|
| 59 |
+
high_price = data.iloc[peaks]['Close'].max()
|
| 60 |
+
low_price = data.iloc[troughs]['Close'].min()
|
| 61 |
+
|
| 62 |
+
# Fibonacci Levels
|
| 63 |
+
fib_levels = [0, 0.236, 0.382, 0.5, 0.618, 0.786, 1]
|
| 64 |
+
price_diff = high_price - low_price
|
| 65 |
+
for i, level in enumerate(fib_levels):
|
| 66 |
+
data[f'Fib_Level_{i}'] = high_price - price_diff * level
|
| 67 |
+
|
| 68 |
+
# Add Fibonacci Levels to the plot
|
| 69 |
+
for i in range(7):
|
| 70 |
+
fig.add_trace(go.Scatter(x=data.index, y=[data[f'Fib_Level_{i}'][0]]*len(data), name=f'Fib Level {fib_levels[i]*100}%', line=dict(dash='dot')))
|
| 71 |
+
|
| 72 |
+
# Display the chart
|
| 73 |
+
st.plotly_chart(fig)
|