Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import yfinance as yf
|
3 |
+
import pandas as pd
|
4 |
+
import pandas_ta as ta
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
|
7 |
+
# Caching the stock data fetch function to improve performance
|
8 |
+
@st.cache
|
9 |
+
def fetch_stock_data(ticker, period, interval):
|
10 |
+
return yf.download(ticker, period=period, interval=interval)
|
11 |
+
|
12 |
+
# Streamlit interface setup
|
13 |
+
st.title("Enhanced Breakout Trading Analysis Tool")
|
14 |
+
|
15 |
+
# User inputs
|
16 |
+
ticker = st.text_input("Enter Stock Ticker:", value="AAPL")
|
17 |
+
timeframe_options = ["1d", "1wk", "1mo"]
|
18 |
+
timeframe = st.selectbox("Select Time Frame:", options=timeframe_options, index=0)
|
19 |
+
period = st.selectbox("Select Period:", options=["6mo", "1y", "2y"], index=1)
|
20 |
+
analyze_button = st.button("Analyze Breakout Points")
|
21 |
+
|
22 |
+
if analyze_button:
|
23 |
+
try:
|
24 |
+
# Fetching the stock data with the selected period and interval
|
25 |
+
stock_data = fetch_stock_data(ticker, period, timeframe)
|
26 |
+
|
27 |
+
if not stock_data.empty:
|
28 |
+
# Calculating technical indicators
|
29 |
+
stock_data['SMA50'] = ta.sma(stock_data['Close'], length=50)
|
30 |
+
stock_data['SMA200'] = ta.sma(stock_data['Close'], length=200)
|
31 |
+
stock_data['RSI'] = ta.rsi(stock_data['Close'], length=14)
|
32 |
+
macd = ta.macd(stock_data['Close'])
|
33 |
+
stock_data['MACD'] = macd['MACD_12_26_9']
|
34 |
+
stock_data['MACDSignal'] = macd['MACDs_12_26_9']
|
35 |
+
|
36 |
+
# Identifying SMA crossover points as breakout signals
|
37 |
+
crossover_points = stock_data[(stock_data['SMA50'] > stock_data['SMA200']) & (stock_data['SMA50'].shift(1) < stock_data['SMA200'].shift(1))]
|
38 |
+
|
39 |
+
# Plotting
|
40 |
+
fig, ax = plt.subplots(2, 1, figsize=(10, 12), sharex=True)
|
41 |
+
|
42 |
+
# Price and SMAs
|
43 |
+
ax[0].plot(stock_data['Close'], label='Close Price', color='skyblue')
|
44 |
+
ax[0].plot(stock_data['SMA50'], label='50-Day SMA', color='green')
|
45 |
+
ax[0].plot(stock_data['SMA200'], label='200-Day SMA', color='red')
|
46 |
+
ax[0].scatter(crossover_points.index, crossover_points['Close'], color='magenta', label='Breakout Points', zorder=5)
|
47 |
+
ax[0].set_title(f"{ticker} Breakout Points Analysis")
|
48 |
+
ax[0].legend()
|
49 |
+
|
50 |
+
# RSI and MACD
|
51 |
+
ax[1].plot(stock_data['RSI'], label='RSI', color='purple')
|
52 |
+
ax[1].axhline(70, linestyle='--', color='grey', alpha=0.5)
|
53 |
+
ax[1].axhline(30, linestyle='--', color='grey', alpha=0.5)
|
54 |
+
ax[1].plot(stock_data['MACD'], label='MACD', color='blue')
|
55 |
+
ax[1].plot(stock_data['MACDSignal'], label='MACD Signal', color='orange')
|
56 |
+
ax[1].legend()
|
57 |
+
|
58 |
+
# Display plot in Streamlit
|
59 |
+
st.pyplot(fig)
|
60 |
+
else:
|
61 |
+
st.error("No data found for the specified ticker. Please try another ticker.")
|
62 |
+
|
63 |
+
except Exception as e:
|
64 |
+
st.error(f"An error occurred: {e}")
|
65 |
+
|