From 6d8385f475b0ff22d26240937642fa3fb815ec0b Mon Sep 17 00:00:00 2001 From: Artiom Misiru Date: Thu, 2 Jan 2025 11:42:07 +0200 Subject: [PATCH] Fix getCandles operation --- CHANGELOG.md | 8 ++++++-- README.md | 6 ++++++ src/Client.php | 13 ++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32cfdba..711964d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,14 @@ # Release Notes -## [6.2.0](https://github.com/timirey/xapi-php/compare/6.0.4..6.0.5) - 2024-12-27 +## [6.2.1](https://github.com/timirey/xapi-php/compare/6.2.0..6.2.1) - 2025-01-02 + +- Add fix for `FetchCandles`. + +## [6.2.0](https://github.com/timirey/xapi-php/compare/6.1.0..6.2.0) - 2024-12-27 - Add new `QuoteId` and `MarginMode`. -## [6.1.0](https://github.com/timirey/xapi-php/compare/6.0.4..6.0.5) - 2024-12-27 +## [6.1.0](https://github.com/timirey/xapi-php/compare/6.0.5..6.1.0) - 2024-12-27 - Remove timeout for stream connection. diff --git a/README.md b/README.md index 7515ab7..fe25d35 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,12 @@ $client->fetchBalance( Subscribes for and unsubscribes from API chart candles. The interval of every candle is 1 minute. A new candle arrives every minute. +#### Existing issue +The **getCandles** API operation is unstable, often failing to subscribe immediately to the desired symbol. + +#### Workaround +The package currently requests the symbol's history before subscribing. See [fetchCandles](https://github.com/timirey/xapi-php/blob/e386b023429c19b0a2700300acc02ed7e211a3aa/src/Client.php#L590) method. + ```PHP use Timirey\XApi\Responses\Data\CandleStreamRecord; use Timirey\XApi\Responses\FetchCandlesResponse; diff --git a/src/Client.php b/src/Client.php index b8e9ba2..7c72cb5 100644 --- a/src/Client.php +++ b/src/Client.php @@ -8,6 +8,7 @@ use Timirey\XApi\Enums\Cmd; use Timirey\XApi\Enums\Host; use Timirey\XApi\Enums\Level; +use Timirey\XApi\Enums\Period; use Timirey\XApi\Exceptions\ErrorResponseException; use Timirey\XApi\Exceptions\InvalidPayloadException; use Timirey\XApi\Exceptions\InvalidResponseException; @@ -574,7 +575,9 @@ final public function fetchBalance(callable $callback): void } /** - * Subscribe to candles stream. + * Subscribe to candles stream. This operation is not stable. + * + * @see https://github.com/timirey/xapi-php/compare/6.2.0...6.2.1 * * @param string $symbol Symbol for which to get the candles. * @param callable $callback Callback function to handle the response. @@ -589,6 +592,14 @@ final public function fetchBalance(callable $callback): void */ final public function fetchCandles(string $symbol, callable $callback): void { + $chartLastInfoRecord = new ChartLastInfoRecord( + period: Period::PERIOD_M1, + start: new DateTime(), + symbol: $symbol, + ); + + $this->getChartLastRequest($chartLastInfoRecord); + $this->subscribe( new FetchCandlesPayload($this->streamSessionId, $symbol), FetchCandlesResponse::class,