netflypsb commited on
Commit
1c84c73
·
verified ·
1 Parent(s): 8ea0122

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_data
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 with Multiple Logic")
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['SMA9'] = ta.sma(stock_data['Close'], length=9)
30
+ stock_data['SMA20'] = ta.sma(stock_data['Close'], length=20)
31
+ stock_data['SMA50'] = ta.sma(stock_data['Close'], length=50)
32
+ stock_data['SMA200'] = ta.sma(stock_data['Close'], length=200)
33
+ stock_data['RSI'] = ta.rsi(stock_data['Close'], length=14)
34
+ macd = ta.macd(stock_data['Close'])
35
+ stock_data['MACD'] = macd['MACD_12_26_9']
36
+ stock_data['MACDSignal'] = macd['MACDs_12_26_9']
37
+
38
+ # Identifying breakout points for all three logics
39
+ crossover_points_logic1 = stock_data[(stock_data['SMA9'] > stock_data['SMA20']) & (stock_data['SMA9'].shift(1) < stock_data['SMA20'].shift(1))]
40
+ crossover_points_logic2 = stock_data[(stock_data['SMA20'] > stock_data['SMA50']) & (stock_data['SMA20'].shift(1) < stock_data['SMA50'].shift(1))]
41
+ crossover_points_original = stock_data[(stock_data['SMA50'] > stock_data['SMA200']) & (stock_data['SMA50'].shift(1) < stock_data['SMA200'].shift(1))]
42
+
43
+ # Plotting
44
+ fig, ax = plt.subplots(2, 1, figsize=(10, 12), sharex=True)
45
+
46
+ # Price, SMAs, and breakout points for all logics
47
+ ax[0].plot(stock_data['Close'], label='Close Price', color='skyblue')
48
+ ax[0].plot(stock_data['SMA9'], label='9-Day SMA', color='orange')
49
+ ax[0].plot(stock_data['SMA20'], label='20-Day SMA', color='purple')
50
+ ax[0].plot(stock_data['SMA50'], label='50-Day SMA', color='green')
51
+ ax[0].plot(stock_data['SMA200'], label='200-Day SMA', color='red')
52
+ ax[0].scatter(crossover_points_logic1.index, crossover_points_logic1['Close'], color='gold', label='Logic 1 Breakouts', zorder=5)
53
+ ax[0].scatter(crossover_points_logic2.index, crossover_points_logic2['Close'], color='violet', label='Logic 2 Breakouts', zorder=5)
54
+ ax[0].scatter(crossover_points_original.index, crossover_points_original['Close'], color='magenta', label='Original Logic Breakouts', zorder=5)
55
+ ax[0].set_title(f"{ticker} Breakout Points Analysis")
56
+ ax[0].legend()
57
+
58
+ ## RSI and MACD
59
+ ax[1].plot(stock_data['RSI'], label='RSI', color='purple')
60
+ ax[1].axhline(70, linestyle='--', color='grey', alpha=0.5, label='Overbought')
61
+ ax[1].axhline(30, linestyle='--', color='grey', alpha=0.5, label='Oversold')
62
+ ax[1].plot(stock_data['MACD'], label='MACD', color='blue')
63
+ ax[1].plot(stock_data['MACDSignal'], label='MACD Signal', color='orange')
64
+ ax[1].set_title('RSI & MACD')
65
+ ax[1].legend()
66
+
67
+ # Display plot in Streamlit
68
+ st.pyplot(fig)
69
+ else:
70
+ st.error("No data found for the specified ticker. Please try another ticker.")
71
+
72
+ except Exception as e:
73
+ st.error(f"An error occurred: {e}")
74
+