Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add indices->quotes to methods #11

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.5.0-beta

Added indices->quotes to parallelize and speed up multiple index quotes.

## v0.4.4-beta

Update options->option_chain to use enum values rather than the enum itself.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ $client = new MarketDataApp\Client('your_api_token');

// Indices
$quote = $client->indices->quote('VIX');
$quotes = $client->indices->quotes(['VIX', 'DJI']);
$candles = $client->indices->candles(
symbol: "VIX",
from: '2022-09-01',
Expand Down
27 changes: 26 additions & 1 deletion src/Endpoints/Indices.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use MarketDataApp\Client;
use MarketDataApp\Endpoints\Responses\Indices\Candles;
use MarketDataApp\Endpoints\Responses\Indices\Quote;
use MarketDataApp\Endpoints\Responses\Indices\Quotes;
use MarketDataApp\Exceptions\ApiException;

class Indices
Expand All @@ -24,11 +25,35 @@ public function __construct($client)
*
* @param string $symbol The index symbol, without any leading or trailing index identifiers. For example, use DJI
* do not use $DJI, ^DJI, .DJI, DJI.X, etc.
*
* @param bool $fifty_two_week Enable the output of 52-week high and 52-week low data in the quote output.
*
* @throws GuzzleException|ApiException
*/
public function quote(string $symbol, bool $fifty_two_week = false): Quote
{
return new Quote($this->client->execute(self::BASE_URL . "quotes/{$symbol}", ['52week' => $fifty_two_week]));
return new Quote($this->client->execute(self::BASE_URL . "quotes/$symbol", ['52week' => $fifty_two_week]));
}



/**
* Get a real-time price quote for a multiple indices by doing parallel requests.
*
* @param array $symbols The ticker symbols to return in the response.
* @param bool $fifty_two_week Enable the output of 52-week high and 52-week low data in the quote output.
*
* @throws \Throwable
*/
public function quotes(array $symbols, bool $fifty_two_week = false): Quotes
{
// Execute standard quotes in parallel
$calls = [];
foreach ($symbols as $symbol) {
$calls[] = [self::BASE_URL . "quotes/$symbol", ['52week' => $fifty_two_week]];
}

return new Quotes($this->client->executeInParallel($calls));
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/Endpoints/Responses/Indices/Quotes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace MarketDataApp\Endpoints\Responses\Indices;

class Quotes
{
/** @var Quote[] $quotes */
public array $quotes;

public function __construct(array $quotes)
{
foreach ($quotes as $quote) {
$this->quotes[] = new Quote($quote);
}
}
}
3 changes: 1 addition & 2 deletions src/Endpoints/Stocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ public function quote(string $symbol, bool $fifty_two_week = false): Quote
* Get a real-time price quote for a multiple stocks by doing parallel requests.
*
* @param array $symbols The ticker symbols to return in the response.
* @param bool $fifty_two_week Enable the output of 52-week high and 52-week low data in the quote output. By
* default this parameter is false if omitted.
* @param bool $fifty_two_week Enable the output of 52-week high and 52-week low data in the quote output.
*
* @throws \Throwable
*/
Expand Down
17 changes: 16 additions & 1 deletion tests/Integration/IndicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class IndicesTest extends TestCase

protected function setUp(): void
{
error_reporting(E_ALL);
$token = "your_api_token";
$client = new Client($token);
$this->client = $client;
Expand All @@ -37,6 +36,22 @@ public function testQuote_success()
}


public function testQuotes_success()
{
$response = $this->client->indices->quotes(['VIX']);

$this->assertInstanceOf(Quote::class, $response->quotes[0]);
$this->assertEquals('string', gettype($response->quotes[0]->status));
$this->assertEquals('string', gettype($response->quotes[0]->symbol));
$this->assertEquals('double', gettype($response->quotes[0]->last));
$this->assertTrue(in_array(gettype($response->quotes[0]->change), ['double', 'NULL']));
$this->assertTrue(in_array(gettype($response->quotes[0]->change_percent), ['double', 'NULL']));
$this->assertTrue(in_array(gettype($response->quotes[0]->fifty_two_week_high), ['double', 'NULL']));
$this->assertTrue(in_array(gettype($response->quotes[0]->fifty_two_week_low), ['double', 'NULL']));
$this->assertInstanceOf(Carbon::class, $response->quotes[0]->updated);
}


/**
* @throws GuzzleException
*/
Expand Down
52 changes: 52 additions & 0 deletions tests/Unit/IndicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use MarketDataApp\Endpoints\Responses\Indices\Candle;
use MarketDataApp\Endpoints\Responses\Indices\Candles;
use MarketDataApp\Endpoints\Responses\Indices\Quote;
use MarketDataApp\Endpoints\Responses\Indices\Quotes;
use MarketDataApp\Exceptions\ApiException;
use MarketDataApp\Tests\Traits\MockResponses;
use PHPUnit\Framework\TestCase;
Expand All @@ -22,6 +23,17 @@ class IndicesTest extends TestCase

private Client $client;

private array $aapl_mocked_response = [
's' => 'ok',
'symbol' => ['AAPL'],
'last' => [50.5],
'change' => [30.2],
'changepct' => [2.4],
'52weekHigh' => [4023.5],
'52weekLow' => [2035.0],
'updated' => ['2020-01-01T00:00:00.000000Z'],
];

protected function setUp(): void
{
$token = "your_api_token";
Expand Down Expand Up @@ -55,6 +67,46 @@ public function testQuote_success()
$this->assertEquals(Carbon::parse($mocked_response['updated'][0]), $response->updated);
}



/**
* @throws GuzzleException
* @throws \Throwable
*/
public function testQuotes_success()
{
$msft_mocked_response = [
's' => 'ok',
'symbol' => ['MSFT'],
'last' => [300.67],
'change' => [5.2],
'changepct' => [2.2],
'52weekHigh' => [320.5],
'52weekLow' => [200.0],
'updated' => ['2020-01-01T00:00:00.000000Z'],
];
$this->setMockResponses([
new Response(200, [], json_encode($this->aapl_mocked_response)),
new Response(200, [], json_encode($msft_mocked_response)),
]);

$quotes = $this->client->indices->quotes(['AAPL', 'MSFT']);
$this->assertInstanceOf(Quotes::class, $quotes);
foreach ($quotes->quotes as $quote) {
$this->assertInstanceOf(Quote::class, $quote);
$mocked_response = $quote->symbol === "AAPL" ? $this->aapl_mocked_response : $msft_mocked_response;

$this->assertEquals($mocked_response['s'], $quote->status);
$this->assertEquals($mocked_response['symbol'][0], $quote->symbol);
$this->assertEquals($mocked_response['last'][0], $quote->last);
$this->assertEquals($mocked_response['change'][0], $quote->change);
$this->assertEquals($mocked_response['changepct'][0], $quote->change_percent);
$this->assertEquals($mocked_response['52weekHigh'][0], $quote->fifty_two_week_high);
$this->assertEquals($mocked_response['52weekLow'][0], $quote->fifty_two_week_low);
$this->assertEquals(Carbon::parse($mocked_response['updated'][0]), $quote->updated);
}
}

public function testQuote_noData_success()
{
$mocked_response = [
Expand Down