improve sync daily changes command

This commit is contained in:
hackerESQ
2024-09-15 11:28:13 -05:00
parent 9708716c71
commit 9696339828
5 changed files with 73 additions and 16 deletions
+3 -1
View File
@@ -64,9 +64,11 @@ class SyncDailyChange extends Command implements PromptsForMissingInput
$portfolio = Portfolio::findOrFail($this->argument('portfolio_id'));
$this->output->write('Syncing daily change history... This may take a moment.', false);
$portfolio->syncDailyChanges();
$this->line('Awesome! Daily change history for '. $portfolio->title .' has been completed.');
$this->output->write('Awesome! Daily change history for '. $portfolio->title .' has been completed.', false);
} catch (\Throwable $e) {
+12 -2
View File
@@ -8,7 +8,6 @@ use App\Models\Portfolio;
use App\Models\MarketData;
use App\Models\Transaction;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -255,7 +254,18 @@ class Holding extends Model
COALESCE(SUM(CASE WHEN transactions.transaction_type = 'BUY' THEN transactions.quantity ELSE 0 END), 0) -
COALESCE(SUM(CASE WHEN transactions.transaction_type = 'SELL' THEN transactions.quantity ELSE 0 END), 0) AS `owned`
"),
DB::raw("COALESCE(SUM(CASE WHEN transaction_type = 'BUY' THEN (quantity * cost_basis) ELSE 0 END), 0) AS `cost_basis`"),
DB::raw("
COALESCE(CASE
WHEN (
COALESCE(SUM(CASE WHEN transactions.transaction_type = 'BUY' THEN transactions.quantity ELSE 0 END), 0) -
COALESCE(SUM(CASE WHEN transactions.transaction_type = 'SELL' THEN transactions.quantity ELSE 0 END), 0)
) = 0 THEN 0
ELSE SUM(CASE
WHEN transactions.transaction_type = 'BUY' THEN transactions.quantity * transactions.cost_basis
ELSE 0
END)
END, 0) AS cost_basis
"),
DB::raw("COALESCE(SUM(CASE WHEN transaction_type = 'SELL' THEN ((sale_price - cost_basis) * quantity) ELSE 0 END), 0) AS `realized_gains`")
])
->leftJoin('transactions', function ($join) {
+16 -13
View File
@@ -101,41 +101,44 @@ class Portfolio extends Model
->select('holdings.symbol', 'holdings.portfolio_id', DB::raw('min(transactions.date) as first_transaction_date')) // get first transaction date
->groupBy(['holdings.symbol', 'holdings.portfolio_id'])
->get();
$dividends = Dividend::whereIn('symbol', $holdings->pluck('symbol'))->get();
$total_performance = [];
$holdings->each(function($holding) use (&$total_performance) {
$holdings->each(function($holding) use (&$total_performance, $dividends) {
$holding->setRelation('dividends', $dividends->where('symbol', $holding->symbol));
$all_history = app(MarketDataInterface::class)->history($holding->symbol, $holding->first_transaction_date, now());
$quantity = $holding->dailyPerformance($holding->first_transaction_date, now());
$daily_performance = $holding->dailyPerformance($holding->first_transaction_date, now());
$dividends = $holding->dividends->keyBy(function ($dividend, $key) {
return $dividend['date']->format('Y-m-d');
})->toArray();
});
$dividends_earned = 0;
$daily_performance = [];
$daily = [];
$all_history->sortBy('date')->each(function ($history, $date) use ($quantity, $dividends, &$daily_performance, &$dividends_earned) {
$all_history->sortBy('date')->each(function ($history, $date) use ($daily_performance, $dividends, &$daily, &$dividends_earned) {
$close = Arr::get($history, 'close', 0);
$total_market_value = $quantity->get($date)->owned * $close;
$dividend = Arr::get($dividends, $date, []);
$dividends_earned += $quantity->get($date)->owned * Arr::get($dividend, 'dividend_amount', 0);
$total_market_value = $daily_performance->get($date)->owned * $close;
$dividends_earned += $daily_performance->get($date)->owned * ($dividends->get($date)?->dividend_amount ?? 0);
$daily_performance[$date] = [
$daily[$date] = [
'date' => $date,
'portfolio_id' => $this->id,
'total_market_value' => $total_market_value,
'total_cost_basis' => $quantity->get($date)->cost_basis,
'total_gain' => $total_market_value - $quantity->get($date)->cost_basis,
'realized_gains' => $quantity->get($date)->realized_gains,
'total_cost_basis' => $daily_performance->get($date)->cost_basis,
'total_gain' => $total_market_value - $daily_performance->get($date)->cost_basis,
'realized_gains' => $daily_performance->get($date)->realized_gains,
'total_dividends_earned' => $dividends_earned
];
});
foreach ($daily_performance as $date => $performance) {
foreach ($daily as $date => $performance) {
if (!isset($total_performance[$date])) {
$total_performance[$date] = $performance;