Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 setup
|
9 |
+
sidebar = st.sidebar
|
10 |
+
symbol = sidebar.text_input("Enter stock symbol:", "AAPL")
|
11 |
+
period = sidebar.selectbox("Select period:", ["1mo", "3mo", "6mo", "1y", "2y", "5y", "10y", "ytd", "max"])
|
12 |
+
|
13 |
+
# Download stock data
|
14 |
+
data = yf.download(symbol, period=period)
|
15 |
+
|
16 |
+
# Calculate Moving Averages
|
17 |
+
data['MA50'] = data['Close'].rolling(window=50).mean()
|
18 |
+
data['MA200'] = data['Close'].rolling(window=200).mean()
|
19 |
+
data['MA20'] = data['Close'].rolling(window=20).mean()
|
20 |
+
|
21 |
+
# Detecting significant peaks and troughs
|
22 |
+
peaks, _ = find_peaks(data['Close'], prominence=1) # Adjust prominence as needed
|
23 |
+
troughs, _ = find_peaks(-data['Close'], prominence=1) # Finding troughs by inverting the data
|
24 |
+
|
25 |
+
# Ensure there are peaks and troughs detected
|
26 |
+
if len(peaks) == 0 or len(troughs) == 0:
|
27 |
+
st.write("No significant peaks or troughs detected in the selected period.")
|
28 |
+
else:
|
29 |
+
# Using the most significant peak and trough for Fibonacci levels
|
30 |
+
high_price = data.iloc[peaks]['Close'].max()
|
31 |
+
low_price = data.iloc[troughs]['Close'].min()
|
32 |
+
|
33 |
+
# Calculate Fibonacci Levels
|
34 |
+
fib_levels = [0, 0.236, 0.382, 0.5, 0.618, 0.786, 1]
|
35 |
+
price_diff = high_price - low_price
|
36 |
+
for i, level in enumerate(fib_levels):
|
37 |
+
data[f'Fib_Level_{i}'] = high_price - price_diff * level
|
38 |
+
|
39 |
+
# Plotting
|
40 |
+
fig = go.Figure()
|
41 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Close Price', line=dict(color='black')))
|
42 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['MA50'], name='50-Period MA', line=dict(color='blue')))
|
43 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['MA200'], name='200-Period MA', line=dict(color='red')))
|
44 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['MA20'], name='20-Period MA', line=dict(color='green')))
|
45 |
+
|
46 |
+
# Add traces for Fibonacci Levels
|
47 |
+
for i in range(7):
|
48 |
+
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')))
|
49 |
+
|
50 |
+
# Display the chart
|
51 |
+
st.plotly_chart(fig)
|