Files
investbrain/app/Console/Commands/RefreshMarketData.php
T

69 lines
1.6 KiB
PHP
Raw Normal View History

2024-08-29 23:36:44 -05:00
<?php
2025-01-28 17:33:54 -06:00
declare(strict_types=1);
2024-08-29 23:36:44 -05:00
namespace App\Console\Commands;
2024-09-01 21:18:09 -05:00
use App\Models\Holding;
2024-08-30 21:58:38 -05:00
use App\Models\MarketData;
2024-08-29 23:36:44 -05:00
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
2024-08-29 23:36:44 -05:00
2024-08-30 21:58:38 -05:00
class RefreshMarketData extends Command
2024-08-29 23:36:44 -05:00
{
/**
* The name and signature of the console command.
*
* @var string
*/
2024-09-06 22:26:54 -05:00
protected $signature = 'refresh:market-data
{--force : Ignore refresh delay}
{--user= : Limit refresh to user\'s holdings}';
2024-08-29 23:36:44 -05:00
/**
* The console command description.
*
* @var string
*/
2024-08-30 21:58:38 -05:00
protected $description = 'Refresh market data from market data provider';
2024-08-29 23:36:44 -05:00
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
2024-09-18 21:15:52 -05:00
$force = $this->option('force') ?? false;
2025-01-28 17:14:49 -06:00
2024-08-30 21:58:38 -05:00
// get all symbols from market data
2024-09-05 21:21:18 -05:00
$holdings = Holding::where('quantity', '>', 0)
2025-01-28 17:14:49 -06:00
->select(['symbol'])
->distinct();
if ($this->option('user')) {
$holdings->myHoldings($this->option('user'));
}
2024-08-29 23:36:44 -05:00
foreach ($holdings->get() as $holding) {
2025-01-28 17:14:49 -06:00
$this->line('Refreshing '.$holding->symbol);
2024-09-01 21:18:09 -05:00
try {
MarketData::getMarketData($holding->symbol, $force);
} catch (\Throwable $e) {
Log::error('Could not refresh '.$holding->symbol.' ('.$e->getMessage().')');
}
2024-08-29 23:36:44 -05:00
}
}
}