shyam gupta commited on
Commit
ef082e5
2 Parent(s): f434875 6e5813f

Merge pull request #2 from bhanuprasanna527/main

Browse files
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
@@ -34,7 +34,6 @@ st.header(
34
  divider="rainbow",
35
  )
36
 
37
-
38
  list_df = pd.read_csv("Data/Company List.csv")
39
 
40
  company_name = list_df["Name"].to_list()
@@ -62,6 +61,15 @@ st.markdown(
62
  st.markdown('<p class="big-font">Select Multiple Companies</p>', unsafe_allow_html=True)
63
 
64
  com_sel_name = st.multiselect("", company_name, default=None)
 
 
 
 
 
 
 
 
 
65
 
66
  com_sel = [company_dict[i] for i in com_sel_name]
67
 
@@ -69,10 +77,15 @@ num_tick = len(com_sel)
69
 
70
  if num_tick > 1:
71
 
72
- com_data = yf.download(com_sel, start="1900-01-01", end="2024-03-08")["Adj Close"]
 
 
 
 
 
73
  for i in com_data.columns:
74
  com_data.dropna(axis=1, how='all', inplace=True)
75
- com_data.dropna(inplace=True)
76
  num_tick = len(com_data.columns)
77
 
78
  if num_tick > 1:
@@ -81,7 +94,6 @@ if num_tick > 1:
81
  com_sel_name_temp.append(company_symbol_dict[i])
82
 
83
  com_sel = com_data.columns.to_list()
84
- com_sel_name.sort()
85
 
86
  st.dataframe(com_data, use_container_width=True)
87
 
@@ -94,11 +106,11 @@ if num_tick > 1:
94
  ## Rebalancing Random Weights
95
  rebal_weig = rand_weig / np.sum(rand_weig)
96
 
97
- ## Calculate the Expected Returns, Annualize it by * 247.0
98
- exp_ret = np.sum((log_return.mean() * rebal_weig) * 247)
99
 
100
- ## Calculate the Expected Volatility, Annualize it by * 247.0
101
- exp_vol = np.sqrt(np.dot(rebal_weig.T, np.dot(log_return.cov() * 247, rebal_weig)))
102
 
103
  ## Calculate the Sharpe Ratio.
104
  sharpe_ratio = exp_ret / exp_vol
 
34
  divider="rainbow",
35
  )
36
 
 
37
  list_df = pd.read_csv("Data/Company List.csv")
38
 
39
  company_name = list_df["Name"].to_list()
 
61
  st.markdown('<p class="big-font">Select Multiple Companies</p>', unsafe_allow_html=True)
62
 
63
  com_sel_name = st.multiselect("", company_name, default=None)
64
+ com_sel_date = []
65
+
66
+ for i in com_sel_name:
67
+ d = st.date_input(
68
+ f"Select your vacation for next year - {i}",
69
+ value= pd.Timestamp('2021-01-01'),
70
+ format="YYYY-MM-DD",
71
+ )
72
+ com_sel_date.append(d)
73
 
74
  com_sel = [company_dict[i] for i in com_sel_name]
75
 
 
77
 
78
  if num_tick > 1:
79
 
80
+ com_data = pd.DataFrame()
81
+ for cname, cdate in zip(com_sel, com_sel_date):
82
+ stock_data_temp = yf.download(cname, start=cdate, end=pd.Timestamp.now().strftime('%Y-%m-%d'))['Adj Close']
83
+ stock_data_temp.name = cname
84
+ com_data = pd.merge(com_data, stock_data_temp, how="outer", right_index=True, left_index=True)
85
+
86
  for i in com_data.columns:
87
  com_data.dropna(axis=1, how='all', inplace=True)
88
+ # com_data.dropna(inplace=True)
89
  num_tick = len(com_data.columns)
90
 
91
  if num_tick > 1:
 
94
  com_sel_name_temp.append(company_symbol_dict[i])
95
 
96
  com_sel = com_data.columns.to_list()
 
97
 
98
  st.dataframe(com_data, use_container_width=True)
99
 
 
106
  ## Rebalancing Random Weights
107
  rebal_weig = rand_weig / np.sum(rand_weig)
108
 
109
+ ## Calculate the Expected Returns, Annualize it by * 252.0
110
+ exp_ret = np.sum((log_return.mean() * rebal_weig) * 252)
111
 
112
+ ## Calculate the Expected Volatility, Annualize it by * 252.0
113
+ exp_vol = np.sqrt(np.dot(rebal_weig.T, np.dot(log_return.cov() * 252, rebal_weig)))
114
 
115
  ## Calculate the Sharpe Ratio.
116
  sharpe_ratio = exp_ret / exp_vol