|
| 1 | +import yfinance as yf |
| 2 | +import pandas as pd |
| 3 | +import schedule |
| 4 | +import time |
| 5 | +import matplotlib.pyplot as plt |
| 6 | + |
| 7 | +# Fetch dividend data for a single stock |
| 8 | +def fetch_dividend_data(ticker): |
| 9 | + stock = yf.Ticker(ticker) |
| 10 | + dividend_data = stock.dividends # Fetch dividends history |
| 11 | + return dividend_data |
| 12 | + |
| 13 | +# Calculate total expected dividend income for a single stock |
| 14 | +def calculate_dividend_income(ticker, shares_owned): |
| 15 | + dividends = fetch_dividend_data(ticker) |
| 16 | + if dividends.empty: |
| 17 | + print(f"No dividend data available for {ticker}") |
| 18 | + return 0 |
| 19 | + else: |
| 20 | + # Sum up the total dividends paid in the last year |
| 21 | + total_dividends_per_share = dividends.last('365D').sum() # Dividends in the last 365 days |
| 22 | + total_income = total_dividends_per_share * shares_owned |
| 23 | + return total_income |
| 24 | + |
| 25 | +# Watchlist with the number of shares owned for each stock |
| 26 | +portfolio = { |
| 27 | + "AAPL": 50, # 50 shares of Apple |
| 28 | + "MSFT": 30, # 30 shares of Microsoft |
| 29 | + "T": 100, # 100 shares of AT&T |
| 30 | + "JNJ": 25 # 25 shares of Johnson & Johnson |
| 31 | +} |
| 32 | + |
| 33 | +# Track expected dividend income for all stocks in the portfolio |
| 34 | +def track_dividend_income(portfolio): |
| 35 | + dividend_summary = pd.DataFrame(columns=["Stock", "Shares Owned", "Expected Dividend Income"]) |
| 36 | + |
| 37 | + for stock, shares_owned in portfolio.items(): |
| 38 | + income = calculate_dividend_income(stock, shares_owned) |
| 39 | + dividend_summary = pd.concat([dividend_summary, pd.DataFrame({"Stock": [stock], "Shares Owned": [shares_owned], "Expected Dividend Income": [income]})]) |
| 40 | + |
| 41 | + return dividend_summary |
| 42 | + |
| 43 | +# Create a dashboard to visualize expected dividend income for all stocks |
| 44 | +def create_dashboard(portfolio): |
| 45 | + income_summary = track_dividend_income(portfolio) |
| 46 | + |
| 47 | + fig, ax = plt.subplots(figsize=(10, 6)) |
| 48 | + income_summary.plot(x="Stock", y="Expected Dividend Income", kind="bar", ax=ax) |
| 49 | + plt.title("Expected Dividend Income from Portfolio") |
| 50 | + plt.xlabel("Stock") |
| 51 | + plt.ylabel("Expected Dividend Income (in USD)") |
| 52 | + plt.show() |
| 53 | + |
| 54 | +# Job to calculate and save dividend income daily |
| 55 | +def job(): |
| 56 | + print("Updating expected dividend income data...") |
| 57 | + summary = track_dividend_income(portfolio) |
| 58 | + summary.to_csv('dividend_income_tracker.csv', index=False) |
| 59 | + print(summary) |
| 60 | + |
| 61 | +# Schedule to run every day at 10 AM |
| 62 | +schedule.every().day.at("10:00").do(job) |
| 63 | + |
| 64 | +# Initial calculation and dashboard creation |
| 65 | +create_dashboard(portfolio) |
| 66 | + |
| 67 | +# Start the schedule loop after creating the dashboard |
| 68 | +while True: |
| 69 | + schedule.run_pending() |
| 70 | + time.sleep(1) |
0 commit comments