From 38e004ebb5c86b13ddfa05437be50c19908c46ce Mon Sep 17 00:00:00 2001 From: Cuttlas Date: Wed, 24 Jul 2024 18:45:10 +0330 Subject: [PATCH] fix selector --- main.py | 13 +---- menu.py | 18 ++++++ pages/simple_chart.py | 11 +--- pages/social_page.py | 11 +--- pages/technical.py | 125 ++++++++++++++++++++++++------------------ 5 files changed, 97 insertions(+), 81 deletions(-) diff --git a/main.py b/main.py index 80a6fe6..310a8c6 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,6 @@ import shutil import streamlit as st -import pandas as pd import altair as alt from login import check_local_token @@ -11,7 +10,7 @@ from pages.helper.quarterly_chart import add_quartely_charts from pages.helper.query import Queries from request import vasahm_query -from menu import add_menu +from menu import add_list_selector, add_menu from text_constant import MAIN_PAGE @@ -47,14 +46,8 @@ def sfmono(): add_menu() st.components.v1.html(MAIN_PAGE, height=60, scrolling=False) -df = pd.read_csv("data.csv").dropna() -list_of_name = df['name'].to_list() -if "stock" in st.query_params: - STOCK_INDEX = list_of_name.index(st.query_params.stock) -else: - STOCK_INDEX = 0 -name = st.sidebar.selectbox("لیست سهام", options = list_of_name, index=STOCK_INDEX) -selected_stock = df.iloc[df.loc[df['name'] == name].index[0]] + +name, selected_stock = add_list_selector() dollar_toggle = st.sidebar.toggle( "نمایش به دلار", help="با فعال کردن این گزینه تمامی مبالغ بر اساس دلار بازمحاسبه می گردد." diff --git a/menu.py b/menu.py index b97c633..4c8f69b 100644 --- a/menu.py +++ b/menu.py @@ -1,5 +1,6 @@ """Creating a custom menu for application""" import streamlit as st +import pandas as pd def add_menu(): """adding a custom menu to app""" @@ -13,3 +14,20 @@ def add_menu(): st.sidebar.page_link("pages/social_page.py", label="اطلاعات رفتاری", icon="🌐") # st.sidebar.page_link("pages/simple_chart.py", label="نمودار ساده ماهانه", icon="📋") st.sidebar.page_link("pages/changelog.py", label="تازه ها", icon="💬") + + +def add_list_selector(): + """create and init list selector""" + if "stock_index" not in st.session_state: + st.session_state.stock_index = 0 + df = pd.read_csv("data.csv").dropna() + list_of_name = df['name'].to_list() + if "stock" in st.query_params: + st.session_state.stock_index = list_of_name.index(st.query_params.stock) + name = st.sidebar.selectbox( + "لیست سهام", + options = list_of_name, + index=st.session_state.stock_index) + st.session_state.stock_index = int((df.loc[df['name'] == name].index[0]).astype(str)) + selected_stock = df.iloc[df.loc[df['name'] == name].index[0]] + return name, selected_stock diff --git a/pages/simple_chart.py b/pages/simple_chart.py index 79b6d4b..300afce 100644 --- a/pages/simple_chart.py +++ b/pages/simple_chart.py @@ -7,7 +7,7 @@ from request import get_stock_monthly # from slider import create_range_slider -from menu import add_menu +from menu import add_list_selector, add_menu @@ -23,14 +23,7 @@ if "ver" in st.session_state: st.sidebar.header(f'Vasahm DashBoard `{st.session_state.ver}`') -df = pd.read_csv("data.csv").dropna() -list_of_name = df['name'].to_list() -if "stock" in st.query_params: - STOCK_INDEX = list_of_name.index(st.query_params.stock) -else: - STOCK_INDEX = 0 - -name = st.selectbox("لیست سهام", options = list_of_name, index=STOCK_INDEX) +name, _ = add_list_selector() dfg = get_stock_monthly(name) diff --git a/pages/social_page.py b/pages/social_page.py index 0a130a9..5307a4e 100644 --- a/pages/social_page.py +++ b/pages/social_page.py @@ -7,7 +7,7 @@ from login import check_local_token from pages.helper.query import Queries from request import vasahm_query -from menu import add_menu +from menu import add_list_selector, add_menu @@ -22,14 +22,7 @@ add_menu() -df = pd.read_csv("data.csv").dropna() -list_of_name = df['name'].to_list() -if "stock" in st.query_params: - STOCK_INDEX = list_of_name.index(st.query_params.stock) -else: - STOCK_INDEX = 0 -name = st.sidebar.selectbox("لیست سهام", options = list_of_name, index=STOCK_INDEX) -selected_stock = df.iloc[df.loc[df['name'] == name].index[0]] +name, _ = add_list_selector() if "ver" in st.session_state: st.sidebar.header(f'Vasahm DashBoard `{st.session_state.ver}`') diff --git a/pages/technical.py b/pages/technical.py index 677330b..72a7d54 100644 --- a/pages/technical.py +++ b/pages/technical.py @@ -1,21 +1,15 @@ """Comapre monthly data in a customized way""" import streamlit as st +import streamlit.components.v1 as components import pandas as pd -import altair as alt from login import check_local_token -import numpy as np from pages.helper.gauge_chart import plot_gauge from pages.helper.query import Queries from pages.helper.ta_metrics import add_ta_metrics -from request import get_stock_monthly -# from slider import create_range_slider -from menu import add_menu -from finta import TA +from menu import add_list_selector, add_menu from request import vasahm_query -import plotly.graph_objs as go -import streamlit.components.v1 as components @@ -31,14 +25,7 @@ if "ver" in st.session_state: st.sidebar.header(f'Vasahm DashBoard `{st.session_state.ver}`') -df = pd.read_csv("data.csv").dropna() -list_of_name = df['name'].to_list() -if "stock" in st.query_params: - STOCK_INDEX = list_of_name.index(st.query_params.stock) -else: - STOCK_INDEX = 0 - -name = st.sidebar.selectbox("لیست سهام", options = list_of_name, index=STOCK_INDEX) +name, _ = add_list_selector() dollar_toggle = st.sidebar.toggle( "نمایش به دلار", help="با فعال کردن این گزینه تمامی مبالغ بر اساس دلار بازمحاسبه می گردد." @@ -55,7 +42,8 @@ st.error(stock_data, icon="🚨") else: components.iframe(f"https://chart.vasahm.com/?name={name}", height=500) - stock_data_history = pd.DataFrame(stock_data, columns=["date","open","high","low","close", "volume"]) + stock_data_history = pd.DataFrame( + stock_data, columns=["date","open","high","low","close", "volume"]) stock_data_history['date'] = pd.to_datetime(stock_data_history['date']) stock_data_history['open'] = stock_data_history['open'].astype(int) stock_data_history['high'] = stock_data_history['high'].astype(int) @@ -68,50 +56,81 @@ stock_data_history.set_index("date", inplace=True) col1, col2 = st.columns(2) - + move_gauge = col1.empty() osi_gauge = col2.empty() - - current_value_moving = 0 - current_value_indicator = 0 + # Pylint: diable=invalid-name + cvm = 0 + # Pylint: diable=invalid-name + cvi = 0 col1, col2 = st.columns(2) col11, col12, col13 = col1.columns(3) - current_value_moving = add_ta_metrics(stock_data_history, [10],"EMA (10)", "10 period EMA", col11, current_value_moving, "EMA") - current_value_moving = add_ta_metrics(stock_data_history, [20], "EMA (20)", "20 period EMA", col12, current_value_moving, "EMA") - current_value_moving = add_ta_metrics(stock_data_history, [30], "EMA (30)", "30 period EMA", col13, current_value_moving, "EMA") - current_value_moving = add_ta_metrics(stock_data_history, [50], "EMA (50)", "50 period EMA", col11, current_value_moving, "EMA") - current_value_moving = add_ta_metrics(stock_data_history, [100], "EMA (100)", "100 period EMA", col12, current_value_moving, "EMA") - current_value_moving = add_ta_metrics(stock_data_history, [200], "EMA (200)", "200 period EMA", col13, current_value_moving, "EMA") - - current_value_moving = add_ta_metrics(stock_data_history, [10],"SMA (10)", "10 period SMA", col11, current_value_moving, "SMA") - current_value_moving = add_ta_metrics(stock_data_history, [20], "SMA (20)", "20 period SMA", col12, current_value_moving, "SMA") - current_value_moving = add_ta_metrics(stock_data_history, [30], "SMA (30)", "30 period SMA", col13, current_value_moving, "SMA") - current_value_moving = add_ta_metrics(stock_data_history, [50], "SMA (50)", "50 period SMA", col11, current_value_moving, "SMA") - current_value_moving = add_ta_metrics(stock_data_history, [100], "SMA (100)", "100 period SMA", col12, current_value_moving, "SMA") - current_value_moving = add_ta_metrics(stock_data_history, [200], "SMA (200)", "200 period SMA", col13, current_value_moving, "SMA") - - current_value_moving = add_ta_metrics(stock_data_history, [20], "VAMA (20)", "20 period VAMA", col11, current_value_moving, "VAMA") - current_value_moving = add_ta_metrics(stock_data_history, [9], "HMA (9)", "9 period HMA.", col12, current_value_moving, "HMA") + cvm = add_ta_metrics( + stock_data_history, [10],"EMA (10)", "10 period EMA", col11, cvm, "EMA") + cvm = add_ta_metrics( + stock_data_history, [20], "EMA (20)", "20 period EMA", col12, cvm, "EMA") + cvm = add_ta_metrics( + stock_data_history, [30], "EMA (30)", "30 period EMA", col13, cvm, "EMA") + cvm = add_ta_metrics( + stock_data_history, [50], "EMA (50)", "50 period EMA", col11, cvm, "EMA") + cvm = add_ta_metrics( + stock_data_history, [100], "EMA (100)", "100 period EMA", col12, cvm, "EMA") + cvm = add_ta_metrics( + stock_data_history, [200], "EMA (200)", "200 period EMA", col13, cvm, "EMA") + + cvm = add_ta_metrics( + stock_data_history, [10],"SMA (10)", "10 period SMA", col11, cvm, "SMA") + cvm = add_ta_metrics( + stock_data_history, [20], "SMA (20)", "20 period SMA", col12, cvm, "SMA") + cvm = add_ta_metrics( + stock_data_history, [30], "SMA (30)", "30 period SMA", col13, cvm, "SMA") + cvm = add_ta_metrics( + stock_data_history, [50], "SMA (50)", "50 period SMA", col11, cvm, "SMA") + cvm = add_ta_metrics( + stock_data_history, [100], "SMA (100)", "100 period SMA", col12, cvm, "SMA") + cvm = add_ta_metrics( + stock_data_history, [200], "SMA (200)", "200 period SMA", col13, cvm, "SMA") + cvm = add_ta_metrics( + stock_data_history, [20], "VAMA (20)", "20 period VAMA", col11, cvm, "VAMA") + cvm = add_ta_metrics( + stock_data_history, [9], "HMA (9)", "9 period HMA.", col12, cvm, "HMA") col21, col22, col23 = col2.columns(3) - current_value_indicator = add_ta_metrics(stock_data_history, [14], "RSI (14)", "14 period RSI", col21, current_value_indicator, "RSI") - current_value_indicator = add_ta_metrics(stock_data_history, [14], "STOCH (14)", "14 period STOCH %K", col22, current_value_indicator, "STOCH") - current_value_indicator = add_ta_metrics(stock_data_history, [14], "CCI (14)", "14 period CCI", col23, current_value_indicator, "CCI") - current_value_indicator = add_ta_metrics(stock_data_history, [14], "ADX (14)", "14 period ADX.", col21, current_value_indicator, "ADX") - current_value_indicator = add_ta_metrics(stock_data_history, [14], "AO (14)", "AO", col22, current_value_indicator, "AO") - current_value_indicator = add_ta_metrics(stock_data_history, [10], "MOM", "MOM", col23, current_value_indicator, "MOM") - current_value_indicator = add_ta_metrics(stock_data_history, [10,26], "MACD", "SIGNAL", col21, current_value_indicator, "MACD") - current_value_indicator = add_ta_metrics(stock_data_history, [14,14], "STOCHRSI", "14 period stochastic RSI.", col22, current_value_indicator, "STOCHRSI") - current_value_indicator = add_ta_metrics(stock_data_history, [14], "WILLIAMS", "14 Williams %R", col23, current_value_indicator, "WILLIAMS") - current_value_indicator = add_ta_metrics(stock_data_history, [], "EBBP BULL", "Bull.", col21, current_value_indicator, "EBBP") - current_value_indicator = add_ta_metrics(stock_data_history, [], "EBBP BEAR", "Bear.", col22, current_value_indicator, "EBBP") - current_value_indicator = add_ta_metrics(stock_data_history, [], "UO", 0, col23, current_value_indicator, "UO") - - - plot_gauge(current_value_moving, -15, 15, "Moving Averages Gauge", move_gauge) - plot_gauge(current_value_indicator, -9, 9, "Oscillators Gauge", osi_gauge) \ No newline at end of file + cvi = add_ta_metrics( + stock_data_history, [14], "RSI (14)", "14 period RSI", col21, cvi, "RSI") + cvi = add_ta_metrics( + stock_data_history, [14], "STOCH (14)", "14 period STOCH %K", col22, cvi, "STOCH") + cvi = add_ta_metrics( + stock_data_history, [14], "CCI (14)", "14 period CCI", col23, cvi, "CCI") + cvi = add_ta_metrics( + stock_data_history, [14], "ADX (14)", "14 period ADX.", col21, cvi, "ADX") + cvi = add_ta_metrics( + stock_data_history, [14], "AO (14)", "AO", col22, cvi, "AO") + cvi = add_ta_metrics( + stock_data_history, [10], "MOM", "MOM", col23, cvi, "MOM") + cvi = add_ta_metrics( + stock_data_history, [10,26], "MACD", "SIGNAL", col21, cvi, "MACD") + cvi = add_ta_metrics( + stock_data_history, + [14,14], + "STOCHRSI", + "14 period stochastic RSI.", + col22, + cvi, + "STOCHRSI") + cvi = add_ta_metrics( + stock_data_history, [14], "WILLIAMS", "14 Williams %R", col23, cvi, "WILLIAMS") + cvi = add_ta_metrics( + stock_data_history, [], "EBBP BULL", "Bull.", col21, cvi, "EBBP") + cvi = add_ta_metrics( + stock_data_history, [], "EBBP BEAR", "Bear.", col22, cvi, "EBBP") + cvi = add_ta_metrics( + stock_data_history, [], "UO", 0, col23, cvi, "UO") + + plot_gauge(cvm, -15, 15, "Moving Averages Gauge", move_gauge) + plot_gauge(cvi, -9, 9, "Oscillators Gauge", osi_gauge)