From cf6f4980cc0b72a900142c5ecc93c13e44ec9b24 Mon Sep 17 00:00:00 2001 From: cuttlas90 Date: Sun, 14 Jul 2024 11:42:57 +0330 Subject: [PATCH] add performance chart --- funds/fund.py | 14 +++++++------- pages/leveraged_funds.py | 31 +++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/funds/fund.py b/funds/fund.py index b10fe9a..72255fe 100644 --- a/funds/fund.py +++ b/funds/fund.py @@ -38,19 +38,19 @@ def update_fund_data(self): response = requests.get(self.fund_address+"/Chart/TotalNAV?type=getnavtotal", timeout=60) json_object = json.loads(response.text) - self.performance = [] + self.performance = {} if len(json_object[2]['List']) >= 7: - self.performance.append((json_object[2]['List'][6]["y"]-json_object[2]['List'][0]["y"])/json_object[2]['List'][0]["y"]) + self.performance["هفتگی"]=(json_object[2]['List'][6]["y"]-json_object[2]['List'][0]["y"])/json_object[2]['List'][0]["y"] else: - self.performance.append(0) + self.performance["هفتگی"]=0 if len(json_object[2]['List']) >= 23: - self.performance.append((json_object[2]['List'][22]["y"]-json_object[2]['List'][0]["y"])/json_object[2]['List'][0]["y"]) + self.performance["ماهانه"]=(json_object[2]['List'][22]["y"]-json_object[2]['List'][0]["y"])/json_object[2]['List'][0]["y"] else: - self.performance.append(0) + self.performance["ماهانه"]=0 if len(json_object[2]['List']) >= 67: - self.performance.append((json_object[2]['List'][66]["y"]-json_object[2]['List'][0]["y"])/json_object[2]['List'][0]["y"]) + self.performance["فصلی"]=(json_object[2]['List'][66]["y"]-json_object[2]['List'][0]["y"])/json_object[2]['List'][0]["y"] else: - self.performance.append(0) + self.performance["فصلی"]=0 def update_closing_price(self): url = "https://cdn.tsetmc.com/api/ClosingPrice/GetClosingPriceInfo/{}".format(self.code) diff --git a/pages/leveraged_funds.py b/pages/leveraged_funds.py index d6ca844..a0fcf4d 100644 --- a/pages/leveraged_funds.py +++ b/pages/leveraged_funds.py @@ -89,11 +89,26 @@ chart_product = alt.layer(chart, labels).resolve_scale(color='independent') st.altair_chart(chart_product, use_container_width=True) -# with col2: -# st.header('میزان دارایی نقد و اوراق', divider='rainbow') -# chart = alt.Chart(funds_df).mark_bar().encode( -# alt.X('name:N', title='نام صندوق'), -# alt.Y('performance:Q', title="میزان دارایی نقد و اوراق").axis(format='%'), -# alt.Color('name:N', title='نام صندوق'), -# ) -# st.altair_chart(chart, use_container_width=True) +with col2: + d = [] + for index, row in funds_df.iterrows(): + for key, value in row["performance"].items(): + d.append({ + "fund": row["name"], + "time": key, + "perfor": value + }) + performance_df = pd.DataFrame(d) + st.header('عملکرد صندوقها', divider='rainbow') + chart = alt.Chart(performance_df).mark_bar().encode( + alt.X('time:N', title='زمان', axis=alt.Axis(labelAngle=0)), + alt.Y('perfor:Q', title="میزان دارایی نقد و اوراق",axis=alt.Axis(grid=False)).axis(format='%'), + alt.Color('fund:N', title='نام صندوق'), + alt.XOffset('fund:N', title='نام صندوق'), + tooltip=[alt.Tooltip("perfor:Q", format=",.2%", title='عملکرد'), alt.Tooltip("fund:N", title='صندوق'),] + ) + + chart_product = alt.layer(chart).resolve_scale(color='independent') + st.altair_chart(chart_product, use_container_width=True) + +# InvalidHeader \ No newline at end of file