Skip to content

Commit 63e98ea

Browse files
committed
Add Bulk Candles
1 parent d8cd00a commit 63e98ea

4 files changed

Lines changed: 204 additions & 45 deletions

File tree

src/Endpoints/Indices.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function quote(string $symbol, bool $fifty_two_week = false): Quote
3737
*
3838
* @param string $symbol The index symbol, without any leading or trailing index identifiers. For example, use DJI
3939
* do not use $DJI, ^DJI, .DJI, DJI.X, etc.
40+
*
4041
* @param Carbon $from The leftmost candle on a chart (inclusive). If you use countback, to is not required.
4142
* @param Carbon|null $to The rightmost candle on a chart (inclusive).
4243
* @param string $resolution The duration of each candle.
@@ -45,8 +46,10 @@ public function quote(string $symbol, bool $fifty_two_week = false): Quote
4546
* Weekly Resolutions: (weekly, W, 1W, 2W, ...)
4647
* Monthly Resolutions: (monthly, M, 1M, 2M, ...)
4748
* Yearly Resolutions:(yearly, Y, 1Y, 2Y, ...)
49+
*
4850
* @param int|null $countback Will fetch a number of candles before (to the left of) to. If you use from, countback
4951
* is not required.
52+
*
5053
* @return Candles
5154
* @throws GuzzleException|ApiException
5255
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace MarketDataApp\Endpoints\Responses\Stocks;
4+
5+
use Carbon\Carbon;
6+
7+
class BulkCandles
8+
{
9+
10+
// Will always be ok when there is data for the candles requested.
11+
public string $status;
12+
13+
// The ticker symbols of the stock.
14+
public array $symbols = [];
15+
16+
/** @var Candle[] $candles */
17+
public array $candles = [];
18+
19+
public function __construct(object $response)
20+
{
21+
// Convert the response to this object.
22+
$this->status = $response->s;
23+
24+
if ($this->status === 'ok') {
25+
$this->symbols = array_map('trim', explode(',', $response->symbol));
26+
for ($i = 0; $i < count($response->o); $i++) {
27+
$this->candles[] = new Candle(
28+
$response->o[$i],
29+
$response->h[$i],
30+
$response->l[$i],
31+
$response->c[$i],
32+
$response->v[$i],
33+
Carbon::parse($response->t[$i]),
34+
);
35+
}
36+
}
37+
}
38+
}

src/Endpoints/Stocks.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Carbon\Carbon;
66
use GuzzleHttp\Exception\GuzzleException;
77
use MarketDataApp\Client;
8+
use MarketDataApp\Endpoints\Responses\Stocks\BulkCandles;
89
use MarketDataApp\Endpoints\Responses\Stocks\BulkQuotes;
910
use MarketDataApp\Endpoints\Responses\Stocks\Candles;
1011
use MarketDataApp\Endpoints\Responses\Stocks\Quote;
@@ -22,6 +23,49 @@ public function __construct($client)
2223
$this->client = $client;
2324
}
2425

26+
/**
27+
* Get bulk candle data for stocks. This endpoint returns bulk daily candle data for multiple stocks. Unlike the
28+
* standard candles endpoint, this endpoint returns a single daily for each symbol provided. The typical use-case
29+
* for this endpoint is to get a complete market snapshot during trading hours, though it can also be used for bulk
30+
* snapshots of historical daily candles.
31+
*
32+
* @param array $symbols The ticker symbols to return in the response, separated by commas. The symbols parameter
33+
* may be omitted if the snapshot parameter is set to true.
34+
*
35+
* @param string $resolution The duration of each candle. Only daily candles are supported at this time.
36+
* Daily Resolutions: (daily, D, 1D, 2D, ...)
37+
*
38+
* @param bool $snapshot Returns candles for all available symbols for the date indicated. The symbols parameter can
39+
* be omitted if snapshot is set to true.
40+
*
41+
* @param Carbon|null $date The date of the candles to be returned. If no date is specified, during market hours the
42+
* candles returned will be from the current session. If the market is closed the candles will be from the most
43+
* recent session.
44+
*
45+
* @param bool $adjustsplits Adjust historical data for historical splits and reverse splits. Market Data uses
46+
* the CRSP methodology for adjustment. Daily candles default: true.
47+
*
48+
* @return Candles
49+
* @throws ApiException
50+
* @throws GuzzleException
51+
*/
52+
public function bulkCandles(
53+
array $symbols = [],
54+
string $resolution = 'D',
55+
bool $snapshot = false,
56+
Carbon $date = null,
57+
bool $adjustsplits = false,
58+
): BulkCandles {
59+
if (empty($symbols) && !$snapshot) {
60+
throw new \InvalidArgumentException('Either symbols or snapshot must be set');
61+
}
62+
63+
$symbols = implode(',', array_map('trim', $symbols));
64+
return new BulkCandles($this->client->execute(self::BASE_URL . "bulkcandles/{$resolution}/",
65+
compact('symbols', 'date', 'snapshot', 'adjustsplits')
66+
));
67+
}
68+
2569
/**
2670
* Get historical price candles for an index.
2771
*
@@ -34,23 +78,30 @@ public function __construct($client)
3478
* Weekly Resolutions: (weekly, W, 1W, 2W, ...)
3579
* Monthly Resolutions: (monthly, M, 1M, 2M, ...)
3680
* Yearly Resolutions:(yearly, Y, 1Y, 2Y, ...)
81+
*
3782
* @param int|null $countback Will fetch a number of candles before (to the left of) to. If you use from, countback
3883
* is not required.
84+
*
3985
* @param string|null $exchange Use to specify the exchange of the ticker. This is useful when you need to specify
4086
* a stock that quotes on several exchanges with the same symbol. You may specify the exchange using the EXCHANGE
4187
* ACRONYM, MIC CODE, or two digit YAHOO FINANCE EXCHANGE CODE. If no exchange is specified symbols will be matched
4288
* to US exchanges first.
89+
*
4390
* @param bool $extended Include extended hours trading sessions when returning intraday candles. Daily resolutions
4491
* never return extended hours candles. The default is false.
92+
*
4593
* @param string|null $country Use to specify the country of the exchange (not the country of the company) in
4694
* conjunction with the symbol argument. This argument is useful when you know the ticker symbol and the country of
4795
* the exchange, but not the exchange code. Use the two digit ISO 3166 country code. If no country is specified, US
4896
* exchanges will be assumed.
97+
*
4998
* @param bool $adjustsplits Adjust historical data for for historical splits and reverse splits. Market Data uses
5099
* the CRSP methodology for adjustment. Daily candles default: true. Intraday candles default: false.
100+
*
51101
* @param bool $adjustdividends CAUTION: Adjusted dividend data is planned for the future, but not yet implemented.
52102
* All data is currently returned unadjusted for dividends. Market Data uses the CRSP methodology for adjustment.
53103
* Daily candles default: true. Intraday candles default: false.
104+
*
54105
* @return Candles
55106
* @throws GuzzleException|ApiException
56107
*/
@@ -77,7 +128,8 @@ public function candles(
77128
* @param string $symbol The company's ticker symbol.
78129
* @param bool $fifty_two_week Enable the output of 52-week high and 52-week low data in the quote output. By
79130
* default this parameter is false if omitted.
80-
* @throws GuzzleException
131+
*
132+
* @throws GuzzleException|ApiException
81133
*/
82134
public function quote(string $symbol, bool $fifty_two_week = false): Quote
83135
{
@@ -91,6 +143,7 @@ public function quote(string $symbol, bool $fifty_two_week = false): Quote
91143
* @param array $symbols The ticker symbols to return in the response.
92144
* @param bool $fifty_two_week Enable the output of 52-week high and 52-week low data in the quote output. By
93145
* default this parameter is false if omitted.
146+
*
94147
* @throws \Throwable
95148
*/
96149
public function quotes(array $symbols, bool $fifty_two_week = false): Quotes|BulkQuotes
@@ -113,8 +166,10 @@ public function quotes(array $symbols, bool $fifty_two_week = false): Quotes|Bul
113166
*
114167
* @param array $symbols The ticker symbols to return in the response, separated by commas. The symbols parameter
115168
* may be omitted if the snapshot parameter is set to true.
169+
*
116170
* @param bool $snapshot Returns a full market snapshot with quotes for all symbols when set to true. The symbols
117171
* parameter may be omitted if the snapshot parameter is set.
172+
*
118173
* @throws GuzzleException
119174
* @throws \Exception
120175
*/

0 commit comments

Comments
 (0)