如何在coinbase和GDAX中使用level参数

时间:2017-08-29 22:19:00

标签: php coinbase-api coinbase-php gdax-api

我在Github

上使用以下库

我需要从GDAX获得订单。我通过执行以下操作来完成此操作:

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

使用上面我只得到1级,根据GDAX,我会得到&#34;只有最好的出价并且询问&#34;输出是这样的:

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
                )

        )

文档说明&#34;默认情况下,仅返回内部(即最佳)出价和提问。这相当于书本深度为1级。如果您想查看更大的订单簿,请指定级别查询参数。&#34;

文档状态还指出,级别2获得&#34;前50个出价并询问(汇总)&#34;,3级获得&#34;完整订单簿(非汇总)&#34;

Github上的类包含以下与我的查询相关的代码:

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

以及&#39; book&#39;:

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

现在我想将我的函数$getOrderBook = $exchange->getOrderBook($exchangeProduct)称为2级或3级。

如何在不修改从Github导入的代码的情况下这样做呢?

使用URL,输出应如下所示:

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

感谢。

2 个答案:

答案 0 :(得分:0)

我担心唯一的方法是扩展课程并覆盖相关方法。

目前,$endpoints属性中指定的URI由getEndpoint方法填充。这填补了您在问题标题中提到的%s。您可以扩展此类并覆盖该方法:

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
}

然后您还必须覆盖orderBook方法:

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

或者,您可以向Github库提交拉取请求,调整代码以支持level

答案 1 :(得分:0)

您可以覆盖终点,因为它已声明为public

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

尽管如此,最好根据Scopey's answer中的建议创建扩展API的PR。