Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,9 @@ import streamlit as st
|
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
import matplotlib.gridspec as gridspec
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
# Define pattern functions
|
| 10 |
pattern_funcs = [
|
| 11 |
("Two Crows", talib.CDL2CROWS),
|
|
@@ -72,69 +75,103 @@ pattern_funcs = [
|
|
| 72 |
]
|
| 73 |
|
| 74 |
# Streamlit app setup
|
| 75 |
-
st.title('Candlestick Pattern Detection')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# Sidebar inputs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
st.sidebar.header('Input Parameters')
|
| 79 |
symbol = st.sidebar.text_input('Enter Stock Ticker', 'SIE.DE')
|
| 80 |
start_date = st.sidebar.date_input('Start Date', pd.to_datetime('2020-01-01'))
|
| 81 |
end_date = st.sidebar.date_input('End Date', pd.to_datetime('2024-01-01'))
|
| 82 |
|
| 83 |
if st.sidebar.button('Run'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
data = yf.download(symbol, start=start_date, end=end_date)
|
| 85 |
|
| 86 |
if not data.empty:
|
| 87 |
for pattern_name, pattern_func in pattern_funcs:
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
| 139 |
else:
|
| 140 |
-
st.write("No data found for the given ticker and date range.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
import matplotlib.gridspec as gridspec
|
| 8 |
|
| 9 |
+
# Set page configuration
|
| 10 |
+
st.set_page_config(layout="wide")
|
| 11 |
+
|
| 12 |
# Define pattern functions
|
| 13 |
pattern_funcs = [
|
| 14 |
("Two Crows", talib.CDL2CROWS),
|
|
|
|
| 75 |
]
|
| 76 |
|
| 77 |
# Streamlit app setup
|
| 78 |
+
st.title('Candlestick Pattern Detection App')
|
| 79 |
+
|
| 80 |
+
st.write("""
|
| 81 |
+
This tool automatically detects 60+ candlestick patterns in stock price data.
|
| 82 |
+
You can input the stock ticker (e.g 'AAPL') or crypto pair (e.g.'BTC-USD'), start date, and end date in the sidebar menu to analyze.
|
| 83 |
+
The app will fetch the historical data and highlight detected patterns on the charts.
|
| 84 |
+
The charts display the stock price data along with vertical lines indicating where patterns were found.
|
| 85 |
+
Each pattern chart shows the stock price movement around the last 5 occurrences of the pattern.
|
| 86 |
+
""")
|
| 87 |
|
| 88 |
# Sidebar inputs
|
| 89 |
+
st.sidebar.header('How to Use')
|
| 90 |
+
st.sidebar.write("""
|
| 91 |
+
1. Enter the stock ticker symbol.
|
| 92 |
+
2. Select the start and end dates.
|
| 93 |
+
3. Select the patterns you want to detect.
|
| 94 |
+
4. Click the "Run" button to generate the analysis.
|
| 95 |
+
""")
|
| 96 |
+
|
| 97 |
st.sidebar.header('Input Parameters')
|
| 98 |
symbol = st.sidebar.text_input('Enter Stock Ticker', 'SIE.DE')
|
| 99 |
start_date = st.sidebar.date_input('Start Date', pd.to_datetime('2020-01-01'))
|
| 100 |
end_date = st.sidebar.date_input('End Date', pd.to_datetime('2024-01-01'))
|
| 101 |
|
| 102 |
if st.sidebar.button('Run'):
|
| 103 |
+
run_analysis = True
|
| 104 |
+
else:
|
| 105 |
+
run_analysis = False
|
| 106 |
+
|
| 107 |
+
st.sidebar.header('Candlestick Patterns')
|
| 108 |
+
selected_patterns = {name: st.sidebar.checkbox(name, value=True) for name, func in pattern_funcs}
|
| 109 |
+
|
| 110 |
+
if run_analysis:
|
| 111 |
data = yf.download(symbol, start=start_date, end=end_date)
|
| 112 |
|
| 113 |
if not data.empty:
|
| 114 |
for pattern_name, pattern_func in pattern_funcs:
|
| 115 |
+
if selected_patterns[pattern_name]:
|
| 116 |
+
data[pattern_name] = pattern_func(data['Open'], data['High'], data['Low'], data['Close'])
|
| 117 |
+
pattern_dates = data[data[pattern_name] != 0].index
|
| 118 |
+
|
| 119 |
+
if len(pattern_dates) == 0:
|
| 120 |
+
continue
|
| 121 |
+
|
| 122 |
+
fig = plt.figure(figsize=(20, 10))
|
| 123 |
+
gs = gridspec.GridSpec(2, 4)
|
| 124 |
+
|
| 125 |
+
mc = mpf.make_marketcolors(up='g', down='r', inherit=True)
|
| 126 |
+
custom_style = mpf.make_mpf_style(marketcolors=mc)
|
| 127 |
+
|
| 128 |
+
ax1 = plt.subplot(gs[0, :3])
|
| 129 |
+
data[['Close']].plot(ax=ax1, color='blue')
|
| 130 |
+
for date in pattern_dates:
|
| 131 |
+
ax1.axvline(date, color='red', linestyle='--', label=pattern_name if pattern_name not in [l.get_label() for l in ax1.lines] else "")
|
| 132 |
+
ax1.annotate(date.strftime('%Y-%m-%d'), (date, data['Close'].loc[date]), xytext=(-15, 10 + 20),
|
| 133 |
+
textcoords='offset points', color='red', fontsize=12, rotation=90)
|
| 134 |
+
|
| 135 |
+
window = 5 # Days before and after the pattern
|
| 136 |
+
for i in range(5):
|
| 137 |
+
if len(pattern_dates) > i:
|
| 138 |
+
pattern_date = pattern_dates[-(i + 1)]
|
| 139 |
+
|
| 140 |
+
start_date_window = pattern_date - pd.Timedelta(days=window)
|
| 141 |
+
end_date_window = min(data.index[-1], pattern_date + pd.Timedelta(days=window))
|
| 142 |
+
valid_dates = pd.date_range(start=start_date_window, end=end_date_window).intersection(data.index)
|
| 143 |
+
|
| 144 |
+
subset = data.loc[valid_dates]
|
| 145 |
+
|
| 146 |
+
if i == 0:
|
| 147 |
+
ax = plt.subplot(gs[0, 3])
|
| 148 |
+
else:
|
| 149 |
+
ax = plt.subplot(gs[1, i - 1])
|
| 150 |
+
|
| 151 |
+
mpf.plot(subset, type='candle', ax=ax, volume=False, show_nontrading=False, style=custom_style)
|
| 152 |
+
ax.set_title(f'{pattern_name} Pattern {i + 1} for {symbol}')
|
| 153 |
+
|
| 154 |
+
x_ticks = list(range(0, len(valid_dates), 1))
|
| 155 |
+
x_labels = [date.strftime('%Y-%m-%d') for date in valid_dates]
|
| 156 |
+
ax.set_xticks(x_ticks)
|
| 157 |
+
ax.set_xticklabels(x_labels, rotation=90)
|
| 158 |
+
|
| 159 |
+
ax1.set_title(f"{symbol} Stock Price and {pattern_name} Pattern Detection")
|
| 160 |
+
ax1.legend(loc='best')
|
| 161 |
+
ax1.grid(True)
|
| 162 |
+
ax1.set_xlabel("Date")
|
| 163 |
+
ax1.set_ylabel("Price")
|
| 164 |
+
plt.tight_layout()
|
| 165 |
+
|
| 166 |
+
st.pyplot(fig)
|
| 167 |
else:
|
| 168 |
+
st.write("No data found for the given ticker and date range.")
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
hide_streamlit_style = """
|
| 172 |
+
<style>
|
| 173 |
+
#MainMenu {visibility: hidden;}
|
| 174 |
+
footer {visibility: hidden;}
|
| 175 |
+
</style>
|
| 176 |
+
"""
|
| 177 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|