How can I use 'fetch_order_book' and pass optional params?
See original GitHub issueThanks 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:
- Created 6 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top GitHub Comments
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.I also added examples on this to the Manual here.
And uploaded the following examples into the repo:
Thanks for pointing this out!