Spaces:
Sleeping
Sleeping
Bhanu Prasanna
commited on
Commit
·
f37e29b
1
Parent(s):
8fa480e
Update
Browse files- .ipynb_checkpoints/CapiPort-checkpoint.ipynb +179 -5
- CapiPort.ipynb +198 -5
.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",
|
@@ -30,6 +30,14 @@
|
|
30 |
" 1.1) Asset Class choosen - Equity (Stocks)\n",
|
31 |
" 2) Select the Companies which you want to use to build a Portfolio.\n",
|
32 |
" 2.1) Companies choosen - \n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
" 3) To try various Statistical Methods relating to Portfolio Optimisation.\n",
|
34 |
" 3.1) Method 1 - Result\n",
|
35 |
" 3.2) Method 2 - Result\n",
|
@@ -44,7 +52,173 @@
|
|
44 |
},
|
45 |
{
|
46 |
"cell_type": "markdown",
|
47 |
-
"id": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
"metadata": {},
|
49 |
"source": [
|
50 |
"## <u>STEPS FOR IMPLEMENTING<u>\n",
|
@@ -72,7 +246,7 @@
|
|
72 |
{
|
73 |
"cell_type": "code",
|
74 |
"execution_count": null,
|
75 |
-
"id": "
|
76 |
"metadata": {},
|
77 |
"outputs": [],
|
78 |
"source": []
|
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "markdown",
|
5 |
+
"id": "37b7d816",
|
6 |
"metadata": {},
|
7 |
"source": [
|
8 |
"# CapiPort - PORTFOLIO OPTIMISATION"
|
|
|
10 |
},
|
11 |
{
|
12 |
"cell_type": "markdown",
|
13 |
+
"id": "b66aca9c",
|
14 |
"metadata": {},
|
15 |
"source": [
|
16 |
" Two things to consider for Portfolio Optimisation:\n",
|
|
|
21 |
},
|
22 |
{
|
23 |
"cell_type": "markdown",
|
24 |
+
"id": "d0c0e49d",
|
25 |
"metadata": {},
|
26 |
"source": [
|
27 |
" Basic process of Portfolio Optimisation:\n",
|
|
|
30 |
" 1.1) Asset Class choosen - Equity (Stocks)\n",
|
31 |
" 2) Select the Companies which you want to use to build a Portfolio.\n",
|
32 |
" 2.1) Companies choosen - \n",
|
33 |
+
" 2.1.1) Tata Power - TATAPOWER.NS\n",
|
34 |
+
" 2.1.2) Tata Motors - TATAMOTORS.NS\n",
|
35 |
+
" 2.1.3) Tata Steel - TATASTEEL.NS\n",
|
36 |
+
" 2.1.4) Zomato - ZOMATO.NS\n",
|
37 |
+
" 2.1.5) NHPC - NHPC.NS\n",
|
38 |
+
" 2.1.6) NCC - NCC.NS\n",
|
39 |
+
" 2.1.7) IREDA - IREDA.NS\n",
|
40 |
+
" 2.1.8) IRCON - IRCON.NS\n",
|
41 |
" 3) To try various Statistical Methods relating to Portfolio Optimisation.\n",
|
42 |
" 3.1) Method 1 - Result\n",
|
43 |
" 3.2) Method 2 - Result\n",
|
|
|
52 |
},
|
53 |
{
|
54 |
"cell_type": "markdown",
|
55 |
+
"id": "dbfe76bf",
|
56 |
+
"metadata": {},
|
57 |
+
"source": [
|
58 |
+
"## Importing the Libraries"
|
59 |
+
]
|
60 |
+
},
|
61 |
+
{
|
62 |
+
"cell_type": "code",
|
63 |
+
"execution_count": 15,
|
64 |
+
"id": "20883492",
|
65 |
+
"metadata": {},
|
66 |
+
"outputs": [],
|
67 |
+
"source": [
|
68 |
+
"import yfinance as yf\n",
|
69 |
+
"\n",
|
70 |
+
"\n",
|
71 |
+
"from scipy.optimize import minimize\n",
|
72 |
+
"\n",
|
73 |
+
"\n",
|
74 |
+
"\n",
|
75 |
+
"import pandas as pd\n",
|
76 |
+
"import numpy as np\n",
|
77 |
+
"import matplotlib.pyplot as plt"
|
78 |
+
]
|
79 |
+
},
|
80 |
+
{
|
81 |
+
"cell_type": "markdown",
|
82 |
+
"id": "a0a3c96e",
|
83 |
+
"metadata": {},
|
84 |
+
"source": [
|
85 |
+
"## Get Stock Data"
|
86 |
+
]
|
87 |
+
},
|
88 |
+
{
|
89 |
+
"cell_type": "code",
|
90 |
+
"execution_count": 16,
|
91 |
+
"id": "f3937e5a",
|
92 |
+
"metadata": {},
|
93 |
+
"outputs": [],
|
94 |
+
"source": [
|
95 |
+
"def get_historical_returns(tickers, start_date, end_date):\n",
|
96 |
+
" \"\"\"\n",
|
97 |
+
" Fetch historical returns data for the given tickers.\n",
|
98 |
+
"\n",
|
99 |
+
" Args:\n",
|
100 |
+
" - tickers: list of strings, tickers of assets\n",
|
101 |
+
" - start_date: string, start date in the format 'YYYY-MM-DD'\n",
|
102 |
+
" - end_date: string, end date in the format 'YYYY-MM-DD'\n",
|
103 |
+
"\n",
|
104 |
+
" Returns:\n",
|
105 |
+
" - pandas DataFrame, historical returns data\n",
|
106 |
+
" \"\"\"\n",
|
107 |
+
" data = yf.download(tickers, start=start_date, end=end_date)['Adj Close']\n",
|
108 |
+
" returns = data.pct_change().dropna()\n",
|
109 |
+
" return returns\n",
|
110 |
+
"\n",
|
111 |
+
"def get_risk_free_rate_india():\n",
|
112 |
+
" \"\"\"\n",
|
113 |
+
" Get the risk-free rate for the Indian market using the yield of the 10-year Indian Government Bond.\n",
|
114 |
+
"\n",
|
115 |
+
" Returns:\n",
|
116 |
+
" - float, risk-free rate\n",
|
117 |
+
" \"\"\"\n",
|
118 |
+
" # Ticker symbol for the 10-year Indian Government Bond yield\n",
|
119 |
+
" bond_ticker = 'INR=X' # You can replace this with the actual ticker symbol for the bond\n",
|
120 |
+
"\n",
|
121 |
+
" # Fetch the bond data\n",
|
122 |
+
" bond_data = yf.Ticker(bond_ticker)\n",
|
123 |
+
"\n",
|
124 |
+
" # Get the latest yield\n",
|
125 |
+
" risk_free_rate_india = bond_data.history(period='1d')['Close'][-1] / 100\n",
|
126 |
+
" return risk_free_rate_india"
|
127 |
+
]
|
128 |
+
},
|
129 |
+
{
|
130 |
+
"cell_type": "code",
|
131 |
+
"execution_count": 26,
|
132 |
+
"id": "0a9e0e4e",
|
133 |
+
"metadata": {},
|
134 |
+
"outputs": [
|
135 |
+
{
|
136 |
+
"name": "stderr",
|
137 |
+
"output_type": "stream",
|
138 |
+
"text": [
|
139 |
+
"[*********************100%%**********************] 8 of 8 completed\n",
|
140 |
+
"/var/folders/jx/_r4pg95j3pzdd581p_wql9pc0000gn/T/ipykernel_17224/562231645.py:14: FutureWarning: The default fill_method='pad' in DataFrame.pct_change is deprecated and will be removed in a future version. Call ffill before calling pct_change to retain current behavior and silence this warning.\n",
|
141 |
+
" returns = data.pct_change().dropna()\n",
|
142 |
+
"/var/folders/jx/_r4pg95j3pzdd581p_wql9pc0000gn/T/ipykernel_17224/4250087689.py:15: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
|
143 |
+
" risk_free_rate_india = bond_data.history(period='1d')['Close'][-1] / 100\n"
|
144 |
+
]
|
145 |
+
}
|
146 |
+
],
|
147 |
+
"source": [
|
148 |
+
"equity_list = [\"TATAPOWER.NS\", \"TATAMOTORS.NS\", \"TATASTEEL.NS\", \"ZOMATO.NS\", \"NHPC.NS\", \"NCC.NS\", \"IREDA.NS\", \"IRCON.NS\"]\n",
|
149 |
+
"\n",
|
150 |
+
"equity_data = get_historical_returns(equity_list, \"1900-01-01\", \"2024-03-04\")\n",
|
151 |
+
"\n",
|
152 |
+
"risk_free_rate = get_risk_free_rate_india()"
|
153 |
+
]
|
154 |
+
},
|
155 |
+
{
|
156 |
+
"cell_type": "code",
|
157 |
+
"execution_count": null,
|
158 |
+
"id": "2ad5b748",
|
159 |
+
"metadata": {},
|
160 |
+
"outputs": [],
|
161 |
+
"source": [
|
162 |
+
"def sharpe_ratio(weights, returns, risk_free_rate):\n",
|
163 |
+
" \"\"\"\n",
|
164 |
+
" Calculate the Sharpe Ratio of a portfolio.\n",
|
165 |
+
"\n",
|
166 |
+
" Args:\n",
|
167 |
+
" - weights: array-like, weights of assets in the portfolio\n",
|
168 |
+
" - returns: pandas DataFrame, historical returns of assets\n",
|
169 |
+
" - risk_free_rate: float, risk-free rate of return\n",
|
170 |
+
"\n",
|
171 |
+
" Returns:\n",
|
172 |
+
" - float, Sharpe Ratio of the portfolio\n",
|
173 |
+
" \"\"\"\n",
|
174 |
+
" portfolio_return = np.sum(weights * returns.mean() * 252) # 252 trading days in a year\n",
|
175 |
+
" portfolio_std_dev = np.sqrt(np.dot(weights.T, np.dot(returns.cov() * 252, weights)))\n",
|
176 |
+
" sharpe_ratio = (portfolio_return - risk_free_rate) / portfolio_std_dev\n",
|
177 |
+
" return -sharpe_ratio # Minimize negative Sharpe Ratio for maximization\n",
|
178 |
+
"\n",
|
179 |
+
"\n",
|
180 |
+
"\n",
|
181 |
+
"def optimize_portfolio(returns, risk_free_rate):\n",
|
182 |
+
" \"\"\"\n",
|
183 |
+
" Optimize portfolio to maximize the Sharpe Ratio.\n",
|
184 |
+
"\n",
|
185 |
+
" Args:\n",
|
186 |
+
" - returns: pandas DataFrame, historical returns of assets\n",
|
187 |
+
" - risk_free_rate: float, risk-free rate of return\n",
|
188 |
+
"\n",
|
189 |
+
" Returns:\n",
|
190 |
+
" - array, optimal weights of assets in the portfolio\n",
|
191 |
+
" \"\"\"\n",
|
192 |
+
" num_assets = len(returns.columns)\n",
|
193 |
+
" initial_weights = np.array([1 / num_assets] * num_assets)\n",
|
194 |
+
" bounds = [(0, 1)] * num_assets # Bounds for asset weights (0 <= weight <= 1)\n",
|
195 |
+
" constraints = ({'type': 'eq', 'fun': lambda weights: np.sum(weights) - 1}) # Sum of weights equals 1 constraint\n",
|
196 |
+
"\n",
|
197 |
+
" optimized_result = minimize(sharpe_ratio, initial_weights, args=(returns, risk_free_rate),\n",
|
198 |
+
" method='SLSQP', bounds=bounds, constraints=constraints)\n",
|
199 |
+
"\n",
|
200 |
+
" return optimized_result.x"
|
201 |
+
]
|
202 |
+
},
|
203 |
+
{
|
204 |
+
"cell_type": "code",
|
205 |
+
"execution_count": null,
|
206 |
+
"id": "afa17f35",
|
207 |
+
"metadata": {},
|
208 |
+
"outputs": [],
|
209 |
+
"source": []
|
210 |
+
},
|
211 |
+
{
|
212 |
+
"cell_type": "code",
|
213 |
+
"execution_count": null,
|
214 |
+
"id": "a17e46ca",
|
215 |
+
"metadata": {},
|
216 |
+
"outputs": [],
|
217 |
+
"source": []
|
218 |
+
},
|
219 |
+
{
|
220 |
+
"cell_type": "markdown",
|
221 |
+
"id": "38f5c7c7",
|
222 |
"metadata": {},
|
223 |
"source": [
|
224 |
"## <u>STEPS FOR IMPLEMENTING<u>\n",
|
|
|
246 |
{
|
247 |
"cell_type": "code",
|
248 |
"execution_count": null,
|
249 |
+
"id": "df48c03f",
|
250 |
"metadata": {},
|
251 |
"outputs": [],
|
252 |
"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",
|
@@ -30,6 +30,14 @@
|
|
30 |
" 1.1) Asset Class choosen - Equity (Stocks)\n",
|
31 |
" 2) Select the Companies which you want to use to build a Portfolio.\n",
|
32 |
" 2.1) Companies choosen - \n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
" 3) To try various Statistical Methods relating to Portfolio Optimisation.\n",
|
34 |
" 3.1) Method 1 - Result\n",
|
35 |
" 3.2) Method 2 - Result\n",
|
@@ -44,7 +52,192 @@
|
|
44 |
},
|
45 |
{
|
46 |
"cell_type": "markdown",
|
47 |
-
"id": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
"metadata": {},
|
49 |
"source": [
|
50 |
"## <u>STEPS FOR IMPLEMENTING<u>\n",
|
@@ -72,7 +265,7 @@
|
|
72 |
{
|
73 |
"cell_type": "code",
|
74 |
"execution_count": null,
|
75 |
-
"id": "
|
76 |
"metadata": {},
|
77 |
"outputs": [],
|
78 |
"source": []
|
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "markdown",
|
5 |
+
"id": "db58d421",
|
6 |
"metadata": {},
|
7 |
"source": [
|
8 |
"# CapiPort - PORTFOLIO OPTIMISATION"
|
|
|
10 |
},
|
11 |
{
|
12 |
"cell_type": "markdown",
|
13 |
+
"id": "a9c51b1c",
|
14 |
"metadata": {},
|
15 |
"source": [
|
16 |
" Two things to consider for Portfolio Optimisation:\n",
|
|
|
21 |
},
|
22 |
{
|
23 |
"cell_type": "markdown",
|
24 |
+
"id": "80e6fed1",
|
25 |
"metadata": {},
|
26 |
"source": [
|
27 |
" Basic process of Portfolio Optimisation:\n",
|
|
|
30 |
" 1.1) Asset Class choosen - Equity (Stocks)\n",
|
31 |
" 2) Select the Companies which you want to use to build a Portfolio.\n",
|
32 |
" 2.1) Companies choosen - \n",
|
33 |
+
" 2.1.1) Tata Power - TATAPOWER.NS\n",
|
34 |
+
" 2.1.2) Tata Motors - TATAMOTORS.NS\n",
|
35 |
+
" 2.1.3) Tata Steel - TATASTEEL.NS\n",
|
36 |
+
" 2.1.4) Zomato - ZOMATO.NS\n",
|
37 |
+
" 2.1.5) NHPC - NHPC.NS\n",
|
38 |
+
" 2.1.6) NCC - NCC.NS\n",
|
39 |
+
" 2.1.7) IREDA - IREDA.NS\n",
|
40 |
+
" 2.1.8) IRCON - IRCON.NS\n",
|
41 |
" 3) To try various Statistical Methods relating to Portfolio Optimisation.\n",
|
42 |
" 3.1) Method 1 - Result\n",
|
43 |
" 3.2) Method 2 - Result\n",
|
|
|
52 |
},
|
53 |
{
|
54 |
"cell_type": "markdown",
|
55 |
+
"id": "e630d5b1",
|
56 |
+
"metadata": {},
|
57 |
+
"source": [
|
58 |
+
"## Importing the Libraries"
|
59 |
+
]
|
60 |
+
},
|
61 |
+
{
|
62 |
+
"cell_type": "code",
|
63 |
+
"execution_count": 15,
|
64 |
+
"id": "7de120f8",
|
65 |
+
"metadata": {},
|
66 |
+
"outputs": [],
|
67 |
+
"source": [
|
68 |
+
"import yfinance as yf\n",
|
69 |
+
"\n",
|
70 |
+
"\n",
|
71 |
+
"from scipy.optimize import minimize\n",
|
72 |
+
"\n",
|
73 |
+
"\n",
|
74 |
+
"\n",
|
75 |
+
"import pandas as pd\n",
|
76 |
+
"import numpy as np\n",
|
77 |
+
"import matplotlib.pyplot as plt"
|
78 |
+
]
|
79 |
+
},
|
80 |
+
{
|
81 |
+
"cell_type": "markdown",
|
82 |
+
"id": "91fcb51f",
|
83 |
+
"metadata": {},
|
84 |
+
"source": [
|
85 |
+
"## Get Stock Data"
|
86 |
+
]
|
87 |
+
},
|
88 |
+
{
|
89 |
+
"cell_type": "code",
|
90 |
+
"execution_count": 16,
|
91 |
+
"id": "4f30295f",
|
92 |
+
"metadata": {},
|
93 |
+
"outputs": [],
|
94 |
+
"source": [
|
95 |
+
"def get_historical_returns(tickers, start_date, end_date):\n",
|
96 |
+
" \"\"\"\n",
|
97 |
+
" Fetch historical returns data for the given tickers.\n",
|
98 |
+
"\n",
|
99 |
+
" Args:\n",
|
100 |
+
" - tickers: list of strings, tickers of assets\n",
|
101 |
+
" - start_date: string, start date in the format 'YYYY-MM-DD'\n",
|
102 |
+
" - end_date: string, end date in the format 'YYYY-MM-DD'\n",
|
103 |
+
"\n",
|
104 |
+
" Returns:\n",
|
105 |
+
" - pandas DataFrame, historical returns data\n",
|
106 |
+
" \"\"\"\n",
|
107 |
+
" data = yf.download(tickers, start=start_date, end=end_date)['Adj Close']\n",
|
108 |
+
" returns = data.pct_change().dropna()\n",
|
109 |
+
" return returns\n",
|
110 |
+
"\n",
|
111 |
+
"def get_risk_free_rate_india():\n",
|
112 |
+
" \"\"\"\n",
|
113 |
+
" Get the risk-free rate for the Indian market using the yield of the 10-year Indian Government Bond.\n",
|
114 |
+
"\n",
|
115 |
+
" Returns:\n",
|
116 |
+
" - float, risk-free rate\n",
|
117 |
+
" \"\"\"\n",
|
118 |
+
" # Ticker symbol for the 10-year Indian Government Bond yield\n",
|
119 |
+
" bond_ticker = 'INR=X' # You can replace this with the actual ticker symbol for the bond\n",
|
120 |
+
"\n",
|
121 |
+
" # Fetch the bond data\n",
|
122 |
+
" bond_data = yf.Ticker(bond_ticker)\n",
|
123 |
+
"\n",
|
124 |
+
" # Get the latest yield\n",
|
125 |
+
" risk_free_rate_india = bond_data.history(period='1d')['Close'][-1] / 100\n",
|
126 |
+
" return risk_free_rate_india"
|
127 |
+
]
|
128 |
+
},
|
129 |
+
{
|
130 |
+
"cell_type": "code",
|
131 |
+
"execution_count": 26,
|
132 |
+
"id": "c2f02cbe",
|
133 |
+
"metadata": {},
|
134 |
+
"outputs": [
|
135 |
+
{
|
136 |
+
"name": "stderr",
|
137 |
+
"output_type": "stream",
|
138 |
+
"text": [
|
139 |
+
"[*********************100%%**********************] 8 of 8 completed\n",
|
140 |
+
"/var/folders/jx/_r4pg95j3pzdd581p_wql9pc0000gn/T/ipykernel_17224/562231645.py:14: FutureWarning: The default fill_method='pad' in DataFrame.pct_change is deprecated and will be removed in a future version. Call ffill before calling pct_change to retain current behavior and silence this warning.\n",
|
141 |
+
" returns = data.pct_change().dropna()\n",
|
142 |
+
"/var/folders/jx/_r4pg95j3pzdd581p_wql9pc0000gn/T/ipykernel_17224/4250087689.py:15: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
|
143 |
+
" risk_free_rate_india = bond_data.history(period='1d')['Close'][-1] / 100\n"
|
144 |
+
]
|
145 |
+
}
|
146 |
+
],
|
147 |
+
"source": [
|
148 |
+
"equity_list = [\"TATAPOWER.NS\", \"TATAMOTORS.NS\", \"TATASTEEL.NS\", \"ZOMATO.NS\", \"NHPC.NS\", \"NCC.NS\", \"IREDA.NS\", \"IRCON.NS\"]\n",
|
149 |
+
"\n",
|
150 |
+
"equity_data = get_historical_returns(equity_list, \"1900-01-01\", \"2024-03-04\")\n",
|
151 |
+
"\n",
|
152 |
+
"risk_free_rate = get_risk_free_rate_india()"
|
153 |
+
]
|
154 |
+
},
|
155 |
+
{
|
156 |
+
"cell_type": "code",
|
157 |
+
"execution_count": 28,
|
158 |
+
"id": "b9cb34f2",
|
159 |
+
"metadata": {},
|
160 |
+
"outputs": [],
|
161 |
+
"source": [
|
162 |
+
"def sharpe_ratio(weights, returns, risk_free_rate):\n",
|
163 |
+
" \"\"\"\n",
|
164 |
+
" Calculate the Sharpe Ratio of a portfolio.\n",
|
165 |
+
"\n",
|
166 |
+
" Args:\n",
|
167 |
+
" - weights: array-like, weights of assets in the portfolio\n",
|
168 |
+
" - returns: pandas DataFrame, historical returns of assets\n",
|
169 |
+
" - risk_free_rate: float, risk-free rate of return\n",
|
170 |
+
"\n",
|
171 |
+
" Returns:\n",
|
172 |
+
" - float, Sharpe Ratio of the portfolio\n",
|
173 |
+
" \"\"\"\n",
|
174 |
+
" portfolio_return = np.sum(weights * returns.mean() * 252) # 252 trading days in a year\n",
|
175 |
+
" portfolio_std_dev = np.sqrt(np.dot(weights.T, np.dot(returns.cov() * 252, weights)))\n",
|
176 |
+
" sharpe_ratio = (portfolio_return - risk_free_rate) / portfolio_std_dev\n",
|
177 |
+
" return -sharpe_ratio # Minimize negative Sharpe Ratio for maximization\n",
|
178 |
+
"\n",
|
179 |
+
"\n",
|
180 |
+
"\n",
|
181 |
+
"def optimize_portfolio(returns, risk_free_rate):\n",
|
182 |
+
" \"\"\"\n",
|
183 |
+
" Optimize portfolio to maximize the Sharpe Ratio.\n",
|
184 |
+
"\n",
|
185 |
+
" Args:\n",
|
186 |
+
" - returns: pandas DataFrame, historical returns of assets\n",
|
187 |
+
" - risk_free_rate: float, risk-free rate of return\n",
|
188 |
+
"\n",
|
189 |
+
" Returns:\n",
|
190 |
+
" - array, optimal weights of assets in the portfolio\n",
|
191 |
+
" \"\"\"\n",
|
192 |
+
" num_assets = len(returns.columns)\n",
|
193 |
+
" initial_weights = np.array([1 / num_assets] * num_assets)\n",
|
194 |
+
" bounds = [(0, 1)] * num_assets # Bounds for asset weights (0 <= weight <= 1)\n",
|
195 |
+
" constraints = ({'type': 'eq', 'fun': lambda weights: np.sum(weights) - 1}) # Sum of weights equals 1 constraint\n",
|
196 |
+
"\n",
|
197 |
+
" optimized_result = minimize(sharpe_ratio, initial_weights, args=(returns, risk_free_rate),\n",
|
198 |
+
" method='SLSQP', bounds=bounds, constraints=constraints)\n",
|
199 |
+
"\n",
|
200 |
+
" return optimized_result.x"
|
201 |
+
]
|
202 |
+
},
|
203 |
+
{
|
204 |
+
"cell_type": "code",
|
205 |
+
"execution_count": 33,
|
206 |
+
"id": "6af1a0ad",
|
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": "39b7e975",
|
234 |
+
"metadata": {},
|
235 |
+
"outputs": [],
|
236 |
+
"source": []
|
237 |
+
},
|
238 |
+
{
|
239 |
+
"cell_type": "markdown",
|
240 |
+
"id": "a672bdae",
|
241 |
"metadata": {},
|
242 |
"source": [
|
243 |
"## <u>STEPS FOR IMPLEMENTING<u>\n",
|
|
|
265 |
{
|
266 |
"cell_type": "code",
|
267 |
"execution_count": null,
|
268 |
+
"id": "00d89b09",
|
269 |
"metadata": {},
|
270 |
"outputs": [],
|
271 |
"source": []
|