question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I use 'fetch_order_book' and pass optional params?

See original GitHub issue

Thanks so much for publishing your library! I’ve learned a lot by studying your code.

I’m able to retrieve order book data from CEX with a code like so:

import ccxt
ex = CexExchange()
results = ex.fetch_order_book(symbol='BTC/USD')

The results contain all of the order book ask and bid offers, works great. But, I only want to fetch the first 10 asks and bids (i.e. depth = 10). According to CEX’s api docs, a url like the following does the trick:

https://cex.io/api/order_book/BTC/USD/?depth=10

Which raises the question, “How do I do this using the CCXT library?” Having RTFM 😊, it is my understanding the params argument can be used “to pass a custom setting or an optional parameter to your unified query.” (ref: https://github.com/ccxt-dev/ccxt/wiki/Manual#api-methods–endpoints, end of sub-section Unified API). I have tried the following code, but it raises an error:

import ccxt
ex = CexExchange()
results = ex.fetch_order_book(symbol='BTC/USD', params={'depth': 10})

The exception raised is:

Error
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/local/lib/python3.6/unittest/case.py", line 601, in run
    testMethod()
  File "/home/mellertson/Documents/Development_Projects/Money_Maker/Python_Code/MoneyMaker/tests/Exchanges/test_Exchange_classes.py", line 668, in test_fetch_order_book____1_as_depth
    aResult = ex.fetch_order_book(symbol=iSymbol, params={'depth': 10})
  File "/home/mellertson/Documents/python_virtual_envs/python3.6/lib/python3.6/site-packages/ccxt/exchanges.py", line 7235, in fetch_order_book
    timestamp = orderbook['timestamp'] * 1000
KeyError: 'timestamp'

I also tried modifying ccxt.cex.fetch_order_book method, but to no avail. I made the following modifications:

def fetch_order_book(self, symbol, params={}):
    self.load_markets()
    orderbook =  self.publicGetOrderBookPairDepth(self.extend({
        'pair': self.market_id(symbol),
    }, params))
    timestamp = orderbook['timestamp'] * 1000
    return self.parse_order_book(orderbook, timestamp)

And of modified the string in the params dictionary to read ‘order_book/{pair}{depth}’.

Perhaps I am misunderstanding the docs. But, I’m at a loss for how to get this to work. Any help would be greatly appreciated!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mellertsoncommented, Sep 30, 2017

I ran all tests for the exchange “cex” before making any changes to the code. I noted, there is a problem with the metod ccxt.cex.fetch_balance, but that seems unrelated to this issue.

I then made the fix, and ran tests for all languages, but again only for the exchange “cex”. All tests passed, except for the one noted above regarding ccxt.cex.fetch_balance. It looks like the code fix worked, and didn’t break anything.

0reactions
kroitorcommented, Sep 30, 2017

I also added examples on this to the Manual here.

And uploaded the following examples into the repo:

Thanks for pointing this out!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass optional parameters while omitting some other ...
As specified in the documentation, use undefined : export interface INotificationService { error(message: string, title?: string, autoHideAfter?
Read more >
Exchanges — ccxt 2.4.71 documentation
Every exchange has a set of properties and methods, most of which you can override by passing an associative array of params to...
Read more >
Named and Optional Arguments - C# Programming Guide
Optional arguments enable you to omit arguments for some parameters. Both techniques can be used with methods, indexers, constructors, ...
Read more >
Using Python Optional Arguments When Defining Functions
In this tutorial, you'll learn about Python optional arguments and how to define functions with default ... An argument is a value you...
Read more >
How to pass optional parameters to a function in Python?
Passing without using keyword arguments · The order of parameters should be maintained i.e. the order in which parameters are defined in function ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found