UNPKG

kamiswiss-ccxt

Version:

A JavaScript / Python / PHP cryptocurrency trading library with support for 130+ exchanges

401 lines (382 loc) 15.3 kB
'use strict'; // --------------------------------------------------------------------------- const Exchange = require ('./base/Exchange'); // --------------------------------------------------------------------------- module.exports = class independentreserve extends Exchange { describe () { return this.deepExtend (super.describe (), { 'id': 'independentreserve', 'name': 'Independent Reserve', 'countries': [ 'AU', 'NZ' ], // Australia, New Zealand 'rateLimit': 1000, 'has': { 'CORS': false, }, 'urls': { 'logo': 'https://user-images.githubusercontent.com/1294454/30521662-cf3f477c-9bcb-11e7-89bc-d1ac85012eda.jpg', 'api': { 'public': 'https://api.independentreserve.com/Public', 'private': 'https://api.independentreserve.com/Private', }, 'www': 'https://www.independentreserve.com', 'doc': 'https://www.independentreserve.com/API', }, 'api': { 'public': { 'get': [ 'GetValidPrimaryCurrencyCodes', 'GetValidSecondaryCurrencyCodes', 'GetValidLimitOrderTypes', 'GetValidMarketOrderTypes', 'GetValidOrderTypes', 'GetValidTransactionTypes', 'GetMarketSummary', 'GetOrderBook', 'GetTradeHistorySummary', 'GetRecentTrades', 'GetFxRates', ], }, 'private': { 'post': [ 'PlaceLimitOrder', 'PlaceMarketOrder', 'CancelOrder', 'GetOpenOrders', 'GetClosedOrders', 'GetClosedFilledOrders', 'GetOrderDetails', 'GetAccounts', 'GetTransactions', 'GetDigitalCurrencyDepositAddress', 'GetDigitalCurrencyDepositAddresses', 'SynchDigitalCurrencyDepositAddressWithBlockchain', 'WithdrawDigitalCurrency', 'RequestFiatWithdrawal', 'GetTrades', ], }, }, 'fees': { 'trading': { 'taker': 0.5 / 100, 'maker': 0.5 / 100, 'percentage': true, 'tierBased': false, }, }, }); } async fetchMarkets (params = {}) { const baseCurrencies = await this.publicGetGetValidPrimaryCurrencyCodes (params); const quoteCurrencies = await this.publicGetGetValidSecondaryCurrencyCodes (params); const result = []; for (let i = 0; i < baseCurrencies.length; i++) { const baseId = baseCurrencies[i]; const baseIdUppercase = baseId.toUpperCase (); const base = this.commonCurrencyCode (baseIdUppercase); for (let j = 0; j < quoteCurrencies.length; j++) { const quoteId = quoteCurrencies[j]; const quoteIdUppercase = quoteId.toUpperCase (); const quote = this.commonCurrencyCode (quoteIdUppercase); const id = baseId + '/' + quoteId; const symbol = base + '/' + quote; result.push ({ 'id': id, 'symbol': symbol, 'base': base, 'quote': quote, 'baseId': baseId, 'quoteId': quoteId, 'info': id, }); } } return result; } async fetchBalance (params = {}) { await this.loadMarkets (); const balances = await this.privatePostGetAccounts (params); const result = { 'info': balances }; for (let i = 0; i < balances.length; i++) { const balance = balances[i]; const currencyId = this.safeString (balance, 'CurrencyCode'); const uppercase = currencyId.toUpperCase (); const code = this.commonCurrencyCode (uppercase); let account = this.account (); account['free'] = balance['AvailableBalance']; account['total'] = balance['TotalBalance']; account['used'] = account['total'] - account['free']; result[code] = account; } return this.parseBalance (result); } async fetchOrderBook (symbol, limit = undefined, params = {}) { await this.loadMarkets (); const market = this.market (symbol); const request = { 'primaryCurrencyCode': market['baseId'], 'secondaryCurrencyCode': market['quoteId'], }; const response = await this.publicGetGetOrderBook (this.extend (request, params)); const timestamp = this.parse8601 (response['CreatedTimestampUtc']); return this.parseOrderBook (response, timestamp, 'BuyOrders', 'SellOrders', 'Price', 'Volume'); } parseTicker (ticker, market = undefined) { const timestamp = this.parse8601 (ticker['CreatedTimestampUtc']); let symbol = undefined; if (market) { symbol = market['symbol']; } const last = ticker['LastPrice']; return { 'symbol': symbol, 'timestamp': timestamp, 'datetime': this.iso8601 (timestamp), 'high': ticker['DayHighestPrice'], 'low': ticker['DayLowestPrice'], 'bid': ticker['CurrentHighestBidPrice'], 'bidVolume': undefined, 'ask': ticker['CurrentLowestOfferPrice'], 'askVolume': undefined, 'vwap': undefined, 'open': undefined, 'close': last, 'last': last, 'previousClose': undefined, 'change': undefined, 'percentage': undefined, 'average': ticker['DayAvgPrice'], 'baseVolume': ticker['DayVolumeXbtInSecondaryCurrrency'], 'quoteVolume': undefined, 'info': ticker, }; } async fetchTicker (symbol, params = {}) { await this.loadMarkets (); const market = this.market (symbol); const request = { 'primaryCurrencyCode': market['baseId'], 'secondaryCurrencyCode': market['quoteId'], }; const response = await this.publicGetGetMarketSummary (this.extend (request, params)); return this.parseTicker (response, market); } parseOrder (order, market = undefined) { let symbol = undefined; if (market === undefined) { symbol = market['symbol']; } else { market = this.findMarket (order['PrimaryCurrencyCode'] + '/' + order['SecondaryCurrencyCode']); } let orderType = this.safeValue (order, 'Type'); if (orderType.indexOf ('Market') >= 0) { orderType = 'market'; } else if (orderType.indexOf ('Limit') >= 0) { orderType = 'limit'; } let side = undefined; if (orderType.indexOf ('Bid') >= 0) { side = 'buy'; } else if (orderType.indexOf ('Offer') >= 0) { side = 'sell'; } const timestamp = this.parse8601 (order['CreatedTimestampUtc']); let amount = this.safeFloat (order, 'VolumeOrdered'); if (amount === undefined) { amount = this.safeFloat (order, 'Volume'); } const filled = this.safeFloat (order, 'VolumeFilled'); let remaining = undefined; const feeRate = this.safeFloat (order, 'FeePercent'); let feeCost = undefined; if (amount !== undefined) { if (filled !== undefined) { remaining = amount - filled; if (feeRate !== undefined) { feeCost = feeRate * filled; } } } let feeCurrency = undefined; if (market !== undefined) { symbol = market['symbol']; feeCurrency = market['base']; } const fee = { 'rate': feeRate, 'cost': feeCost, 'currency': feeCurrency, }; const id = this.safeString (order, 'OrderGuid'); const status = this.parseOrderStatus (this.safeString (order, 'Status')); const cost = this.safeFloat (order, 'Value'); const average = this.safeFloat (order, 'AvgPrice'); const price = this.safeFloat (order, 'Price', average); return { 'info': order, 'id': id, 'timestamp': timestamp, 'datetime': this.iso8601 (timestamp), 'lastTradeTimestamp': undefined, 'symbol': symbol, 'type': orderType, 'side': side, 'price': price, 'cost': cost, 'average': average, 'amount': amount, 'filled': filled, 'remaining': remaining, 'status': status, 'fee': fee, }; } parseOrderStatus (status) { const statuses = { 'Open': 'open', 'PartiallyFilled': 'open', 'Filled': 'closed', 'PartiallyFilledAndCancelled': 'canceled', 'Cancelled': 'canceled', 'PartiallyFilledAndExpired': 'canceled', 'Expired': 'canceled', }; return this.safeString (statuses, status, status); } async fetchOrder (id, symbol = undefined, params = {}) { await this.loadMarkets (); const response = await this.privatePostGetOrderDetails (this.extend ({ 'orderGuid': id, }, params)); let market = undefined; if (symbol !== undefined) { market = this.market (symbol); } return this.parseOrder (response, market); } async fetchMyTrades (symbol = undefined, since = undefined, limit = 50, params = {}) { await this.loadMarkets (); const pageIndex = this.safeInteger (params, 'pageIndex', 1); if (limit === undefined) { limit = 50; } const request = this.ordered ({ 'pageIndex': pageIndex, 'pageSize': limit, }); const response = await this.privatePostGetTrades (this.extend (request, params)); let market = undefined; if (symbol !== undefined) { market = this.market (symbol); } return this.parseTrades (response['Data'], market, since, limit); } parseTrade (trade, market = undefined) { let timestamp = this.parse8601 (trade['TradeTimestampUtc']); const id = this.safeString (trade, 'TradeGuid'); const orderId = this.safeString (trade, 'OrderGuid'); const price = this.safeFloat2 (trade, 'Price', 'SecondaryCurrencyTradePrice'); const amount = this.safeFloat2 (trade, 'VolumeTraded', 'PrimaryCurrencyAmount'); let symbol = undefined; if (market !== undefined) { symbol = market['symbol']; } let side = this.safeString (trade, 'OrderType'); if (side !== undefined) { if (side.indexOf ('Bid') >= 0) { side = 'buy'; } else if (side.indexOf ('Offer') >= 0) { side = 'sell'; } } return { 'id': id, 'info': trade, 'timestamp': timestamp, 'datetime': this.iso8601 (timestamp), 'symbol': symbol, 'order': orderId, 'type': undefined, 'side': side, 'price': price, 'amount': amount, 'fee': undefined, }; } async fetchTrades (symbol, since = undefined, limit = undefined, params = {}) { await this.loadMarkets (); const market = this.market (symbol); const request = { 'primaryCurrencyCode': market['baseId'], 'secondaryCurrencyCode': market['quoteId'], 'numberOfRecentTradesToRetrieve': 50, // max = 50 }; const response = await this.publicGetGetRecentTrades (this.extend (request, params)); return this.parseTrades (response['Trades'], market, since, limit); } async createOrder (symbol, type, side, amount, price = undefined, params = {}) { await this.loadMarkets (); const market = this.market (symbol); const capitalizedOrderType = this.capitalize (type); const method = 'privatePostPlace' + capitalizedOrderType + 'Order'; let orderType = capitalizedOrderType; orderType += (side === 'sell') ? 'Offer' : 'Bid'; const request = this.ordered ({ 'primaryCurrencyCode': market['baseId'], 'secondaryCurrencyCode': market['quoteId'], 'orderType': orderType, }); if (type === 'limit') { request['price'] = price; } request['volume'] = amount; const response = await this[method] (this.extend (request, params)); return { 'info': response, 'id': response['OrderGuid'], }; } async cancelOrder (id, symbol = undefined, params = {}) { await this.loadMarkets (); const request = { 'orderGuid': id, }; return await this.privatePostCancelOrder (this.extend (request, params)); } sign (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) { let url = this.urls['api'][api] + '/' + path; if (api === 'public') { if (Object.keys (params).length) { url += '?' + this.urlencode (params); } } else { this.checkRequiredCredentials (); const nonce = this.nonce (); const auth = [ url, 'apiKey=' + this.apiKey, 'nonce=' + nonce.toString (), ]; const keys = Object.keys (params); for (let i = 0; i < keys.length; i++) { const key = keys[i]; const value = params[key].toString (); auth.push (key + '=' + value); } const message = auth.join (','); const signature = this.hmac (this.encode (message), this.encode (this.secret)); const query = this.ordered ({}); query['apiKey'] = this.apiKey; query['nonce'] = nonce; query['signature'] = signature.toUpperCase (); for (let i = 0; i < keys.length; i++) { const key = keys[i]; query[key] = params[key]; } body = this.json (query); headers = { 'Content-Type': 'application/json' }; } return { 'url': url, 'method': method, 'body': body, 'headers': headers }; } };