From 6198dcfe5c02dbc8ac233c06dbbe1d86231cd2e7 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 6 Mar 2018 08:57:01 +0100 Subject: [PATCH] Context Implemented --- .vs/AACloudPlatform/v15/.suo | Bin 60416 -> 68096 bytes ...tring => AAMariam.azure.storage.connstring | 0 Context.js | 106 ++++++++++++------ Interval Executor.js | 6 +- 4 files changed, 77 insertions(+), 35 deletions(-) rename Mariam.azure.storage.connstring => AAMariam.azure.storage.connstring (100%) diff --git a/.vs/AACloudPlatform/v15/.suo b/.vs/AACloudPlatform/v15/.suo index 438e2df8dc776cb67261b96bf5ebde2ee6a3dda5..4703b39c6ac18d948ceec953c4e424ad6beeab44 100644 GIT binary patch delta 1028 zcmcIjUr19?7(d_H8FOpMOlxzQOj5KU8Ym;_-z*BB#Gq8@j|&MaAJXhr_>gxkQ$lg; zb2*~0-OWHzNRN~cCHKLH5VAiSMfhNEK~cR7lBnCc!-*M2J@g%Z-0%MQzTfvd_nsra zF~~hE=+2{LF>HibL~0QDp=p}V>bZa9;09`x*M7aW5u0cL;yghO0CRl7&`gc4th(*F zD|k~eW1XTC-BzCf<2}|0tYFf86k?dLwSt0DMk{pjM27YowJru(^+HMab9(Wq=Qw09 zO1rQ~@Hu;g!5J>)XePLbQhorOxRC!18hD~0mi;#Zdm;{9phh$50>_sL)V5?%2Vm3r z9+0tYs2aU*x7ZGg$skbfCdq{rQX%$9xqME-^nWRDi&Jz{hF3dwa=F6`qu1qSMCV7E zd6G1ev!t0EC1qmmCFh8Q?<#lLk{NM|f0ikXmS9cIg(wcE?R(=tQ#K{@(@Irw=v z13!$eNn0u{3p==P#s#LDXZoop*gji|>cb-37IN4c$EU+>YWvg6EjL~VJ~?(RdF^`D zy#5zo*##*@;#55%FA_-lLceDp20bNzgqvEBmgrtpK>r~-+FuU*2hZu|E5g=yadQGV OV%@}T6|AbVg)pj1U6WK2pD&;Dd9X>WW9aVnCpjg z-DMluh_qrU>yp9Hhcq=U>i=dLeTyvwamaFhlr!6W=Wml z7J5s{JWlcg57=Z$hT?jrgl#u&0i`s7BQRC^6Q5><)yDFL0K_t)15jhT9DpP90FJhX z$a4fu%_)$ZH$b~`08FJAhLi|JmpGBij+ohU*8kt0Ybs$@QpM)=8RrXuPeusj0ktT? zm%fyL!;Kz5JiiKTV?RjRR=%CN$Um|PsJXK@&}f-IhQjd<<=qY6Yl%CI>+S*j16yR z2eS_`SOYSXM^)-sf)@WNNu3q^A>OkwBf+=6Abik@JTLDV6F^$-id+6 d!O^a(R{N{Vt+(DMzKT2H^V1lx)OUTn_&42ZCg=bF diff --git a/Mariam.azure.storage.connstring b/AAMariam.azure.storage.connstring similarity index 100% rename from Mariam.azure.storage.connstring rename to AAMariam.azure.storage.connstring diff --git a/Context.js b/Context.js index 64805b94f..10833d0e8 100644 --- a/Context.js +++ b/Context.js @@ -13,24 +13,62 @@ const MODULE_NAME = "Context"; + /* + + Here we will keep the last status report, to be available during the whole process. The Status Report is a file the bot process + reads and saves again after each execution. Its main purpose is to know when the last execution was in order to locate the execution + context. When the bot runs for the first time it takes some vital parameters from there and it checks them through its lifecycle to see + if they changed. The Status Report file can eventually be manipulated by the bot operators / developers in order to change those parameters + or to point the last execution to a different date. Humans are not supose to manipulate the Execution Histroy or the execution Context files. + + The Execution History is basically an index with dates of all the executions the bot did across its history. It allows the bot plotter + to know which datetimes have informacion about the bots execution in order to display it. + + The Execution Context file records all the context information of the bot at the moment of execution and the final state of all of its + positions on the market. + + */ + thisObject = { + statusReport: undefined, // Here is the information that defines which was the last sucessfull execution and some other details. + executionHistory: undefined, // This is the record of bot execution. + executionContext: undefined, // Here is the business information of the last execution of this bot process. + newHistoryRecord : { + date: processDatetime, + rate: 0, // This will be used to know where to plot this information in the time line. + newPositions: 0, + newTrades: 0, + movedPositions: 0 + }, initialize: initialize, saveAll: saveAll }; + /* + + During the process we will create a new History Record. This will go to the Context History file which essentially mantains an + index of all the bots executions. This file will later be plotted by the bot s plotter on the timeline, allowing end users to + know where there is information related to the actions taken by the bot. + + */ + + const EXCHANGE_NAME = "Poloniex"; let bot = BOT; const DEBUG_MODULE = require('./Debug Log'); const logger = DEBUG_MODULE.newDebugLog(); logger.fileName = MODULE_NAME; - let statusReport; // This is the typical Status Report that defines which was the last sucessfull execution and a few other pieces of info. - let executionHistory; // This is - let executionContext; // Here is the information of the last execution of this bot process. + /* Storage account to be used here. */ + + const FILE_STORAGE = require('./Azure File Storage'); + let cloudStorage = FILE_STORAGE.newAzureFileStorage(bot); + + let processDatetime; return thisObject; - function initialize(callBackFunction) { + function initialize(pProcessDatetime, callBackFunction) { try { /* @@ -40,6 +78,11 @@ */ + cloudStorage.initialize(bot.name); + + thisObject.newHistoryRecord.date = pProcessDatetime; + processDatetime = pProcessDatetime; + getStatusReport(); function getStatusReport() { @@ -49,15 +92,15 @@ let fileName = "Status.Report.json" let filePath = EXCHANGE_NAME + "/" + bot.name + "/" + bot.dataSetVersion + "/Processes/" + bot.process; - mariamAzureFileStorage.getTextFile(filePath, fileName, onFileReceived, true); + cloudStorage.getTextFile(filePath, fileName, onFileReceived, true); function onFileReceived(text) { try { - statusReport = JSON.parse(text); + thisObject.statusReport = JSON.parse(text); - if (statusReport.lastExecution === undefined) { + if (thisObject.statusReport.lastExecution === undefined) { createConext(); @@ -77,8 +120,7 @@ */ - const logText = "[ERROR] 'getStatusReport' - Bot cannot execute without a status report. ERROR : " + err.message; - logger.write(logText); + logger.write("[ERROR] initialize -> getStatusReport -> Bot cannot execute without the Status report. -> Err = " + err.message); } } @@ -89,13 +131,13 @@ let fileName = "Execution.History.json" let filePath = EXCHANGE_NAME + "/" + bot.name + "/" + bot.dataSetVersion + "/Output/" + bot.process; - mariamAzureFileStorage.getTextFile(filePath, fileName, onFileReceived, true); + cloudStorage.getTextFile(filePath, fileName, onFileReceived, true); function onFileReceived(text) { try { - executionHistory = JSON.parse(text); + thisObject.executionHistory = JSON.parse(text); getExecutionContext(); } catch (err) { @@ -108,8 +150,7 @@ */ - const logText = "[ERROR] 'getExecutionHistory' - Bot cannot execute without the Execution History. ERROR : " + err.message; - logger.write(logText); + logger.write("[ERROR] initialize -> getExecutionHistory -> Bot cannot execute without the Execution History. -> Err = " + err.message); } } @@ -117,21 +158,21 @@ function getExecutionContext() { - let date = new Date(statusReport.lastExecution); + let date = new Date(thisObject.statusReport.lastExecution); let fileName = "Execution.Context.json" let dateForPath = date.getUTCFullYear() + '/' + utilities.pad(date.getUTCMonth() + 1, 2) + '/' + utilities.pad(date.getUTCDate(), 2) + '/' + utilities.pad(date.getUTCHours(), 2) + '/' + utilities.pad(date.getUTCMinutes(), 2); let filePath = EXCHANGE_NAME + "/" + bot.name + "/" + bot.dataSetVersion + "/Output/" + bot.process + "/" + dateForPath; - mariamAzureFileStorage.getTextFile(filePath, fileName, onFileReceived, true); + cloudStorage.getTextFile(filePath, fileName, onFileReceived, true); function onFileReceived(text) { try { - executionContext = JSON.parse(text); + thisObject.executionContext = JSON.parse(text); - executionContext.transactions = []; // We record here the transactions that happened duting this execution. + thisObject.executionContext.transactions = []; // We record here the transactions that happened duting this execution. ordersExecutionCheck(); @@ -145,8 +186,7 @@ */ - const logText = "[ERROR] 'getExecutionContext' - Bot cannot execute without the Execution Context. ERROR : " + err.message; - logger.write(logText); + logger.write("[ERROR] initialize -> getExecutionContext -> Bot cannot execute without the Execution Context. -> Err = " + err.message); } } @@ -161,9 +201,9 @@ */ - executionHistory = []; + thisObject.executionHistory = []; - executionContext = { + thisObject.executionContext = { investment: { assetA: 0, assetB: 0 @@ -176,7 +216,7 @@ transactions: [] }; - getCandles(); + callBackFunction(); } @@ -205,15 +245,15 @@ let dateForPath = processDatetime.getUTCFullYear() + '/' + utilities.pad(processDatetime.getUTCMonth() + 1, 2) + '/' + utilities.pad(processDatetime.getUTCDate(), 2) + '/' + utilities.pad(processDatetime.getUTCHours(), 2) + '/' + utilities.pad(processDatetime.getUTCMinutes(), 2); let filePath = EXCHANGE_NAME + "/" + bot.name + "/" + bot.dataSetVersion + "/Output/" + bot.process + "/" + dateForPath; - utilities.createFolderIfNeeded(filePath, mariamAzureFileStorage, onFolderCreated); + utilities.createFolderIfNeeded(filePath, cloudStorage, onFolderCreated); function onFolderCreated() { try { - let fileContent = JSON.stringify(executionContext); + let fileContent = JSON.stringify(thisObject.executionContext); - mariamAzureFileStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated); + cloudStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated); function onFileCreated() { @@ -248,7 +288,7 @@ let fileName = "Execution.History.json" let filePath = EXCHANGE_NAME + "/" + bot.name + "/" + bot.dataSetVersion + "/Output/" + bot.process; - utilities.createFolderIfNeeded(filePath, mariamAzureFileStorage, onFolderCreated); + utilities.createFolderIfNeeded(filePath, cloudStorage, onFolderCreated); function onFolderCreated() { @@ -262,11 +302,11 @@ newHistoryRecord.movedPositions ]; - executionHistory.push(newRecord); + thisObject.executionHistory.push(newRecord); - let fileContent = JSON.stringify(executionHistory); + let fileContent = JSON.stringify(thisObject.executionHistory); - mariamAzureFileStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated); + cloudStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated); function onFileCreated() { @@ -301,17 +341,17 @@ let fileName = "Status.Report.json" let filePath = EXCHANGE_NAME + "/" + bot.name + "/" + bot.dataSetVersion + "/Processes/" + bot.process; - utilities.createFolderIfNeeded(filePath, mariamAzureFileStorage, onFolderCreated); + utilities.createFolderIfNeeded(filePath, cloudStorage, onFolderCreated); function onFolderCreated() { try { - statusReport.lastExecution = processDatetime; + thisObject.statusReport.lastExecution = processDatetime; - let fileContent = JSON.stringify(statusReport); + let fileContent = JSON.stringify(thisObject.statusReport); - mariamAzureFileStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated); + cloudStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated); function onFileCreated() { diff --git a/Interval Executor.js b/Interval Executor.js index b0eb151e7..9225ae402 100644 --- a/Interval Executor.js +++ b/Interval Executor.js @@ -203,5 +203,7 @@ } }; - - +/* +let processDatetime = new Date(); // This will be considered the process date and time, so as to have it consistenly all over the execution. +processDatetime = new Date(processDatetime.valueOf() - 30 * 24 * 60 * 60 * 1000); // we go 30 days back in time since candles are currently not up to date. +*/