fix: need to chunk alpaca history requests
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Interfaces\MarketData\Types\Dividend;
|
|||||||
use App\Interfaces\MarketData\Types\Ohlc;
|
use App\Interfaces\MarketData\Types\Ohlc;
|
||||||
use App\Interfaces\MarketData\Types\Quote;
|
use App\Interfaces\MarketData\Types\Quote;
|
||||||
use App\Interfaces\MarketData\Types\Split;
|
use App\Interfaces\MarketData\Types\Split;
|
||||||
|
use Carbon\CarbonInterval;
|
||||||
use Illuminate\Http\Client\PendingRequest;
|
use Illuminate\Http\Client\PendingRequest;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
@@ -23,6 +24,11 @@ class AlpacaMarketData implements MarketDataInterface
|
|||||||
public string $apiBaseUrl = 'https://api.alpaca.markets/';
|
public string $apiBaseUrl = 'https://api.alpaca.markets/';
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->createNewClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createNewClient()
|
||||||
{
|
{
|
||||||
$this->client = Http::withOptions([
|
$this->client = Http::withOptions([
|
||||||
'headers' => [
|
'headers' => [
|
||||||
@@ -54,6 +60,8 @@ class AlpacaMarketData implements MarketDataInterface
|
|||||||
1440,
|
1440,
|
||||||
function () use ($symbol) {
|
function () use ($symbol) {
|
||||||
|
|
||||||
|
$this->createNewClient();
|
||||||
|
|
||||||
$basic = $this->client->baseUrl($this->apiBaseUrl)->get("v2/assets/{$symbol}")->json();
|
$basic = $this->client->baseUrl($this->apiBaseUrl)->get("v2/assets/{$symbol}")->json();
|
||||||
$fifty_two_week = $this->client->baseUrl($this->dataBaseUrl)->withQueryParameters([
|
$fifty_two_week = $this->client->baseUrl($this->dataBaseUrl)->withQueryParameters([
|
||||||
'timeframe' => '12M',
|
'timeframe' => '12M',
|
||||||
@@ -125,16 +133,34 @@ class AlpacaMarketData implements MarketDataInterface
|
|||||||
|
|
||||||
public function history(string $symbol, $startDate, $endDate): Collection
|
public function history(string $symbol, $startDate, $endDate): Collection
|
||||||
{
|
{
|
||||||
|
$startDate = Carbon::parse($startDate);
|
||||||
|
$endDate = Carbon::parse($endDate)->subHours(36); // alpaca has sip data limits
|
||||||
|
|
||||||
|
$allHistory = collect();
|
||||||
|
|
||||||
|
$chunks = 1000;
|
||||||
|
|
||||||
|
$period = CarbonInterval::days($chunks)->toPeriod($startDate, $endDate);
|
||||||
|
foreach ($period as $startDate) {
|
||||||
|
|
||||||
|
$chunkEnd = $startDate->copy()->addDays($chunks - 1);
|
||||||
|
|
||||||
|
if ($chunkEnd->gt($endDate)) {
|
||||||
|
$chunkEnd = $endDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->createNewClient();
|
||||||
|
|
||||||
$response = $this->client->baseUrl($this->dataBaseUrl)->withQueryParameters([
|
$response = $this->client->baseUrl($this->dataBaseUrl)->withQueryParameters([
|
||||||
'timeframe' => '1D',
|
'timeframe' => '1D',
|
||||||
'start' => Carbon::parse($startDate)->format('Y-m-d'),
|
'start' => $startDate->format('Y-m-d'),
|
||||||
'end' => Carbon::parse($endDate)->subHours(36)->format('Y-m-d'), // todo: can't query recent SIP data
|
'end' => $chunkEnd->format('Y-m-d'),
|
||||||
])->get("v2/stocks/{$symbol}/bars");
|
])->get("v2/stocks/{$symbol}/bars");
|
||||||
|
|
||||||
$history = $response->json('bars');
|
$history = $response->json('bars');
|
||||||
|
|
||||||
return collect($history)
|
$chunkedHistory = collect($history)
|
||||||
->map(function ($history) use ($symbol) {
|
->mapWithKeys(function ($history) use ($symbol) {
|
||||||
|
|
||||||
$date = Carbon::parse($history['t'])->format('Y-m-d');
|
$date = Carbon::parse($history['t'])->format('Y-m-d');
|
||||||
|
|
||||||
@@ -144,5 +170,10 @@ class AlpacaMarketData implements MarketDataInterface
|
|||||||
'close' => Arr::get($history, 'c'),
|
'close' => Arr::get($history, 'c'),
|
||||||
])];
|
])];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$allHistory = $allHistory->merge($chunkedHistory);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $allHistory;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user