File size: 3,195 Bytes
0f3e2e6
 
 
 
 
 
 
3c46bca
0f3e2e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39f1ef2
 
 
 
 
 
0f3e2e6
 
 
 
39f1ef2
0f3e2e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import yfinance as yf
import pandas as pd
import pandas_ta as ta
import matplotlib.pyplot as plt

# Caching the stock data fetch function to improve performance
@st.cache_data
def fetch_stock_data(ticker, period, interval):
    return yf.download(ticker, period=period, interval=interval)

# Streamlit interface setup
st.title("Enhanced Breakout Trading Analysis Tool")

# User inputs
ticker = st.text_input("Enter Stock Ticker:", value="AAPL")
timeframe_options = ["1d", "1wk", "1mo"]
timeframe = st.selectbox("Select Time Frame:", options=timeframe_options, index=0)
period = st.selectbox("Select Period:", options=["6mo", "1y", "2y"], index=1)
analyze_button = st.button("Analyze Breakout Points")

if analyze_button:
    try:
        # Fetching the stock data with the selected period and interval
        stock_data = fetch_stock_data(ticker, period, timeframe)

        if not stock_data.empty:
            # Calculating technical indicators
            stock_data['SMA50'] = ta.sma(stock_data['Close'], length=50)
            stock_data['SMA200'] = ta.sma(stock_data['Close'], length=200)
            stock_data['RSI'] = ta.rsi(stock_data['Close'], length=14)
            macd = ta.macd(stock_data['Close'])
            stock_data['MACD'] = macd['MACD_12_26_9']
            stock_data['MACDSignal'] = macd['MACDs_12_26_9']

            # Adjusting the section that identifies crossover points to handle NaN values.
            if not stock_data[['SMA50', 'SMA200']].isna().all(axis=None):  # Check if not all values are NaN
                valid_data = stock_data.dropna(subset=['SMA50', 'SMA200'])
                crossover_points = valid_data[(valid_data['SMA50'] > valid_data['SMA200']) & (valid_data['SMA50'].shift(1) < valid_data['SMA200'].shift(1))]
            else:
                crossover_points = pd.DataFrame()  # Empty DataFrame if all SMA50 or SMA200 values are NaN

            # Plotting
            fig, ax = plt.subplots(2, 1, figsize=(10, 12), sharex=True)

            # Price, SMAs, and breakout points
            ax[0].plot(stock_data['Close'], label='Close Price', color='skyblue')
            ax[0].plot(stock_data['SMA50'], label='50-Day SMA', color='green')
            ax[0].plot(stock_data['SMA200'], label='200-Day SMA', color='red')
            ax[0].scatter(crossover_points.index, crossover_points['Close'], color='magenta', label='Breakout Points', zorder=5)
            ax[0].set_title(f"{ticker} Breakout Points Analysis")
            ax[0].legend()

            # RSI and MACD
            ax[1].plot(stock_data['RSI'], label='RSI', color='purple')
            ax[1].axhline(70, linestyle='--', color='grey', alpha=0.5)
            ax[1].axhline(30, linestyle='--', color='grey', alpha=0.5)
            ax[1].plot(stock_data['MACD'], label='MACD', color='blue')
            ax[1].plot(stock_data['MACDSignal'], label='MACD Signal', color='orange')
            ax[1].legend()

            # Display plot in Streamlit
            st.pyplot(fig)
        else:
            st.error("No data found for the specified ticker. Please try another ticker.")

    except Exception as e:
        st.error(f"An error occurred: {e}")