I am trying to get DividendYield from a company for the last 20 years using ChartJs in laravel project. The arrays came from HTTP client API. The formula is like that :$dividendPayed / $dailyPrice * 100. The problem i am facing is that the $dividendPayed is once in three months so it makes that array shorter that the one which contains daily prices.
private function getDividend()
{
$dividend = [];
$response = Http::get('https://some-endpoint-for-once-in-3-months-dividend');
$response->throw();
foreach($response->json('historical') as $stock){
$dividend [] = $stock['dividend'];
}
return $dividend;
//THIS ARRAY RETURNS LET'S SAY FOR EXAMPLE 50 RESULTS
// $dividend = [
0 => 0.23
1 => 0.23
2 => 0.22
3 => 0.22
..........
50 => .43
]
}
private function getPrice()
{
$price = [];
$response = Http::get('https://some-endpoint-for-daily-prices');
$response->throw();
foreach($response->json('historical') as $stockPrice){
$price [] = $stockPrice['close'];
}
return $price;
}
//THIS ARRAY RETURNS LET'S SAY FOR EXAMPLE 240 RESULTS
// $price = [
0 => 147.27
1 => 143.39
2 => 143.86
3 => 143.75
4 => 142.41
5 => 138.38
..........
240 => 300.43
]
I also have to mention that the 'date' in the chart for labels (day by day for the last 20 years) is taken from the same endpoint as $dailyPrice.
CodePudding user response:
Since you didn't mention the full response from https://some-endpoint-for-once-in-3-months-dividend and https://some-endpoint-for-daily-prices. Im assuming a date key exists with the responses for example
['historical'=>[['date'=>'2022-03-03','price'=>0.2]];
For fetching the API responses
private function fetchApiData(string $api,string $dateKey,string $priceKey,string $mainRespKey='historical'){
$data = [];
$response = Http::get($api);
$response->throw();
return $response->json($mainRespKey);}
For calculating the DividendYield
public function DividendYield(){
$dividend = $this->fetchApiData('https://some-endpoint-for-once-in-3-months-dividend','date','dividend');
$dailyPrice = $this->fetchApiData('https://some-endpoint-for-daily-prices','date','price');
if(!empty($dividend)){
$dividendColl = collect($dividend);
return collect($dailyPrice)->whereIn('date',$dividendColl->pluck('date'))
->map(fn($price)=>$dividendColl->where('date',$price['date'])
->map(fn($dividend)=> ['date'=>$price['date'],
'DividendYield'=>(($dividend['dividend']/$price['price'])*100)]))->collapse();
}}
CodePudding user response:
So first you need to group your $price
by month, assuming that the dd()
in your comment is what price
is, by using:
$price = collect($price)->groupBy(function($val) {
return Carbon::parse($val->date)->format('m');
});
then you need to loop through the $dividend
:
$result = array();
foreach ($dividend as $key => $value) {
$mounthResult = array();
foreach ($price[key] as $p) {
// assuming dividend is the price you mean
array_push($mounthResult, $value/$p->dividend*100);
}
array_push($result, $temp);
}
The $price[key]
means the month group we want, so if key = 0
that means we are looping the first month.
I hope i get you right, feel free to comment on this to help me understand you more if i'm wrong.