How to use the level parameter with coinbase and GDAX

456 views Asked by At

I'm using the following library on Github

I need to get the order book from GDAX. I do this by doing the following:

$getOrderBook = $exchange->getOrderBook($exchangeProduct);
echo '<pre>';
print_r($getOrderBook);
echo '<pre>';

Using the above I only get Level 1 which according to GDAX I'll get the "Only the best bid and ask" and the output is something like this:

Array
(
    [sequence] => 2402392394
    [bids] => Array
        (
            [0] => Array
                (
                    [0] => 3857.13
                    [1] => 0.14
                    [2] => 1
                )

        )

    [asks] => Array
        (
            [0] => Array
                (
                    [0] => 3859.99
                    [1] => 0.0475099
                    [2] => 2
                )

        )

The documentation states that "By default, only the inside (i.e. best) bid and ask are returned. This is equivalent to a book depth of 1 level. If you would like to see a larger order book, specify the level query parameter."

The documentation states also states that level 2 gets the "Top 50 bids and asks (aggregated)", and level 3 get the "Full order book (non aggregated)".

The class on Github contains the following code that relates to my query:

  public function getOrderBook($product = 'BTC-USD') {
        //$this->validate('product', $product);
        return $this->request('book', array('id' => $product));
    }

and for 'book':

public $endpoints = array(
    'book' => array('method' => 'GET', 'uri' => '/products/%s/book'),
);

Now I would like to call my function $getOrderBook = $exchange->getOrderBook($exchangeProduct) for level 2 or 3.

How can I do this without modifying the code I imported from Github, please?

Using a URL the output should be as follows:

https://api.gdax.com/products/BTC-EUR/book?level=2

Thanks.

2

There are 2 answers

0
Scopey On

I'm afraid the only way to do this is to extend the class and override the relevant methods.

Currently the URI specified in the $endpoints property is filled by the getEndpoint method. That's filling the %s you mentioned in the title of your question. You can extend this class and override that method:

protected function getEndpoint($key, $params) {
    // Check if the level has been specified and pull it from the $params
    $level = null;
    if (isset($params['level'])) {
        $level = $params['level'];
        unset($params['level']);
    }
    // Run the existing endpoint parse
    $endpoint = parent::getEndpoint($key, $params);
    // Add on the level
    if ($level !== null) {
        $endpoint['uri'] .= '?level='.$level;
    }

    return $endpoint
}

Then you will also have to override the orderBook method:

public function getOrderBook($product = 'BTC-USD', $level = null) {
    return $this->request('book', array('id' => $product, 'level' => $level));
}

Alternatively you can submit a pull request to the Github library adjusting the code to support level.

0
bishop On

You can override the end-point, as it's declared public:

$exchange = new CoinbaseExchange;
// ...
$exchange->endpoints['book']['uri'] = '/products/%s/book?level=2';
$getOrderBook = $exchange->getOrderBook($exchangeProduct);

Though, it'd be better to create a PR extending the API as proposed in Scopey's answer.