add history interface

This commit is contained in:
hackerESQ
2024-09-09 20:50:18 -05:00
parent 23047d976e
commit 3284a31bb1
4 changed files with 62 additions and 0 deletions
@@ -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')
];
});
}
}
@@ -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);
}
}
@@ -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;
}
@@ -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(),
];
});
}
}