Maximizing Dividends: A Step-by-Step Algorithm 💰

Jan 25, 2025

Investing in dividend-yielding stocks can be a great way to generate passive income. The key to maximizing your returns lies in selecting the right stocks and allocating your capital strategically. This blog post walks you through an algorithm to maximize dividends using Python, complete with code snippets and explanations.


Problem Statement

Given a list of stocks with their respective last traded price (LTP) and dividend yield, and a fixed capital, how can we:

  1. Select the best stocks to invest in?
  2. Allocate the capital efficiently to maximize dividends?

Goal: Maximize total dividend amount while investing a minimal amount of capital.


Algorithm Overview

Steps:

  1. Sort stocks by dividend yield: Start by prioritizing stocks offering the highest dividend yield.
  2. Allocate capital strategically: Invest in the highest-yielding stocks while adhering to constraints:
    • Total investment per stock should not exceed 20% of the total capital. Just to reduce risk of portfolio and diversify it.
    • Ensure you stay within your available budget.
  3. Calculate dividends: Track the total dividends and investments made.

Code Implementation

1. Sorting Stocks by Dividend Yield

def print_stocks_by_yield(stocks):
    # Step 1: Sort stocks based on dividend yield (highest to lowest)
    stocks_sorted = sorted(stocks, key=lambda x: x['yield'], reverse=True)

    # Print the stocks in decreasing order of dividend yield
    print("Stocks sorted by Dividend Yield (high to low):")
    for stock in stocks_sorted:
        print(f"{stock['name']} - LTP: {stock['ltp']}, Yield: {stock['yield']}%")

    return stocks_sorted

2. Maximizing Dividends

def maximize_dividends(stocks, capital):
    # Sort stocks by dividend yield in descending order
    stocks_sorted = print_stocks_by_yield(stocks)

    total_dividend = 0
    total_investment = 0
    stock_purchases = []  # List to keep track of purchased stocks and number of shares

    # Define max allowable investment per stock (20% of total capital)
    max_investment_per_stock = 0.20 * capital

    # Step 3: Invest in stocks until the capital is exhausted
    for stock in stocks_sorted:
        price_per_share = stock['ltp']

        # Calculate how many shares we can buy without exceeding the capital
        max_shares = min((capital - total_investment) // price_per_share, max_investment_per_stock // price_per_share)

        if max_shares > 0:
            # Update total investment and calculate dividend from yield
            total_investment += max_shares * price_per_share
            total_dividend += (max_shares * price_per_share) * (stock['yield'] / 100)

            # Append the stock and number of shares to the purchases list
            stock_purchases.append({
                'name': stock['name'],
                'shares': max_shares,
                'investment': max_shares * price_per_share
            })

        # Stop if capital is fully utilized
        if total_investment >= capital:
            break

    return stock_purchases, total_dividend, total_investment

3. Example Execution

Here’s an example to demonstrate the algorithm in action:

# Input data: list of stocks with LTP and dividend yield
stocks = [
    {'name': 'COAL India', 'ltp': 383.05, 'yield': 6.66},
    {'name': 'BPCL', 'ltp': 263.80, 'yield': 7.96},
    {'name': 'ONGC', 'ltp': 256.51, 'yield': 4.78},
    {'name': 'DBCROP', 'ltp': 259.10, 'yield': 5.02},
    {'name': 'Embassy', 'ltp': 365, 'yield': 7.81},
    {'name': 'Great Eastern', 'ltp': 938, 'yield': 3.07},
    {'name': 'Castrol', 'ltp': 175, 'yield': 4.29},
    {'name': 'IL&FS', 'ltp': 10.6, 'yield': 6.60},
    {'name': 'Pipavav', 'ltp': 151, 'yield': 4.84},
    {'name': 'TV Today', 'ltp': 198, 'yield': 4.30}
]

# Capital to invest
capital = 100000

# Execute the algorithm
purchases, total_dividend, total_investment = maximize_dividends(stocks, capital)

# Output the results
print("\nStocks to Buy:")
for purchase in purchases:
    print(f"{purchase['name']}: {purchase['shares']} shares, Investment: {purchase['investment']}")

print(f"\nTotal Dividend: {total_dividend}")
print(f"Total Investment: {total_investment}")

Sample Output

Stocks sorted by Dividend Yield (high to low):
BPCL - LTP: 263.8, Yield: 7.96%
Embassy - LTP: 365, Yield: 7.81%
COAL India - LTP: 383.05, Yield: 6.66%
IL&FS - LTP: 10.6, Yield: 6.6%
DBCROP - LTP: 259.1, Yield: 5.02%
Pipavav - LTP: 151, Yield: 4.84%
ONGC - LTP: 256.51, Yield: 4.78%
TV Today - LTP: 198, Yield: 4.3%
Castrol - LTP: 175, Yield: 4.29%
Great Eastern - LTP: 938, Yield: 3.07%

Stocks to Buy:
BPCL: 75.0 shares, Investment: 19785.0
Embassy: 54.0 shares, Investment: 19710.0
COAL India: 52.0 shares, Investment: 19918.6
IL&FS: 1886.0 shares, Investment: 19991.6
DBCROP: 77.0 shares, Investment: 19950.7
Pipavav: 4.0 shares, Investment: 604.0

Total Dividend: 6791.0201
Total Investment: 99959.9

Key Takeaways

  1. Sorting by dividend yield ensures we maximize returns on investment.
  2. Capital constraints and stock-specific investment caps are crucial for risk management.
  3. This approach provides a systematic way to allocate capital efficiently among dividend-paying stocks.

Happy investing!

Last updated: Jan 25, 2025

Ravi Vaniya