Spaces:
Sleeping
Sleeping
sankhyikii
commited on
Commit
·
79fd3f0
1
Parent(s):
17c56fd
Merge branch 'shyam' of https://github.com/bhanuprasanna527/CapiPort into shyam
Browse files- Notebooks/Untitled.ipynb +156 -9
- main.py +15 -3
Notebooks/Untitled.ipynb
CHANGED
@@ -86,8 +86,92 @@
|
|
86 |
"outputs": [
|
87 |
{
|
88 |
"data": {
|
89 |
-
"text/
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
},
|
92 |
"execution_count": 4,
|
93 |
"metadata": {},
|
@@ -111,7 +195,9 @@
|
|
111 |
"outputs": [
|
112 |
{
|
113 |
"data": {
|
114 |
-
"text/plain":
|
|
|
|
|
115 |
},
|
116 |
"execution_count": 5,
|
117 |
"metadata": {},
|
@@ -150,8 +236,68 @@
|
|
150 |
"outputs": [
|
151 |
{
|
152 |
"data": {
|
153 |
-
"text/
|
154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
},
|
156 |
"execution_count": 7,
|
157 |
"metadata": {},
|
@@ -179,12 +325,13 @@
|
|
179 |
},
|
180 |
{
|
181 |
"cell_type": "code",
|
182 |
-
"
|
183 |
-
"
|
184 |
"metadata": {
|
185 |
"collapsed": false
|
186 |
},
|
187 |
-
"
|
|
|
188 |
}
|
189 |
],
|
190 |
"metadata": {
|
@@ -203,7 +350,7 @@
|
|
203 |
"name": "python",
|
204 |
"nbconvert_exporter": "python",
|
205 |
"pygments_lexer": "ipython3",
|
206 |
-
"version": "3.10.
|
207 |
}
|
208 |
},
|
209 |
"nbformat": 4,
|
|
|
86 |
"outputs": [
|
87 |
{
|
88 |
"data": {
|
89 |
+
"text/html": [
|
90 |
+
"<div>\n",
|
91 |
+
"<style scoped>\n",
|
92 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
93 |
+
" vertical-align: middle;\n",
|
94 |
+
" }\n",
|
95 |
+
"\n",
|
96 |
+
" .dataframe tbody tr th {\n",
|
97 |
+
" vertical-align: top;\n",
|
98 |
+
" }\n",
|
99 |
+
"\n",
|
100 |
+
" .dataframe thead th {\n",
|
101 |
+
" text-align: right;\n",
|
102 |
+
" }\n",
|
103 |
+
"</style>\n",
|
104 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
105 |
+
" <thead>\n",
|
106 |
+
" <tr style=\"text-align: right;\">\n",
|
107 |
+
" <th></th>\n",
|
108 |
+
" <th>0</th>\n",
|
109 |
+
" <th>1</th>\n",
|
110 |
+
" <th>2</th>\n",
|
111 |
+
" <th>3</th>\n",
|
112 |
+
" <th>4</th>\n",
|
113 |
+
" <th>Name</th>\n",
|
114 |
+
" <th>Ticker</th>\n",
|
115 |
+
" </tr>\n",
|
116 |
+
" </thead>\n",
|
117 |
+
" <tbody>\n",
|
118 |
+
" <tr>\n",
|
119 |
+
" <th>count</th>\n",
|
120 |
+
" <td>2062</td>\n",
|
121 |
+
" <td>2062</td>\n",
|
122 |
+
" <td>2062</td>\n",
|
123 |
+
" <td>2062</td>\n",
|
124 |
+
" <td>2062</td>\n",
|
125 |
+
" <td>2062</td>\n",
|
126 |
+
" <td>2062</td>\n",
|
127 |
+
" </tr>\n",
|
128 |
+
" <tr>\n",
|
129 |
+
" <th>unique</th>\n",
|
130 |
+
" <td>2062</td>\n",
|
131 |
+
" <td>2044</td>\n",
|
132 |
+
" <td>2060</td>\n",
|
133 |
+
" <td>2056</td>\n",
|
134 |
+
" <td>125</td>\n",
|
135 |
+
" <td>2061</td>\n",
|
136 |
+
" <td>2062</td>\n",
|
137 |
+
" </tr>\n",
|
138 |
+
" <tr>\n",
|
139 |
+
" <th>top</th>\n",
|
140 |
+
" <td>20 Microns Ltd. 20MICRONS</td>\n",
|
141 |
+
" <td>₹1.14 -4.16%</td>\n",
|
142 |
+
" <td>₹8.8/₹3.79</td>\n",
|
143 |
+
" <td>₹128.05 Crs</td>\n",
|
144 |
+
" <td>Pharmaceuticals</td>\n",
|
145 |
+
" <td>Gallantt Ispat Ltd.</td>\n",
|
146 |
+
" <td>20MICRONS</td>\n",
|
147 |
+
" </tr>\n",
|
148 |
+
" <tr>\n",
|
149 |
+
" <th>freq</th>\n",
|
150 |
+
" <td>1</td>\n",
|
151 |
+
" <td>4</td>\n",
|
152 |
+
" <td>2</td>\n",
|
153 |
+
" <td>2</td>\n",
|
154 |
+
" <td>105</td>\n",
|
155 |
+
" <td>2</td>\n",
|
156 |
+
" <td>1</td>\n",
|
157 |
+
" </tr>\n",
|
158 |
+
" </tbody>\n",
|
159 |
+
"</table>\n",
|
160 |
+
"</div>"
|
161 |
+
],
|
162 |
+
"text/plain": [
|
163 |
+
" 0 1 2 3 \\\n",
|
164 |
+
"count 2062 2062 2062 2062 \n",
|
165 |
+
"unique 2062 2044 2060 2056 \n",
|
166 |
+
"top 20 Microns Ltd. 20MICRONS ₹1.14 -4.16% ₹8.8/₹3.79 ₹128.05 Crs \n",
|
167 |
+
"freq 1 4 2 2 \n",
|
168 |
+
"\n",
|
169 |
+
" 4 Name Ticker \n",
|
170 |
+
"count 2062 2062 2062 \n",
|
171 |
+
"unique 125 2061 2062 \n",
|
172 |
+
"top Pharmaceuticals Gallantt Ispat Ltd. 20MICRONS \n",
|
173 |
+
"freq 105 2 1 "
|
174 |
+
]
|
175 |
},
|
176 |
"execution_count": 4,
|
177 |
"metadata": {},
|
|
|
195 |
"outputs": [
|
196 |
{
|
197 |
"data": {
|
198 |
+
"text/plain": [
|
199 |
+
"2062"
|
200 |
+
]
|
201 |
},
|
202 |
"execution_count": 5,
|
203 |
"metadata": {},
|
|
|
236 |
"outputs": [
|
237 |
{
|
238 |
"data": {
|
239 |
+
"text/html": [
|
240 |
+
"<div>\n",
|
241 |
+
"<style scoped>\n",
|
242 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
243 |
+
" vertical-align: middle;\n",
|
244 |
+
" }\n",
|
245 |
+
"\n",
|
246 |
+
" .dataframe tbody tr th {\n",
|
247 |
+
" vertical-align: top;\n",
|
248 |
+
" }\n",
|
249 |
+
"\n",
|
250 |
+
" .dataframe thead th {\n",
|
251 |
+
" text-align: right;\n",
|
252 |
+
" }\n",
|
253 |
+
"</style>\n",
|
254 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
255 |
+
" <thead>\n",
|
256 |
+
" <tr style=\"text-align: right;\">\n",
|
257 |
+
" <th></th>\n",
|
258 |
+
" <th>0</th>\n",
|
259 |
+
" <th>1</th>\n",
|
260 |
+
" <th>2</th>\n",
|
261 |
+
" <th>3</th>\n",
|
262 |
+
" <th>4</th>\n",
|
263 |
+
" <th>Name</th>\n",
|
264 |
+
" <th>Ticker</th>\n",
|
265 |
+
" </tr>\n",
|
266 |
+
" </thead>\n",
|
267 |
+
" <tbody>\n",
|
268 |
+
" <tr>\n",
|
269 |
+
" <th>610</th>\n",
|
270 |
+
" <td>Gallantt Ispat Ltd.\\n ...</td>\n",
|
271 |
+
" <td>₹64.15 -0.77%</td>\n",
|
272 |
+
" <td>₹76/₹44.64</td>\n",
|
273 |
+
" <td>₹1807.11 Crs</td>\n",
|
274 |
+
" <td>Iron & Steel</td>\n",
|
275 |
+
" <td>Gallantt Ispat Ltd.</td>\n",
|
276 |
+
" <td>GALLISPAT</td>\n",
|
277 |
+
" </tr>\n",
|
278 |
+
" <tr>\n",
|
279 |
+
" <th>611</th>\n",
|
280 |
+
" <td>Gallantt Ispat Ltd.\\n ...</td>\n",
|
281 |
+
" <td>₹216.94 +1.33%</td>\n",
|
282 |
+
" <td>₹236.4/₹49.54</td>\n",
|
283 |
+
" <td>₹5235.8 Crs</td>\n",
|
284 |
+
" <td>Iron & Steel</td>\n",
|
285 |
+
" <td>Gallantt Ispat Ltd.</td>\n",
|
286 |
+
" <td>GALLANTT</td>\n",
|
287 |
+
" </tr>\n",
|
288 |
+
" </tbody>\n",
|
289 |
+
"</table>\n",
|
290 |
+
"</div>"
|
291 |
+
],
|
292 |
+
"text/plain": [
|
293 |
+
" 0 1 \\\n",
|
294 |
+
"610 Gallantt Ispat Ltd.\\n ... ₹64.15 -0.77% \n",
|
295 |
+
"611 Gallantt Ispat Ltd.\\n ... ₹216.94 +1.33% \n",
|
296 |
+
"\n",
|
297 |
+
" 2 3 4 Name Ticker \n",
|
298 |
+
"610 ₹76/₹44.64 ₹1807.11 Crs Iron & Steel Gallantt Ispat Ltd. GALLISPAT \n",
|
299 |
+
"611 ₹236.4/₹49.54 ₹5235.8 Crs Iron & Steel Gallantt Ispat Ltd. GALLANTT "
|
300 |
+
]
|
301 |
},
|
302 |
"execution_count": 7,
|
303 |
"metadata": {},
|
|
|
325 |
},
|
326 |
{
|
327 |
"cell_type": "code",
|
328 |
+
"execution_count": null,
|
329 |
+
"id": "e9274e3c3011e6fc",
|
330 |
"metadata": {
|
331 |
"collapsed": false
|
332 |
},
|
333 |
+
"outputs": [],
|
334 |
+
"source": []
|
335 |
}
|
336 |
],
|
337 |
"metadata": {
|
|
|
350 |
"name": "python",
|
351 |
"nbconvert_exporter": "python",
|
352 |
"pygments_lexer": "ipython3",
|
353 |
+
"version": "3.10.12"
|
354 |
}
|
355 |
},
|
356 |
"nbformat": 4,
|
main.py
CHANGED
@@ -3,6 +3,9 @@ import numpy as np
|
|
3 |
import yfinance as yf
|
4 |
import streamlit as st
|
5 |
import plotly.graph_objects as go
|
|
|
|
|
|
|
6 |
|
7 |
with open(r"style/style.css") as css:
|
8 |
st.markdown(f"<style>{css.read()}</style>", unsafe_allow_html=True)
|
@@ -152,9 +155,13 @@ if num_tick > 1:
|
|
152 |
## Create an Array to store the Sharpe Ratios as they are generated
|
153 |
sharpe_arr = np.zeros(num_of_port)
|
154 |
|
155 |
-
##
|
|
|
|
|
156 |
|
157 |
-
|
|
|
|
|
158 |
## Let's first Calculate the Weights
|
159 |
weig = np.array(np.random.random(num_tick))
|
160 |
weig = weig / np.sum(weig)
|
@@ -170,10 +177,15 @@ if num_tick > 1:
|
|
170 |
|
171 |
## Calculate and Append the Sharpe Ratio to Sharpe Ratio Array
|
172 |
sharpe_arr[ind] = ret_arr[ind] / vol_arr[ind]
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
## Let's create a Data Frame with Weights, Returns, Volatitlity, and the Sharpe Ratio
|
175 |
sim_data = [ret_arr, vol_arr, sharpe_arr, all_weights]
|
176 |
-
|
177 |
## Create a Data Frame using above, then Transpose it
|
178 |
sim_df = pd.DataFrame(data=sim_data).T
|
179 |
|
|
|
3 |
import yfinance as yf
|
4 |
import streamlit as st
|
5 |
import plotly.graph_objects as go
|
6 |
+
import time
|
7 |
+
import sys
|
8 |
+
|
9 |
|
10 |
with open(r"style/style.css") as css:
|
11 |
st.markdown(f"<style>{css.read()}</style>", unsafe_allow_html=True)
|
|
|
155 |
## Create an Array to store the Sharpe Ratios as they are generated
|
156 |
sharpe_arr = np.zeros(num_of_port)
|
157 |
|
158 |
+
## Track Progress with a Bar
|
159 |
+
progress_text = "Operation in progress. Please wait."
|
160 |
+
my_bar = st.progress(0, text=progress_text)
|
161 |
|
162 |
+
## Let's start the Monte Carlo Simulation
|
163 |
+
for ind in range(num_of_port): # Corrected the range to iterate from 0 to num_of_port
|
164 |
+
time.sleep(0.001)
|
165 |
## Let's first Calculate the Weights
|
166 |
weig = np.array(np.random.random(num_tick))
|
167 |
weig = weig / np.sum(weig)
|
|
|
177 |
|
178 |
## Calculate and Append the Sharpe Ratio to Sharpe Ratio Array
|
179 |
sharpe_arr[ind] = ret_arr[ind] / vol_arr[ind]
|
180 |
+
if ind % 100 == 0:
|
181 |
+
print(ind)
|
182 |
+
my_bar.progress((ind + 1) / num_of_port, text=progress_text)
|
183 |
+
# clear progress bar
|
184 |
+
my_bar.empty()
|
185 |
|
186 |
## Let's create a Data Frame with Weights, Returns, Volatitlity, and the Sharpe Ratio
|
187 |
sim_data = [ret_arr, vol_arr, sharpe_arr, all_weights]
|
188 |
+
|
189 |
## Create a Data Frame using above, then Transpose it
|
190 |
sim_df = pd.DataFrame(data=sim_data).T
|
191 |
|