Skip to content

Commit

Permalink
added examples/[y/cryptopia-fetch-order-books.py and minor fixes to c…
Browse files Browse the repository at this point in the history
…ryptopia fetchOrderBook fix ccxt#1930
  • Loading branch information
kroitor committed Feb 17, 2018
1 parent 8d99a05 commit 165fc6f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
33 changes: 33 additions & 0 deletions examples/py/cryptopia-fetch-order-books.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-

import os
import sys
from pprint import pprint

root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(root + '/python')

import ccxt # noqa: E402

id = 'cryptopia'

# instantiate the exchange by id
exchange = getattr(ccxt, id)({
# 'proxy':'https://cors-anywhere.herokuapp.com/',
})

# load all markets from the exchange
markets = exchange.load_markets()


# this will work
result = exchange.fetch_order_books(['ETH/BTC', 'LTC/BTC'])
pprint(result)

# this will also work
result = exchange.fetch_order_books(exchange.symbols[0:10])
pprint(result)

# this will not work (too many symbols)
result = exchange.fetch_order_books(exchange.symbols)
pprint(result)
22 changes: 14 additions & 8 deletions js/cryptopia.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,25 @@ module.exports = class cryptopia extends Exchange {
return this.parseOrderBook (orderbook, undefined, 'Buy', 'Sell', 'Price', 'Volume');
}

joinMarketIds (ids, glue = '-') {
let result = ids[0].toString ();
for (let i = 1; i < ids.length; i++) {
result += glue + ids[i].toString ();
}
return result;
}

async fetchOrderBooks (symbols = undefined, params = {}) {
await this.loadMarkets ();
let ids = undefined;
if (!symbols) {
ids = this.ids.join ('-');
// max URL length is 2083 symbols, including http schema, hostname, tld, etc...
if (ids.length > 2048) {
let numIds = this.ids.length;
let numIds = this.ids.length;
// max URL length is 2083 characters, including http schema, hostname, tld, etc...
if (numIds > 2048)
throw new ExchangeError (this.id + ' has ' + numIds.toString () + ' symbols exceeding max URL length, you are required to specify a list of symbols in the first argument to fetchOrderBooks');
}
ids = this.joinMarketIds (this.ids);
} else {
ids = this.marketIds (symbols);
ids = ids.join ('-');
ids = this.joinMarketIds (this.marketIds (symbols));
}
let response = await this.publicGetGetMarketOrderGroupsIds (this.extend ({
'ids': ids,
Expand All @@ -193,7 +199,7 @@ module.exports = class cryptopia extends Exchange {
let result = {};
for (let i = 0; i < orderbooks.length; i++) {
let orderbook = orderbooks[i];
let id = this.safeString (orderbook, 'TradePairId');
let id = this.safeInteger (orderbook, 'TradePairId');
let symbol = id;
if (id in this.markets_by_id) {
let market = this.markets_by_id[id];
Expand Down

0 comments on commit 165fc6f

Please sign in to comment.