open position method working

This commit is contained in:
Luis Fernando Molina committed 2020-02-06 16:44:31 +01:00
1 parent 1dc1e8b676
commit ce963250c0
2 files changed
+42 -30

No files matched your search

+10 -10
View File
@@ -32,7 +32,7 @@
let utilities = UTILITIES.newCloudUtilities(logger);
let exchangePositions = []; // These are the open positions at the exchange at the account the bot is authorized to use.
let openOrders = []; // These are the open positions at the exchange at the account the bot is authorized to use.
let openPositions = []; // These are the open positions the bot knows it made by itself.
let context;
@@ -246,15 +246,15 @@
case "Live": {
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] getPositionsAtExchange -> Live Mode Detected."); }
exchangeAPI.getOpenPositions(bot.market, onResponse);
exchangeAPI.getOpenOrders(bot.market, onResponse);
break;
}
case "Backtest": {
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] getPositionsAtExchange -> Backtest Mode Detected."); }
let exchangePositions = []; // We simulate all positions were executed.
onResponse(global.DEFAULT_OK_RESPONSE, exchangePositions);
let openOrders = []; // We simulate all positions were executed.
onResponse(global.DEFAULT_OK_RESPONSE, openOrders);
break;
}
@@ -266,15 +266,15 @@
}
}
function onResponse(err, pExchangePositions) {
function onResponse(err, pOpenOrders) {
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] getPositionsAtExchange -> onResponse -> Entering function."); }
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] getPositionsAtExchange -> onResponse -> pExchangePositions = " + JSON.stringify(pExchangePositions)); }
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] getPositionsAtExchange -> onResponse -> pOpenOrders = " + JSON.stringify(pOpenOrders)); }
switch (err.result) {
case global.DEFAULT_OK_RESPONSE.result: { // Everything went well, we have the information requested.
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] getPositionsAtExchange -> onResponse -> Execution finished well."); }
exchangePositions = pExchangePositions;
openOrders = pOpenOrders;
ordersExecutionCheck(callBack);
return;
}
@@ -365,11 +365,11 @@
let position = context.executionContext.positions[i];
let exchangePosition;
for (let j = 0; j < exchangePositions.length; j++) {
for (let j = 0; j < openOrders.length; j++) {
if (position.id === exchangePositions[j].id) {
if (position.id === openOrders[j].id) {
exchangePosition = exchangePositions[j];
exchangePosition = openOrders[j];
positionFound();
return;
}
+32 -20
View File
@@ -8,7 +8,7 @@
let thisObject = {
initialize: initialize,
getTicker: getTicker,
getOpenPositions: getOpenPositions,
getOpenOrders: getOpenOrders,
getExecutedTrades: getExecutedTrades,
putPosition: putPosition,
movePosition: movePosition,
@@ -18,7 +18,7 @@
let exchangeId
let options = {}
let exchange
const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset
const ccxt = require('ccxt')
return thisObject;
@@ -65,9 +65,10 @@
* last Number
* };
*/
async function getTicker(pMarket, callBackFunction) {
async function getTicker(market, callBackFunction) {
try {
logInfo("getTicker -> Entering function.");
const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset
let ticker
if (exchange.has['fetchTicker']) {
@@ -89,27 +90,38 @@
* The object returned is an array of positions
*
*/
function getOpenPositions(pMarket, callBack) {
async function getOpenOrders(market, callBackFunction) {
try {
logInfo("getOpenPositions -> Entering function. pMarket = " + JSON.stringify(pMarket));
apiClient.getOpenPositions(pMarket, callBack);
logInfo("getOpenOrders -> Entering function. market = " + JSON.stringify(market));
const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset
let openOrders
if (exchange.has['fetchOpenOrders']) {
openOrders = await(exchange.fetchOpenOrders (symbol))
callBackFunction(global.DEFAULT_OK_RESPONSE, openOrders)
} else {
logError("getTicker -> Exchange does not support fetchOpenOrders command.");
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
} catch (err) {
logError("getOpenPositions -> Error = " + err.message);
callBack(global.DEFAULT_FAIL_RESPONSE);
logError("getOpenOrders -> Error = " + err.message);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
/*
* Returns the trades for a given order number.
* The object returned is an array of positions
* The object returned is an array of trades
*/
function getExecutedTrades(pPositionId, callBack) {
function getExecutedTrades(pPositionId, callBackFunction) {
try {
logInfo("getExecutedTrades -> Entering function. pPositionId = " + pPositionId);
apiClient.getExecutedTrades(pPositionId, callBack);
apiClient.getExecutedTrades(pPositionId, callBackFunction);
} catch (err) {
logError("getExecutedTrades -> Error = " + err.message);
callBack(global.DEFAULT_FAIL_RESPONSE);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
@@ -117,7 +129,7 @@
* Creates a new buy or sell order.
* The orderNumber is returned. String
*/
function putPosition(pMarket, pType, pRate, pAmountA, pAmountB, callBack) {
function putPosition(pMarket, pType, pRate, pAmountA, pAmountB, callBackFunction) {
try {
logInfo("putPosition -> Entering function.");
@@ -132,16 +144,16 @@
logInfo("putPosition -> pAmountB = " + amountB);
if (pType === "buy") {
apiClient.buy(pMarket.assetA, pMarket.assetB, rate, amountB, callBack);
apiClient.buy(pMarket.assetA, pMarket.assetB, rate, amountB, callBackFunction);
} else if (pType === "sell") {
apiClient.sell(pMarket.assetA, pMarket.assetB, rate, amountB, callBack);
apiClient.sell(pMarket.assetA, pMarket.assetB, rate, amountB, callBackFunction);
} else {
logError("putPosition -> pType must be either 'buy' or 'sell'.");
callBack(global.DEFAULT_FAIL_RESPONSE);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
} catch (err) {
logError("putPosition -> err = " + JSON.stringify(err.stack) || err.message);
callBack(global.DEFAULT_FAIL_RESPONSE);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
@@ -149,16 +161,16 @@
* Move an existing position to the new rate.
* The new orderNumber is returned.
*/
function movePosition(pPosition, pNewRate, pNewAmountB, callBack) {
function movePosition(pPosition, pNewRate, pNewAmountB, callBackFunction) {
try {
logInfo("movePosition -> Entering function.");
logInfo("movePosition -> pPosition = " + JSON.stringify(pPosition));
logInfo("movePosition -> pNewRate = " + truncDecimals(pNewRate));
apiClient.movePosition(pPosition, truncDecimals(pNewRate), truncDecimals(pNewAmountB), callBack);
apiClient.movePosition(pPosition, truncDecimals(pNewRate), truncDecimals(pNewAmountB), callBackFunction);
} catch (err) {
logError("movePosition -> err = " + err.message);
callBack(global.DEFAULT_FAIL_RESPONSE);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}