Skip to content

Commit cc063eb

Browse files
authored
Merge pull request #11 from MarketDataApp/add-indices-quotes-method
Add indices->quotes to methods
2 parents d45cc74 + f2315c7 commit cc063eb

7 files changed

Lines changed: 116 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v0.5.0-beta
4+
5+
Added indices->quotes to parallelize and speed up multiple index quotes.
6+
37
## v0.4.4-beta
48

59
Update options->option_chain to use enum values rather than the enum itself.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ $client = new MarketDataApp\Client('your_api_token');
2424

2525
// Indices
2626
$quote = $client->indices->quote('VIX');
27+
$quotes = $client->indices->quotes(['VIX', 'DJI']);
2728
$candles = $client->indices->candles(
2829
symbol: "VIX",
2930
from: '2022-09-01',

src/Endpoints/Indices.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use MarketDataApp\Client;
77
use MarketDataApp\Endpoints\Responses\Indices\Candles;
88
use MarketDataApp\Endpoints\Responses\Indices\Quote;
9+
use MarketDataApp\Endpoints\Responses\Indices\Quotes;
910
use MarketDataApp\Exceptions\ApiException;
1011

1112
class Indices
@@ -24,11 +25,35 @@ public function __construct($client)
2425
*
2526
* @param string $symbol The index symbol, without any leading or trailing index identifiers. For example, use DJI
2627
* do not use $DJI, ^DJI, .DJI, DJI.X, etc.
28+
*
29+
* @param bool $fifty_two_week Enable the output of 52-week high and 52-week low data in the quote output.
30+
*
2731
* @throws GuzzleException|ApiException
2832
*/
2933
public function quote(string $symbol, bool $fifty_two_week = false): Quote
3034
{
31-
return new Quote($this->client->execute(self::BASE_URL . "quotes/{$symbol}", ['52week' => $fifty_two_week]));
35+
return new Quote($this->client->execute(self::BASE_URL . "quotes/$symbol", ['52week' => $fifty_two_week]));
36+
}
37+
38+
39+
40+
/**
41+
* Get a real-time price quote for a multiple indices by doing parallel requests.
42+
*
43+
* @param array $symbols The ticker symbols to return in the response.
44+
* @param bool $fifty_two_week Enable the output of 52-week high and 52-week low data in the quote output.
45+
*
46+
* @throws \Throwable
47+
*/
48+
public function quotes(array $symbols, bool $fifty_two_week = false): Quotes
49+
{
50+
// Execute standard quotes in parallel
51+
$calls = [];
52+
foreach ($symbols as $symbol) {
53+
$calls[] = [self::BASE_URL . "quotes/$symbol", ['52week' => $fifty_two_week]];
54+
}
55+
56+
return new Quotes($this->client->executeInParallel($calls));
3257
}
3358

3459
/**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace MarketDataApp\Endpoints\Responses\Indices;
4+
5+
class Quotes
6+
{
7+
/** @var Quote[] $quotes */
8+
public array $quotes;
9+
10+
public function __construct(array $quotes)
11+
{
12+
foreach ($quotes as $quote) {
13+
$this->quotes[] = new Quote($quote);
14+
}
15+
}
16+
}

src/Endpoints/Stocks.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ public function quote(string $symbol, bool $fifty_two_week = false): Quote
162162
* Get a real-time price quote for a multiple stocks by doing parallel requests.
163163
*
164164
* @param array $symbols The ticker symbols to return in the response.
165-
* @param bool $fifty_two_week Enable the output of 52-week high and 52-week low data in the quote output. By
166-
* default this parameter is false if omitted.
165+
* @param bool $fifty_two_week Enable the output of 52-week high and 52-week low data in the quote output.
167166
*
168167
* @throws \Throwable
169168
*/

tests/Integration/IndicesTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class IndicesTest extends TestCase
1616

1717
protected function setUp(): void
1818
{
19-
error_reporting(E_ALL);
2019
$token = "your_api_token";
2120
$client = new Client($token);
2221
$this->client = $client;
@@ -37,6 +36,22 @@ public function testQuote_success()
3736
}
3837

3938

39+
public function testQuotes_success()
40+
{
41+
$response = $this->client->indices->quotes(['VIX']);
42+
43+
$this->assertInstanceOf(Quote::class, $response->quotes[0]);
44+
$this->assertEquals('string', gettype($response->quotes[0]->status));
45+
$this->assertEquals('string', gettype($response->quotes[0]->symbol));
46+
$this->assertEquals('double', gettype($response->quotes[0]->last));
47+
$this->assertTrue(in_array(gettype($response->quotes[0]->change), ['double', 'NULL']));
48+
$this->assertTrue(in_array(gettype($response->quotes[0]->change_percent), ['double', 'NULL']));
49+
$this->assertTrue(in_array(gettype($response->quotes[0]->fifty_two_week_high), ['double', 'NULL']));
50+
$this->assertTrue(in_array(gettype($response->quotes[0]->fifty_two_week_low), ['double', 'NULL']));
51+
$this->assertInstanceOf(Carbon::class, $response->quotes[0]->updated);
52+
}
53+
54+
4055
/**
4156
* @throws GuzzleException
4257
*/

tests/Unit/IndicesTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use MarketDataApp\Endpoints\Responses\Indices\Candle;
1212
use MarketDataApp\Endpoints\Responses\Indices\Candles;
1313
use MarketDataApp\Endpoints\Responses\Indices\Quote;
14+
use MarketDataApp\Endpoints\Responses\Indices\Quotes;
1415
use MarketDataApp\Exceptions\ApiException;
1516
use MarketDataApp\Tests\Traits\MockResponses;
1617
use PHPUnit\Framework\TestCase;
@@ -22,6 +23,17 @@ class IndicesTest extends TestCase
2223

2324
private Client $client;
2425

26+
private array $aapl_mocked_response = [
27+
's' => 'ok',
28+
'symbol' => ['AAPL'],
29+
'last' => [50.5],
30+
'change' => [30.2],
31+
'changepct' => [2.4],
32+
'52weekHigh' => [4023.5],
33+
'52weekLow' => [2035.0],
34+
'updated' => ['2020-01-01T00:00:00.000000Z'],
35+
];
36+
2537
protected function setUp(): void
2638
{
2739
$token = "your_api_token";
@@ -55,6 +67,46 @@ public function testQuote_success()
5567
$this->assertEquals(Carbon::parse($mocked_response['updated'][0]), $response->updated);
5668
}
5769

70+
71+
72+
/**
73+
* @throws GuzzleException
74+
* @throws \Throwable
75+
*/
76+
public function testQuotes_success()
77+
{
78+
$msft_mocked_response = [
79+
's' => 'ok',
80+
'symbol' => ['MSFT'],
81+
'last' => [300.67],
82+
'change' => [5.2],
83+
'changepct' => [2.2],
84+
'52weekHigh' => [320.5],
85+
'52weekLow' => [200.0],
86+
'updated' => ['2020-01-01T00:00:00.000000Z'],
87+
];
88+
$this->setMockResponses([
89+
new Response(200, [], json_encode($this->aapl_mocked_response)),
90+
new Response(200, [], json_encode($msft_mocked_response)),
91+
]);
92+
93+
$quotes = $this->client->indices->quotes(['AAPL', 'MSFT']);
94+
$this->assertInstanceOf(Quotes::class, $quotes);
95+
foreach ($quotes->quotes as $quote) {
96+
$this->assertInstanceOf(Quote::class, $quote);
97+
$mocked_response = $quote->symbol === "AAPL" ? $this->aapl_mocked_response : $msft_mocked_response;
98+
99+
$this->assertEquals($mocked_response['s'], $quote->status);
100+
$this->assertEquals($mocked_response['symbol'][0], $quote->symbol);
101+
$this->assertEquals($mocked_response['last'][0], $quote->last);
102+
$this->assertEquals($mocked_response['change'][0], $quote->change);
103+
$this->assertEquals($mocked_response['changepct'][0], $quote->change_percent);
104+
$this->assertEquals($mocked_response['52weekHigh'][0], $quote->fifty_two_week_high);
105+
$this->assertEquals($mocked_response['52weekLow'][0], $quote->fifty_two_week_low);
106+
$this->assertEquals(Carbon::parse($mocked_response['updated'][0]), $quote->updated);
107+
}
108+
}
109+
58110
public function testQuote_noData_success()
59111
{
60112
$mocked_response = [

0 commit comments

Comments
 (0)