Files
investbrain/app/Interfaces/MarketData/YahooMarketData.php
T

108 lines
3.5 KiB
PHP
Raw Normal View History

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;
use App\Interfaces\MarketData\Types\Ohlc;
use App\Interfaces\MarketData\Types\Quote;
use App\Interfaces\MarketData\Types\Split;
2025-04-09 19:25:15 -05:00
use Illuminate\Support\Carbon;
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
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);
2025-04-09 19:25:15 -05:00
if (is_null($quote?->getRegularMarketPrice())) {
throw new \Exception('Could not find ticker on Yahoo');
}
return new Quote([
'name' => $quote?->getLongName() ?? $quote?->getShortName(),
'symbol' => $symbol,
2025-04-09 19:25:15 -05:00
'currency' => $quote?->getCurrency(),
'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,
2025-04-09 19:25:15 -05:00
'meta_data' => [
'exchange' => $quote?->getExchange(),
'asset_type' => $quote?->getQuoteType(),
'source' => 'yahoo',
],
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
2025-04-09 19:25:15 -05:00
$date = Carbon::parse($history->getDate())->toDateString();
2024-09-11 21:58:34 -05:00
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
}