Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(allow_output_mutation=True)
|
9 |
+
def fetch_stock_data(ticker, period, interval):
|
10 |
+
try:
|
11 |
+
data = yf.download(ticker, period=period, interval=interval)
|
12 |
+
return data
|
13 |
+
except Exception as e:
|
14 |
+
st.error(f"Failed to fetch data: {e}")
|
15 |
+
return pd.DataFrame() # Return an empty DataFrame on failure
|
16 |
+
|
17 |
+
# Streamlit interface setup
|
18 |
+
st.title("Enhanced Breakout Trading Analysis Tool with Multiple Logic")
|
19 |
+
|
20 |
+
# User inputs
|
21 |
+
ticker = st.text_input("Enter Stock Ticker:", value="AAPL")
|
22 |
+
timeframe_options = ["1d", "1wk", "1mo"]
|
23 |
+
timeframe = st.selectbox("Select Time Frame:", options=timeframe_options, index=0)
|
24 |
+
period = st.selectbox("Select Period:", options=["6mo", "1y", "2y"], index=1)
|
25 |
+
analyze_button = st.button("Analyze Breakout Points")
|
26 |
+
|
27 |
+
if analyze_button:
|
28 |
+
stock_data = fetch_stock_data(ticker, period, timeframe)
|
29 |
+
|
30 |
+
if stock_data.empty:
|
31 |
+
st.error("No data found for the specified ticker. Please try another ticker.")
|
32 |
+
else:
|
33 |
+
# Calculating technical indicators
|
34 |
+
stock_data['SMA9'] = ta.sma(stock_data['Close'], length=9)
|
35 |
+
stock_data['SMA20'] = ta.sma(stock_data['Close'], length=20)
|
36 |
+
stock_data['SMA50'] = ta.sma(stock_data['Close'], length=50)
|
37 |
+
stock_data['SMA200'] = ta.sma(stock_data['Close'], length=200)
|
38 |
+
stock_data.dropna(subset=['SMA9', 'SMA20', 'SMA50', 'SMA200'], inplace=True)
|
39 |
+
|
40 |
+
if stock_data.empty:
|
41 |
+
st.error("Insufficient data for the selected period to compute all moving averages. Try a longer period.")
|
42 |
+
else:
|
43 |
+
# Identifying breakout points for all three logics after ensuring data validity
|
44 |
+
crossover_points_logic1 = stock_data[(stock_data['SMA9'] > stock_data['SMA20']) & (stock_data['SMA9'].shift(1) < stock_data['SMA20'].shift(1))]
|
45 |
+
crossover_points_logic2 = stock_data[(stock_data['SMA20'] > stock_data['SMA50']) & (stock_data['SMA20'].shift(1) < stock_data['SMA50'].shift(1))]
|
46 |
+
crossover_points_original = stock_data[(stock_data['SMA50'] > stock_data['SMA200']) & (stock_data['SMA50'].shift(1) < stock_data['SMA200'].shift(1))]
|
47 |
+
|
48 |
+
# Plotting
|
49 |
+
fig, ax = plt.subplots(2, 1, figsize=(10, 12), sharex=True)
|
50 |
+
|
51 |
+
# Price, SMAs, and breakout points for all logics
|
52 |
+
ax[0].plot(stock_data['Close'], label='Close Price', color='skyblue')
|
53 |
+
ax[0].plot(stock_data['SMA9'], label='9-Day SMA', color='orange')
|
54 |
+
ax[0].plot(stock_data['SMA20'], label='20-Day SMA', color='purple')
|
55 |
+
ax[0].plot(stock_data['SMA50'], label='50-Day SMA', color='green')
|
56 |
+
ax[0].plot(stock_data['SMA200'], label='200-Day SMA', color='red')
|
57 |
+
ax[0].scatter(crossover_points_logic1.index, crossover_points_logic1['Close'], color='gold', label='Logic 1 Breakouts', zorder=5)
|
58 |
+
ax[0].scatter(crossover_points_logic2.index, crossover_points_logic2['Close'], color='violet', label='Logic 2 Breakouts', zorder=5)
|
59 |
+
ax[0].scatter(crossover_points_original.index, crossover_points_original['Close'], color='magenta', label='Original Logic Breakouts', zorder=5)
|
60 |
+
ax[0].set_title(f"{ticker} Breakout Points Analysis")
|
61 |
+
ax[0].legend()
|
62 |
+
|
63 |
+
# Additional technical indicators (if necessary) could be plotted here
|
64 |
+
|
65 |
+
# Display plot in Streamlit
|
66 |
+
st.pyplot(fig)
|
67 |
+
|