carpenstar / bybitapi-sdk-core
Requires
- php: >=7.4
This package is auto-updated.
Last update: 2024-10-01 16:03:32 UTC
README
Bybit SDK
注意:这是一个非官方SDK,由独立开发者提供。
如果您对设置有任何疑问,或者关于找到的bug的信息,您可以在问题中留言,通过发送邮件到 mighty.vlad@gmail.com(俄语,英语)或在telegram: @novisad0189
是的——星级越多,这个项目将越活跃 :-)
要求
- PHP >= 7.4,以及websockets-package的额外要求
- posix - 扩展
- pcntl - 扩展
安装
composer require carpenstar/bybitapi-sdk-spot:3.*
composer require carpenstar/bybitapi-sdk-derivatives:3.*
composer require carpenstar/bybitapi-sdk-websockets:3.*
市场数据 - V5 (开发中...)
composer require carpenstar/bybitapi-sdk-v5-market:5.*
API密钥生成
https://testnet.bybit.com/app/user/api-management - 测试网
https://www.bybit.com/app/user/api-management - 生产环境
应用程序实例
use Carpenstar\ByBitAPI\BybitAPI; $sdk = new BybitAPI(); // Setting the host for the next call to the exchange API $sdk->setHost('https://api-testnet.bybit.com'); // Setting an API key that will be applied the next time you access the exchange API (optional, since the parameter is required when accessing private endpoints) $sdk->setApiKey('apiKey'); // Setting a secret key that will be applied the next time you access the exchange API (optional, because the parameter is required when accessing private endpoints) $sdk->setSecret('apiSecret'); // A wrapper function that allows you to set connection parameters with one call $sdk->setCredentials('https://api-testnet.bybit.com', 'apiKey', 'apiSecret') // The function is used to access endpoints that do not require authorization (see endpoint description) $sdk->publicEndpoint(<'Endpoint class name'>, <'DTO containing request parameters'>); // The function is used to access endpoints that require authorization (see endpoint description) $sdk->privateEndpoint(<'Endpoint class name'>, <'DTO containing request parameters'>)
REST - 查询
所有可以调用的端点分为两种类型——公共(不需要授权)和私有(每个请求都需要授权)。
// The function is used to access endpoints that do not require authorization (see endpoint description) $sdk->publicEndpoint(<'Endpoint class name'>, <'DTO containing request parameters'>); // The function is used to access endpoints that require authorization (see endpoint description) $sdk->privateEndpoint(<'Endpoint class name'>, <'DTO containing request parameters'>)
以衍生品/行情信息端点为例,让我们向交易所API发起请求
use Carpenstar\ByBitAPI\BybitAPI; use Carpenstar\ByBitAPI\Derivatives\MarketData\TickerInfo\Request\TickerInfoRequest; use Carpenstar\ByBitAPI\Derivatives\MarketData\TickerInfo\TickerInfo; // Here we set only the host, because we don’t need authorization $sdk = (new BybitAPI())->setCredentials('https://api-testnet.bybit.com'); // Prepare the endpoint for the request: $endpoint = $sdk->publicEndpoint(TickerInfo::class, (new TickerInfoRequest())->setSymbol('BTCUSDT')); // Start execution of the request: $sdk->execute();
execute()函数在完成请求后始终返回一个对象,无论请求是否成功实现Carpenstar\ByBitAPI\Core\Interfaces\IResponseInterface接口
namespace Carpenstar\ByBitAPI\Core\Interfaces; use Carpenstar\ByBitAPI\Core\Objects\AbstractResponse; interface IResponseInterface { public function getReturnCode(): int; // Request completion code. If successful, it will always be 0 public function getReturnMessage(): string; // Return message, usually 'OK' public function getExtendedInfo(): array; // Extended information public function getResult(): AbstractResponse; // Endpoint-specific DTO object containing the response from the exchange API }
要获取响应的主体,请调用getResult()函数,它将返回一个包含有关行情信息的DTO对象
在行情信息端点的情况下,这个DTO将是一个实现了Carpenstar\ByBitAPI\Derivatives\MarketData\TickerInfo\Interfaces\TickerInfoResponse接口的对象
namespace Carpenstar\ByBitAPI\Derivatives\MarketData\TickerInfo\Interfaces; interface ITickerInfoResponseInterface { /** * @return ITickerInfoResponseItemInterface */ public function getTickerInfo(): ITickerInfoResponseItemInterface; }
接下来,调用getTickerInfo()函数将使我们能够获取有关行情信息的对象(以下DTO实现了ITickerInfoResponseItemInterface接口)
namespace Carpenstar\ByBitAPI\Derivatives\MarketData\TickerInfo\Interfaces; interface ITickerInfoResponseItemInterface { /** * Symbol name * @return string */ public function getSymbol(): string; /** * Best bid price * @return float */ public function getBidPrice(): float; /** * Best ask price * @return float */ public function getAskPrice(): float; /** * Last transaction price * @return float */ public function getLastPrice(): float; /** * Direction of price change * @return string */ public function getLastTickDirection(): string; /** * Price of 24 hours ago * @return float */ public function getPrevPrice24h(): float; /** * Percentage change of market price relative to 24h * @return float */ public function getPrice24hPcnt(): float; /** * The highest price in the last 24 hours * @return float */ public function getHighPrice24h(): float; /** * Lowest price in the last 24 hours * @return float */ public function getLowPrice24h(): float; /** * Hourly market price an hour ago * @return float */ public function getPrevPrice1h(): float; /** * Mark price * @return float */ public function getMarkPrice(): float; /** * Index price * @return float */ public function getIndexPrice(): float; /** * Open interest * @return float */ public function getOpenInterests(): float; /** * Turnover in the last 24 hours * @return float */ public function getTurnover24h(): float; /** * Trading volume in the last 24 hours * @return float */ public function getVolume24h(): float; /** * Funding rate * @return float */ public function getFundingRate(): float; /** * Next timestamp for funding to settle * @return \DateTime */ public function getNextFundingTime(): \DateTime; /** * Predicted delivery price. It has value when 30 min before delivery * @return float */ public function getPredictedDeliveryPrice(): float; /** * Basis rate for futures * @return float */ public function getBasisRate(): float; /** * Delivery fee rate * @return float */ public function getDeliveryFeeRate(): float; /** * Delivery timestamp * @return \DateTime */ public function getDeliveryTime(): \DateTime; /** * Open interest value * @return float */ public function getOpenInterestValue(): float; }
总结上述内容,以下示例代码可能看起来像这样
use Carpenstar\ByBitAPI\BybitAPI; use Carpenstar\ByBitAPI\Derivatives\MarketData\TickerInfo\Interfaces\ITickerInfoResponseItemInterface; use Carpenstar\ByBitAPI\Derivatives\MarketData\TickerInfo\Request\TickerInfoRequest; use Carpenstar\ByBitAPI\Derivatives\MarketData\TickerInfo\TickerInfo; $sdk = (new BybitAPI())->setCredentials('https://api-testnet.bybit.com'); $endpoint = $sdk->publicEndpoint(TickerInfo::class, (new TickerInfoRequest())->setSymbol('BTCUSDT')); $endpointResponse = $sdk->execute(); echo "Return code: {$endpointResponse->getReturnCode()}\n"; echo "Return message: {$endpointResponse->getReturnMessage()}\n"; /** @var ITickerInfoResponseItemInterface $tickerInfo */ $tickerInfo = $endpointResponse->getResult()->getTickerInfo(); echo "Symbol: {$tickerInfo->getSymbol()}\n"; echo "Bid Price: {$tickerInfo->getBidPrice()}\n"; echo "Ask Price: {$tickerInfo->getAskPrice()}\n"; echo "Last Price: {$tickerInfo->getLastPrice()}\n"; echo "Last Tick Direction: {$tickerInfo->getLastTickDirection()}\n"; echo "Prev Price 24 hours: {$tickerInfo->getPrevPrice24h()}\n"; echo "Prev Price 24 hours(%): {$tickerInfo->getPrice24hPcnt()}\n"; echo "High Price 24 hours: {$tickerInfo->getHighPrice24h()}\n"; echo "Low Price 24 hours: {$tickerInfo->getLowPrice24h()}\n"; echo "Prev price 1 hour: {$tickerInfo->getPrevPrice1h()}\n"; echo "Mark Price: {$tickerInfo->getMarkPrice()}\n"; echo "Index Price: {$tickerInfo->getIndexPrice()}\n"; echo "Open Interest: {$tickerInfo->getOpenInterests()}\n"; echo "Open Interest Value: {$tickerInfo->getOpenInterestValue()}\n"; echo "Turnover 24 hours: {$tickerInfo->getTurnover24h()}\n"; echo "Volume 24 hours: {$tickerInfo->getVolume24h()}\n"; echo "Funding Rate: {$tickerInfo->getFundingRate()}\n"; echo "Next Funding Time: {$tickerInfo->getNextFundingTime()->format("Y-m-d H:i:s")}\n"; echo "Predicted Delivery Price: {$tickerInfo->getPredictedDeliveryPrice()}\n"; echo "Basis Rate: {$tickerInfo->getBasisRate()}\n"; echo "Delivery Fee Rate: {$tickerInfo->getDeliveryFeeRate()}\n"; echo "Open Interests Value: {$tickerInfo->getOpenInterestValue()}\n"; /** * Return code: 0 * Return message: OK * Symbol: BTCUSDT * Bid Price: 59933.6 * Ask Price: 59935.7 * Last Price: 59938 * Last Tick Direction: ZeroMinusTick * Prev Price 24 hours: 58627.5 * Prev Price 24 hours(%): 0.022352 * High Price 24 hours: 63074.5 * Low Price 24 hours: 58267.4 * Prev price 1 hour: 59997 * Mark Price: 59938 * Index Price: 59957.26 * Open Interest: 208384.158 * Open Interest Value: 12490129662.2 * Turnover 24 hours: 2907929540.5417 * Volume 24 hours: 48504.964 * Funding Rate: 8.407E-5 * Next Funding Time: 2024-07-15 00:00:00 * Predicted Delivery Price: 0 * Basis Rate: 0 * Delivery Fee Rate: 0 * Open Interests Value: 12490129662.2 */
API调用的示例以及可用请求/响应对象的描述可以在每个端点的页面找到