diff --git a/app/Interfaces/MarketData/AlphaVantageMarketData.php b/app/Interfaces/MarketData/AlphaVantageMarketData.php index 30ad653..355742c 100644 --- a/app/Interfaces/MarketData/AlphaVantageMarketData.php +++ b/app/Interfaces/MarketData/AlphaVantageMarketData.php @@ -85,4 +85,26 @@ class AlphaVantageMarketData implements MarketDataInterface ]; }); } + + public function history($symbol, $startDate, $endDate): Collection + { + + $history = Alphavantage::timeSeries()->daily($symbol); + + $history = Arr::get($history, 'Time Series (Daily)', []); + + return collect($history) + ->filter(function ($history, $date) use ($startDate, $endDate) { + + return Carbon::parse($date)->between($startDate, $endDate); + }) + ->map(function($history, $date) use ($symbol) { + + return [ + 'symbol' => $symbol, + 'date' => Carbon::parse($date)->format('Y-m-d'), + 'close' => (float) Arr::get($history, '4. close') + ]; + }); + } } \ No newline at end of file diff --git a/app/Interfaces/MarketData/FakeMarketData.php b/app/Interfaces/MarketData/FakeMarketData.php index 28fe05f..9a3cfd4 100644 --- a/app/Interfaces/MarketData/FakeMarketData.php +++ b/app/Interfaces/MarketData/FakeMarketData.php @@ -63,4 +63,19 @@ class FakeMarketData implements MarketDataInterface ], ]); } + + public function history($symbol, $startDate, $endDate): Collection + { + + for ($i = 0; $i < 14; $i++) { + + $series[] = [ + 'symbol' => $symbol, + 'date' => now()->subDays($i)->format('Y-m-d'), + 'close' => (float) rand(1, 100), + ]; + } + + return collect($series); + } } \ No newline at end of file diff --git a/app/Interfaces/MarketData/MarketDataInterface.php b/app/Interfaces/MarketData/MarketDataInterface.php index 75ffa2f..f220a7b 100644 --- a/app/Interfaces/MarketData/MarketDataInterface.php +++ b/app/Interfaces/MarketData/MarketDataInterface.php @@ -45,4 +45,15 @@ interface MarketDataInterface * @return Collection */ public function splits(String $symbol, \DateTimeInterface $startDate, \DateTimeInterface $endDate): Collection; + + /** + * Get historical close data + * + * @param String $symbol + * @param \DateTimeInterface $startDate + * @param \DateTimeInterface $endDate + * + * @return Collection + */ + public function history(String $symbol, \DateTimeInterface $startDate, \DateTimeInterface $endDate): Collection; } diff --git a/app/Interfaces/MarketData/YahooMarketData.php b/app/Interfaces/MarketData/YahooMarketData.php index c83a48e..ebbc217 100644 --- a/app/Interfaces/MarketData/YahooMarketData.php +++ b/app/Interfaces/MarketData/YahooMarketData.php @@ -72,4 +72,18 @@ class YahooMarketData implements MarketDataInterface ]; }); } + + public function history($symbol, $startDate, $endDate): Collection + { + + return collect($this->client->getHistoricalQuoteData($symbol, ApiClient::INTERVAL_1_DAY, $startDate, $endDate)) + ->map(function($history) use ($symbol) { + + return [ + 'symbol' => $symbol, + 'date' => $history->getDate()->format('Y-m-d'), + 'close' => (float) $history->getClose(), + ]; + }); + } } \ No newline at end of file