Skip to content

Instantly share code, notes, and snippets.

@cdtweb
Last active January 4, 2021 22:38
Show Gist options
  • Save cdtweb/b888f0f6f5c68d7d82b467124262a37a to your computer and use it in GitHub Desktop.
Save cdtweb/b888f0f6f5c68d7d82b467124262a37a to your computer and use it in GitHub Desktop.
Simple Rolling DRIP Estimator
<?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++;
}
@cdtweb
Copy link
Author

cdtweb commented Jan 4, 2021

Run this like php estimate.php and the end result outputs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment