Last active
January 4, 2021 22:38
-
-
Save cdtweb/b888f0f6f5c68d7d82b467124262a37a to your computer and use it in GitHub Desktop.
Simple Rolling DRIP Estimator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
This script will take into account your current positions and calculate your dividend reinvestment over time. | |
@todo Pull positions from somewhere, a local json file maybe? | |
@todo Pull live-ish position data so stockPrice isn't stale. | |
@todo Use of CLI arguments | |
@todo There is a bug with addMonthly that really inflates the number of shares | |
@todo Fractional shares on addMonthly? | |
@todo Turn into a class... slipper slope | |
*/ | |
$positions = [ | |
'SRET' => ['stockPrice' => 8.73, 'dividendTerm' => 'monthly', 'dividendPercent' => 8.88, 'shares' => 158.903, 'addMonthly' => 0], | |
'SPY' => ['stockPrice' => 369.06, 'dividendTerm' => 'quarterly', 'dividendPercent' => 1.52, 'shares' => 4.032, 'addMonthly' => 0], | |
]; | |
// Calculate intial values | |
foreach($positions as &$pos){ | |
$pos['value'] = number_format($pos['stockPrice'] * $pos['shares'], 2, '.', ''); | |
} | |
unset($pos); | |
function monthlyDividends(&$positions) { | |
foreach ($positions as &$position) { | |
if ($position['dividendTerm'] == 'monthly') { | |
$dividendPercent = $position['dividendPercent'] / 100 / 12; | |
$dividendPaymentTotal = $position['shares'] * ($position['stockPrice'] * $dividendPercent); | |
$position['shares'] += number_format($dividendPaymentTotal / $position['stockPrice'], 3, '.', ''); | |
$position['value'] = number_format($position['stockPrice'] * $position['shares'], 2, '.', ''); | |
} | |
} | |
unset($position); | |
} | |
function quarterlyDividends(&$positions) { | |
foreach ($positions as $symbol => &$position) { | |
if ($position['dividendTerm'] == 'quarterly') { | |
$dividendPercent = $position['dividendPercent'] / 100 / 4; | |
$dividendPaymentTotal = $position['shares'] * ($position['stockPrice'] * $dividendPercent); | |
$newShares = number_format($dividendPaymentTotal / $position['stockPrice'], 3, '.', ''); | |
$position['shares'] += $newShares; | |
$position['value'] = number_format($position['stockPrice'] * $position['shares'], 2, '.', ''); | |
} | |
} | |
unset($position); | |
} | |
function addMonthlyShares(&$positions) { | |
foreach ($positions as $symbol => &$position) { | |
if (isset($position['addMonthly']) && $position['addMonthly'] > 0) { | |
$position['shares'] += $position['addMonthly']; | |
$position['value'] = number_format($position['stockPrice'] * $position['shares'], 2, '.', ''); | |
} | |
} | |
unset($position); | |
} | |
echo "Starting positions (Symbol -> Shares -> Value):" . PHP_EOL; | |
foreach ($positions as $symbol => $position) { | |
echo "\t{$symbol} \t\t {$position['shares']} \t\t {$position['value']}" . PHP_EOL; | |
} | |
echo "Portfolio Value: " . array_sum(array_column($positions, 'value')) . PHP_EOL; | |
echo PHP_EOL; | |
$year = 1; | |
$years = 10; | |
$months = 12; | |
while($year <= $years){ | |
for($i = 1; $i <= $months; $i++){ | |
monthlyDividends($positions); | |
if ($i % 3 == 0) { | |
quarterlyDividends($positions); | |
} | |
addMonthlyShares($positions); | |
} | |
echo "Positions after year #$year (Symbol -> Shares -> Value):" . PHP_EOL; | |
foreach ($positions as $symbol => $position) { | |
echo "\t{$symbol} \t\t {$position['shares']} \t\t {$position['value']}" . PHP_EOL; | |
} | |
echo "Portfolio Value: " . array_sum(array_column($positions, 'value')) . PHP_EOL; | |
echo PHP_EOL; | |
$year++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run this like
php estimate.php
and the end result outputs