Update app.py
Browse files
app.py
CHANGED
@@ -3,10 +3,15 @@ import yfinance as yf
|
|
3 |
import pandas as pd
|
4 |
import plotly.graph_objs as go
|
5 |
|
|
|
6 |
def fetch_data(ticker, start_date, end_date):
|
7 |
data = yf.download(ticker, start=start_date, end=end_date)
|
8 |
return data
|
9 |
|
|
|
|
|
|
|
|
|
10 |
def calculate_indicators(data):
|
11 |
# Bollinger Bands
|
12 |
data['Middle Band'] = data['Close'].rolling(window=20).mean()
|
@@ -19,11 +24,25 @@ def calculate_indicators(data):
|
|
19 |
|
20 |
return data
|
21 |
|
22 |
-
def identify_signals(data):
|
|
|
23 |
data['Buy Signal'] = ((data['Close'] < data['Lower Band']) & (data['Close'].shift(1) > data['Lower Band'])) | \
|
24 |
((data['Close'] > data['MA5']) & (data['Close'].shift(1) < data['MA5']))
|
25 |
data['Sell Signal'] = ((data['Close'] > data['Upper Band']) & (data['Close'].shift(1) < data['Upper Band'])) | \
|
26 |
((data['Close'] < data['MA5']) & (data['Close'].shift(1) > data['MA5']))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
return data
|
28 |
|
29 |
def plot_data(data):
|
@@ -61,8 +80,9 @@ def main():
|
|
61 |
|
62 |
if st.button("Analyze"):
|
63 |
data = fetch_data(ticker, start_date, end_date)
|
|
|
64 |
data = calculate_indicators(data)
|
65 |
-
data = identify_signals(data)
|
66 |
fig = plot_data(data)
|
67 |
st.plotly_chart(fig, use_container_width=True)
|
68 |
|
|
|
3 |
import pandas as pd
|
4 |
import plotly.graph_objs as go
|
5 |
|
6 |
+
|
7 |
def fetch_data(ticker, start_date, end_date):
|
8 |
data = yf.download(ticker, start=start_date, end=end_date)
|
9 |
return data
|
10 |
|
11 |
+
def fetch_weekly_data(ticker, start_date, end_date):
|
12 |
+
data = yf.download(ticker, start=start_date, end=end_date, interval='1wk')
|
13 |
+
return data
|
14 |
+
|
15 |
def calculate_indicators(data):
|
16 |
# Bollinger Bands
|
17 |
data['Middle Band'] = data['Close'].rolling(window=20).mean()
|
|
|
24 |
|
25 |
return data
|
26 |
|
27 |
+
def identify_signals(data, weekly_data):
|
28 |
+
# Calculate Buy and Sell signals on daily data
|
29 |
data['Buy Signal'] = ((data['Close'] < data['Lower Band']) & (data['Close'].shift(1) > data['Lower Band'])) | \
|
30 |
((data['Close'] > data['MA5']) & (data['Close'].shift(1) < data['MA5']))
|
31 |
data['Sell Signal'] = ((data['Close'] > data['Upper Band']) & (data['Close'].shift(1) < data['Upper Band'])) | \
|
32 |
((data['Close'] < data['MA5']) & (data['Close'].shift(1) > data['MA5']))
|
33 |
+
|
34 |
+
# Filter signals by volume
|
35 |
+
avg_volume = data['Volume'].rolling(window=20).mean()
|
36 |
+
data['Buy Signal'] = data['Buy Signal'] & (data['Volume'] > avg_volume)
|
37 |
+
data['Sell Signal'] = data['Sell Signal'] & (data['Volume'] > avg_volume)
|
38 |
+
|
39 |
+
# Confirm signals with weekly data
|
40 |
+
weekly_data = calculate_indicators(weekly_data)
|
41 |
+
data['Weekly Buy Signal'] = weekly_data['Buy Signal'].reindex(data.index, method='ffill')
|
42 |
+
data['Weekly Sell Signal'] = weekly_data['Sell Signal'].reindex(data.index, method='ffill')
|
43 |
+
data['Buy Signal'] = data['Buy Signal'] & data['Weekly Buy Signal']
|
44 |
+
data['Sell Signal'] = data['Sell Signal'] & data['Weekly Sell Signal']
|
45 |
+
|
46 |
return data
|
47 |
|
48 |
def plot_data(data):
|
|
|
80 |
|
81 |
if st.button("Analyze"):
|
82 |
data = fetch_data(ticker, start_date, end_date)
|
83 |
+
weekly_data = fetch_weekly_data(ticker, start_date, end_date)
|
84 |
data = calculate_indicators(data)
|
85 |
+
data = identify_signals(data, weekly_data)
|
86 |
fig = plot_data(data)
|
87 |
st.plotly_chart(fig, use_container_width=True)
|
88 |
|