Skip to content

Commit

Permalink
Merge branch 'egorFiNE-livecoin-fetchOrder'
Browse files Browse the repository at this point in the history
  • Loading branch information
kroitor committed Jun 5, 2018
2 parents 34fc146 + fcbf398 commit e6281c7
Showing 1 changed file with 88 additions and 35 deletions.
123 changes: 88 additions & 35 deletions js/livecoin.js
Expand Up @@ -22,6 +22,7 @@ module.exports = class livecoin extends Exchange {
'fetchCurrencies': true,
'fetchTradingFees': true,
'fetchOrders': true,
'fetchOrder': true,
'fetchOpenOrders': true,
'fetchClosedOrders': true,
'withdraw': true,
Expand Down Expand Up @@ -378,63 +379,115 @@ module.exports = class livecoin extends Exchange {
return this.parseTrades (response, market, since, limit);
}

async fetchOrder (id, symbol = undefined, params = {}) {
await this.loadMarkets ();
let request = {
'orderId': id,
};
let response = await this.privateGetExchangeOrder (this.extend (request, params));
return this.parseOrder (response);
}

parseOrderStatus (status) {
const statuses = {
'OPEN': 'open',
'PARTIALLY_FILLED': 'open',
'EXECUTED': 'closed',
'PARTIALLY_FILLED_AND_CANCELLED': 'canceled',
};
if (status in statuses)
return statuses[status];
return status;
}

parseOrder (order, market = undefined) {
let timestamp = this.safeInteger (order, 'lastModificationTime');
if (!timestamp)
timestamp = this.parse8601 (order['lastModificationTime']);
let timestamp = undefined;
let datetime = undefined;
if ('lastModificationTime' in order) {
timestamp = this.safeString (order, 'lastModificationTime');
if (typeof timestamp !== 'undefined') {
if (timestamp.indexOf ('T') >= 0) {
timestamp = this.parse8601 (timestamp);
} else {
timestamp = this.safeInteger (order, 'lastModificationTime');
}
}
}
if (timestamp) {
datetime = this.iso8601 (timestamp);
}
// TODO currently not supported by livecoin
// let trades = this.parseTrades (order['trades'], market, since, limit);
let trades = undefined;
if ('trades' in order)
// TODO currently not supported by livecoin
// trades = this.parseTrades (order['trades'], market, since, limit);
trades = undefined;
let status = undefined;
if (order['orderStatus'] === 'OPEN' || order['orderStatus'] === 'PARTIALLY_FILLED') {
status = 'open';
} else if (order['orderStatus'] === 'EXECUTED' || order['orderStatus'] === 'PARTIALLY_FILLED_AND_CANCELLED') {
status = 'closed';
} else {
status = 'canceled';
let status = this.safeString (order, 'status');
status = this.safeString (order, 'orderStatus', status);
status = this.parseOrderStatus (status);
let symbol = undefined;
if (typeof market === 'undefined') {
let marketId = this.safeString (order, 'currencyPair');
marketId = this.safeString (order, 'symbol', marketId);
if (marketId in this.markets_by_id)
market = this.markets_by_id[marketId];
}
let symbol = order['currencyPair'];
let parts = symbol.split ('/');
let quote = parts[1];
// let [ base, quote ] = symbol.split ('/');
let type = undefined;
let side = undefined;
if (order['type'].indexOf ('MARKET') >= 0) {
type = 'market';
} else {
type = 'limit';
}
if (order['type'].indexOf ('SELL') >= 0) {
side = 'sell';
} else {
side = 'buy';
if ('type' in order) {
let orderType = order['type'].toLowerCase ().split ('_');
type = orderType[0];
side = orderType[1];
}
let price = this.safeFloat (order, 'price', 0.0);
let cost = this.safeFloat (order, 'commissionByTrade', 0.0);
let remaining = this.safeFloat (order, 'remainingQuantity', 0.0);
let price = this.safeFloat (order, 'price');
// of the next two lines the latter overrides the former, if present in the order structure
let remaining = this.safeFloat (order, 'remainingQuantity');
remaining = this.safeFloat (order, 'remaining_quantity', remaining);
let amount = this.safeFloat (order, 'quantity', remaining);
let filled = amount - remaining;
let filled = undefined;
if (typeof remaining !== 'undefined') {
filled = amount - remaining;
}
let cost = undefined;
if (status === 'open') {
if (typeof filled !== 'undefined' && typeof amount !== 'undefined' && typeof price !== 'undefined') {
cost = amount * price;
}
} else if (status === 'closed' || status === 'canceled') {
if (typeof filled !== 'undefined' && typeof price !== 'undefined') {
cost = filled * price;
}
}
if (typeof cost !== 'undefined') {
return undefined;
}
const feeRate = this.safeFloat (order, 'commission_rate');
let feeCost = undefined;
if (typeof cost !== 'undefined') {
feeCost = cost * feeRate;
}
let feeCurrency = undefined;
if (typeof market !== 'undefined') {
symbol = market['symbol'];
feeCurrency = market['quote'];
}
return {
'info': order,
'id': order['id'],
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'datetime': datetime,
'lastTradeTimestamp': undefined,
'status': status,
'symbol': symbol,
'type': type,
'side': side,
'price': price,
'cost': cost,
'amount': amount,
'cost': cost,
'filled': filled,
'remaining': remaining,
'trades': trades,
'fee': {
'cost': cost,
'currency': quote,
'cost': feeCost,
'currency': feeCurrency,
'rate': feeRate,
},
};
}
Expand Down

0 comments on commit e6281c7

Please sign in to comment.