diff --git a/.vs/AACloud/v15/.suo b/.vs/AACloud/v15/.suo index f6c65bdd5..e77727f3a 100644 Binary files a/.vs/AACloud/v15/.suo and b/.vs/AACloud/v15/.suo differ diff --git a/Assistant.js b/Assistant.js index e5df97d14..14fe9acfb 100644 --- a/Assistant.js +++ b/Assistant.js @@ -256,7 +256,7 @@ let sumAssetA = 0; let sumAssetB = 0; - for (k = 0; k < pTrades.length; k++) { + for (let k = 0; k < pTrades.length; k++) { sumAssetA = sumAssetA + pTrades[k].amountA; sumAssetB = sumAssetB + pTrades[k].amountB; @@ -312,6 +312,73 @@ } } } + + function confirmOrderWasExecuted(pTrades) { + + try { + + if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> loopBody -> confirmOrderWasExecuted -> Entering function."); } + if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> loopBody -> confirmOrderWasExecuted -> pTrades = " + JSON.stringify(pTrades)); } + + /* + + To confirm everything is ok, we will add all the amounts on trades asociated to the order and + they must be equal to the one on file. Otherwise something very strange could have happened, + in which case we will halt the bot execution. + + */ + + let sumAssetA = 0; + let sumAssetB = 0; + + for (let k = 0; k < pTrades.length; k++) { + + sumAssetA = sumAssetA + pTrades[k].amountA; + sumAssetB = sumAssetB + pTrades[k].amountB; + + } + + /* We add the fees */ + + sumAssetA = sumAssetA + exchangePositions[j].fee; + + if ( + position.amountA !== sumAssetA || + position.amountB !== sumAssetB + ) { + logger.write("[ERROR] ordersExecutionCheck -> loopBody -> confirmOrderWasExecuted -> Cannot be confirmed that the order was executed. It must be manually cancelled by the user or cancelled by the exchange itself."); + logger.write("[HINT] ordersExecutionCheck -> loopBody -> confirmOrderWasExecuted -> Verify also that you were not running under a Exchange Simulation and you turned it off without deleting the Status Report file."); + callBack(global.DEFAULT_FAIL_RESPONSE); + return; + } + + /* + + Confirmed that order was executed. Next thing to do is to remember the trades and change its status. + + */ + + position.status = "executed"; + + applyTradesToContext(pTrades); + + let newTransaction = { + type: position.type + " executed", + position: position + }; + + context.executionContext.transactions.push(newTransaction); + + /* All done. */ + + next(); + + } catch (err) { + logger.write("[ERROR] ordersExecutionCheck -> loopBody -> confirmOrderWasExecuted -> err = " + err.message); + callBack(global.DEFAULT_FAIL_RESPONSE); + return; + } + } } /* Position not found: we need to know if the order was executed. */ @@ -366,71 +433,7 @@ } } - function confirmOrderWasExecuted(pTrades) { - try { - - if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> loopBody -> confirmOrderWasExecuted -> Entering function."); } - if (FULL_LOG === true) { logger.write("[INFO] ordersExecutionCheck -> loopBody -> confirmOrderWasExecuted -> pTrades = " + JSON.stringify(pTrades)); } - - /* - - To confirm everything is ok, we will add all the amounts on trades asociated to the order and - they must be equal to the one on file. Otherwise something very strange could have happened, - in which case we will halt the bot execution. - - */ - - let sumAssetA = 0; - let sumAssetB = 0; - - for (k = 0; k < pTrades.length; k++) { - - sumAssetA = sumAssetA + pTrades[k].amountA; - sumAssetB = sumAssetB + pTrades[k].amountB; - - } - - /* We add the fees */ - - sumAssetA = sumAssetA + exchangePositions[j].fee; - - if ( - position.amountA !== sumAssetA || - position.amountB !== sumAssetB - ) { - logger.write("[ERROR] ordersExecutionCheck -> loopBody -> confirmOrderWasExecuted -> Cannot be confirmed that the order was executed. It must be manually cancelled by the user or cancelled by the exchange itself."); - callBack(global.DEFAULT_FAIL_RESPONSE); - return; - } - - /* - - Confirmed that order was executed. Next thing to do is to remember the trades and change its status. - - */ - - position.status = "executed"; - - applyTradesToContext(pTrades); - - let newTransaction = { - type: position.type + " executed", - position: position - }; - - context.executionContext.transactions.push(newTransaction); - - /* All done. */ - - next(); - - } catch (err) { - logger.write("[ERROR] ordersExecutionCheck -> loopBody -> confirmOrderWasExecuted -> err = " + err.message); - callBack(global.DEFAULT_FAIL_RESPONSE); - return; - } - } } catch (err) { logger.write("[ERROR] ordersExecutionCheck -> loopBody -> err = " + err.message); diff --git a/Context.js b/Context.js index 65579ab92..741c0eaa9 100644 --- a/Context.js +++ b/Context.js @@ -76,14 +76,17 @@ let cloudStorage = FILE_STORAGE.newFileStorage(bot); + let dependencies; + return thisObject; - function initialize(callBackFunction) { + function initialize(pDependencies, callBackFunction) { try { if (FULL_LOG === true) { logger.write("[INFO] initialize -> Entering function."); } + dependencies = pDependencies; /* Here we get the positions the bot did and that are recorded at the bot storage account. We will use them through out the rest @@ -148,46 +151,18 @@ if (FULL_LOG === true) { logger.write("[INFO] initialize -> getStatusReport -> Entering function."); } - statusReportModule = STATUS_REPORT.newStatusReport(BOT, DEBUG_MODULE, FILE_STORAGE, UTILITIES); - statusReportModule.initialize(bot, onInitilized); + let key = bot.devTeam + "-" + bot.codeName + "-" + bot.process + "-" + bot.dataSetVersion; - function onInitilized(err) { + statusReportModule = dependencies.statusReports.get(key); + thisObject.statusReport = statusReportModule.file; - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - logger.write("[ERROR] initialize -> getStatusReport -> onInitilized -> err = " + err.message); - callBackFunction(err); - return; - } + if (thisObject.statusReport.lastExecution !== undefined) { - statusReportModule.load(onLoad); - } + getExecutionHistory(callBack); // Happens when the Status Report exists. - function onLoad(err) { + } else { - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - logger.write("[INFO] initialize -> getStatusReport -> onLoad -> Execution finished well. :-)"); - thisObject.statusReport = statusReportModule.file; - getExecutionHistory(callBack); - return; - } - case global.CUSTOM_OK_RESPONSE.result: { // We need to see if we can handle this. - logger.write("[ERROR] initialize -> getStatusReport -> onLoad -> err.message = " + err.message); - - if (err.message === "Status Report was never created.") { - createConext(callBack); - } else { - callBackFunction(err); // we cant handle this here. - } - return; - } - default: - { - logger.write("[ERROR] initialize -> getStatusReport -> onLoad -> Operation Failed."); - callBackFunction(err); - return; - } - } + createConext(callBack); // Happens when the Status Report does NOT exists. } } catch (err) { diff --git a/Debug Log.js b/Debug Log.js index 248b42043..396a36642 100644 --- a/Debug Log.js +++ b/Debug Log.js @@ -84,7 +84,7 @@ function write(Message) { - if (firstCall === true) { createFolders();} + if (firstCall === true) { createFolders(); } if (global.loopCointer !== loopCounter) { diff --git a/Dependencies.js b/Dependencies.js index acd80df80..086003b69 100644 --- a/Dependencies.js +++ b/Dependencies.js @@ -94,7 +94,6 @@ } return; } - } default: { diff --git a/Project.njsproj b/Project.njsproj index 2b2f32442..0b82ae228 100644 --- a/Project.njsproj +++ b/Project.njsproj @@ -13,7 +13,7 @@ 14.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) Project - Poloniex-Live-Trades + Trading-Process C:\Program Files\nodejs\node.exe False diff --git a/Trading Bot Main Loop.js b/Trading Bot Main Loop.js index f737bc032..2781dda7a 100644 --- a/Trading Bot Main Loop.js +++ b/Trading Bot Main Loop.js @@ -70,6 +70,7 @@ const DATASOURCE = require(ROOT_DIR + 'Datasource'); const ASSISTANT = require(ROOT_DIR + 'Assistant'); const STATUS_REPORT = require(ROOT_DIR + 'Status Report'); + const DEPENDENCIES = require(ROOT_DIR + 'Dependencies'); /* We define the datetime for the process that we are running now. This will be the official processing time for both the infraestructure and the bot. */ @@ -86,14 +87,45 @@ let nextWaitTime; - initializeContext(); + initializeDependencies(); + function initializeDependencies() { + + if (FULL_LOG === true) { logger.write("[INFO] run -> loop -> initializeDependencies -> Entering function."); } + + dependencies = DEPENDENCIES.newDependencies(bot, DEBUG_MODULE, STATUS_REPORT, FILE_STORAGE, UTILITIES); + + dependencies.initialize(processConfig.dependencies, undefined, undefined, onInizialized); + + function onInizialized(err) { + + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + logger.write("[INFO] run -> loop -> initializeDependencies -> onInizialized > Execution finished well. :-)"); + initializeContext(); + return; + } + 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("[ERROR] run -> loop -> initializeDependencies -> onInizialized > Retry Later. Requesting Execution Retry."); + nextWaitTime = 'Retry'; + loopControl(nextWaitTime); + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. + logger.write("[ERROR] run -> loop -> initializeDependencies -> onInizialized > Operation Failed. Aborting the process."); + callBackFunction(err); + return; + } + } + } + } + function initializeContext() { if (FULL_LOG === true) { logger.write("[INFO] run -> loop -> initializeContext -> Entering function."); } context = CONTEXT.newContext(bot, DEBUG_MODULE, FILE_STORAGE, UTILITIES, STATUS_REPORT); - context.initialize(onInizialized); + context.initialize(dependencies, onInizialized); function onInizialized(err) { @@ -103,20 +135,17 @@ initializeDatasource(); return; } - 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("[ERROR] run -> loop -> initializeContext -> onInizialized > Retry Later. Requesting Execution Retry."); nextWaitTime = 'Retry'; loopControl(nextWaitTime); return; } - break; case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. logger.write("[ERROR] run -> loop -> initializeContext -> onInizialized > Operation Failed. Aborting the process."); callBackFunction(err); return; } - break; } } } @@ -136,20 +165,17 @@ initializeExchangeAPI(); return; } - 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("[ERROR] run -> loop -> initializeDatasource -> onInizialized > Retry Later. Requesting Execution Retry."); nextWaitTime = 'Retry'; loopControl(nextWaitTime); return; } - break; case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. logger.write("[ERROR] run -> loop -> initializeDatasource -> onInizialized > Operation Failed. Aborting the process."); callBackFunction(err); return; } - break; } } } @@ -169,20 +195,17 @@ initializeAssistant(); return; } - 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("[ERROR] run -> loop -> initializeExchangeAPI -> onInizialized > Retry Later. Requesting Execution Retry."); nextWaitTime = 'Retry'; loopControl(nextWaitTime); return; } - break; case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. logger.write("[ERROR] run -> loop -> initializeExchangeAPI -> onInizialized > Operation Failed. Aborting the process."); callBackFunction(err); return; } - break; } } } @@ -202,20 +225,17 @@ initializeUserBot(); return; } - 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("[ERROR] run -> loop -> initializeAssistant -> onInizialized > Retry Later. Requesting Execution Retry."); nextWaitTime = 'Retry'; loopControl(nextWaitTime); return; } - break; case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. logger.write("[ERROR] run -> loop -> initializeAssistant -> onInizialized > Operation Failed. Aborting the process."); callBackFunction(err); return; } - break; } } } @@ -241,20 +261,17 @@ startUserBot(); return; } - 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("[ERROR] run -> loop -> initializeUserBot -> onInizialized > Retry Later. Requesting Execution Retry."); nextWaitTime = 'Retry'; loopControl(nextWaitTime); return; } - break; case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. logger.write("[ERROR] run -> loop -> initializeUserBot -> onInizialized > Operation Failed. Aborting the process."); callBackFunction(err); return; } - break; } } } @@ -273,20 +290,17 @@ saveContext(); return; } - 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("[ERROR] run -> loop -> startUserBot -> onFinished > Retry Later. Requesting Execution Retry."); nextWaitTime = 'Retry'; loopControl(nextWaitTime); return; } - break; case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. logger.write("[ERROR] run -> loop -> startUserBot -> onFinished > Operation Failed. Aborting the process."); callBackFunction(err); return; } - break; } } } @@ -306,19 +320,16 @@ loopControl(nextWaitTime); return; } - 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("[ERROR] run -> loop -> saveContext -> onFinished > Can not retry at this point."); callBackFunction(global.DEFAULT_FAIL_RESPONSE); return; } - break; case global.DEFAULT_FAIL_RESPONSE.result: { // This is an unexpected exception that we do not know how to handle. logger.write("[ERROR] run -> loop -> saveContext -> onFinished > Operation Failed. Aborting the process."); callBackFunction(err); return; } - break; } } } diff --git a/this.config.json b/this.config.json index 3fe3629e1..539bb127a 100644 --- a/this.config.json +++ b/this.config.json @@ -6,10 +6,10 @@ "patch": 0 }, "bot": { - "path": "../Bots/AAMasters/AACharly-Extraction-Bot" + "path": "../Bots/AAMasters/AAMariam-Trading-Bot" }, "stopGracefully": "false", "exchangeSimulationMode": "false", - "storageConnStringFolder": "Mixed", + "storageConnStringFolder": "Production", "maxLogLoops": 10 } \ No newline at end of file