From c9e3528cec07e5d58950910e7c8c339393ca2c51 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 18 Mar 2018 08:41:06 +0100 Subject: [PATCH] Mariam Tested and Running --- .vs/AACloud/v15/.suo | Bin 81408 -> 88576 bytes Assistant.js | 133 ++++++++++++++++++++------------------- Context.js | 47 ++++---------- Debug Log.js | 2 +- Dependencies.js | 1 - Project.njsproj | 2 +- Trading Bot Main Loop.js | 57 ++++++++++------- this.config.json | 4 +- 8 files changed, 117 insertions(+), 129 deletions(-) diff --git a/.vs/AACloud/v15/.suo b/.vs/AACloud/v15/.suo index f6c65bdd5b6f0747facd1b605a51f82962b7d046..e77727f3afb00517494975d8831baad6de38a2d0 100644 GIT binary patch delta 5330 zcmeHLdvKG-5&!O!WJ|WNCH#>50OS`ov1KGM7#l$)EDJLQKd{S?ln~3tfT&~FmP~mF zES}Q_+F&h22~CL+grp=;TVFcRBn4rZPNtBCci|smGn14_OXwtanE-+A3dsoLCT)|< zWYV7J=iS?Hzq{Mr+uQHtx+8YIA=zeN%I;~gJd;BjKStJj@Jak&Z;uXtKxr;wbWn@L zV&o$cr9s(UlxdNlibxB}NY8})Sj0>OSw9NWBr0ZxB1z$hC$YZ}k%(B@QI(PNV`P>g zeuQX5JdJ2VG$Uw&7No5Rct?^M??h%z@Bz&vL*C8L4o2YgCjN=77=dYF5g_*s7lFu( z_6&UVJp>IzkW^}B0LgV*r%aL_vc7Mkr;=3C=y$cpNODHR3J!~T&hE!B0+sQ zQi_Cx<0c0L7>Yn7Qjk(4NNGIMas>H00a1V;S5V7`K53k!2qkIofiy!>g50Q%s$AI= zWGWGJ!6eIOwH;L|aLL4z>QGdVScD*-FGeguI1n`6u&##|qiV$RpvF&-uI;Ew5xbCq zJF@vnw3eYrVdx0xj+!WRM!mtlOhvwjK}oEhJqv9y?Mw{=_+JHWvDIRpCb^$2kt|Ud zh%M&>nj42`mdgUmrTt3SO#2Me^sM^3%|kTrOke z1KQUixzL}S#{|vvUg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ecuted -> 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