Spaces:
Runtime error
Runtime error
Create app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283import streamlit as stimport yfinance as yfimport pandas as pdimport pandas_ta as taimport matplotlib.pyplot as plt
# Caching the stock data fetch function to improve performance@st.cache(suppress_st_warning=True, allow_output_mutation=True)def fetch_stock_data(ticker, period, interval): """ Fetches stock data for the given ticker, period, and interval. """ return yf.download(ticker, period=period, interval=interval)
# Streamlit interface setupst.title("Enhanced Breakout Trading Analysis Tool")
# User inputsticker = st.text_input("Enter Stock Ticker:", value="AAPL")
# Updated to include a 1-hour time frame optiontimeframe_options = ["1d", "1wk", "1mo", "1h"]timeframe = st.selectbox("Select Time Frame:", options=timeframe_options, index=3)
# Updated to include a 1-month period optionperiod_options = ["1mo", "3mo", "6mo", "1y", "2y"]period = st.selectbox("Select Period:", options=period_options, index=0)
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['SMA9'] = ta.sma(stock_data['Close'], length=9) stock_data['SMA20'] = ta.sma(stock_data['Close'], length=20) 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']
# Identifying breakout points for all three logics crossover_points_logic1 = stock_data[(stock_data['SMA9'] > stock_data['SMA20']) & (stock_data['SMA9'].shift(1) < stock_data['SMA20'].shift(1))] crossover_points_logic2 = stock_data[(stock_data['SMA20'] > stock_data['SMA50']) & (stock_data['SMA20'].shift(1) < stock_data['SMA50'].shift(1))] crossover_points_original = stock_data[(stock_data['SMA50'] > stock_data['SMA200']) & (stock_data['SMA50'].shift(1) < stock_data['SMA200'].shift(1))]
# Plotting fig, ax = plt.subplots(2, 1, figsize=(10, 12), sharex=True)
# Price, SMAs, and breakout points for all logics ax[0].plot(stock_data['Close'], label='Close Price', color='skyblue') ax[0].plot(stock_data['SMA9'], label='9-Day SMA', color='orange') ax[0].plot(stock_data['SMA20'], label='20-Day SMA', color='purple') 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_logic1.index, crossover_points_logic1['Close'], color='gold', label='Logic 1 Breakouts', zorder=5) ax[0].scatter(crossover_points_logic2.index, crossover_points_logic2['Close'], color='violet', label='Logic 2 Breakouts', zorder=5) ax[0].scatter(crossover_points_original.index, crossover_points_original['Close'], color='magenta', label='Original Logic Breakouts', 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, label='Overbought') ax[1].axhline(30, linestyle='--', color='grey', alpha=0.5, label='Oversold') ax[1].plot(stock_data['MACD'], label='MACD', color='blue') ax[1].plot(stock_data['MACDSignal'], label='MACD Signal', color='orange') ax[1].set_title('RSI & MACD') 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}") app.pyAdded+83