Update indicators.py
Browse files- indicators.py +13 -38
indicators.py
CHANGED
@@ -2,7 +2,7 @@ import pandas as pd
|
|
2 |
import pandas_ta as ta
|
3 |
import plotly.graph_objects as go
|
4 |
import streamlit as st
|
5 |
-
from ai_insights import get_ai_insight
|
6 |
|
7 |
def calculate_indicators(data, ma_window, rsi_window, macd_fast, macd_slow, macd_signal):
|
8 |
"""
|
@@ -21,27 +21,27 @@ def calculate_indicators(data, ma_window, rsi_window, macd_fast, macd_slow, macd
|
|
21 |
pandas.DataFrame: Data with calculated indicators.
|
22 |
"""
|
23 |
|
24 |
-
#
|
25 |
data['MA'] = data['Close'].rolling(window=ma_window).mean()
|
26 |
|
27 |
-
#
|
28 |
data['stddev'] = data['Close'].rolling(window=ma_window).std()
|
29 |
data['Upper_BB'] = data['MA'] + (2 * data['stddev'])
|
30 |
data['Lower_BB'] = data['MA'] - (2 * data['stddev'])
|
31 |
|
32 |
-
#
|
33 |
delta = data['Close'].diff(1)
|
34 |
gain = (delta.where(delta > 0, 0)).rolling(window=rsi_window).mean()
|
35 |
loss = (-delta.where(delta < 0, 0)).rolling(window=rsi_window).mean()
|
36 |
rs = gain / loss
|
37 |
data['RSI'] = 100 - (100 / (1 + rs))
|
38 |
|
39 |
-
#
|
40 |
data['MACD'] = data['Close'].ewm(span=macd_fast, adjust=False).mean() - data['Close'].ewm(span=macd_slow, adjust=False).mean()
|
41 |
data['MACD_Signal'] = data['MACD'].ewm(span=macd_signal, adjust=False).mean()
|
42 |
data['MACD_Hist'] = data['MACD'] - data['MACD_Signal']
|
43 |
|
44 |
-
#
|
45 |
fig_candlestick = go.Figure()
|
46 |
fig_candlestick.add_trace(go.Candlestick(
|
47 |
x=data.index,
|
@@ -56,56 +56,31 @@ def calculate_indicators(data, ma_window, rsi_window, macd_fast, macd_slow, macd
|
|
56 |
fig_candlestick.add_trace(go.Scatter(x=data.index, y=data['Lower_BB'], mode='lines', name="Lower BB", line=dict(color="purple", dash='dot')))
|
57 |
fig_candlestick.update_layout(title="π Stock Price with Bollinger Bands & MA", xaxis_rangeslider_visible=True, template="plotly_dark", height=500, width=900)
|
58 |
|
59 |
-
#
|
60 |
fig_rsi = go.Figure()
|
61 |
fig_rsi.add_trace(go.Scatter(x=data.index, y=data['RSI'], mode='lines', name="RSI", line=dict(color="orange")))
|
62 |
fig_rsi.add_trace(go.Scatter(x=data.index, y=[70]*len(data), mode='lines', name="Overbought (70)", line=dict(color="red", dash='dash')))
|
63 |
fig_rsi.add_trace(go.Scatter(x=data.index, y=[30]*len(data), mode='lines', name="Oversold (30)", line=dict(color="green", dash='dash')))
|
64 |
fig_rsi.update_layout(title="π Relative Strength Index (RSI)", xaxis_rangeslider_visible=False, template="plotly_dark", height=500, width=900)
|
65 |
|
66 |
-
#
|
67 |
fig_macd = go.Figure()
|
68 |
fig_macd.add_trace(go.Scatter(x=data.index, y=data['MACD'], mode='lines', name="MACD", line=dict(color="blue")))
|
69 |
fig_macd.add_trace(go.Scatter(x=data.index, y=data['MACD_Signal'], mode='lines', name="MACD Signal", line=dict(color="red")))
|
70 |
fig_macd.add_trace(go.Bar(x=data.index, y=data['MACD_Hist'], name="MACD Histogram", marker_color="purple"))
|
71 |
fig_macd.update_layout(title="π MACD Indicator", xaxis_rangeslider_visible=False, template="plotly_dark", height=500, width=900)
|
72 |
|
73 |
-
#
|
74 |
st.subheader("π Stock Price with Bollinger Bands & Moving Averages")
|
75 |
-
st.plotly_chart(fig_candlestick, use_container_width=True) #
|
76 |
|
77 |
st.subheader("π Relative Strength Index (RSI)")
|
78 |
-
st.plotly_chart(fig_rsi, use_container_width=True) #
|
79 |
|
80 |
st.subheader("π MACD Indicator")
|
81 |
-
st.plotly_chart(fig_macd, use_container_width=True) #
|
82 |
|
83 |
|
84 |
print(data.columns)
|
85 |
|
86 |
-
|
87 |
-
# π **Display Charts and AI Insights in a 2x2 Grid**
|
88 |
-
col1, col2 = st.columns((7, 1.5)) # 2/3 chart, 1/3 AI insight
|
89 |
-
with col1:
|
90 |
-
st.plotly_chart(fig_candlestick, use_container_width=True) # π Stock Price
|
91 |
-
with col2:
|
92 |
-
st.subheader("π‘ AI Insight for Moving Average")
|
93 |
-
st.write(ma_insight)
|
94 |
-
st.subheader("π‘ AI Insight for Bollinger Bands")
|
95 |
-
st.write(bb_insight)
|
96 |
-
|
97 |
-
col3, col4 = st.columns((7, 1.5))
|
98 |
-
with col3:
|
99 |
-
st.plotly_chart(fig_rsi, use_container_width=True) # π RSI
|
100 |
-
with col4:
|
101 |
-
st.subheader("π‘ AI Insight for RSI")
|
102 |
-
st.write(rsi_insight)
|
103 |
-
|
104 |
-
col5, col6 = st.columns((7, 1.5))
|
105 |
-
with col5:
|
106 |
-
st.plotly_chart(fig_macd, use_container_width=True) # π MACD
|
107 |
-
with col6:
|
108 |
-
st.subheader("π‘ AI Insight for MACD")
|
109 |
-
st.write(macd_insight)
|
110 |
-
'''
|
111 |
-
return data # β
Return only data, all charts & AI insights are displayed in Streamlit
|
|
|
2 |
import pandas_ta as ta
|
3 |
import plotly.graph_objects as go
|
4 |
import streamlit as st
|
5 |
+
from ai_insights import get_ai_insight
|
6 |
|
7 |
def calculate_indicators(data, ma_window, rsi_window, macd_fast, macd_slow, macd_signal):
|
8 |
"""
|
|
|
21 |
pandas.DataFrame: Data with calculated indicators.
|
22 |
"""
|
23 |
|
24 |
+
# Calculate Moving Average (MA)
|
25 |
data['MA'] = data['Close'].rolling(window=ma_window).mean()
|
26 |
|
27 |
+
# Calculate Bollinger Bands
|
28 |
data['stddev'] = data['Close'].rolling(window=ma_window).std()
|
29 |
data['Upper_BB'] = data['MA'] + (2 * data['stddev'])
|
30 |
data['Lower_BB'] = data['MA'] - (2 * data['stddev'])
|
31 |
|
32 |
+
# Calculate RSI
|
33 |
delta = data['Close'].diff(1)
|
34 |
gain = (delta.where(delta > 0, 0)).rolling(window=rsi_window).mean()
|
35 |
loss = (-delta.where(delta < 0, 0)).rolling(window=rsi_window).mean()
|
36 |
rs = gain / loss
|
37 |
data['RSI'] = 100 - (100 / (1 + rs))
|
38 |
|
39 |
+
# Calculate MACD
|
40 |
data['MACD'] = data['Close'].ewm(span=macd_fast, adjust=False).mean() - data['Close'].ewm(span=macd_slow, adjust=False).mean()
|
41 |
data['MACD_Signal'] = data['MACD'].ewm(span=macd_signal, adjust=False).mean()
|
42 |
data['MACD_Hist'] = data['MACD'] - data['MACD_Signal']
|
43 |
|
44 |
+
# Create Candlestick Chart with Bollinger Bands & MA
|
45 |
fig_candlestick = go.Figure()
|
46 |
fig_candlestick.add_trace(go.Candlestick(
|
47 |
x=data.index,
|
|
|
56 |
fig_candlestick.add_trace(go.Scatter(x=data.index, y=data['Lower_BB'], mode='lines', name="Lower BB", line=dict(color="purple", dash='dot')))
|
57 |
fig_candlestick.update_layout(title="π Stock Price with Bollinger Bands & MA", xaxis_rangeslider_visible=True, template="plotly_dark", height=500, width=900)
|
58 |
|
59 |
+
# Create RSI Chart
|
60 |
fig_rsi = go.Figure()
|
61 |
fig_rsi.add_trace(go.Scatter(x=data.index, y=data['RSI'], mode='lines', name="RSI", line=dict(color="orange")))
|
62 |
fig_rsi.add_trace(go.Scatter(x=data.index, y=[70]*len(data), mode='lines', name="Overbought (70)", line=dict(color="red", dash='dash')))
|
63 |
fig_rsi.add_trace(go.Scatter(x=data.index, y=[30]*len(data), mode='lines', name="Oversold (30)", line=dict(color="green", dash='dash')))
|
64 |
fig_rsi.update_layout(title="π Relative Strength Index (RSI)", xaxis_rangeslider_visible=False, template="plotly_dark", height=500, width=900)
|
65 |
|
66 |
+
# Create MACD Chart
|
67 |
fig_macd = go.Figure()
|
68 |
fig_macd.add_trace(go.Scatter(x=data.index, y=data['MACD'], mode='lines', name="MACD", line=dict(color="blue")))
|
69 |
fig_macd.add_trace(go.Scatter(x=data.index, y=data['MACD_Signal'], mode='lines', name="MACD Signal", line=dict(color="red")))
|
70 |
fig_macd.add_trace(go.Bar(x=data.index, y=data['MACD_Hist'], name="MACD Histogram", marker_color="purple"))
|
71 |
fig_macd.update_layout(title="π MACD Indicator", xaxis_rangeslider_visible=False, template="plotly_dark", height=500, width=900)
|
72 |
|
73 |
+
# Display Charts One Below Another**
|
74 |
st.subheader("π Stock Price with Bollinger Bands & Moving Averages")
|
75 |
+
st.plotly_chart(fig_candlestick, use_container_width=True) # Stock Chart
|
76 |
|
77 |
st.subheader("π Relative Strength Index (RSI)")
|
78 |
+
st.plotly_chart(fig_rsi, use_container_width=True) # RSI Chart
|
79 |
|
80 |
st.subheader("π MACD Indicator")
|
81 |
+
st.plotly_chart(fig_macd, use_container_width=True) # MACD Chart
|
82 |
|
83 |
|
84 |
print(data.columns)
|
85 |
|
86 |
+
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|