Carsten Stahl commited on
Commit
48067f6
1 Parent(s): f6db2ed

commentated main.py

Browse files
.gitignore CHANGED
@@ -6,3 +6,9 @@
6
  *.idea
7
  *app.py
8
  *.env
 
 
 
 
 
 
 
6
  *.idea
7
  *app.py
8
  *.env
9
+
10
+ # VSCode settings
11
+ .vscode
12
+
13
+ # Pycache
14
+ */__pycache__
main.py CHANGED
@@ -18,9 +18,11 @@ import plotly.express as px
18
  import plotly.graph_objects as go
19
 
20
  streamlit_style()
21
-
22
  company_list_df = pd.read_csv("utilities/data/Company List.csv")
23
 
 
 
24
  company_name = company_list_df["Name"].to_list()
25
  company_symbol = (company_list_df["Ticker"] + ".NS").to_list()
26
 
@@ -37,6 +39,8 @@ streamlit_company_list_input = st.multiselect(
37
  "Select Multiple Companies", company_name, default=None
38
  )
39
 
 
 
40
  optimisation_method = st.selectbox(
41
  "Choose an optimization method accordingly",
42
  (
@@ -57,6 +61,8 @@ if optimisation_method == "Efficient Frontier":
57
  ),
58
  )
59
 
 
 
60
  company_name_to_symbol = [name_to_symbol_dict[i] for i in streamlit_company_list_input]
61
 
62
  number_of_symbols = len(company_name_to_symbol)
@@ -70,9 +76,11 @@ start_date = st.date_input(
70
 
71
  initial_investment = st.number_input("How much would you want to invest?", value=45000)
72
 
 
73
  if number_of_symbols > 1:
74
  company_data = pd.DataFrame()
75
 
 
76
  for cname in company_name_to_symbol:
77
  stock_data_temp = yf.download(
78
  cname, start=start_date, end=pd.Timestamp.now().strftime("%Y-%m-%d")
@@ -86,6 +94,7 @@ if number_of_symbols > 1:
86
  left_index=True,
87
  )
88
 
 
89
  company_data.dropna(axis=1, how="all", inplace=True)
90
 
91
  company_data.dropna(inplace=True)
@@ -99,16 +108,20 @@ if number_of_symbols > 1:
99
 
100
  number_of_symbols = len(company_data.columns)
101
 
 
102
  st.dataframe(company_data, use_container_width=True)
103
 
 
104
  if number_of_symbols > 1:
105
  company_stock_returns_data = company_data.pct_change().dropna()
106
 
 
107
  mu = 0
108
  S = 0
109
  ef = 0
110
  company_asset_weights = 0
111
 
 
112
  if optimisation_method == "Efficient Frontier":
113
  mu = expected_returns.mean_historical_return(company_data)
114
  S = risk_models.sample_cov(company_data)
@@ -138,10 +151,9 @@ if number_of_symbols > 1:
138
  company_asset_weights, orient="index", columns=["Weight"]
139
  ).reset_index()
140
 
 
141
  company_asset_weights.columns = ["Ticker", "Allocation"]
142
 
143
- company_asset_weights_copy = company_asset_weights
144
-
145
  company_asset_weights["Name"] = [
146
  symbol_to_name_dict[i] for i in company_asset_weights["Ticker"]
147
  ]
@@ -150,8 +162,8 @@ if number_of_symbols > 1:
150
 
151
  st.dataframe(company_asset_weights, use_container_width=True)
152
 
153
- ef.portfolio_performance()
154
 
 
155
  (
156
  expected_annual_return,
157
  annual_volatility,
@@ -169,6 +181,7 @@ if number_of_symbols > 1:
169
 
170
  st_portfolio_performance.columns = ["Metrics", "Summary"]
171
 
 
172
  if optimisation_method == "Efficient Frontier":
173
  st.write(
174
  "Optimization Method - ",
@@ -181,8 +194,10 @@ if number_of_symbols > 1:
181
 
182
  st.dataframe(st_portfolio_performance, use_container_width=True)
183
 
 
184
  plots.pie_chart_company_asset_weights(company_asset_weights)
185
 
 
186
  portfolio_returns = (
187
  company_stock_returns_data * list(ef.clean_weights().values())
188
  ).sum(axis=1)
@@ -193,6 +208,8 @@ if number_of_symbols > 1:
193
 
194
  cumulative_returns = (portfolio_returns + 1).cumprod() * initial_investment
195
 
 
 
196
  tab1, tab2, tab3 = st.tabs(["Plots", "Annual Returns", "Montly Returns"])
197
 
198
  with tab1:
 
18
  import plotly.graph_objects as go
19
 
20
  streamlit_style()
21
+ # data import
22
  company_list_df = pd.read_csv("utilities/data/Company List.csv")
23
 
24
+
25
+ # company selction -------------------------------------------------------------------
26
  company_name = company_list_df["Name"].to_list()
27
  company_symbol = (company_list_df["Ticker"] + ".NS").to_list()
28
 
 
39
  "Select Multiple Companies", company_name, default=None
40
  )
41
 
42
+ # method selection ---------------------------------------------------------------------
43
+
44
  optimisation_method = st.selectbox(
45
  "Choose an optimization method accordingly",
46
  (
 
61
  ),
62
  )
63
 
64
+ # selection of starting date and amount to invest --------------------------------------
65
+
66
  company_name_to_symbol = [name_to_symbol_dict[i] for i in streamlit_company_list_input]
67
 
68
  number_of_symbols = len(company_name_to_symbol)
 
76
 
77
  initial_investment = st.number_input("How much would you want to invest?", value=45000)
78
 
79
+ # Optimization and summary of results
80
  if number_of_symbols > 1:
81
  company_data = pd.DataFrame()
82
 
83
+ # get the stock data for the companies
84
  for cname in company_name_to_symbol:
85
  stock_data_temp = yf.download(
86
  cname, start=start_date, end=pd.Timestamp.now().strftime("%Y-%m-%d")
 
94
  left_index=True,
95
  )
96
 
97
+ # cleaning the data
98
  company_data.dropna(axis=1, how="all", inplace=True)
99
 
100
  company_data.dropna(inplace=True)
 
108
 
109
  number_of_symbols = len(company_data.columns)
110
 
111
+ # showing the stock data in the UI
112
  st.dataframe(company_data, use_container_width=True)
113
 
114
+ # only continue with sim, if more than one company was fetched
115
  if number_of_symbols > 1:
116
  company_stock_returns_data = company_data.pct_change().dropna()
117
 
118
+ # Config for the simulation
119
  mu = 0
120
  S = 0
121
  ef = 0
122
  company_asset_weights = 0
123
 
124
+ # Do the portfolio optimization
125
  if optimisation_method == "Efficient Frontier":
126
  mu = expected_returns.mean_historical_return(company_data)
127
  S = risk_models.sample_cov(company_data)
 
151
  company_asset_weights, orient="index", columns=["Weight"]
152
  ).reset_index()
153
 
154
+ # cleaning the returned data from the optimization and outputing results
155
  company_asset_weights.columns = ["Ticker", "Allocation"]
156
 
 
 
157
  company_asset_weights["Name"] = [
158
  symbol_to_name_dict[i] for i in company_asset_weights["Ticker"]
159
  ]
 
162
 
163
  st.dataframe(company_asset_weights, use_container_width=True)
164
 
 
165
 
166
+ # get portfolio performance and refactor the data
167
  (
168
  expected_annual_return,
169
  annual_volatility,
 
181
 
182
  st_portfolio_performance.columns = ["Metrics", "Summary"]
183
 
184
+ # output the method used above the results
185
  if optimisation_method == "Efficient Frontier":
186
  st.write(
187
  "Optimization Method - ",
 
194
 
195
  st.dataframe(st_portfolio_performance, use_container_width=True)
196
 
197
+ # plot the pie chart with the asset weights
198
  plots.pie_chart_company_asset_weights(company_asset_weights)
199
 
200
+ # summarizing the asset returns with optimized portfolio
201
  portfolio_returns = (
202
  company_stock_returns_data * list(ef.clean_weights().values())
203
  ).sum(axis=1)
 
208
 
209
  cumulative_returns = (portfolio_returns + 1).cumprod() * initial_investment
210
 
211
+ # Output the results in the tab
212
+
213
  tab1, tab2, tab3 = st.tabs(["Plots", "Annual Returns", "Montly Returns"])
214
 
215
  with tab1:
utilities/py/__pycache__/plots.cpython-311.pyc ADDED
Binary file (3.54 kB). View file
 
utilities/py/__pycache__/styling.cpython-311.pyc ADDED
Binary file (1.69 kB). View file
 
utilities/py/__pycache__/summary_tables.cpython-311.pyc ADDED
Binary file (6.03 kB). View file