add dividends and splits to alphavantage provider

This commit is contained in:
hackerESQ
2024-08-31 23:25:26 -05:00
parent d585153a0b
commit d68fc89f75
@@ -3,6 +3,7 @@
namespace App\Interfaces\MarketData; namespace App\Interfaces\MarketData;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Tschucki\Alphavantage\Facades\Alphavantage; use Tschucki\Alphavantage\Facades\Alphavantage;
@@ -36,13 +37,18 @@ class AlphaVantageMarketData implements MarketDataInterface
public function dividends($symbol, $startDate, $endDate): Collection public function dividends($symbol, $startDate, $endDate): Collection
{ {
return collect($this->client->getHistoricalDividendData($symbol, $startDate, $endDate)) $dividends = Alphavantage::fundamentals()->dividends($symbol);
return collect($dividends)
->where('ex_dividend_date', '>=', $startDate)
->where('ex_dividend_date', '<', $endDate)
->map(function($dividend) use ($symbol) { ->map(function($dividend) use ($symbol) {
return [ return [
'symbol' => $symbol, 'symbol' => $symbol,
'date' => $dividend->getDate()->format('Y-m-d H:i:s'), 'date' => Carbon::parse(Arr::get($dividend, 'ex_dividend_date'))
'dividend_amount' => $dividend->getDividends(), ->format('Y-m-d H:i:s'),
'dividend_amount' => Arr::get($dividend, 'amount'),
]; ];
}); });
} }
@@ -50,14 +56,18 @@ class AlphaVantageMarketData implements MarketDataInterface
public function splits($symbol, $startDate, $endDate): Collection public function splits($symbol, $startDate, $endDate): Collection
{ {
return collect($this->client->getHistoricalSplitData($symbol, $startDate, $endDate)) $splits = Alphavantage::fundamentals()->splits($symbol);
return collect($splits)
->where('effective_date', '>=', $startDate)
->where('effective_date', '<', $endDate)
->map(function($split) use ($symbol) { ->map(function($split) use ($symbol) {
$split_amount = explode(':', $split->getStockSplits());
return [ return [
'symbol' => $symbol, 'symbol' => $symbol,
'date' => $split->getDate()->format('Y-m-d H:i:s'), 'date' => Carbon::parse(Arr::get($split, 'effective_date'))
'split_amount' => $split_amount[0] / $split_amount[1], ->format('Y-m-d H:i:s'),
'split_amount' => Arr::get($split, 'split_factor'),
]; ];
}); });
} }