Bhanu Prasanna commited on
Commit
efb847a
·
1 Parent(s): 17c56fd
Files changed (2) hide show
  1. app.py +83 -0
  2. main.py +20 -8
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import yfinance as yf
4
+ import streamlit as st
5
+ import plotly.graph_objects as go
6
+ import datetime
7
+
8
+ with open(r"style/style.css") as css:
9
+ st.markdown(f"<style>{css.read()}</style>", unsafe_allow_html=True)
10
+
11
+ st.markdown(
12
+ "<h1 style='text-align: center;'><u>CapiPort</u></h1>", unsafe_allow_html=True
13
+ )
14
+
15
+ st.markdown(
16
+ "<h5 style='text-align: center; color: gray;'>Your Portfolio Optimisation Tool</h5>",
17
+ unsafe_allow_html=True,
18
+ )
19
+ st.header(
20
+ "",
21
+ divider="rainbow",
22
+ )
23
+
24
+ color = "Quest"
25
+ st.markdown(
26
+ "<h1 style='text-align: center;'>🔍 Quest for financial excellence begins with meticulous portfolio optimization</u></h1>",
27
+ unsafe_allow_html=True,
28
+ )
29
+
30
+ st.header(
31
+ "",
32
+ divider="rainbow",
33
+ )
34
+
35
+ list_df = pd.read_csv("Data/Company List.csv")
36
+
37
+ company_name = list_df["Name"].to_list()
38
+ company_symbol = (list_df["Ticker"] + ".NS").to_list()
39
+
40
+ company_dict = dict()
41
+ company_symbol_dict = dict()
42
+
43
+ for CSymbol, CName in zip(company_symbol, company_name):
44
+ company_dict[CName] = CSymbol
45
+
46
+ for CSymbol, CName in zip(company_symbol, company_name):
47
+ company_symbol_dict[CSymbol] = CName
48
+
49
+ st.markdown(
50
+ """
51
+ <style>
52
+ .big-font {
53
+ font-size:20px;
54
+ }
55
+ </style>""",
56
+ unsafe_allow_html=True,
57
+ )
58
+
59
+ st.markdown('<p class="big-font">Select Multiple Companies</p>', unsafe_allow_html=True)
60
+
61
+ com_sel_name = st.multiselect("", company_name, default=None)
62
+ com_sel_date = []
63
+
64
+ for i in com_sel_name:
65
+ d = st.date_input(
66
+ f"Select your vacation for next year - {i}",
67
+ format="YYYY-MM-DD",
68
+ )
69
+ com_sel_date.append(d)
70
+
71
+ com_sel = [company_dict[i] for i in com_sel_name]
72
+
73
+ num_tick = len(com_sel)
74
+
75
+ if num_tick > 1:
76
+
77
+ com_data = pd.DataFrame()
78
+ for cname, cdate in zip(com_sel, com_sel_date):
79
+ stock_data_temp = yf.download(cname, start=cdate, end=pd.Timestamp.now().strftime('%Y-%m-%d'))
80
+ com_data[cname] = stock_data_temp["Adj Close"]
81
+
82
+
83
+
main.py CHANGED
@@ -31,7 +31,6 @@ st.header(
31
  divider="rainbow",
32
  )
33
 
34
-
35
  list_df = pd.read_csv("Data/Company List.csv")
36
 
37
  company_name = list_df["Name"].to_list()
@@ -59,6 +58,15 @@ st.markdown(
59
  st.markdown('<p class="big-font">Select Multiple Companies</p>', unsafe_allow_html=True)
60
 
61
  com_sel_name = st.multiselect("", company_name, default=None)
 
 
 
 
 
 
 
 
 
62
 
63
  com_sel = [company_dict[i] for i in com_sel_name]
64
 
@@ -66,10 +74,15 @@ num_tick = len(com_sel)
66
 
67
  if num_tick > 1:
68
 
69
- com_data = yf.download(com_sel, start="1900-01-01", end="2024-03-08")["Adj Close"]
 
 
 
 
 
70
  for i in com_data.columns:
71
  com_data.dropna(axis=1, how='all', inplace=True)
72
- com_data.dropna(inplace=True)
73
  num_tick = len(com_data.columns)
74
 
75
  if num_tick > 1:
@@ -78,7 +91,6 @@ if num_tick > 1:
78
  com_sel_name_temp.append(company_symbol_dict[i])
79
 
80
  com_sel = com_data.columns.to_list()
81
- com_sel_name.sort()
82
 
83
  st.dataframe(com_data, use_container_width=True)
84
 
@@ -91,11 +103,11 @@ if num_tick > 1:
91
  ## Rebalancing Random Weights
92
  rebal_weig = rand_weig / np.sum(rand_weig)
93
 
94
- ## Calculate the Expected Returns, Annualize it by * 247.0
95
- exp_ret = np.sum((log_return.mean() * rebal_weig) * 247)
96
 
97
- ## Calculate the Expected Volatility, Annualize it by * 247.0
98
- exp_vol = np.sqrt(np.dot(rebal_weig.T, np.dot(log_return.cov() * 247, rebal_weig)))
99
 
100
  ## Calculate the Sharpe Ratio.
101
  sharpe_ratio = exp_ret / exp_vol
 
31
  divider="rainbow",
32
  )
33
 
 
34
  list_df = pd.read_csv("Data/Company List.csv")
35
 
36
  company_name = list_df["Name"].to_list()
 
58
  st.markdown('<p class="big-font">Select Multiple Companies</p>', unsafe_allow_html=True)
59
 
60
  com_sel_name = st.multiselect("", company_name, default=None)
61
+ com_sel_date = []
62
+
63
+ for i in com_sel_name:
64
+ d = st.date_input(
65
+ f"Select your vacation for next year - {i}",
66
+ value= pd.Timestamp('2021-01-01'),
67
+ format="YYYY-MM-DD",
68
+ )
69
+ com_sel_date.append(d)
70
 
71
  com_sel = [company_dict[i] for i in com_sel_name]
72
 
 
74
 
75
  if num_tick > 1:
76
 
77
+ com_data = pd.DataFrame()
78
+ for cname, cdate in zip(com_sel, com_sel_date):
79
+ stock_data_temp = yf.download(cname, start=cdate, end=pd.Timestamp.now().strftime('%Y-%m-%d'))['Adj Close']
80
+ stock_data_temp.name = cname
81
+ com_data = pd.merge(com_data, stock_data_temp, how="outer", right_index=True, left_index=True)
82
+
83
  for i in com_data.columns:
84
  com_data.dropna(axis=1, how='all', inplace=True)
85
+ # com_data.dropna(inplace=True)
86
  num_tick = len(com_data.columns)
87
 
88
  if num_tick > 1:
 
91
  com_sel_name_temp.append(company_symbol_dict[i])
92
 
93
  com_sel = com_data.columns.to_list()
 
94
 
95
  st.dataframe(com_data, use_container_width=True)
96
 
 
103
  ## Rebalancing Random Weights
104
  rebal_weig = rand_weig / np.sum(rand_weig)
105
 
106
+ ## Calculate the Expected Returns, Annualize it by * 252.0
107
+ exp_ret = np.sum((log_return.mean() * rebal_weig) * 252)
108
 
109
+ ## Calculate the Expected Volatility, Annualize it by * 252.0
110
+ exp_vol = np.sqrt(np.dot(rebal_weig.T, np.dot(log_return.cov() * 252, rebal_weig)))
111
 
112
  ## Calculate the Sharpe Ratio.
113
  sharpe_ratio = exp_ret / exp_vol