Spaces:
Sleeping
Sleeping
Bhanu Prasanna
commited on
Commit
·
33e55ac
1
Parent(s):
f37e29b
Update
Browse files- .ipynb_checkpoints/CapiPort-checkpoint.ipynb +36 -17
- CapiPort.ipynb +363 -13
- CapiPort.py +80 -0
- EQUITY_L.csv +0 -0
- main.py +31 -0
.ipynb_checkpoints/CapiPort-checkpoint.ipynb
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "markdown",
|
5 |
-
"id": "
|
6 |
"metadata": {},
|
7 |
"source": [
|
8 |
"# CapiPort - PORTFOLIO OPTIMISATION"
|
@@ -10,7 +10,7 @@
|
|
10 |
},
|
11 |
{
|
12 |
"cell_type": "markdown",
|
13 |
-
"id": "
|
14 |
"metadata": {},
|
15 |
"source": [
|
16 |
" Two things to consider for Portfolio Optimisation:\n",
|
@@ -21,7 +21,7 @@
|
|
21 |
},
|
22 |
{
|
23 |
"cell_type": "markdown",
|
24 |
-
"id": "
|
25 |
"metadata": {},
|
26 |
"source": [
|
27 |
" Basic process of Portfolio Optimisation:\n",
|
@@ -52,7 +52,7 @@
|
|
52 |
},
|
53 |
{
|
54 |
"cell_type": "markdown",
|
55 |
-
"id": "
|
56 |
"metadata": {},
|
57 |
"source": [
|
58 |
"## Importing the Libraries"
|
@@ -61,7 +61,7 @@
|
|
61 |
{
|
62 |
"cell_type": "code",
|
63 |
"execution_count": 15,
|
64 |
-
"id": "
|
65 |
"metadata": {},
|
66 |
"outputs": [],
|
67 |
"source": [
|
@@ -79,7 +79,7 @@
|
|
79 |
},
|
80 |
{
|
81 |
"cell_type": "markdown",
|
82 |
-
"id": "
|
83 |
"metadata": {},
|
84 |
"source": [
|
85 |
"## Get Stock Data"
|
@@ -88,7 +88,7 @@
|
|
88 |
{
|
89 |
"cell_type": "code",
|
90 |
"execution_count": 16,
|
91 |
-
"id": "
|
92 |
"metadata": {},
|
93 |
"outputs": [],
|
94 |
"source": [
|
@@ -129,7 +129,7 @@
|
|
129 |
{
|
130 |
"cell_type": "code",
|
131 |
"execution_count": 26,
|
132 |
-
"id": "
|
133 |
"metadata": {},
|
134 |
"outputs": [
|
135 |
{
|
@@ -154,8 +154,8 @@
|
|
154 |
},
|
155 |
{
|
156 |
"cell_type": "code",
|
157 |
-
"execution_count":
|
158 |
-
"id": "
|
159 |
"metadata": {},
|
160 |
"outputs": [],
|
161 |
"source": [
|
@@ -202,23 +202,42 @@
|
|
202 |
},
|
203 |
{
|
204 |
"cell_type": "code",
|
205 |
-
"execution_count":
|
206 |
-
"id": "
|
207 |
"metadata": {},
|
208 |
-
"outputs": [
|
209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
},
|
211 |
{
|
212 |
"cell_type": "code",
|
213 |
"execution_count": null,
|
214 |
-
"id": "
|
215 |
"metadata": {},
|
216 |
"outputs": [],
|
217 |
"source": []
|
218 |
},
|
219 |
{
|
220 |
"cell_type": "markdown",
|
221 |
-
"id": "
|
222 |
"metadata": {},
|
223 |
"source": [
|
224 |
"## <u>STEPS FOR IMPLEMENTING<u>\n",
|
@@ -246,7 +265,7 @@
|
|
246 |
{
|
247 |
"cell_type": "code",
|
248 |
"execution_count": null,
|
249 |
-
"id": "
|
250 |
"metadata": {},
|
251 |
"outputs": [],
|
252 |
"source": []
|
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "markdown",
|
5 |
+
"id": "5f6ccd5b",
|
6 |
"metadata": {},
|
7 |
"source": [
|
8 |
"# CapiPort - PORTFOLIO OPTIMISATION"
|
|
|
10 |
},
|
11 |
{
|
12 |
"cell_type": "markdown",
|
13 |
+
"id": "b1962897",
|
14 |
"metadata": {},
|
15 |
"source": [
|
16 |
" Two things to consider for Portfolio Optimisation:\n",
|
|
|
21 |
},
|
22 |
{
|
23 |
"cell_type": "markdown",
|
24 |
+
"id": "9d025126",
|
25 |
"metadata": {},
|
26 |
"source": [
|
27 |
" Basic process of Portfolio Optimisation:\n",
|
|
|
52 |
},
|
53 |
{
|
54 |
"cell_type": "markdown",
|
55 |
+
"id": "76496c3d",
|
56 |
"metadata": {},
|
57 |
"source": [
|
58 |
"## Importing the Libraries"
|
|
|
61 |
{
|
62 |
"cell_type": "code",
|
63 |
"execution_count": 15,
|
64 |
+
"id": "bdaab00e",
|
65 |
"metadata": {},
|
66 |
"outputs": [],
|
67 |
"source": [
|
|
|
79 |
},
|
80 |
{
|
81 |
"cell_type": "markdown",
|
82 |
+
"id": "0d4befd2",
|
83 |
"metadata": {},
|
84 |
"source": [
|
85 |
"## Get Stock Data"
|
|
|
88 |
{
|
89 |
"cell_type": "code",
|
90 |
"execution_count": 16,
|
91 |
+
"id": "33dc5711",
|
92 |
"metadata": {},
|
93 |
"outputs": [],
|
94 |
"source": [
|
|
|
129 |
{
|
130 |
"cell_type": "code",
|
131 |
"execution_count": 26,
|
132 |
+
"id": "3fec74c6",
|
133 |
"metadata": {},
|
134 |
"outputs": [
|
135 |
{
|
|
|
154 |
},
|
155 |
{
|
156 |
"cell_type": "code",
|
157 |
+
"execution_count": 28,
|
158 |
+
"id": "70190a08",
|
159 |
"metadata": {},
|
160 |
"outputs": [],
|
161 |
"source": [
|
|
|
202 |
},
|
203 |
{
|
204 |
"cell_type": "code",
|
205 |
+
"execution_count": 33,
|
206 |
+
"id": "ebf6b6bf",
|
207 |
"metadata": {},
|
208 |
+
"outputs": [
|
209 |
+
{
|
210 |
+
"name": "stdout",
|
211 |
+
"output_type": "stream",
|
212 |
+
"text": [
|
213 |
+
"TATAPOWER.NS : 0.0\n",
|
214 |
+
"TATAMOTORS.NS : 0.236548217499089\n",
|
215 |
+
"TATASTEEL.NS : 0.17568900379556238\n",
|
216 |
+
"ZOMATO.NS : 0.07826482194498546\n",
|
217 |
+
"NHPC.NS : 0.21116461887342103\n",
|
218 |
+
"NCC.NS : 0.0\n",
|
219 |
+
"IREDA.NS : 0.0\n",
|
220 |
+
"IRCON.NS : 0.2983333378869751\n"
|
221 |
+
]
|
222 |
+
}
|
223 |
+
],
|
224 |
+
"source": [
|
225 |
+
"optimal_weights = optimize_portfolio(equity_data, risk_free_rate)\n",
|
226 |
+
"for i,j in zip(equity_list, optimal_weights):\n",
|
227 |
+
" print(i, \" : \", j)"
|
228 |
+
]
|
229 |
},
|
230 |
{
|
231 |
"cell_type": "code",
|
232 |
"execution_count": null,
|
233 |
+
"id": "2a5e7bdf",
|
234 |
"metadata": {},
|
235 |
"outputs": [],
|
236 |
"source": []
|
237 |
},
|
238 |
{
|
239 |
"cell_type": "markdown",
|
240 |
+
"id": "3a6c6060",
|
241 |
"metadata": {},
|
242 |
"source": [
|
243 |
"## <u>STEPS FOR IMPLEMENTING<u>\n",
|
|
|
265 |
{
|
266 |
"cell_type": "code",
|
267 |
"execution_count": null,
|
268 |
+
"id": "e4137507",
|
269 |
"metadata": {},
|
270 |
"outputs": [],
|
271 |
"source": []
|
CapiPort.ipynb
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "markdown",
|
5 |
-
"id": "
|
6 |
"metadata": {},
|
7 |
"source": [
|
8 |
"# CapiPort - PORTFOLIO OPTIMISATION"
|
@@ -10,7 +10,7 @@
|
|
10 |
},
|
11 |
{
|
12 |
"cell_type": "markdown",
|
13 |
-
"id": "
|
14 |
"metadata": {},
|
15 |
"source": [
|
16 |
" Two things to consider for Portfolio Optimisation:\n",
|
@@ -21,7 +21,7 @@
|
|
21 |
},
|
22 |
{
|
23 |
"cell_type": "markdown",
|
24 |
-
"id": "
|
25 |
"metadata": {},
|
26 |
"source": [
|
27 |
" Basic process of Portfolio Optimisation:\n",
|
@@ -50,9 +50,359 @@
|
|
50 |
" 6) Final Result"
|
51 |
]
|
52 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
{
|
54 |
"cell_type": "markdown",
|
55 |
-
"id": "
|
56 |
"metadata": {},
|
57 |
"source": [
|
58 |
"## Importing the Libraries"
|
@@ -61,7 +411,7 @@
|
|
61 |
{
|
62 |
"cell_type": "code",
|
63 |
"execution_count": 15,
|
64 |
-
"id": "
|
65 |
"metadata": {},
|
66 |
"outputs": [],
|
67 |
"source": [
|
@@ -79,7 +429,7 @@
|
|
79 |
},
|
80 |
{
|
81 |
"cell_type": "markdown",
|
82 |
-
"id": "
|
83 |
"metadata": {},
|
84 |
"source": [
|
85 |
"## Get Stock Data"
|
@@ -88,7 +438,7 @@
|
|
88 |
{
|
89 |
"cell_type": "code",
|
90 |
"execution_count": 16,
|
91 |
-
"id": "
|
92 |
"metadata": {},
|
93 |
"outputs": [],
|
94 |
"source": [
|
@@ -129,7 +479,7 @@
|
|
129 |
{
|
130 |
"cell_type": "code",
|
131 |
"execution_count": 26,
|
132 |
-
"id": "
|
133 |
"metadata": {},
|
134 |
"outputs": [
|
135 |
{
|
@@ -155,7 +505,7 @@
|
|
155 |
{
|
156 |
"cell_type": "code",
|
157 |
"execution_count": 28,
|
158 |
-
"id": "
|
159 |
"metadata": {},
|
160 |
"outputs": [],
|
161 |
"source": [
|
@@ -203,7 +553,7 @@
|
|
203 |
{
|
204 |
"cell_type": "code",
|
205 |
"execution_count": 33,
|
206 |
-
"id": "
|
207 |
"metadata": {},
|
208 |
"outputs": [
|
209 |
{
|
@@ -230,14 +580,14 @@
|
|
230 |
{
|
231 |
"cell_type": "code",
|
232 |
"execution_count": null,
|
233 |
-
"id": "
|
234 |
"metadata": {},
|
235 |
"outputs": [],
|
236 |
"source": []
|
237 |
},
|
238 |
{
|
239 |
"cell_type": "markdown",
|
240 |
-
"id": "
|
241 |
"metadata": {},
|
242 |
"source": [
|
243 |
"## <u>STEPS FOR IMPLEMENTING<u>\n",
|
@@ -265,7 +615,7 @@
|
|
265 |
{
|
266 |
"cell_type": "code",
|
267 |
"execution_count": null,
|
268 |
-
"id": "
|
269 |
"metadata": {},
|
270 |
"outputs": [],
|
271 |
"source": []
|
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "markdown",
|
5 |
+
"id": "5f6ccd5b",
|
6 |
"metadata": {},
|
7 |
"source": [
|
8 |
"# CapiPort - PORTFOLIO OPTIMISATION"
|
|
|
10 |
},
|
11 |
{
|
12 |
"cell_type": "markdown",
|
13 |
+
"id": "b1962897",
|
14 |
"metadata": {},
|
15 |
"source": [
|
16 |
" Two things to consider for Portfolio Optimisation:\n",
|
|
|
21 |
},
|
22 |
{
|
23 |
"cell_type": "markdown",
|
24 |
+
"id": "9d025126",
|
25 |
"metadata": {},
|
26 |
"source": [
|
27 |
" Basic process of Portfolio Optimisation:\n",
|
|
|
50 |
" 6) Final Result"
|
51 |
]
|
52 |
},
|
53 |
+
{
|
54 |
+
"cell_type": "code",
|
55 |
+
"execution_count": 15,
|
56 |
+
"id": "ab093e0e",
|
57 |
+
"metadata": {},
|
58 |
+
"outputs": [
|
59 |
+
{
|
60 |
+
"data": {
|
61 |
+
"text/html": [
|
62 |
+
"<div>\n",
|
63 |
+
"<style scoped>\n",
|
64 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
65 |
+
" vertical-align: middle;\n",
|
66 |
+
" }\n",
|
67 |
+
"\n",
|
68 |
+
" .dataframe tbody tr th {\n",
|
69 |
+
" vertical-align: top;\n",
|
70 |
+
" }\n",
|
71 |
+
"\n",
|
72 |
+
" .dataframe thead th {\n",
|
73 |
+
" text-align: right;\n",
|
74 |
+
" }\n",
|
75 |
+
"</style>\n",
|
76 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
77 |
+
" <thead>\n",
|
78 |
+
" <tr style=\"text-align: right;\">\n",
|
79 |
+
" <th></th>\n",
|
80 |
+
" <th>SYMBOL</th>\n",
|
81 |
+
" <th>NAME OF COMPANY</th>\n",
|
82 |
+
" <th>SERIES</th>\n",
|
83 |
+
" <th>DATE OF LISTING</th>\n",
|
84 |
+
" <th>PAID UP VALUE</th>\n",
|
85 |
+
" <th>MARKET LOT</th>\n",
|
86 |
+
" <th>ISIN NUMBER</th>\n",
|
87 |
+
" <th>FACE VALUE</th>\n",
|
88 |
+
" <th>Unnamed: 8</th>\n",
|
89 |
+
" <th>YahooEquiv</th>\n",
|
90 |
+
" <th>Yahoo_Equivalent_Code</th>\n",
|
91 |
+
" </tr>\n",
|
92 |
+
" </thead>\n",
|
93 |
+
" <tbody>\n",
|
94 |
+
" <tr>\n",
|
95 |
+
" <th>0</th>\n",
|
96 |
+
" <td>20MICRONS</td>\n",
|
97 |
+
" <td>20 Microns Limited</td>\n",
|
98 |
+
" <td>EQ</td>\n",
|
99 |
+
" <td>06-Oct-08</td>\n",
|
100 |
+
" <td>5</td>\n",
|
101 |
+
" <td>1</td>\n",
|
102 |
+
" <td>INE144J01027</td>\n",
|
103 |
+
" <td>5</td>\n",
|
104 |
+
" <td>.NS</td>\n",
|
105 |
+
" <td>20MICRONS.NS</td>\n",
|
106 |
+
" <td>'20MICRONS.NS',</td>\n",
|
107 |
+
" </tr>\n",
|
108 |
+
" <tr>\n",
|
109 |
+
" <th>1</th>\n",
|
110 |
+
" <td>21STCENMGM</td>\n",
|
111 |
+
" <td>21st Century Management Services Limited</td>\n",
|
112 |
+
" <td>EQ</td>\n",
|
113 |
+
" <td>03-May-95</td>\n",
|
114 |
+
" <td>10</td>\n",
|
115 |
+
" <td>1</td>\n",
|
116 |
+
" <td>INE253B01015</td>\n",
|
117 |
+
" <td>10</td>\n",
|
118 |
+
" <td>.NS</td>\n",
|
119 |
+
" <td>21STCENMGM.NS</td>\n",
|
120 |
+
" <td>'21STCENMGM.NS',</td>\n",
|
121 |
+
" </tr>\n",
|
122 |
+
" <tr>\n",
|
123 |
+
" <th>2</th>\n",
|
124 |
+
" <td>3IINFOTECH</td>\n",
|
125 |
+
" <td>3i Infotech Limited</td>\n",
|
126 |
+
" <td>EQ</td>\n",
|
127 |
+
" <td>22-Apr-05</td>\n",
|
128 |
+
" <td>10</td>\n",
|
129 |
+
" <td>1</td>\n",
|
130 |
+
" <td>INE748C01020</td>\n",
|
131 |
+
" <td>10</td>\n",
|
132 |
+
" <td>.NS</td>\n",
|
133 |
+
" <td>3IINFOTECH.NS</td>\n",
|
134 |
+
" <td>'3IINFOTECH.NS',</td>\n",
|
135 |
+
" </tr>\n",
|
136 |
+
" <tr>\n",
|
137 |
+
" <th>3</th>\n",
|
138 |
+
" <td>3MINDIA</td>\n",
|
139 |
+
" <td>3M India Limited</td>\n",
|
140 |
+
" <td>EQ</td>\n",
|
141 |
+
" <td>13-Aug-04</td>\n",
|
142 |
+
" <td>10</td>\n",
|
143 |
+
" <td>1</td>\n",
|
144 |
+
" <td>INE470A01017</td>\n",
|
145 |
+
" <td>10</td>\n",
|
146 |
+
" <td>.NS</td>\n",
|
147 |
+
" <td>3MINDIA.NS</td>\n",
|
148 |
+
" <td>'3MINDIA.NS',</td>\n",
|
149 |
+
" </tr>\n",
|
150 |
+
" <tr>\n",
|
151 |
+
" <th>4</th>\n",
|
152 |
+
" <td>3PLAND</td>\n",
|
153 |
+
" <td>3P Land Holdings Limited</td>\n",
|
154 |
+
" <td>EQ</td>\n",
|
155 |
+
" <td>19-Jul-95</td>\n",
|
156 |
+
" <td>2</td>\n",
|
157 |
+
" <td>1</td>\n",
|
158 |
+
" <td>INE105C01023</td>\n",
|
159 |
+
" <td>2</td>\n",
|
160 |
+
" <td>.NS</td>\n",
|
161 |
+
" <td>3PLAND.NS</td>\n",
|
162 |
+
" <td>'3PLAND.NS',</td>\n",
|
163 |
+
" </tr>\n",
|
164 |
+
" <tr>\n",
|
165 |
+
" <th>...</th>\n",
|
166 |
+
" <td>...</td>\n",
|
167 |
+
" <td>...</td>\n",
|
168 |
+
" <td>...</td>\n",
|
169 |
+
" <td>...</td>\n",
|
170 |
+
" <td>...</td>\n",
|
171 |
+
" <td>...</td>\n",
|
172 |
+
" <td>...</td>\n",
|
173 |
+
" <td>...</td>\n",
|
174 |
+
" <td>...</td>\n",
|
175 |
+
" <td>...</td>\n",
|
176 |
+
" <td>...</td>\n",
|
177 |
+
" </tr>\n",
|
178 |
+
" <tr>\n",
|
179 |
+
" <th>1660</th>\n",
|
180 |
+
" <td>ZODJRDMKJ</td>\n",
|
181 |
+
" <td>Zodiac JRD- MKJ Limited</td>\n",
|
182 |
+
" <td>EQ</td>\n",
|
183 |
+
" <td>19-Jul-95</td>\n",
|
184 |
+
" <td>10</td>\n",
|
185 |
+
" <td>1</td>\n",
|
186 |
+
" <td>INE077B01018</td>\n",
|
187 |
+
" <td>10</td>\n",
|
188 |
+
" <td>.NS</td>\n",
|
189 |
+
" <td>ZODJRDMKJ.NS</td>\n",
|
190 |
+
" <td>'ZODJRDMKJ.NS',</td>\n",
|
191 |
+
" </tr>\n",
|
192 |
+
" <tr>\n",
|
193 |
+
" <th>1661</th>\n",
|
194 |
+
" <td>ZOTA</td>\n",
|
195 |
+
" <td>Zota Health Care LImited</td>\n",
|
196 |
+
" <td>EQ</td>\n",
|
197 |
+
" <td>19-Aug-19</td>\n",
|
198 |
+
" <td>10</td>\n",
|
199 |
+
" <td>1</td>\n",
|
200 |
+
" <td>INE358U01012</td>\n",
|
201 |
+
" <td>10</td>\n",
|
202 |
+
" <td>.NS</td>\n",
|
203 |
+
" <td>ZOTA.NS</td>\n",
|
204 |
+
" <td>'ZOTA.NS',</td>\n",
|
205 |
+
" </tr>\n",
|
206 |
+
" <tr>\n",
|
207 |
+
" <th>1662</th>\n",
|
208 |
+
" <td>ZUARI</td>\n",
|
209 |
+
" <td>Zuari Agro Chemicals Limited</td>\n",
|
210 |
+
" <td>EQ</td>\n",
|
211 |
+
" <td>27-Nov-12</td>\n",
|
212 |
+
" <td>10</td>\n",
|
213 |
+
" <td>1</td>\n",
|
214 |
+
" <td>INE840M01016</td>\n",
|
215 |
+
" <td>10</td>\n",
|
216 |
+
" <td>.NS</td>\n",
|
217 |
+
" <td>ZUARI.NS</td>\n",
|
218 |
+
" <td>'ZUARI.NS',</td>\n",
|
219 |
+
" </tr>\n",
|
220 |
+
" <tr>\n",
|
221 |
+
" <th>1663</th>\n",
|
222 |
+
" <td>ZUARIGLOB</td>\n",
|
223 |
+
" <td>Zuari Global Limited</td>\n",
|
224 |
+
" <td>BE</td>\n",
|
225 |
+
" <td>12-Apr-95</td>\n",
|
226 |
+
" <td>10</td>\n",
|
227 |
+
" <td>1</td>\n",
|
228 |
+
" <td>INE217A01012</td>\n",
|
229 |
+
" <td>10</td>\n",
|
230 |
+
" <td>.NS</td>\n",
|
231 |
+
" <td>ZUARIGLOB.NS</td>\n",
|
232 |
+
" <td>'ZUARIGLOB.NS',</td>\n",
|
233 |
+
" </tr>\n",
|
234 |
+
" <tr>\n",
|
235 |
+
" <th>1664</th>\n",
|
236 |
+
" <td>ZYDUSWELL</td>\n",
|
237 |
+
" <td>Zydus Wellness Limited</td>\n",
|
238 |
+
" <td>EQ</td>\n",
|
239 |
+
" <td>13-Nov-09</td>\n",
|
240 |
+
" <td>10</td>\n",
|
241 |
+
" <td>1</td>\n",
|
242 |
+
" <td>INE768C01010</td>\n",
|
243 |
+
" <td>10</td>\n",
|
244 |
+
" <td>.NS</td>\n",
|
245 |
+
" <td>ZYDUSWELL.NS</td>\n",
|
246 |
+
" <td>'ZYDUSWELL.NS'</td>\n",
|
247 |
+
" </tr>\n",
|
248 |
+
" </tbody>\n",
|
249 |
+
"</table>\n",
|
250 |
+
"<p>1665 rows × 11 columns</p>\n",
|
251 |
+
"</div>"
|
252 |
+
],
|
253 |
+
"text/plain": [
|
254 |
+
" SYMBOL NAME OF COMPANY SERIES \\\n",
|
255 |
+
"0 20MICRONS 20 Microns Limited EQ \n",
|
256 |
+
"1 21STCENMGM 21st Century Management Services Limited EQ \n",
|
257 |
+
"2 3IINFOTECH 3i Infotech Limited EQ \n",
|
258 |
+
"3 3MINDIA 3M India Limited EQ \n",
|
259 |
+
"4 3PLAND 3P Land Holdings Limited EQ \n",
|
260 |
+
"... ... ... ... \n",
|
261 |
+
"1660 ZODJRDMKJ Zodiac JRD- MKJ Limited EQ \n",
|
262 |
+
"1661 ZOTA Zota Health Care LImited EQ \n",
|
263 |
+
"1662 ZUARI Zuari Agro Chemicals Limited EQ \n",
|
264 |
+
"1663 ZUARIGLOB Zuari Global Limited BE \n",
|
265 |
+
"1664 ZYDUSWELL Zydus Wellness Limited EQ \n",
|
266 |
+
"\n",
|
267 |
+
" DATE OF LISTING PAID UP VALUE MARKET LOT ISIN NUMBER FACE VALUE \\\n",
|
268 |
+
"0 06-Oct-08 5 1 INE144J01027 5 \n",
|
269 |
+
"1 03-May-95 10 1 INE253B01015 10 \n",
|
270 |
+
"2 22-Apr-05 10 1 INE748C01020 10 \n",
|
271 |
+
"3 13-Aug-04 10 1 INE470A01017 10 \n",
|
272 |
+
"4 19-Jul-95 2 1 INE105C01023 2 \n",
|
273 |
+
"... ... ... ... ... ... \n",
|
274 |
+
"1660 19-Jul-95 10 1 INE077B01018 10 \n",
|
275 |
+
"1661 19-Aug-19 10 1 INE358U01012 10 \n",
|
276 |
+
"1662 27-Nov-12 10 1 INE840M01016 10 \n",
|
277 |
+
"1663 12-Apr-95 10 1 INE217A01012 10 \n",
|
278 |
+
"1664 13-Nov-09 10 1 INE768C01010 10 \n",
|
279 |
+
"\n",
|
280 |
+
" Unnamed: 8 YahooEquiv Yahoo_Equivalent_Code \n",
|
281 |
+
"0 .NS 20MICRONS.NS '20MICRONS.NS', \n",
|
282 |
+
"1 .NS 21STCENMGM.NS '21STCENMGM.NS', \n",
|
283 |
+
"2 .NS 3IINFOTECH.NS '3IINFOTECH.NS', \n",
|
284 |
+
"3 .NS 3MINDIA.NS '3MINDIA.NS', \n",
|
285 |
+
"4 .NS 3PLAND.NS '3PLAND.NS', \n",
|
286 |
+
"... ... ... ... \n",
|
287 |
+
"1660 .NS ZODJRDMKJ.NS 'ZODJRDMKJ.NS', \n",
|
288 |
+
"1661 .NS ZOTA.NS 'ZOTA.NS', \n",
|
289 |
+
"1662 .NS ZUARI.NS 'ZUARI.NS', \n",
|
290 |
+
"1663 .NS ZUARIGLOB.NS 'ZUARIGLOB.NS', \n",
|
291 |
+
"1664 .NS ZYDUSWELL.NS 'ZYDUSWELL.NS' \n",
|
292 |
+
"\n",
|
293 |
+
"[1665 rows x 11 columns]"
|
294 |
+
]
|
295 |
+
},
|
296 |
+
"execution_count": 15,
|
297 |
+
"metadata": {},
|
298 |
+
"output_type": "execute_result"
|
299 |
+
}
|
300 |
+
],
|
301 |
+
"source": [
|
302 |
+
"import pandas as pd\n",
|
303 |
+
"\n",
|
304 |
+
"df = pd.read_csv(\"EQUITY_L.csv\")\n",
|
305 |
+
"\n",
|
306 |
+
"df.head(1665)"
|
307 |
+
]
|
308 |
+
},
|
309 |
+
{
|
310 |
+
"cell_type": "code",
|
311 |
+
"execution_count": 4,
|
312 |
+
"id": "bd091648",
|
313 |
+
"metadata": {},
|
314 |
+
"outputs": [
|
315 |
+
{
|
316 |
+
"data": {
|
317 |
+
"text/plain": [
|
318 |
+
"0 20MICRONS.NS\n",
|
319 |
+
"1 21STCENMGM.NS\n",
|
320 |
+
"2 3IINFOTECH.NS\n",
|
321 |
+
"3 3MINDIA.NS\n",
|
322 |
+
"4 3PLAND.NS\n",
|
323 |
+
" ... \n",
|
324 |
+
"1660 ZODJRDMKJ.NS\n",
|
325 |
+
"1661 ZOTA.NS\n",
|
326 |
+
"1662 ZUARI.NS\n",
|
327 |
+
"1663 ZUARIGLOB.NS\n",
|
328 |
+
"1664 ZYDUSWELL.NS\n",
|
329 |
+
"Name: SYMBOL, Length: 1665, dtype: object"
|
330 |
+
]
|
331 |
+
},
|
332 |
+
"execution_count": 4,
|
333 |
+
"metadata": {},
|
334 |
+
"output_type": "execute_result"
|
335 |
+
}
|
336 |
+
],
|
337 |
+
"source": [
|
338 |
+
"df['SYMBOL']+'.NS'"
|
339 |
+
]
|
340 |
+
},
|
341 |
+
{
|
342 |
+
"cell_type": "code",
|
343 |
+
"execution_count": 6,
|
344 |
+
"id": "13602af6",
|
345 |
+
"metadata": {},
|
346 |
+
"outputs": [
|
347 |
+
{
|
348 |
+
"data": {
|
349 |
+
"text/plain": [
|
350 |
+
"0 20 Microns Limited\n",
|
351 |
+
"1 21st Century Management Services Limited\n",
|
352 |
+
"2 3i Infotech Limited\n",
|
353 |
+
"3 3M India Limited\n",
|
354 |
+
"4 3P Land Holdings Limited\n",
|
355 |
+
" ... \n",
|
356 |
+
"1660 Zodiac JRD- MKJ Limited\n",
|
357 |
+
"1661 Zota Health Care LImited\n",
|
358 |
+
"1662 Zuari Agro Chemicals Limited\n",
|
359 |
+
"1663 Zuari Global Limited\n",
|
360 |
+
"1664 Zydus Wellness Limited\n",
|
361 |
+
"Name: NAME OF COMPANY, Length: 1665, dtype: object"
|
362 |
+
]
|
363 |
+
},
|
364 |
+
"execution_count": 6,
|
365 |
+
"metadata": {},
|
366 |
+
"output_type": "execute_result"
|
367 |
+
}
|
368 |
+
],
|
369 |
+
"source": [
|
370 |
+
"df[\"NAME OF COMPANY\"]"
|
371 |
+
]
|
372 |
+
},
|
373 |
+
{
|
374 |
+
"cell_type": "code",
|
375 |
+
"execution_count": 11,
|
376 |
+
"id": "6ec4509a",
|
377 |
+
"metadata": {},
|
378 |
+
"outputs": [
|
379 |
+
{
|
380 |
+
"data": {
|
381 |
+
"text/plain": [
|
382 |
+
"0 20MICRONS.NS'\n",
|
383 |
+
"1 21STCENMGM.NS'\n",
|
384 |
+
"2 3IINFOTECH.NS'\n",
|
385 |
+
"3 3MINDIA.NS'\n",
|
386 |
+
"4 3PLAND.NS'\n",
|
387 |
+
" ... \n",
|
388 |
+
"1660 ZODJRDMKJ.NS'\n",
|
389 |
+
"1661 ZOTA.NS'\n",
|
390 |
+
"1662 ZUARI.NS'\n",
|
391 |
+
"1663 ZUARIGLOB.NS'\n",
|
392 |
+
"1664 ZYDUSWELL.NS\n",
|
393 |
+
"Name: Yahoo_Equivalent_Code, Length: 1665, dtype: object"
|
394 |
+
]
|
395 |
+
},
|
396 |
+
"execution_count": 11,
|
397 |
+
"metadata": {},
|
398 |
+
"output_type": "execute_result"
|
399 |
+
}
|
400 |
+
],
|
401 |
+
"source": []
|
402 |
+
},
|
403 |
{
|
404 |
"cell_type": "markdown",
|
405 |
+
"id": "76496c3d",
|
406 |
"metadata": {},
|
407 |
"source": [
|
408 |
"## Importing the Libraries"
|
|
|
411 |
{
|
412 |
"cell_type": "code",
|
413 |
"execution_count": 15,
|
414 |
+
"id": "bdaab00e",
|
415 |
"metadata": {},
|
416 |
"outputs": [],
|
417 |
"source": [
|
|
|
429 |
},
|
430 |
{
|
431 |
"cell_type": "markdown",
|
432 |
+
"id": "0d4befd2",
|
433 |
"metadata": {},
|
434 |
"source": [
|
435 |
"## Get Stock Data"
|
|
|
438 |
{
|
439 |
"cell_type": "code",
|
440 |
"execution_count": 16,
|
441 |
+
"id": "33dc5711",
|
442 |
"metadata": {},
|
443 |
"outputs": [],
|
444 |
"source": [
|
|
|
479 |
{
|
480 |
"cell_type": "code",
|
481 |
"execution_count": 26,
|
482 |
+
"id": "3fec74c6",
|
483 |
"metadata": {},
|
484 |
"outputs": [
|
485 |
{
|
|
|
505 |
{
|
506 |
"cell_type": "code",
|
507 |
"execution_count": 28,
|
508 |
+
"id": "70190a08",
|
509 |
"metadata": {},
|
510 |
"outputs": [],
|
511 |
"source": [
|
|
|
553 |
{
|
554 |
"cell_type": "code",
|
555 |
"execution_count": 33,
|
556 |
+
"id": "ebf6b6bf",
|
557 |
"metadata": {},
|
558 |
"outputs": [
|
559 |
{
|
|
|
580 |
{
|
581 |
"cell_type": "code",
|
582 |
"execution_count": null,
|
583 |
+
"id": "2a5e7bdf",
|
584 |
"metadata": {},
|
585 |
"outputs": [],
|
586 |
"source": []
|
587 |
},
|
588 |
{
|
589 |
"cell_type": "markdown",
|
590 |
+
"id": "3a6c6060",
|
591 |
"metadata": {},
|
592 |
"source": [
|
593 |
"## <u>STEPS FOR IMPLEMENTING<u>\n",
|
|
|
615 |
{
|
616 |
"cell_type": "code",
|
617 |
"execution_count": null,
|
618 |
+
"id": "e4137507",
|
619 |
"metadata": {},
|
620 |
"outputs": [],
|
621 |
"source": []
|
CapiPort.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import yfinance as yf
|
2 |
+
|
3 |
+
from scipy.optimize import minimize
|
4 |
+
|
5 |
+
import pandas as pd
|
6 |
+
import numpy as np
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
|
9 |
+
def get_historical_returns(tickers, start_date, end_date):
|
10 |
+
"""
|
11 |
+
Fetch historical returns data for the given tickers.
|
12 |
+
|
13 |
+
Args:
|
14 |
+
- tickers: list of strings, tickers of assets
|
15 |
+
- start_date: string, start date in the format 'YYYY-MM-DD'
|
16 |
+
- end_date: string, end date in the format 'YYYY-MM-DD'
|
17 |
+
|
18 |
+
Returns:
|
19 |
+
- pandas DataFrame, historical returns data
|
20 |
+
"""
|
21 |
+
data = yf.download(tickers, start=start_date, end=end_date)['Adj Close']
|
22 |
+
returns = data.pct_change().dropna()
|
23 |
+
return returns
|
24 |
+
|
25 |
+
def get_risk_free_rate_india():
|
26 |
+
"""
|
27 |
+
Get the risk-free rate for the Indian market using the yield of the 10-year Indian Government Bond.
|
28 |
+
|
29 |
+
Returns:
|
30 |
+
- float, risk-free rate
|
31 |
+
"""
|
32 |
+
# Ticker symbol for the 10-year Indian Government Bond yield
|
33 |
+
bond_ticker = 'INR=X' # You can replace this with the actual ticker symbol for the bond
|
34 |
+
|
35 |
+
# Fetch the bond data
|
36 |
+
bond_data = yf.Ticker(bond_ticker)
|
37 |
+
|
38 |
+
# Get the latest yield
|
39 |
+
risk_free_rate_india = bond_data.history(period='1d')['Close'][-1] / 100
|
40 |
+
return risk_free_rate_india
|
41 |
+
|
42 |
+
def sharpe_ratio(weights, returns, risk_free_rate):
|
43 |
+
"""
|
44 |
+
Calculate the Sharpe Ratio of a portfolio.
|
45 |
+
|
46 |
+
Args:
|
47 |
+
- weights: array-like, weights of assets in the portfolio
|
48 |
+
- returns: pandas DataFrame, historical returns of assets
|
49 |
+
- risk_free_rate: float, risk-free rate of return
|
50 |
+
|
51 |
+
Returns:
|
52 |
+
- float, Sharpe Ratio of the portfolio
|
53 |
+
"""
|
54 |
+
portfolio_return = np.sum(weights * returns.mean() * 252) # 252 trading days in a year
|
55 |
+
portfolio_std_dev = np.sqrt(np.dot(weights.T, np.dot(returns.cov() * 252, weights)))
|
56 |
+
sharpe_ratio = (portfolio_return - risk_free_rate) / portfolio_std_dev
|
57 |
+
return -sharpe_ratio # Minimize negative Sharpe Ratio for maximization
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
def optimize_portfolio(returns, risk_free_rate):
|
62 |
+
"""
|
63 |
+
Optimize portfolio to maximize the Sharpe Ratio.
|
64 |
+
|
65 |
+
Args:
|
66 |
+
- returns: pandas DataFrame, historical returns of assets
|
67 |
+
- risk_free_rate: float, risk-free rate of return
|
68 |
+
|
69 |
+
Returns:
|
70 |
+
- array, optimal weights of assets in the portfolio
|
71 |
+
"""
|
72 |
+
num_assets = len(returns.columns)
|
73 |
+
initial_weights = np.array([1 / num_assets] * num_assets)
|
74 |
+
bounds = [(0, 1)] * num_assets # Bounds for asset weights (0 <= weight <= 1)
|
75 |
+
constraints = ({'type': 'eq', 'fun': lambda weights: np.sum(weights) - 1}) # Sum of weights equals 1 constraint
|
76 |
+
|
77 |
+
optimized_result = minimize(sharpe_ratio, initial_weights, args=(returns, risk_free_rate),
|
78 |
+
method='SLSQP', bounds=bounds, constraints=constraints)
|
79 |
+
|
80 |
+
return optimized_result.x
|
EQUITY_L.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
main.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import CapiPort
|
2 |
+
import streamlit as st
|
3 |
+
import pandas as pd
|
4 |
+
|
5 |
+
df = pd.read_csv("EQUITY_L.csv")
|
6 |
+
|
7 |
+
CompanySymbol = (df["SYMBOL"] + '.NS').to_list()
|
8 |
+
CompanyName = df["NAME OF COMPANY"].to_list()
|
9 |
+
|
10 |
+
CompanyDict = dict()
|
11 |
+
|
12 |
+
for CSymbol, CName in zip(CompanySymbol, CompanyName):
|
13 |
+
CompanyDict[CName] = CSymbol
|
14 |
+
|
15 |
+
EquitiesInvested = st.multiselect('Select Multiple Companies', CompanyName, default = None)
|
16 |
+
|
17 |
+
st.write("Need at Least two Companies to provide the Optimal Portfolio.")
|
18 |
+
|
19 |
+
if len(EquitiesInvested) > 1:
|
20 |
+
EquitiesInvested = [CompanyDict[i] for i in EquitiesInvested]
|
21 |
+
|
22 |
+
st.write(EquitiesInvested)
|
23 |
+
|
24 |
+
equity_data = CapiPort.get_historical_returns(EquitiesInvested, "1900-01-01", "2024-03-04")
|
25 |
+
|
26 |
+
risk_free_rate = CapiPort.get_risk_free_rate_india()
|
27 |
+
|
28 |
+
optimal_weights = CapiPort.optimize_portfolio(equity_data, risk_free_rate)
|
29 |
+
|
30 |
+
for i,j in zip(EquitiesInvested, optimal_weights):
|
31 |
+
st.write(i, " : ", j)
|