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

71 lines
2.3 KiB
PHP
Raw Normal View History

2024-08-01 13:53:10 -05:00
<?php
namespace App\Interfaces\MarketData;
use Illuminate\Support\Collection;
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-08-31 23:06:19 -05:00
public function __construct(
public $client
) {
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
}
public function exists(String $symbol): Bool
{
2024-08-24 22:19:40 -05:00
2024-08-01 13:53:10 -05:00
return $this->quote($symbol)->isNotEmpty();
}
public function quote($symbol): Collection
{
2024-08-24 22:19:40 -05:00
2024-08-01 13:53:10 -05:00
$quote = $this->client->getQuote($symbol);
if (empty($quote)) return collect();
return collect([
'name' => $quote->getLongName() ?? $quote->getShortName(),
'symbol' => $quote->getSymbol(),
'market_value' => $quote->getRegularMarketPrice(),
'fifty_two_week_high' => $quote->getFiftyTwoWeekHigh(),
'fifty_two_week_low' => $quote->getFiftyTwoWeekLow(),
2024-08-24 22:19:40 -05:00
'forward_pe' => $quote->getForwardPE(),
'trailing_pe' => $quote->getTrailingPE(),
'market_cap' => $quote->getMarketCap()
2024-08-01 13:53:10 -05:00
]);
}
public function dividends($symbol, $startDate, $endDate): Collection
{
2024-08-24 22:19:40 -05:00
2024-08-01 13:53:10 -05:00
return collect($this->client->getHistoricalDividendData($symbol, $startDate, $endDate))
->map(function($dividend) use ($symbol) {
2024-08-24 22:19:40 -05:00
2024-08-01 13:53:10 -05:00
return [
'symbol' => $symbol,
'date' => $dividend->getDate()->format('Y-m-d H:i:s'),
'dividend_amount' => $dividend->getDividends(),
];
});
}
public function splits($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))
->map(function($split) use ($symbol) {
$split_amount = explode(':', $split->getStockSplits());
return [
'symbol' => $symbol,
'date' => $split->getDate()->format('Y-m-d H:i:s'),
'split_amount' => $split_amount[0] / $split_amount[1],
];
});
}
}