linwj/bybit

Bybit API Like the official document interface, Support for arbitrary extension.

1.1.1 2023-12-20 03:18 UTC

This package is auto-updated.

Last update: 2024-10-27 08:50:06 UTC


README

It is recommended that you read the official document first

Bybit docs https://bybit-exchange.github.io/docs/linear/

All interface methods are initialized the same as those provided by Bybit. See details src/api

中文文档

Other exchanges API

Exchanges It includes all of the following exchanges and is highly recommended.

Bitmex Support Websocket

Okex Support Websocket

Huobi Support Websocket

Binance Support Websocket

Kucoin

Mxc

Coinbase

ZB

Bitfinex

Bittrex

Kraken

Gate

Bigone

Crex24

Bybit

Coinbene

Bitget

Poloniex

If you don't find the exchange SDK you want, you can tell me and I'll join them.

Installation

composer require linwj/bybit

Support V5 More

$bybit=new BybitV5($key,$secret);

//You can set special needs
$bybit->setOptions([
    //Set the request timeout to 60 seconds by default
    'timeout'=>10,

    'headers'=>[
        //X-Referer or Referer - 經紀商用戶專用的頭參數
        //X-BAPI-RECV-WINDOW 默認值為5000
        //cdn-request-id
        'X-BAPI-RECV-WINDOW'=>'6000',
    ]
]);


try {
    $result=$bybit->order()->postCreate([
        'category'=>'spot',
        'symbol'=>'BTCUSDT',
        'side'=>'buy',
        'orderType'=>'limit',
        'qty'=>'1',
        'price'=>'1000',

        //'orderLinkId'=>'xxxxxxxxxxx',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->order()->getRealTime([
        'category'=>'spot',
        'symbol'=>'BTCUSDT',

        'orderId'=>'xxxxxxxxxx',
        //'orderLinkId'=>'xxxxxxxxxxx',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}


try {
    $result=$bybit->order()->getSpotBorrowCheck([
        'category'=>'spot',
        'symbol'=>'BTCUSDT',
        'side'=>'by'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

More API V5

V1 Fails Support for more request Settings

$bybit=new BybitLinear();
//or new
//$bybit=new BybitInverse();

//You can set special needs
$bybit->setOptions([
    //Set the request timeout to 60 seconds by default
    'timeout'=>10,
    
    //If you are developing locally and need an agent, you can set this
    //'proxy'=>true,
    //More flexible Settings
    /* 'proxy'=>[
     'http'  => 'http://127.0.0.1:12333',
     'https' => 'http://127.0.0.1:12333',
     'no'    =>  ['.cn']
     ], */
    //Close the certificate
    //'verify'=>false,
]);

USDT Perpetual

Market Data API More

$bybit=new BybitLinear();

try {
    $result=$bybit->publics()->getOrderBookL2([
        'symbol'=>'BTCUSDT'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->publics()->getKline([
        'symbol'=>'BTCUSDT',
        'interval'=>'15',
        'from'=>time()-3600,
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->publics()->getTickers();
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->publics()->getRecentTradingRecords([
        'symbol'=>'BTCUSDT',
        'limit'=>'5',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->publics()->getSymbols();
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

Place Active Order API More

$bybit=new BybitLinear($key,$secret);

try {
    $result=$bybit->privates()->postOrderCreate([
        //'order_link_id'=>'xxxxxxxxxxxxxx',
        'side'=>'Buy',
        'symbol'=>'BTCUSDT',
        'order_type'=>'Limit',
        'qty'=>'1',
        'price'=>'4000',
        'time_in_force'=>'GoodTillCancel',
        'reduce_only'=>'false',
        'close_on_trigger'=>'false',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->getOrderSearch([
        'order_id'=>'xxxxxxxxxxxxx',
        //'order_link_id'=>'xxxxxxxxxxxxxx',
        'symbol'=>'BTCUSDT',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->postOrderReplace([
        'order_id'=>'xxxxxxxxxxxxx',
        'symbol'=>'BTCUSDT',
        'p_r_qty'=>'2',
        'p_r_price'=>'4999'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->postOrderCancel([
        'order_id'=>'xxxxxxxxxxxxx',
        //'order_link_id'=>'xxxxxxxxxxxxxx',
        'symbol'=>'BTCUSDT',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->getOrderList([
        'order_id'=>'xxxxxxxxxxxxx',
        //'order_link_id'=>'xxxxxxxxxxxxxx',
        'symbol'=>'BTCUSDT',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

Place Conditional Order API More

$bybit=new BybitLinear($key,$secret);

try {
    $result=$bybit->privates()->postStopOrderCreate([
        //'order_link_id'=>'xxxxxxxxxxxxxx',
        'side'=>'Buy',
        'symbol'=>'BTCUSDT',
        'order_type'=>'Limit',
        'qty'=>'1',
        'price'=>'4000',
        'time_in_force'=>'GoodTillCancel',
        'reduce_only'=>'false',
        'close_on_trigger'=>'false',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->getStopOrderSearch([
        'order_id'=>'xxxxxxxxxxxxx',
        //'order_link_id'=>'xxxxxxxxxxxxxx',
        'symbol'=>'BTCUSDT',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->postStopOrderReplace([
        'order_id'=>'xxxxxxxxxxxxx',
        'symbol'=>'BTCUSDT',
        'p_r_qty'=>'2',
        'p_r_price'=>'4999'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->postStopOrderCancel([
        'order_id'=>'xxxxxxxxxxxxx',
        //'order_link_id'=>'xxxxxxxxxxxxxx',
        'symbol'=>'BTCUSDT',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->getStopOrderList([
        'order_id'=>'xxxxxxxxxxxxx',
        //'order_link_id'=>'xxxxxxxxxxxxxx',
        'symbol'=>'BTCUSDT',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

My Position API More

$bybit=new BybitLinear($key,$secret);

try {
    $result=$bybit->privates()->getPositionList([
        'symbol'=>'BTCUSDT',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->postChangePositionMargin([
        'symbol'=>'BTCUSDT',
        'margin'=>'1'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->postPositionTradingStop([
        'symbol'=>'BTCUSDT',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->getUserLeverage();
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->postUserLeverageSave([
        'symbol'=>'BTCUSDT',
        'leverage'=>'1'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->getExecutionList([
        'symbol'=>'BTCUSDT',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

More Test

More API

Inverse Perpetual

Market data API More

$bybit=new BybitInverse();

try {
    $result=$bybit->publics()->getOrderBookL2([
        'symbol'=>'BTCUSD'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->publics()->getKlineList([
        'symbol'=>'BTCUSD',
        'interval'=>'15',
        'from'=>time()-3600,
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->publics()->getTickers();
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->publics()->getTradingRecords([
        'symbol'=>'BTCUSD',
        'limit'=>'5',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->publics()->getSymbols();
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

Place Active Order API More

$bybit=new BybitInverse($key,$secret);

try {
    $result=$bybit->privates()->postOrderCreate([
        //'order_link_id'=>'xxxxxxxxxxxxxx',
        'side'=>'Buy',
        'symbol'=>'BTCUSD',
        'order_type'=>'Limit',
        'qty'=>'1',
        'price'=>'4000',
        'time_in_force'=>'GoodTillCancel',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->getOrder([
        'order_id'=>'xxxxxxxxxxxxx',
        //'order_link_id'=>'xxxxxxxxxxxxxx',
        'symbol'=>'BTCUSD',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->postOrderReplace([
        'order_id'=>'xxxxxxxxxxxxx',
        'symbol'=>'BTCUSD',
        'p_r_qty'=>'2',
        'p_r_price'=>'4999'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->postOrderCancel([
        'order_id'=>'xxxxxxxxxxxxx',
        //'order_link_id'=>'xxxxxxxxxxxxxx',
        'symbol'=>'BTCUSD',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->getOrderList([
        'order_id'=>'xxxxxxxxxxxxx',
        //'order_link_id'=>'xxxxxxxxxxxxxx',
        'symbol'=>'BTCUSD',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

Place Conditional Order API More

$bybit=new BybitInverse($key,$secret);

try {
    $result=$bybit->privates()->postStopOrderCreate([
        //'order_link_id'=>'xxxxxxxxxxxxxx',
        'side'=>'Buy',
        'symbol'=>'BTCUSD',
        'order_type'=>'Limit',
        'qty'=>'1',
        'price'=>'4000',
        'time_in_force'=>'GoodTillCancel',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->getStopOrder([
        'order_id'=>'xxxxxxxxxxxxx',
        //'order_link_id'=>'xxxxxxxxxxxxxx',
        'symbol'=>'BTCUSD',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->postStopOrderReplace([
        'order_id'=>'xxxxxxxxxxxxx',
        'symbol'=>'BTCUSD',
        'p_r_qty'=>'2',
        'p_r_price'=>'4999'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->postStopOrderCancel([
        'order_id'=>'xxxxxxxxxxxxx',
        //'order_link_id'=>'xxxxxxxxxxxxxx',
        'symbol'=>'BTCUSD',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->getStopOrderList([
        'order_id'=>'xxxxxxxxxxxxx',
        //'order_link_id'=>'xxxxxxxxxxxxxx',
        'symbol'=>'BTCUSD',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

My Position Api More

$bybit=new BybitInverse($key,$secret);

try {
    $result=$bybit->privates()->getPositionList([
        'symbol'=>'BTCUSD',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->postChangePositionMargin([
        'symbol'=>'BTCUSD',
        'margin'=>'1'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->postPositionTradingStop([
        'symbol'=>'BTCUSD',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->getUserLeverage();
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->postUserLeverageSave([
        'symbol'=>'BTCUSD',
        'leverage'=>'1'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

try {
    $result=$bybit->privates()->getExecutionList([
        'symbol'=>'BTCUSD',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

More Test

More API