|
3 | 3 | // --------------------------------------------------------------------------- |
4 | 4 |
|
5 | 5 | const Exchange = require ('./base/Exchange'); |
6 | | -const { ExchangeError, ArgumentsRequired } = require ('./base/errors'); |
| 6 | +const { ExchangeError, ArgumentsRequired, InvalidOrder } = require ('./base/errors'); |
7 | 7 |
|
8 | 8 | // --------------------------------------------------------------------------- |
9 | 9 |
|
@@ -17,15 +17,33 @@ module.exports = class mercado extends Exchange { |
17 | 17 | 'version': 'v3', |
18 | 18 | 'has': { |
19 | 19 | 'CORS': true, |
20 | | - 'createMarketOrder': false, |
| 20 | + 'createMarketOrder': true, |
21 | 21 | 'fetchOrder': true, |
22 | 22 | 'withdraw': true, |
| 23 | + 'fetchOHLCV': true, |
| 24 | + 'fetchOrders': true, |
| 25 | + 'fetchOpenOrders': true, |
| 26 | + 'fetchTicker': true, |
| 27 | + 'fetchTickers': false, |
| 28 | + }, |
| 29 | + 'timeframes': { |
| 30 | + '5m': '5m', |
| 31 | + '15m': '15m', |
| 32 | + '30m': '30m', |
| 33 | + '1h': '1h', |
| 34 | + '6h': '6h', |
| 35 | + '12h': '12h', |
| 36 | + '1d': '1d', |
| 37 | + '3d': '3d', |
| 38 | + '1w': '1w', |
| 39 | + '2w': '2w', |
23 | 40 | }, |
24 | 41 | 'urls': { |
25 | 42 | 'logo': 'https://user-images.githubusercontent.com/1294454/27837060-e7c58714-60ea-11e7-9192-f05e86adb83f.jpg', |
26 | 43 | 'api': { |
27 | 44 | 'public': 'https://www.mercadobitcoin.net/api', |
28 | 45 | 'private': 'https://www.mercadobitcoin.net/tapi', |
| 46 | + 'v4Public': 'https://www.mercadobitcoin.com.br/v4', |
29 | 47 | }, |
30 | 48 | 'www': 'https://www.mercadobitcoin.com.br', |
31 | 49 | 'doc': [ |
@@ -55,16 +73,23 @@ module.exports = class mercado extends Exchange { |
55 | 73 | 'list_orderbook', |
56 | 74 | 'place_buy_order', |
57 | 75 | 'place_sell_order', |
| 76 | + 'place_market_buy_order', |
| 77 | + 'place_market_sell_order', |
58 | 78 | 'withdraw_coin', |
59 | 79 | ], |
60 | 80 | }, |
| 81 | + 'v4Public': { |
| 82 | + 'get': [ |
| 83 | + '{coin}/candle/', |
| 84 | + ], |
| 85 | + }, |
61 | 86 | }, |
62 | 87 | 'markets': { |
63 | | - 'BTC/BRL': { 'id': 'BRLBTC', 'symbol': 'BTC/BRL', 'base': 'BTC', 'quote': 'BRL', 'suffix': 'Bitcoin' }, |
64 | | - 'LTC/BRL': { 'id': 'BRLLTC', 'symbol': 'LTC/BRL', 'base': 'LTC', 'quote': 'BRL', 'suffix': 'Litecoin' }, |
65 | | - 'BCH/BRL': { 'id': 'BRLBCH', 'symbol': 'BCH/BRL', 'base': 'BCH', 'quote': 'BRL', 'suffix': 'BCash' }, |
66 | | - 'XRP/BRL': { 'id': 'BRLXRP', 'symbol': 'XRP/BRL', 'base': 'XRP', 'quote': 'BRL', 'suffix': 'Ripple' }, |
67 | | - 'ETH/BRL': { 'id': 'BRLETH', 'symbol': 'ETH/BRL', 'base': 'ETH', 'quote': 'BRL', 'suffix': 'Ethereum' }, |
| 88 | + 'BTC/BRL': { 'id': 'BRLBTC', 'symbol': 'BTC/BRL', 'base': 'BTC', 'quote': 'BRL', 'precision': { 'amount': 8, 'price': 5 }, 'suffix': 'Bitcoin' }, |
| 89 | + 'LTC/BRL': { 'id': 'BRLLTC', 'symbol': 'LTC/BRL', 'base': 'LTC', 'quote': 'BRL', 'precision': { 'amount': 8, 'price': 5 }, 'suffix': 'Litecoin' }, |
| 90 | + 'BCH/BRL': { 'id': 'BRLBCH', 'symbol': 'BCH/BRL', 'base': 'BCH', 'quote': 'BRL', 'precision': { 'amount': 8, 'price': 5 }, 'suffix': 'BCash' }, |
| 91 | + 'XRP/BRL': { 'id': 'BRLXRP', 'symbol': 'XRP/BRL', 'base': 'XRP', 'quote': 'BRL', 'precision': { 'amount': 8, 'price': 5 }, 'suffix': 'Ripple' }, |
| 92 | + 'ETH/BRL': { 'id': 'BRLETH', 'symbol': 'ETH/BRL', 'base': 'ETH', 'quote': 'BRL', 'precision': { 'amount': 8, 'price': 5 }, 'suffix': 'Ethereum' }, |
68 | 93 | }, |
69 | 94 | 'fees': { |
70 | 95 | 'trading': { |
@@ -168,48 +193,70 @@ module.exports = class mercado extends Exchange { |
168 | 193 | } |
169 | 194 |
|
170 | 195 | async createOrder (symbol, type, side, amount, price = undefined, params = {}) { |
171 | | - if (type === 'market') |
172 | | - throw new ExchangeError (this.id + ' allows limit orders only'); |
173 | | - let method = 'privatePostPlace' + this.capitalize (side) + 'Order'; |
174 | | - let order = { |
| 196 | + const request = { |
175 | 197 | 'coin_pair': this.marketId (symbol), |
176 | | - 'quantity': amount, |
177 | | - 'limit_price': price, |
178 | 198 | }; |
179 | | - let response = await this[method] (this.extend (order, params)); |
| 199 | + let method = this.capitalize (side) + 'Order'; |
| 200 | + if (type === 'limit') { |
| 201 | + method = 'privatePostPlace' + method; |
| 202 | + request['limit_price'] = this.priceToPrecision (symbol, price); |
| 203 | + request['quantity'] = this.amountToPrecision (symbol, amount); |
| 204 | + } else { |
| 205 | + method = 'privatePostPlaceMarket' + method; |
| 206 | + if (side === 'buy') { |
| 207 | + if (price === undefined) { |
| 208 | + throw new InvalidOrder (this.id + ' createOrder() requires the price argument with market buy orders to calculate total order cost (amount to spend), where cost = amount * price. Supply a price argument to createOrder() call if you want the cost to be calculated for you from price and amount'); |
| 209 | + } |
| 210 | + request['cost'] = this.priceToPrecision (symbol, amount * price); |
| 211 | + } else { |
| 212 | + request['quantity'] = this.amountToPrecision (symbol, amount); |
| 213 | + } |
| 214 | + } |
| 215 | + const response = await this[method] (this.extend (request, params)); |
| 216 | + // TODO: replace this with a call to parseOrder for unification |
180 | 217 | return { |
181 | 218 | 'info': response, |
182 | 219 | 'id': response['response_data']['order']['order_id'].toString (), |
183 | 220 | }; |
184 | 221 | } |
185 | 222 |
|
186 | 223 | async cancelOrder (id, symbol = undefined, params = {}) { |
187 | | - if (symbol === undefined) |
188 | | - throw new ArgumentsRequired (this.id + ' cancelOrder() requires a symbol argument'); |
| 224 | + if (symbol === undefined) { |
| 225 | + throw new ArgumentsRequired (this.id + ' cancelOrder () requires a symbol argument'); |
| 226 | + } |
189 | 227 | await this.loadMarkets (); |
190 | | - let market = this.market (symbol); |
191 | | - let response = await this.privatePostCancelOrder (this.extend ({ |
| 228 | + const market = this.market (symbol); |
| 229 | + const request = { |
192 | 230 | 'coin_pair': market['id'], |
193 | 231 | 'order_id': id, |
194 | | - }, params)); |
| 232 | + }; |
| 233 | + const response = await this.privatePostCancelOrder (this.extend (request, params)); |
195 | 234 | // |
196 | | - // { response_data: { order: { order_id: 2176769, |
197 | | - // coin_pair: "BRLBCH", |
198 | | - // order_type: 2, |
199 | | - // status: 3, |
200 | | - // has_fills: false, |
201 | | - // quantity: "0.10000000", |
202 | | - // limit_price: "1996.15999", |
203 | | - // executed_quantity: "0.00000000", |
204 | | - // executed_price_avg: "0.00000", |
205 | | - // fee: "0.00000000", |
206 | | - // created_timestamp: "1536956488", |
207 | | - // updated_timestamp: "1536956499", |
208 | | - // operations: [] } }, |
209 | | - // status_code: 100, |
210 | | - // server_unix_timestamp: "1536956499" } |
| 235 | + // { |
| 236 | + // response_data: { |
| 237 | + // order: { |
| 238 | + // order_id: 2176769, |
| 239 | + // coin_pair: 'BRLBCH', |
| 240 | + // order_type: 2, |
| 241 | + // status: 3, |
| 242 | + // has_fills: false, |
| 243 | + // quantity: '0.10000000', |
| 244 | + // limit_price: '1996.15999', |
| 245 | + // executed_quantity: '0.00000000', |
| 246 | + // executed_price_avg: '0.00000', |
| 247 | + // fee: '0.00000000', |
| 248 | + // created_timestamp: '1536956488', |
| 249 | + // updated_timestamp: '1536956499', |
| 250 | + // operations: [] |
| 251 | + // } |
| 252 | + // }, |
| 253 | + // status_code: 100, |
| 254 | + // server_unix_timestamp: '1536956499' |
| 255 | + // } |
211 | 256 | // |
212 | | - return this.parseOrder (response['response_data']['order'], market); |
| 257 | + const responseData = this.safeValue (response, 'response_data', {}); |
| 258 | + const order = this.safeValue (responseData, 'order', {}); |
| 259 | + return this.parseOrder (order, market); |
213 | 260 | } |
214 | 261 |
|
215 | 262 | parseOrderStatus (status) { |
@@ -302,16 +349,19 @@ module.exports = class mercado extends Exchange { |
302 | 349 | } |
303 | 350 |
|
304 | 351 | async fetchOrder (id, symbol = undefined, params = {}) { |
305 | | - if (symbol === undefined) |
306 | | - throw new ArgumentsRequired (this.id + ' cancelOrder() requires a symbol argument'); |
| 352 | + if (symbol === undefined) { |
| 353 | + throw new ArgumentsRequired (this.id + ' fetchOrder () requires a symbol argument'); |
| 354 | + } |
307 | 355 | await this.loadMarkets (); |
308 | | - let market = this.market (symbol); |
309 | | - let response = undefined; |
310 | | - response = await this.privatePostGetOrder (this.extend ({ |
| 356 | + const market = this.market (symbol); |
| 357 | + const request = { |
311 | 358 | 'coin_pair': market['id'], |
312 | 359 | 'order_id': parseInt (id), |
313 | | - }, params)); |
314 | | - return this.parseOrder (response['response_data']['order']); |
| 360 | + }; |
| 361 | + const response = await this.privatePostGetOrder (this.extend (request, params)); |
| 362 | + const responseData = this.safeValue (response, 'response_data', {}); |
| 363 | + const order = this.safeValue (responseData, 'order'); |
| 364 | + return this.parseOrder (order, market); |
315 | 365 | } |
316 | 366 |
|
317 | 367 | async withdraw (code, amount, address, tag = undefined, params = {}) { |
@@ -350,10 +400,61 @@ module.exports = class mercado extends Exchange { |
350 | 400 | }; |
351 | 401 | } |
352 | 402 |
|
| 403 | + parseOHLCV (ohlcv, market = undefined, timeframe = '1m', since = undefined, limit = undefined) { |
| 404 | + let timestamp = this.safeInteger (ohlcv, 'timestamp'); |
| 405 | + if (timestamp !== undefined) { |
| 406 | + timestamp = timestamp * 1000; |
| 407 | + } |
| 408 | + return [ |
| 409 | + timestamp, |
| 410 | + this.safeFloat (ohlcv, 'open'), |
| 411 | + this.safeFloat (ohlcv, 'high'), |
| 412 | + this.safeFloat (ohlcv, 'low'), |
| 413 | + this.safeFloat (ohlcv, 'close'), |
| 414 | + this.safeFloat (ohlcv, 'volume'), |
| 415 | + ]; |
| 416 | + } |
| 417 | + |
| 418 | + async fetchOHLCV (symbol, timeframe = '1m', since = undefined, limit = undefined, params = {}) { |
| 419 | + await this.loadMarkets (); |
| 420 | + let market = this.market (symbol); |
| 421 | + let request = { |
| 422 | + 'precision': this.timeframes[timeframe], |
| 423 | + 'coin': market.id.toLowerCase (), |
| 424 | + }; |
| 425 | + if (limit !== undefined && since !== undefined) { |
| 426 | + request['from'] = parseInt (since / 1000); |
| 427 | + request['to'] = this.sum (request['from'], limit * this.parseTimeframe (timeframe)); |
| 428 | + } else if (since !== undefined) { |
| 429 | + request['from'] = parseInt (since / 1000); |
| 430 | + request['to'] = this.sum (this.seconds (), 1); |
| 431 | + } else if (limit !== undefined) { |
| 432 | + request['to'] = this.seconds (); |
| 433 | + request['from'] = request['to'] - (limit * this.parseTimeframe (timeframe)); |
| 434 | + } |
| 435 | + let response = await this.v4PublicGetCoinCandle (this.extend (request, params)); |
| 436 | + return this.parseOHLCVs (response['candles'], market, timeframe, since, limit); |
| 437 | + } |
| 438 | + |
| 439 | + async fetchOrders (symbol = undefined, since = undefined, limit = undefined, params = {}) { |
| 440 | + if (symbol === undefined) { |
| 441 | + throw new ArgumentsRequired (this.id + ' fetchOrders () requires a symbol argument'); |
| 442 | + } |
| 443 | + await this.loadMarkets (); |
| 444 | + const market = this.market (symbol); |
| 445 | + const request = { |
| 446 | + 'coin_pair': market['base'], |
| 447 | + }; |
| 448 | + const response = await this.privatePostListOrders (this.extend (request, params)); |
| 449 | + const responseData = this.safeValue (response, 'response_data', {}); |
| 450 | + const orders = this.safeValue (responseData, 'orders', []); |
| 451 | + return this.parseOrders (orders, market, since, limit); |
| 452 | + } |
| 453 | + |
353 | 454 | sign (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) { |
354 | 455 | let url = this.urls['api'][api] + '/'; |
355 | 456 | let query = this.omit (params, this.extractParams (path)); |
356 | | - if (api === 'public') { |
| 457 | + if (api === 'public' || (api === 'v4Public')) { |
357 | 458 | url += this.implodeParams (path, params); |
358 | 459 | if (Object.keys (query).length) |
359 | 460 | url += '?' + this.urlencode (query); |
|