Update utils.py
Browse files
utils.py
CHANGED
|
@@ -299,36 +299,93 @@ def generate_portfolio(
|
|
| 299 |
|
| 300 |
allocation = []
|
| 301 |
|
| 302 |
-
|
| 303 |
|
|
|
|
| 304 |
for stock, weight in zip(ALL_STOCKS, weights[:-1]):
|
| 305 |
|
| 306 |
-
|
| 307 |
|
| 308 |
price = latest_prices[stock]
|
| 309 |
|
| 310 |
-
shares = int(
|
| 311 |
|
| 312 |
invested = shares * price
|
| 313 |
|
| 314 |
-
|
| 315 |
|
| 316 |
allocation.append({
|
| 317 |
|
| 318 |
"Stock": stock,
|
| 319 |
-
"Price (₹)": round(price,
|
|
|
|
| 320 |
"Shares": shares,
|
| 321 |
-
"Investment (₹)":
|
| 322 |
-
"Weight (%)": invested / budget * 100
|
| 323 |
|
| 324 |
})
|
| 325 |
|
| 326 |
allocation = pd.DataFrame(allocation)
|
| 327 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
allocation = allocation[
|
| 329 |
allocation["Shares"] > 0
|
| 330 |
]
|
| 331 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
allocation = allocation.sort_values(
|
| 333 |
"Investment (₹)",
|
| 334 |
ascending=False
|
|
@@ -339,7 +396,7 @@ def generate_portfolio(
|
|
| 339 |
inplace=True
|
| 340 |
)
|
| 341 |
|
| 342 |
-
return allocation,
|
| 343 |
|
| 344 |
|
| 345 |
# =====================================================
|
|
|
|
| 299 |
|
| 300 |
allocation = []
|
| 301 |
|
| 302 |
+
cash = budget * weights[-1]
|
| 303 |
|
| 304 |
+
# Initial purchase
|
| 305 |
for stock, weight in zip(ALL_STOCKS, weights[:-1]):
|
| 306 |
|
| 307 |
+
target = budget * weight
|
| 308 |
|
| 309 |
price = latest_prices[stock]
|
| 310 |
|
| 311 |
+
shares = int(target // price)
|
| 312 |
|
| 313 |
invested = shares * price
|
| 314 |
|
| 315 |
+
cash += target - invested
|
| 316 |
|
| 317 |
allocation.append({
|
| 318 |
|
| 319 |
"Stock": stock,
|
| 320 |
+
"Price (₹)": round(price,2),
|
| 321 |
+
"Target": target,
|
| 322 |
"Shares": shares,
|
| 323 |
+
"Investment (₹)": invested
|
|
|
|
| 324 |
|
| 325 |
})
|
| 326 |
|
| 327 |
allocation = pd.DataFrame(allocation)
|
| 328 |
|
| 329 |
+
# ---------------------------------------------------
|
| 330 |
+
# Redistribute remaining cash
|
| 331 |
+
# ---------------------------------------------------
|
| 332 |
+
|
| 333 |
+
while True:
|
| 334 |
+
|
| 335 |
+
affordable = allocation[
|
| 336 |
+
allocation["Price (₹)"] <= cash
|
| 337 |
+
]
|
| 338 |
+
|
| 339 |
+
if affordable.empty:
|
| 340 |
+
break
|
| 341 |
+
|
| 342 |
+
# Remaining amount needed to reach target
|
| 343 |
+
affordable = affordable.copy()
|
| 344 |
+
|
| 345 |
+
affordable["Gap"] = (
|
| 346 |
+
affordable["Target"] -
|
| 347 |
+
affordable["Investment (₹)"]
|
| 348 |
+
)
|
| 349 |
+
|
| 350 |
+
affordable = affordable.sort_values(
|
| 351 |
+
"Gap",
|
| 352 |
+
ascending=False
|
| 353 |
+
)
|
| 354 |
+
|
| 355 |
+
bought = False
|
| 356 |
+
|
| 357 |
+
for idx in affordable.index:
|
| 358 |
+
|
| 359 |
+
price = allocation.loc[idx, "Price (₹)"]
|
| 360 |
+
|
| 361 |
+
if price <= cash:
|
| 362 |
+
|
| 363 |
+
allocation.loc[idx, "Shares"] += 1
|
| 364 |
+
|
| 365 |
+
allocation.loc[idx, "Investment (₹)"] += price
|
| 366 |
+
|
| 367 |
+
cash -= price
|
| 368 |
+
|
| 369 |
+
bought = True
|
| 370 |
+
|
| 371 |
+
break
|
| 372 |
+
|
| 373 |
+
if not bought:
|
| 374 |
+
break
|
| 375 |
+
|
| 376 |
allocation = allocation[
|
| 377 |
allocation["Shares"] > 0
|
| 378 |
]
|
| 379 |
|
| 380 |
+
allocation["Weight (%)"] = (
|
| 381 |
+
allocation["Investment (₹)"] /
|
| 382 |
+
budget
|
| 383 |
+
) * 100
|
| 384 |
+
|
| 385 |
+
allocation = allocation.drop(
|
| 386 |
+
columns="Target"
|
| 387 |
+
)
|
| 388 |
+
|
| 389 |
allocation = allocation.sort_values(
|
| 390 |
"Investment (₹)",
|
| 391 |
ascending=False
|
|
|
|
| 396 |
inplace=True
|
| 397 |
)
|
| 398 |
|
| 399 |
+
return allocation, round(cash,2)
|
| 400 |
|
| 401 |
|
| 402 |
# =====================================================
|