key history by date

This commit is contained in:
hackerESQ
2024-09-11 21:58:34 -05:00
parent 2448fe5c76
commit 0f9c72f7cb
3 changed files with 24 additions and 16 deletions
@@ -98,13 +98,15 @@ class AlphaVantageMarketData implements MarketDataInterface
return Carbon::parse($date)->between($startDate, $endDate); return Carbon::parse($date)->between($startDate, $endDate);
}) })
->map(function($history, $date) use ($symbol) { ->mapWithKeys(function($history, $date) use ($symbol) {
$date = Carbon::parse($date)->format('Y-m-d');
return [ return [ $date => [
'symbol' => $symbol, 'symbol' => $symbol,
'date' => Carbon::parse($date)->format('Y-m-d'), 'date' => $date,
'close' => (float) Arr::get($history, '4. close') 'close' => (float) Arr::get($history, '4. close')
]; ]];
}); });
} }
} }
+8 -4
View File
@@ -2,6 +2,7 @@
namespace App\Interfaces\MarketData; namespace App\Interfaces\MarketData;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
class FakeMarketData implements MarketDataInterface class FakeMarketData implements MarketDataInterface
@@ -66,13 +67,16 @@ class FakeMarketData implements MarketDataInterface
public function history($symbol, $startDate, $endDate): Collection public function history($symbol, $startDate, $endDate): Collection
{ {
$numDays = Carbon::parse($startDate)->diffInDays($endDate, true);
for ($i = 0; $i < 14; $i++) { for ($i = 0; $i < $numDays; $i++) {
$series[] = [ $date = now()->subDays($i)->format('Y-m-d');
$series[$date] = [
'symbol' => $symbol, 'symbol' => $symbol,
'date' => now()->subDays($i)->format('Y-m-d'), 'date' => $date,
'close' => (float) rand(1, 100), 'close' => (float) rand(150, 400),
]; ];
} }
@@ -77,13 +77,15 @@ class YahooMarketData implements MarketDataInterface
{ {
return collect($this->client->getHistoricalQuoteData($symbol, ApiClient::INTERVAL_1_DAY, $startDate, $endDate)) return collect($this->client->getHistoricalQuoteData($symbol, ApiClient::INTERVAL_1_DAY, $startDate, $endDate))
->map(function($history) use ($symbol) { ->mapWithKeys(function($history) use ($symbol) {
return [ $date = $history->getDate()->format('Y-m-d');
'symbol' => $symbol,
'date' => $history->getDate()->format('Y-m-d'), return [ $date => [
'close' => (float) $history->getClose(), 'symbol' => $symbol,
]; 'date' => $date,
'close' => (float) $history->getClose(),
]];
}); });
} }
} }