2024-08-01 13:53:10 -05:00
|
|
|
<?php
|
|
|
|
|
|
2025-01-28 17:33:54 -06:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2024-08-01 13:53:10 -05:00
|
|
|
namespace App\Interfaces\MarketData;
|
|
|
|
|
|
2025-01-28 17:14:49 -06:00
|
|
|
use App\Interfaces\MarketData\Types\Dividend;
|
2024-10-29 16:34:18 -05:00
|
|
|
use App\Interfaces\MarketData\Types\Ohlc;
|
|
|
|
|
use App\Interfaces\MarketData\Types\Quote;
|
|
|
|
|
use App\Interfaces\MarketData\Types\Split;
|
2025-01-28 17:14:49 -06:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
use Scheb\YahooFinanceApi\ApiClient;
|
2024-08-26 21:34:23 -05:00
|
|
|
use Scheb\YahooFinanceApi\ApiClientFactory as YahooFinance;
|
2024-08-01 13:53:10 -05:00
|
|
|
|
|
|
|
|
class YahooMarketData implements MarketDataInterface
|
|
|
|
|
{
|
2024-09-01 16:06:29 -05:00
|
|
|
public ApiClient $client;
|
|
|
|
|
|
2025-01-28 17:14:49 -06:00
|
|
|
public function __construct()
|
|
|
|
|
{
|
2024-08-24 22:19:40 -05:00
|
|
|
|
2024-08-01 13:53:10 -05:00
|
|
|
// create yahoo finance client factory
|
2024-08-26 21:34:23 -05:00
|
|
|
$this->client = YahooFinance::createApiClient();
|
2024-08-01 13:53:10 -05:00
|
|
|
}
|
|
|
|
|
|
2025-01-28 17:14:49 -06:00
|
|
|
public function exists(string $symbol): bool
|
2024-08-01 13:53:10 -05:00
|
|
|
{
|
2024-08-24 22:19:40 -05:00
|
|
|
|
2025-01-28 20:32:43 -06:00
|
|
|
return (bool) $this->quote($symbol);
|
2024-08-01 13:53:10 -05:00
|
|
|
}
|
|
|
|
|
|
2025-01-28 17:14:49 -06:00
|
|
|
public function quote(string $symbol): Quote
|
2024-08-01 13:53:10 -05:00
|
|
|
{
|
2024-08-24 22:19:40 -05:00
|
|
|
|
2024-08-01 13:53:10 -05:00
|
|
|
$quote = $this->client->getQuote($symbol);
|
|
|
|
|
|
2024-10-29 16:34:18 -05:00
|
|
|
return new Quote([
|
2025-01-28 20:32:43 -06:00
|
|
|
'name' => $quote?->getLongName() ?? $quote?->getShortName(),
|
2025-01-28 13:30:12 -06:00
|
|
|
'symbol' => $symbol,
|
2025-01-28 20:32:43 -06:00
|
|
|
'market_value' => $quote?->getRegularMarketPrice(),
|
|
|
|
|
'fifty_two_week_high' => $quote?->getFiftyTwoWeekHigh(),
|
|
|
|
|
'fifty_two_week_low' => $quote?->getFiftyTwoWeekLow(),
|
|
|
|
|
'forward_pe' => $quote?->getForwardPE(),
|
|
|
|
|
'trailing_pe' => $quote?->getTrailingPE(),
|
|
|
|
|
'market_cap' => $quote?->getMarketCap(),
|
|
|
|
|
'book_value' => $quote?->getBookValue(),
|
|
|
|
|
'last_dividend_date' => $quote?->getDividendDate(),
|
|
|
|
|
'dividend_yield' => $quote?->getTrailingAnnualDividendYield() * 100,
|
2024-08-01 13:53:10 -05:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-28 17:14:49 -06:00
|
|
|
public function dividends(string $symbol, $startDate, $endDate): Collection
|
2024-08-01 13:53:10 -05:00
|
|
|
{
|
2024-08-24 22:19:40 -05:00
|
|
|
|
2024-08-01 13:53:10 -05:00
|
|
|
return collect($this->client->getHistoricalDividendData($symbol, $startDate, $endDate))
|
2025-01-28 17:14:49 -06:00
|
|
|
->map(function ($dividend) use ($symbol) {
|
|
|
|
|
|
|
|
|
|
return new Dividend([
|
|
|
|
|
'symbol' => $symbol,
|
|
|
|
|
'date' => $dividend->getDate(),
|
|
|
|
|
'dividend_amount' => $dividend->getDividends(),
|
|
|
|
|
]);
|
|
|
|
|
});
|
2024-08-01 13:53:10 -05:00
|
|
|
}
|
|
|
|
|
|
2025-01-28 17:14:49 -06:00
|
|
|
public function splits(string $symbol, $startDate, $endDate): Collection
|
|
|
|
|
{
|
2024-08-24 22:19:40 -05:00
|
|
|
|
2024-08-01 13:53:10 -05:00
|
|
|
return collect($this->client->getHistoricalSplitData($symbol, $startDate, $endDate))
|
2025-01-28 17:14:49 -06:00
|
|
|
->map(function ($split) use ($symbol) {
|
|
|
|
|
$split_amount = explode(':', $split->getStockSplits());
|
|
|
|
|
|
|
|
|
|
return new Split([
|
|
|
|
|
'symbol' => $symbol,
|
|
|
|
|
'date' => $split->getDate(),
|
|
|
|
|
'split_amount' => $split_amount[0] / $split_amount[1],
|
|
|
|
|
]);
|
|
|
|
|
});
|
2024-08-01 13:53:10 -05:00
|
|
|
}
|
2024-09-09 20:50:18 -05:00
|
|
|
|
2025-01-28 17:14:49 -06:00
|
|
|
public function history(string $symbol, $startDate, $endDate): Collection
|
2024-09-09 20:50:18 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
return collect($this->client->getHistoricalQuoteData($symbol, ApiClient::INTERVAL_1_DAY, $startDate, $endDate))
|
2025-01-28 17:14:49 -06:00
|
|
|
->mapWithKeys(function ($history) use ($symbol) {
|
2024-09-09 20:50:18 -05:00
|
|
|
|
2024-09-11 21:58:34 -05:00
|
|
|
$date = $history->getDate()->format('Y-m-d');
|
|
|
|
|
|
2025-01-28 17:14:49 -06:00
|
|
|
return [$date => new Ohlc([
|
|
|
|
|
'symbol' => $symbol,
|
|
|
|
|
'date' => $date,
|
|
|
|
|
'close' => $history->getClose(),
|
|
|
|
|
])];
|
2024-09-09 20:50:18 -05:00
|
|
|
});
|
|
|
|
|
}
|
2025-01-28 17:14:49 -06:00
|
|
|
}
|