174 lines
5.6 KiB
JavaScript
174 lines
5.6 KiB
JavaScript
exports.newExchangeAPI = function newExchangeAPI(logger, BOT) {
|
|
|
|
let MODULE_NAME = "Exchange API";
|
|
let LOG_INFO = true;
|
|
let apiClient;
|
|
let bot = BOT
|
|
|
|
let thisObject = {
|
|
initialize: initialize,
|
|
getTicker: getTicker,
|
|
getOpenPositions: getOpenPositions,
|
|
getExecutedTrades: getExecutedTrades,
|
|
putPosition: putPosition,
|
|
movePosition: movePosition,
|
|
getMaxDecimalPositions: getMaxDecimalPositions
|
|
};
|
|
|
|
let exchangeId
|
|
let options = {}
|
|
let exchange
|
|
const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset
|
|
const ccxt = require('ccxt')
|
|
|
|
return thisObject;
|
|
|
|
function initialize(callBackFunction) {
|
|
try {
|
|
exchangeId = bot.exchange.toLowerCase()
|
|
|
|
let key = process.env.KEY
|
|
let secret = process.env.SECRET
|
|
|
|
if (key === "undefined") { key = undefined }
|
|
if (secret === "undefined") { secret = undefined }
|
|
|
|
const exchangeClass = ccxt[exchangeId]
|
|
const exchangeConstructorParams = {
|
|
'apiKey': key,
|
|
'secret': secret,
|
|
'timeout': 30000,
|
|
'enableRateLimit': true,
|
|
verbose: false,
|
|
options: options
|
|
}
|
|
|
|
exchange = new exchangeClass(exchangeConstructorParams)
|
|
|
|
callBackFunction(global.DEFAULT_OK_RESPONSE)
|
|
} catch (err) {
|
|
logError("initialize -> err = " + err.message);
|
|
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
|
|
}
|
|
}
|
|
|
|
function getMaxDecimalPositions() {
|
|
|
|
}
|
|
|
|
/*
|
|
* Returns the price for a given pair of assets.
|
|
* The object returned is an array of trades:
|
|
* ticker = {
|
|
* bid, Number
|
|
* ask, Number
|
|
* last Number
|
|
* };
|
|
*/
|
|
async function getTicker(pMarket, callBackFunction) {
|
|
try {
|
|
logInfo("getTicker -> Entering function.");
|
|
let ticker
|
|
|
|
if (exchange.has['fetchTicker']) {
|
|
ticker = await (exchange.fetchTicker(symbol))
|
|
callBackFunction(global.DEFAULT_OK_RESPONSE, ticker)
|
|
} else {
|
|
logError("getTicker -> Exchange does not support fetchTicker command.");
|
|
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
|
|
}
|
|
|
|
} catch (err) {
|
|
logError("getTicker -> err = " + err.message);
|
|
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Returns the open positions ath the exchange for a given market and user account.
|
|
* The object returned is an array of positions
|
|
*
|
|
*/
|
|
function getOpenPositions(pMarket, callBack) {
|
|
try {
|
|
logInfo("getOpenPositions -> Entering function. pMarket = " + JSON.stringify(pMarket));
|
|
apiClient.getOpenPositions(pMarket, callBack);
|
|
} catch (err) {
|
|
logError("getOpenPositions -> Error = " + err.message);
|
|
callBack(global.DEFAULT_FAIL_RESPONSE);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Returns the trades for a given order number.
|
|
* The object returned is an array of positions
|
|
*/
|
|
function getExecutedTrades(pPositionId, callBack) {
|
|
try {
|
|
logInfo("getExecutedTrades -> Entering function. pPositionId = " + pPositionId);
|
|
apiClient.getExecutedTrades(pPositionId, callBack);
|
|
} catch (err) {
|
|
logError("getExecutedTrades -> Error = " + err.message);
|
|
callBack(global.DEFAULT_FAIL_RESPONSE);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Creates a new buy or sell order.
|
|
* The orderNumber is returned. String
|
|
*/
|
|
function putPosition(pMarket, pType, pRate, pAmountA, pAmountB, callBack) {
|
|
try {
|
|
logInfo("putPosition -> Entering function.");
|
|
|
|
let rate = truncDecimals(pRate);
|
|
let amountA = truncDecimals(pAmountA);
|
|
let amountB = truncDecimals(pAmountB);
|
|
|
|
logInfo("putPosition -> pMarket = " + pMarket.assetA + "_" + pMarket.assetB);
|
|
logInfo("putPosition -> pType = " + pType);
|
|
logInfo("putPosition -> pRate = " + rate);
|
|
logInfo("putPosition -> pAmountA = " + amountA);
|
|
logInfo("putPosition -> pAmountB = " + amountB);
|
|
|
|
if (pType === "buy") {
|
|
apiClient.buy(pMarket.assetA, pMarket.assetB, rate, amountB, callBack);
|
|
} else if (pType === "sell") {
|
|
apiClient.sell(pMarket.assetA, pMarket.assetB, rate, amountB, callBack);
|
|
} else {
|
|
logError("putPosition -> pType must be either 'buy' or 'sell'.");
|
|
callBack(global.DEFAULT_FAIL_RESPONSE);
|
|
}
|
|
} catch (err) {
|
|
logError("putPosition -> err = " + JSON.stringify(err.stack) || err.message);
|
|
callBack(global.DEFAULT_FAIL_RESPONSE);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Move an existing position to the new rate.
|
|
* The new orderNumber is returned.
|
|
*/
|
|
function movePosition(pPosition, pNewRate, pNewAmountB, callBack) {
|
|
try {
|
|
logInfo("movePosition -> Entering function.");
|
|
logInfo("movePosition -> pPosition = " + JSON.stringify(pPosition));
|
|
logInfo("movePosition -> pNewRate = " + truncDecimals(pNewRate));
|
|
|
|
apiClient.movePosition(pPosition, truncDecimals(pNewRate), truncDecimals(pNewAmountB), callBack);
|
|
} catch (err) {
|
|
logError("movePosition -> err = " + err.message);
|
|
callBack(global.DEFAULT_FAIL_RESPONSE);
|
|
}
|
|
}
|
|
|
|
|
|
function logInfo(message) {
|
|
if (LOG_INFO === true) { logger.write(MODULE_NAME, '[INFO] ' + message) }
|
|
}
|
|
|
|
function logError(message) {
|
|
logger.write(MODULE_NAME, '[ERROR] ' + message)
|
|
}
|
|
};
|