Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import yfinance as yf
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
from datetime import datetime
|
6 |
+
from patterns import Doji, DragonflyDoji, Engulfing, GravestoneDoji, Hammer, HangingMan, Harami, InvertedHammer, LongleggedDoji, ShootingStar, ThreeWhiteSoldiers, ThreeBlackCrows
|
7 |
+
|
8 |
+
def fetch_live_data(ticker, interval='1m', period='1d'):
|
9 |
+
# Fetch live data using yfinance
|
10 |
+
df = yf.download(ticker, interval=interval, period=period)
|
11 |
+
|
12 |
+
# Convert DatetimeIndex to a column
|
13 |
+
df.reset_index(inplace=True)
|
14 |
+
|
15 |
+
return df
|
16 |
+
|
17 |
+
def detect_candlestick_patterns(df):
|
18 |
+
# Instantiate candlestick pattern detectors
|
19 |
+
doji = Doji(df)
|
20 |
+
dragonfly_doji = DragonflyDoji(df)
|
21 |
+
engulfing = Engulfing(df)
|
22 |
+
gravestone_doji = GravestoneDoji(df)
|
23 |
+
hammer = Hammer(df)
|
24 |
+
hanging_man = HangingMan(df)
|
25 |
+
harami = Harami(df)
|
26 |
+
inverted_hammer = InvertedHammer(df)
|
27 |
+
long_legged_doji = LongleggedDoji(df)
|
28 |
+
shooting_star = ShootingStar(df)
|
29 |
+
three_white_soldiers = ThreeWhiteSoldiers(df)
|
30 |
+
three_black_crows = ThreeBlackCrows(df)
|
31 |
+
|
32 |
+
# Compute pattern detections
|
33 |
+
df = doji.compute_pattern()
|
34 |
+
df = dragonfly_doji.compute_pattern()
|
35 |
+
df = engulfing.compute_pattern()
|
36 |
+
df = gravestone_doji.compute_pattern()
|
37 |
+
df = hammer.compute_pattern()
|
38 |
+
df = hanging_man.compute_pattern()
|
39 |
+
df = harami.compute_pattern()
|
40 |
+
df = inverted_hammer.compute_pattern()
|
41 |
+
df = long_legged_doji.compute_pattern()
|
42 |
+
df = shooting_star.compute_pattern()
|
43 |
+
df = three_white_soldiers.compute_pattern()
|
44 |
+
df = three_black_crows.compute_pattern()
|
45 |
+
|
46 |
+
return df
|
47 |
+
|
48 |
+
def plot_candlestick(df):
|
49 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
50 |
+
ax.plot(df['Datetime'], df['Close'], label='Close Price', color='black')
|
51 |
+
ax.set_title('Live Stock Price with Candlestick Patterns')
|
52 |
+
ax.set_xlabel('Datetime')
|
53 |
+
ax.set_ylabel('Close Price')
|
54 |
+
|
55 |
+
# Plot candlestick patterns
|
56 |
+
for pattern, color in zip(['Doji', 'Dragonfly Doji', 'Engulfing', 'Gravestone Doji', 'Hammer', 'Hanging Man', 'Harami', 'Inverted Hammer', 'Long-Legged Doji', 'Shooting Star', 'Three White Soldiers', 'Three Black Crows'],
|
57 |
+
['blue', 'green', 'red', 'purple', 'orange', 'brown', 'pink', 'cyan', 'magenta', 'yellow', 'lime', 'darkgrey']):
|
58 |
+
for i, row in df.iterrows():
|
59 |
+
if row[pattern] == 1:
|
60 |
+
ax.annotate(pattern, xy=(row['Datetime'], row['Close']), xytext=(5, 5), textcoords='offset points',
|
61 |
+
arrowprops=dict(facecolor=color, arrowstyle='wedge,tail_width=0.7', lw=0.7))
|
62 |
+
|
63 |
+
st.pyplot(fig)
|
64 |
+
|
65 |
+
def main():
|
66 |
+
st.title('Live Candlestick Pattern Detection App')
|
67 |
+
ticker = st.text_input('Enter Stock Ticker:', 'AAPL')
|
68 |
+
|
69 |
+
if st.button('Detect Candlestick Patterns'):
|
70 |
+
# Fetch live data continuously
|
71 |
+
while True:
|
72 |
+
# Fetch live data
|
73 |
+
live_data = fetch_live_data(ticker)
|
74 |
+
|
75 |
+
# Detect candlestick patterns
|
76 |
+
live_data = detect_candlestick_patterns(live_data)
|
77 |
+
|
78 |
+
# Display candlestick patterns using Matplotlib and Streamlit
|
79 |
+
plot_candlestick(live_data)
|
80 |
+
|
81 |
+
# Display raw data
|
82 |
+
st.write(live_data)
|
83 |
+
|
84 |
+
if __name__ == "__main__":
|
85 |
+
main()
|