createOrder Implemented

This commit is contained in:
Luis Fernando Molina committed 2020-02-06 18:14:50 +01:00
1 parent 741d1e14c8
commit 3774ef12c6
2 files changed
+24 -178

No files matched your search

+3 -122
View File
@@ -12,7 +12,6 @@
dataDependencies: undefined,
initialize: initialize,
createOrder: createOrder,
movePosition: movePosition,
getPositions: getPositions,
getBalance: getBalance,
getAvailableBalance: getAvailableBalance,
@@ -890,11 +889,11 @@
}
}
function onResponse(err, pOrderId) {
function onResponse(err, order) {
try {
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] createOrder -> onResponse -> Entering function."); }
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] createOrder -> onResponse -> pOrderId = " + pOrderId); }
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] createOrder -> onResponse -> order.id = " + order.id); }
switch (err.result) {
case global.DEFAULT_OK_RESPONSE.result: { // Everything went well, we have the information requested.
@@ -902,7 +901,7 @@
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] createOrder -> onResponse -> Execution finished well."); }
let position = {
id: pOrderId,
id: order.id,
type: pType,
rate: pRate,
amountA: pAmountA,
@@ -967,124 +966,6 @@
}
}
function movePosition(pPosition, pNewRate, callBackFunction) {
try {
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] movePosition -> Entering function."); }
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] movePosition -> pPosition = " + JSON.stringify(pPosition)); }
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] movePosition -> pNewRate = " + pNewRate); }
/* Removing extra decimals. */
pNewRate = thisObject.truncDecimals(pNewRate);
let newAmountB;
if (pPosition.type === "buy") {
newAmountB = thisObject.truncDecimals(pPosition.amountA / pNewRate);
} else {
newAmountB = pPosition.amountB;
}
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] movePosition -> newAmountB = " + newAmountB); }
switch (bot.startMode) {
case "Live": {
exchangeAPI.movePosition(pPosition, pNewRate, newAmountB, onResponse);
return;
}
case "Backtest": {
let positionId = Math.trunc(Math.random(1) * 1000000);
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] createOrder -> Simulating Exchange Response -> orderId = " + positionId); }
onResponse(global.DEFAULT_OK_RESPONSE, positionId);
return;
}
default: {
logger.write(MODULE_NAME, "[ERROR] movePosition -> Unexpected bot.startMode.");
logger.write(MODULE_NAME, "[ERROR] movePosition -> bot.startMode = " + bot.startMode);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
return;
}
}
function onResponse(err, pOrderId) {
try {
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] movePosition -> onResponse -> Entering function."); }
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] movePosition -> onResponse -> pOrderId = " + pOrderId); }
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] movePosition -> onResponse -> Execution finished well."); }
let newPosition = {
id: pOrderId,
type: pPosition.type,
rate: pNewRate,
amountA: pPosition.amountA,
amountB: newAmountB,
date: (bot.processDatetime.valueOf()),
status: "open",
trades: []
};
let oldPosition = JSON.parse(JSON.stringify(pPosition));
/* We need to update the position we have on file. */
for (let i = 0; i < context.executionContext.positions.length; i++) {
if (context.executionContext.positions[i].id === pPosition.id) {
context.executionContext.positions[i] = newPosition;
break;
}
}
let newTransaction = {
type: "movePosition",
oldPosition: oldPosition,
newPosition: newPosition
};
context.executionContext.transactions.push(newTransaction);
context.newHistoryRecord.movedPositions++;
recalculateRateAverages();
callBackFunction(global.DEFAULT_OK_RESPONSE, newPosition);
}
break;
case global.DEFAULT_RETRY_RESPONSE.result: { // Something bad happened, but if we retry in a while it might go through the next time.
logger.write(MODULE_NAME, "[ERROR] movePosition -> onResponse -> Retry Later. Requesting Execution Retry.");
callBackFunction(global.DEFAULT_RETRY_RESPONSE);
return;
}
break;
case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle.
logger.write(MODULE_NAME, "[ERROR] movePosition -> onResponse -> Operation Failed. Aborting the process.");
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
return;
}
break;
}
} catch (err) {
logger.write(MODULE_NAME, "[ERROR] movePosition -> onResponse -> err = " + err.stack);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
} catch (err) {
logger.write(MODULE_NAME, "[ERROR] movePosition -> err = " + err.stack);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
function getPublicTradeHistory(baseAsset, quotedAsset, startTime, endTime, callback) {
if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] getTradeHistory -> Entering function."); }
+21 -56
View File
@@ -11,7 +11,6 @@
getOpenOrders: getOpenOrders,
getOrder: getOrder,
createOrder: createOrder,
movePosition: movePosition,
getMaxDecimalPositions: getMaxDecimalPositions
};
@@ -53,18 +52,9 @@
}
function getMaxDecimalPositions() {
return 8
}
/*
* 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(market, callBackFunction) {
try {
logInfo("getTicker -> Entering function.");
@@ -85,11 +75,6 @@
}
}
/*
* Returns the open positions ath the exchange for a given market and user account.
* The object returned is an array of positions
*
*/
async function getOpenOrders(market, callBackFunction) {
try {
logInfo("getOpenOrders -> Entering function. market = " + JSON.stringify(market));
@@ -111,10 +96,6 @@
}
}
/*
* Returns the trades for a given order number.
* The object returned is an array of trades
*/
function getOrder(orderId, market, callBackFunction) {
try {
logInfo("getOrder -> Entering function. orderId = " + orderId);
@@ -135,56 +116,40 @@
}
}
/*
* Creates a new buy or sell order.
* The orderNumber is returned. String
*/
function createOrder(pMarket, pType, pRate, pAmountA, pAmountB, callBackFunction) {
function createOrder(market, side, pRate, amount, price, callBackFunction) {
try {
logInfo("createOrder -> Entering function.");
let rate = truncDecimals(pRate);
let amountA = truncDecimals(pAmountA);
let amountB = truncDecimals(pAmountB);
const symbol = bot.market.baseAsset + '/' + bot.market.quotedAsset
logInfo("createOrder -> pMarket = " + pMarket.assetA + "_" + pMarket.assetB);
logInfo("createOrder -> pType = " + pType);
logInfo("createOrder -> market = " + market.assetA + "_" + market.assetB);
logInfo("createOrder -> side = " + side);
logInfo("createOrder -> pRate = " + rate);
logInfo("createOrder -> pAmountA = " + amountA);
logInfo("createOrder -> pAmountB = " + amountB);
logInfo("createOrder -> amount = " + amount);
logInfo("createOrder -> price = " + price);
if (pType === "buy") {
apiClient.buy(pMarket.assetA, pMarket.assetB, rate, amountB, callBackFunction);
} else if (pType === "sell") {
apiClient.sell(pMarket.assetA, pMarket.assetB, rate, amountB, callBackFunction);
} else {
logError("createOrder -> pType must be either 'buy' or 'sell'.");
if (side !== "buy" && side !== "sell") {
logError("createOrder -> side must be either 'buy' or 'sell'.");
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
let type = 'market'
let order
if (exchange.has['createOrder']) {
order = await(exchange.createOrder(symbol, type, side, amount, price))
callBackFunction(global.DEFAULT_OK_RESPONSE, order)
} else {
logError("createOrder -> Exchange does not support createOrder command.");
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
} catch (err) {
logError("createOrder -> err = " + JSON.stringify(err.stack) || err.message);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
/*
* Move an existing position to the new rate.
* The new orderNumber is returned.
*/
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), callBackFunction);
} catch (err) {
logError("movePosition -> err = " + err.message);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
function logInfo(message) {
if (LOG_INFO === true) { logger.write(MODULE_NAME, '[INFO] ' + message) }
}