Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,9 @@ import talib
|
|
| 7 |
from pandas import Series
|
| 8 |
from numpy import average as npAverage, nan as npNaN, log as npLog, power as npPower, sqrt as npSqrt, zeros_like as npZeroslike
|
| 9 |
from pandas_ta.utils import get_offset, verify_series
|
| 10 |
-
from datetime import datetime
|
| 11 |
from matplotlib.dates import date2num
|
|
|
|
| 12 |
|
| 13 |
st.set_page_config(layout="wide")
|
| 14 |
|
|
@@ -32,14 +33,10 @@ with st.sidebar.expander("How to Use:", expanded=False):
|
|
| 32 |
- **Run Analysis**: Click 'Run' to apply and visualize.
|
| 33 |
""")
|
| 34 |
|
| 35 |
-
# Function to fetch data
|
| 36 |
@st.cache_data
|
| 37 |
def get_data(ticker, start_date, end_date):
|
| 38 |
-
data = yf.download(ticker, start=start_date, end=end_date
|
| 39 |
-
if isinstance(data.columns, pd.MultiIndex):
|
| 40 |
-
data.columns = data.columns.get_level_values(0)
|
| 41 |
-
if data.empty:
|
| 42 |
-
raise ValueError(f"No data fetched for {ticker} from {start_date} to {end_date}.")
|
| 43 |
return data
|
| 44 |
|
| 45 |
# Function to create the base plot with the stock price
|
|
@@ -409,6 +406,8 @@ def calculate_mcginley_dynamic(prices, n):
|
|
| 409 |
return MD
|
| 410 |
|
| 411 |
# Function to calculate Anchored Moving Average (AMA)
|
|
|
|
|
|
|
| 412 |
def calculate_AMA(prices, anchor_date, data):
|
| 413 |
# Ensure the anchor_date is a pandas Timestamp
|
| 414 |
anchor_date = pd.to_datetime(anchor_date)
|
|
@@ -481,6 +480,8 @@ if 'data' in st.session_state:
|
|
| 481 |
# Moving average method selection
|
| 482 |
#st.sidebar.header("Moving Average Methods")
|
| 483 |
|
|
|
|
|
|
|
| 484 |
# SMA with tooltip
|
| 485 |
with st.sidebar.expander("Simple Moving Average", expanded=False):
|
| 486 |
use_sma = st.checkbox(
|
|
@@ -1078,6 +1079,21 @@ if 'data' in st.session_state:
|
|
| 1078 |
help="Specify the period (in days) for the FMA."
|
| 1079 |
)
|
| 1080 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1081 |
# Grid toggle with tooltip
|
| 1082 |
show_grid = st.sidebar.checkbox(
|
| 1083 |
"Show Grid",
|
|
@@ -1160,8 +1176,11 @@ if 'data' in st.session_state:
|
|
| 1160 |
st.session_state['use_cma'] = use_cma
|
| 1161 |
st.session_state['use_mcginley_dynamic'] = use_mcginley_dynamic
|
| 1162 |
st.session_state['mcginley_dynamic_period'] = mcginley_dynamic_period
|
|
|
|
|
|
|
| 1163 |
st.session_state['use_fma'] = use_fma
|
| 1164 |
st.session_state['fma_period'] = fma_period
|
|
|
|
| 1165 |
|
| 1166 |
# Start with the base price plot
|
| 1167 |
fig = go.Figure(data=st.session_state['price_plot'].data)
|
|
@@ -1186,6 +1205,12 @@ if 'data' in st.session_state:
|
|
| 1186 |
st.session_state['McGinley_Dynamic'] = calculate_mcginley_dynamic(data['Close'].tolist(), mcginley_dynamic_period)
|
| 1187 |
fig.add_trace(go.Scatter(x=data.index, y=st.session_state['McGinley_Dynamic'], mode='lines', name=f'McGinley Dynamic (n={mcginley_dynamic_period})', line=dict(dash='dash', color='orange')))
|
| 1188 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1189 |
# Add FMA if selected
|
| 1190 |
if use_fma:
|
| 1191 |
st.session_state['FMA'] = filtered_moving_average(data['Close'].values, fma_period)
|
|
@@ -1360,6 +1385,7 @@ if 'data' in st.session_state:
|
|
| 1360 |
st.session_state['PWMA'] = np.concatenate([np.array([np.nan]*(pwma_period-1)), pwma_values])
|
| 1361 |
fig.add_trace(go.Scatter(x=data.index, y=st.session_state['PWMA'], mode='lines', name=f'PWMA (n={pwma_period})', line=dict(dash='dash', color='red')))
|
| 1362 |
|
|
|
|
| 1363 |
# Update layout with grid toggle
|
| 1364 |
fig.update_layout(
|
| 1365 |
title=f'{ticker_symbol} Stock Price with Moving Averages',
|
|
@@ -1378,6 +1404,9 @@ if 'data' in st.session_state:
|
|
| 1378 |
if 'current_fig' in st.session_state:
|
| 1379 |
st.plotly_chart(st.session_state['current_fig'], use_container_width=True)
|
| 1380 |
|
|
|
|
|
|
|
|
|
|
| 1381 |
st.markdown(
|
| 1382 |
"""
|
| 1383 |
<style>
|
|
@@ -1390,10 +1419,11 @@ st.markdown(
|
|
| 1390 |
unsafe_allow_html=True
|
| 1391 |
)
|
| 1392 |
|
|
|
|
| 1393 |
hide_streamlit_style = """
|
| 1394 |
<style>
|
| 1395 |
#MainMenu {visibility: hidden;}
|
| 1396 |
footer {visibility: hidden;}
|
| 1397 |
</style>
|
| 1398 |
"""
|
| 1399 |
-
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
|
|
|
| 7 |
from pandas import Series
|
| 8 |
from numpy import average as npAverage, nan as npNaN, log as npLog, power as npPower, sqrt as npSqrt, zeros_like as npZeroslike
|
| 9 |
from pandas_ta.utils import get_offset, verify_series
|
| 10 |
+
from datetime import datetime
|
| 11 |
from matplotlib.dates import date2num
|
| 12 |
+
from datetime import datetime
|
| 13 |
|
| 14 |
st.set_page_config(layout="wide")
|
| 15 |
|
|
|
|
| 33 |
- **Run Analysis**: Click 'Run' to apply and visualize.
|
| 34 |
""")
|
| 35 |
|
| 36 |
+
# Function to fetch data
|
| 37 |
@st.cache_data
|
| 38 |
def get_data(ticker, start_date, end_date):
|
| 39 |
+
data = yf.download(ticker, start=start_date, end=end_date)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return data
|
| 41 |
|
| 42 |
# Function to create the base plot with the stock price
|
|
|
|
| 406 |
return MD
|
| 407 |
|
| 408 |
# Function to calculate Anchored Moving Average (AMA)
|
| 409 |
+
from datetime import datetime
|
| 410 |
+
|
| 411 |
def calculate_AMA(prices, anchor_date, data):
|
| 412 |
# Ensure the anchor_date is a pandas Timestamp
|
| 413 |
anchor_date = pd.to_datetime(anchor_date)
|
|
|
|
| 480 |
# Moving average method selection
|
| 481 |
#st.sidebar.header("Moving Average Methods")
|
| 482 |
|
| 483 |
+
|
| 484 |
+
|
| 485 |
# SMA with tooltip
|
| 486 |
with st.sidebar.expander("Simple Moving Average", expanded=False):
|
| 487 |
use_sma = st.checkbox(
|
|
|
|
| 1079 |
help="Specify the period (in days) for the FMA."
|
| 1080 |
)
|
| 1081 |
|
| 1082 |
+
|
| 1083 |
+
# Anchored Moving Average (AMA) with tooltip
|
| 1084 |
+
#use_ama = st.sidebar.checkbox(
|
| 1085 |
+
# 'Anchored Moving Average (AMA)',
|
| 1086 |
+
# value=st.session_state.get('use_ama', False),
|
| 1087 |
+
# help="Select to apply Anchored Moving Average (AMA) to the stock price."
|
| 1088 |
+
#)
|
| 1089 |
+
#ama_anchor_date = st.sidebar.date_input(
|
| 1090 |
+
# 'AMA Anchor Date',
|
| 1091 |
+
# value=pd.to_datetime(st.session_state.get('ama_anchor_date', '2021-01-01')),
|
| 1092 |
+
# disabled=not use_ama,
|
| 1093 |
+
# help="Select the anchor date for the AMA."
|
| 1094 |
+
#)
|
| 1095 |
+
|
| 1096 |
+
|
| 1097 |
# Grid toggle with tooltip
|
| 1098 |
show_grid = st.sidebar.checkbox(
|
| 1099 |
"Show Grid",
|
|
|
|
| 1176 |
st.session_state['use_cma'] = use_cma
|
| 1177 |
st.session_state['use_mcginley_dynamic'] = use_mcginley_dynamic
|
| 1178 |
st.session_state['mcginley_dynamic_period'] = mcginley_dynamic_period
|
| 1179 |
+
#st.session_state['use_ama'] = use_ama
|
| 1180 |
+
#st.session_state['ama_anchor_date'] = ama_anchor_date
|
| 1181 |
st.session_state['use_fma'] = use_fma
|
| 1182 |
st.session_state['fma_period'] = fma_period
|
| 1183 |
+
# (Save all previous moving average settings here)
|
| 1184 |
|
| 1185 |
# Start with the base price plot
|
| 1186 |
fig = go.Figure(data=st.session_state['price_plot'].data)
|
|
|
|
| 1205 |
st.session_state['McGinley_Dynamic'] = calculate_mcginley_dynamic(data['Close'].tolist(), mcginley_dynamic_period)
|
| 1206 |
fig.add_trace(go.Scatter(x=data.index, y=st.session_state['McGinley_Dynamic'], mode='lines', name=f'McGinley Dynamic (n={mcginley_dynamic_period})', line=dict(dash='dash', color='orange')))
|
| 1207 |
|
| 1208 |
+
# Add AMA if selected
|
| 1209 |
+
#if use_ama:
|
| 1210 |
+
# st.session_state['AMA'] = calculate_AMA(data['Close'].tolist(), ama_anchor_date, data)
|
| 1211 |
+
# fig.add_trace(go.Scatter(x=data.index, y=st.session_state['AMA'], mode='lines', name=f'Anchored MA (Anchor Date={ama_anchor_date})', line=dict(dash='dash', color='red')))
|
| 1212 |
+
# fig.add_shape(type="line", x0=ama_anchor_date, y0=data['Close'].min(), x1=ama_anchor_date, y1=data['Close'].max(), line=dict(color="blue", width=2, dash="dash"))
|
| 1213 |
+
|
| 1214 |
# Add FMA if selected
|
| 1215 |
if use_fma:
|
| 1216 |
st.session_state['FMA'] = filtered_moving_average(data['Close'].values, fma_period)
|
|
|
|
| 1385 |
st.session_state['PWMA'] = np.concatenate([np.array([np.nan]*(pwma_period-1)), pwma_values])
|
| 1386 |
fig.add_trace(go.Scatter(x=data.index, y=st.session_state['PWMA'], mode='lines', name=f'PWMA (n={pwma_period})', line=dict(dash='dash', color='red')))
|
| 1387 |
|
| 1388 |
+
|
| 1389 |
# Update layout with grid toggle
|
| 1390 |
fig.update_layout(
|
| 1391 |
title=f'{ticker_symbol} Stock Price with Moving Averages',
|
|
|
|
| 1404 |
if 'current_fig' in st.session_state:
|
| 1405 |
st.plotly_chart(st.session_state['current_fig'], use_container_width=True)
|
| 1406 |
|
| 1407 |
+
|
| 1408 |
+
|
| 1409 |
+
|
| 1410 |
st.markdown(
|
| 1411 |
"""
|
| 1412 |
<style>
|
|
|
|
| 1419 |
unsafe_allow_html=True
|
| 1420 |
)
|
| 1421 |
|
| 1422 |
+
|
| 1423 |
hide_streamlit_style = """
|
| 1424 |
<style>
|
| 1425 |
#MainMenu {visibility: hidden;}
|
| 1426 |
footer {visibility: hidden;}
|
| 1427 |
</style>
|
| 1428 |
"""
|
| 1429 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|