This commit is contained in:
hackerESQ
2024-08-30 21:58:38 -05:00
parent 2a42ce9d12
commit d6cf867c6d
13 changed files with 361 additions and 122 deletions
@@ -0,0 +1,75 @@
<?php
namespace App\Console\Commands;
use App\Models\User;
use Illuminate\Console\Command;
class CaptureDailyChange extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'daily-change:capture';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Capture summary of daily change for user\'s holdings';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
User::all()->each(function($user){
$this->line('Capturing daily change for ' . $user->name);
$portfolios = $user->portfolios()->withoutWishlists()->with(['holdings.market_data'])->get();
$total_cost_basis = $portfolios->reduce(function ($carry, $portfolio) {
return $carry + $portfolio->holdings->sum('total_cost_basis');
});
$total_dividends = $portfolios->reduce(function ($carry, $portfolio) {
return $carry + $portfolio->holdings->sum('dividends_earned');
});
$realized_gains = $portfolios->reduce(function ($carry, $portfolio) {
return $carry + $portfolio->holdings->sum('realized_gain_loss_dollars');
});
$total_market_value = $portfolios->reduce(function ($carry, $portfolio) {
return $carry + $portfolio->holdings->sum(function($holding) {
return $holding->market_data->market_value * $holding->quantity;
}) ;
});
$user->daily_changes()->create([
'date' => now(),
'total_cost_basis' => $total_cost_basis,
'total_market_value' => $total_market_value,
'total_dividends' => $total_dividends,
'realized_gains' => $realized_gains,
'total_gain_loss' => $total_market_value - $total_cost_basis
]);
});
}
}
@@ -0,0 +1,50 @@
<?php
namespace App\Console\Commands;
use App\Models\Holding;
use App\Models\Dividend;
use Illuminate\Console\Command;
class RefreshDividendData extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'dividend-data:refresh';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Refresh dividend data from data provider';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
// $holdings = Holding::where('quantity', '>', 0)->distinct()->get(['symbol']);
$holdings = Holding::distinct()->get(['symbol']);
foreach ($holdings as $holding) {
$this->line('Refreshing ' . $holding->symbol);
Dividend::refreshDividendData($holding->symbol);
}
}
}
@@ -0,0 +1,50 @@
<?php
namespace App\Console\Commands;
use App\Models\MarketData;
use Illuminate\Console\Command;
class RefreshMarketData extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'market-data:refresh
{--force= : Ignore refresh delay}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Refresh market data from market data provider';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
// get all symbols from market data
$symbols = MarketData::get(['symbol']);
foreach ($symbols as $symbol) {
$this->line('Refreshing ' . $symbol->symbol);
$symbol->refreshMarketData();
}
}
}
+50
View File
@@ -0,0 +1,50 @@
<?php
namespace App\Console\Commands;
use App\Models\Split;
use App\Models\Holding;
use Illuminate\Console\Command;
class RefreshSplitData extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'split-data:refresh
{--force= : Don\'t ask to confirm.}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Refresh split data from data provider';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$holdings = Holding::distinct()->get(['symbol']);
foreach ($holdings as $holding) {
$this->line('Refreshing ' . $holding->symbol);
Split::refreshSplitData($holding->symbol);
}
}
}
@@ -3,11 +3,9 @@
namespace App\Console\Commands;
use App\Models\Holding;
use App\Models\Dividend;
use App\Models\Transaction;
use Illuminate\Console\Command;
class RefreshHoldingData extends Command
class SyncHoldingData extends Command
{
/**
* The name and signature of the console command.
@@ -46,7 +44,7 @@ class RefreshHoldingData extends Command
foreach ($holdings as $holding) {
$this->line('Refreshing ' . $holding->symbol);
$holding->sync();
$holding->syncTransactionsAndDividends();
}
}
}