Skip to content

Commit

Permalink
add technical page
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuttlas90 committed Jul 24, 2024
1 parent 04504c6 commit 8efc5db
Show file tree
Hide file tree
Showing 5 changed files with 364 additions and 0 deletions.
1 change: 1 addition & 0 deletions menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ def add_menu():
st.sidebar.page_link("pages/portfolio.py", label="تحلیل پورتفو", icon="📊")
st.sidebar.page_link("pages/monte_carlo.py", label="مونته کارلو", icon="🧮")
st.sidebar.page_link("pages/leveraged_funds.py", label="صندوقهای اهرمی", icon="🧮")
st.sidebar.page_link("pages/technical.py", label="تکنیکال", icon="📈")
# st.sidebar.page_link("pages/simple_chart.py", label="نمودار ساده ماهانه", icon="📋")
st.sidebar.page_link("pages/changelog.py", label="تازه ها", icon="💬")
70 changes: 70 additions & 0 deletions pages/helper/gauge_chart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import streamlit as st
import pandas as pd
import altair as alt
from login import check_local_token
import numpy as np

from pages.helper.query import Queries
from request import get_stock_monthly
# from slider import create_range_slider
from menu import add_menu
from finta import TA
from request import vasahm_query
import plotly.graph_objs as go
import streamlit.components.v1 as components



def plot_gauge(current_value, min_value, max_value, chart_title, place):
plot_bgcolor = "#def"
quadrant_colors = [plot_bgcolor, "#2bad4e", "#85e043", "#eff229", "#f2a529", "#f25829"]
quadrant_text = ["", "<b>Strong Buy</b>", "<b>Buy</b>", "<b>Neutral</b>", "<b>Sell</b>", "<b>Strong Sell</b>"]
n_quadrants = len(quadrant_colors) - 1

hand_length = np.sqrt(2) / 4
hand_angle = np.pi * (1 - (max(min_value, min(max_value, current_value)) - min_value) / (max_value - min_value))

fig = go.Figure(
data=[
go.Pie(
values=[0.5] + (np.ones(n_quadrants) / 2 / n_quadrants).tolist(),
rotation=90,
hole=0.5,
marker_colors=quadrant_colors,
text=quadrant_text,
textinfo="text",
hoverinfo="skip",
),
],
layout=go.Layout(
showlegend=False,
margin=dict(b=0,t=10,l=10,r=10),
width=450,
height=450,
paper_bgcolor=plot_bgcolor,
annotations=[
go.layout.Annotation(
text=f"<b>{chart_title}</b>",
x=0.5, xanchor="center", xref="paper",
y=0.25, yanchor="bottom", yref="paper",
showarrow=False,
)
],
shapes=[
go.layout.Shape(
type="circle",
x0=0.48, x1=0.52,
y0=0.48, y1=0.52,
fillcolor="#333",
line_color="#333",
),
go.layout.Shape(
type="line",
x0=0.5, x1=0.5 + hand_length * np.cos(hand_angle),
y0=0.5, y1=0.5 + hand_length * np.sin(hand_angle),
line=dict(color="#333", width=4)
)
]
)
)
place.plotly_chart(fig)
20 changes: 20 additions & 0 deletions pages/helper/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,26 @@ def _dollar_query(self, text):
INNER JOIN dollar ON ranked_dates.end_to_period::varchar = dollar.\"Jalali\"
"""
return query_string

def price_query(self, dollar = False):
"""get stock price"""
string = f"""select
date.miladi AS date,
\"openingPrice\" AS open,
\"maxPrice\" AS high,
\"minPrice\" AS low,
\"lastPrice\" AS close,
\"tradeVolume\" AS volume
from
public.stock_price
INNER JOIN stocks ON public.stock_price.stock_id = stocks.id
INNER JOIN date ON public.stock_price.\"tradeDate\" = date.jalali_3::TEXT
where
stocks.name = '{self.name}'
"""
if dollar:
string = self._dollar_query(string)
return string

QUERY_MONTHLY_COMPARE = """WITH
ranked_dates AS (
Expand Down
156 changes: 156 additions & 0 deletions pages/helper/ta_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@

import streamlit as st
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 request import get_stock_monthly
# from slider import create_range_slider
from menu import add_menu
from finta import TA
from request import vasahm_query
import plotly.graph_objs as go
import streamlit.components.v1 as components


def _update_guage(a, b, c, condition):
if condition in ["EMA","SMA","VAMA", "HMA"]:
if b >= c:
a = a +1
else:
a=a-1
return a
elif condition in ["RSI","STOCH"]:
if b >= 70.0:
a = a +1
elif b <= 30.0:
a=a-1
return a
elif condition in ["ADX"]:
if b >= 50.0:
a = a +1
elif b <= 50.0:
a=a-1
return a
elif condition == "CCI":
if b >= 100.0:
a = a -1
elif b <= -100.0:
a=a+1
return a
elif condition in ["AO","MOM", "MACD"]:

if b >= 0.0:
a = a +1
elif b <= 0.0:
a=a-1
return a
elif condition in ["STOCHRSI"]:
if b >= 0.5:
a = a +1
elif b <= 0.5:
a=a-1
return a
elif condition in ["WILLIAMS"]:
if b >= -20.0:
a = a +1
elif b <= -80.0:
a=a-1
return a

def add_ta_metrics(stock_data_history, data,metric_label, col, place, num, ind):
if ind == "EMA":
smas = TA.EMA(stock_data_history, data[0])
sma_df = pd.DataFrame(smas)
place.metric(label=metric_label, value = int(sma_df.iloc[-1][col]), delta = int(sma_df.iloc[-1][col]-sma_df.iloc[-2][col]))
num=_update_guage(num, sma_df.iloc[-1][col], stock_data_history.iloc[-1]['close'],ind)
return num
elif ind == "SMA":
smas = TA.SMA(stock_data_history, data[0])
sma_df = pd.DataFrame(smas)
place.metric(label=metric_label, value = int(sma_df.iloc[-1][col]), delta = int(sma_df.iloc[-1][col]-sma_df.iloc[-2][col]))
num=_update_guage(num, sma_df.iloc[-1][col], stock_data_history.iloc[-1]['close'], ind)
return num
elif ind == "VAMA":
smas = TA.VAMA(stock_data_history, data[0])
sma_df = pd.DataFrame(smas)
place.metric(label=metric_label, value = int(sma_df.iloc[-1][col]), delta = int(sma_df.iloc[-1][col]-sma_df.iloc[-2][col]))
num=_update_guage(num, sma_df.iloc[-1][col], stock_data_history.iloc[-1]['close'], ind)
return num
elif ind == "HMA":
smas = TA.HMA(stock_data_history, data[0])
sma_df = pd.DataFrame(smas)
place.metric(label=metric_label, value = int(sma_df.iloc[-1][col]), delta = int(sma_df.iloc[-1][col]-sma_df.iloc[-2][col]))
num=_update_guage(num, sma_df.iloc[-1][col], stock_data_history.iloc[-1]['close'], ind)
return num
elif ind == "RSI":
smas = TA.RSI(stock_data_history, data[0])
sma_df = pd.DataFrame(smas)
place.metric(label=metric_label, value = int(sma_df.iloc[-1][col]), delta = int(sma_df.iloc[-1][col]-sma_df.iloc[-2][col]))
num=_update_guage(num, sma_df.iloc[-1][col], stock_data_history.iloc[-1]['close'], ind)
return num
elif ind == "STOCH":
smas = TA.STOCH(stock_data_history, data[0])
sma_df = pd.DataFrame(smas)
place.metric(label=metric_label, value = int(sma_df.iloc[-1][col]), delta = int(sma_df.iloc[-1][col]-sma_df.iloc[-2][col]))
num=_update_guage(num, sma_df.iloc[-1][col], stock_data_history.iloc[-1]['close'], ind)
return num
elif ind == "CCI":
smas = TA.CCI(stock_data_history, data[0])
sma_df = pd.DataFrame(smas)
place.metric(label=metric_label, value = int(sma_df.iloc[-1][col]), delta = int(sma_df.iloc[-1][col]-sma_df.iloc[-2][col]))
num=_update_guage(num, sma_df.iloc[-1][col], stock_data_history.iloc[-1]['close'], ind)
return num
elif ind == "ADX":
smas = TA.ADX(stock_data_history, data[0])
sma_df = pd.DataFrame(smas)
place.metric(label=metric_label, value = int(sma_df.iloc[-1][col]), delta = int(sma_df.iloc[-1][col]-sma_df.iloc[-2][col]))
num=_update_guage(num, sma_df.iloc[-1][col], stock_data_history.iloc[-1]['close'], ind)
return num
elif ind == "AO":
smas = TA.AO(stock_data_history, data[0])
sma_df = pd.DataFrame(smas)
place.metric(label=metric_label, value = int(sma_df.iloc[-1][col]), delta = int(sma_df.iloc[-1][col]-sma_df.iloc[-2][col]))
num=_update_guage(num, sma_df.iloc[-1][col], stock_data_history.iloc[-1]['close'], ind)
return num
elif ind == "MOM":
smas = TA.MOM(stock_data_history, data[0])
sma_df = pd.DataFrame(smas)
place.metric(label=metric_label, value = int(sma_df.iloc[-1][col]), delta = int(sma_df.iloc[-1][col]-sma_df.iloc[-2][col]))
num=_update_guage(num, sma_df.iloc[-1][col], stock_data_history.iloc[-1]['close'], ind)
return num
elif ind == "MACD":
smas = TA.MACD(stock_data_history, data[0], data[1])
sma_df = pd.DataFrame(smas)
place.metric(label=metric_label, value = int(sma_df.iloc[-1][col]), delta = int(sma_df.iloc[-1][col]-sma_df.iloc[-2][col]))
num=_update_guage(num, sma_df.iloc[-1][col], stock_data_history.iloc[-1]['close'], ind)
return num
elif ind == "STOCHRSI":
smas = TA.STOCHRSI(stock_data_history, data[0], data[1])
sma_df = pd.DataFrame(smas)
place.metric(label=metric_label, value = int(sma_df.iloc[-1][col]), delta = int(sma_df.iloc[-1][col]-sma_df.iloc[-2][col]))
num=_update_guage(num, sma_df.iloc[-1][col], stock_data_history.iloc[-1]['close'], ind)
return num
elif ind == "WILLIAMS":
smas = TA.WILLIAMS(stock_data_history, data[0])
sma_df = pd.DataFrame(smas)
place.metric(label=metric_label, value = int(sma_df.iloc[-1][col]), delta = int(sma_df.iloc[-1][col]-sma_df.iloc[-2][col]))
num=_update_guage(num, sma_df.iloc[-1][col], stock_data_history.iloc[-1]['close'], ind)
return num
elif ind == "UO":
smas = TA.UO(stock_data_history)
sma_df = pd.DataFrame(smas)
st.write(sma_df.iloc[-1][col])
place.metric(label=metric_label, value = int(sma_df.iloc[-1][col]), delta = int(sma_df.iloc[-1][col]-sma_df.iloc[-2][col]))
_update_guage(num, sma_df.iloc[-1][col], stock_data_history.iloc[-1]['close'], ind)
return num
elif ind == "EBBP":
smas = TA.EBBP(stock_data_history)
sma_df = pd.DataFrame(smas)
place.metric(label=metric_label, value = int(sma_df.iloc[-1][col]), delta = int(sma_df.iloc[-1][col]-sma_df.iloc[-2][col]))
_update_guage(num, sma_df.iloc[-1][col], stock_data_history.iloc[-1]['close'], ind)
return num

117 changes: 117 additions & 0 deletions pages/technical.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
"""Comapre monthly data in a customized way"""

import streamlit as st
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 request import vasahm_query
import plotly.graph_objs as go
import streamlit.components.v1 as components



st.set_page_config(layout='wide',
page_title="وسهم - داده های تکنیکال",
page_icon="./assets/favicon.ico",
initial_sidebar_state='collapsed')


with open( "style.css", encoding='UTF-8') as css:
st.markdown( f'<style>{css.read()}</style>' , unsafe_allow_html= True)
add_menu()
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)
dollar_toggle = st.sidebar.toggle(
"نمایش به دلار",
help="با فعال کردن این گزینه تمامی مبالغ بر اساس دلار بازمحاسبه می گردد."
)



check_local_token()
if "token" in st.session_state:
queries = Queries(name)

error, stock_data = vasahm_query(queries.price_query(dollar=dollar_toggle))
if error:
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['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)
stock_data_history['low'] = stock_data_history['low'].astype(int)
stock_data_history['close'] = stock_data_history['close'].astype(int)
stock_data_history['volume'] = stock_data_history['volume'].astype(int)

data_to_plot = stock_data_history
data_to_plot.drop(["volume"], axis=1)
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


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")

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)

0 comments on commit 8efc5db

Please sign in to comment.