From 42b8225b5edaaac51f75e92e663d791eefe7f984 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 9 Feb 2021 13:17:00 +0100 Subject: [PATCH] Trading Algorithm Implementation --- Projects/ProjectsSchema.json | 9 ++ Projects/Superalgos/Schemas/AppSchema.json | 2 +- .../Low-Frequency-Learning/LearningSystem.js | 57 +++++++++- .../Tensor-Flow-JS/LearningAlgorithm.js | 105 ++++++++++++++++++ TaskServer/MultiProject.js | 14 ++- 5 files changed, 184 insertions(+), 3 deletions(-) create mode 100644 Projects/Superalgos/TS/Bot-Modules/Learning-Bot/Low-Frequency-Learning/Time-Series-Forcasting/Tensor-Flow-JS/LearningAlgorithm.js diff --git a/Projects/ProjectsSchema.json b/Projects/ProjectsSchema.json index 55cec508f..78c9944b7 100644 --- a/Projects/ProjectsSchema.json +++ b/Projects/ProjectsSchema.json @@ -845,6 +845,15 @@ "fileName": "LearningSystem.js", "folderName": "Learning-Bot/Low-Frequency-Learning" } + ], + "algorithmModules": [ + { + "name": "Time Series Forcasting Tensor Flow JS", + "propertyName": "timeSeriesForcastingTensorFlowJS", + "functionName": "newSuperalgosAlgorithmModulesTimeSeriesForcastingTensorFlowJS", + "fileName": "LearningAlgorithm.js", + "folderName": "Learning-Bot/Low-Frequency-Learning/Time-Series-Forcasting/Tensor-Flow-JS" + } ] }, "CL": {} diff --git a/Projects/Superalgos/Schemas/AppSchema.json b/Projects/Superalgos/Schemas/AppSchema.json index 9e1325a71..f1a3e0b8d 100644 --- a/Projects/Superalgos/Schemas/AppSchema.json +++ b/Projects/Superalgos/Schemas/AppSchema.json @@ -10441,7 +10441,7 @@ } ], "initialValues": { - "config": "{ \n\"codeName\": \"\"\n}" + "config": "{ \n\"codeName\": \"Time Series Forcasting Tensor Flow JS\"\n}" }, "addLeftIcons": true, "editors": { diff --git a/Projects/Superalgos/TS/Bot-Modules/Learning-Bot/Low-Frequency-Learning/LearningSystem.js b/Projects/Superalgos/TS/Bot-Modules/Learning-Bot/Low-Frequency-Learning/LearningSystem.js index 7a923628e..0545cc481 100644 --- a/Projects/Superalgos/TS/Bot-Modules/Learning-Bot/Low-Frequency-Learning/LearningSystem.js +++ b/Projects/Superalgos/TS/Bot-Modules/Learning-Bot/Low-Frequency-Learning/LearningSystem.js @@ -26,6 +26,29 @@ exports.newSuperalgosBotModulesLearningSystem = function (processIndex) { let sessionParameters let dynamicIndicators + let learningAlgorithmName = TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.learningParameters.learningAlgorithmModuleObject.config.codeName + let learningAlgorithmModuleObject + /* + We will scan the project schema until we find the module that will run the algorithm + defined by the user at the UI / Session Parameters. + */ + for (let i = 0; i < TS.projects.superalgos.globals.taskConstants.PROJECTS_SCHEMA.length; i++) { + let project = TS.projects.superalgos.globals.taskConstants.PROJECTS_SCHEMA[i] + if (project.name !== TS.projects.superalgos.globals.taskConstants.PROJECT_DEFINITION_NODE.config.codeName) { continue } + for (let j = 0; j < project.TS.algorithmModules.length; j++) { + algorithmModuleDefinition = project.TS.algorithmModules[j] + if (algorithmModuleDefinition.name === learningAlgorithmName) { + + TS.projects.superalgos.globals.processVariables.TOTAL_PROCESS_INSTANCES_CREATED++ + let project = TS.projects[TS.projects.superalgos.globals.taskConstants.PROJECT_DEFINITION_NODE.config.codeName.toLowerCase()] + let botModule = project.algorithmModules[algorithmModuleDefinition.propertyName] + let moduleFunction = botModule[algorithmModuleDefinition.functionName] + learningAlgorithmModuleObject = moduleFunction(processIndex) + } + } + } + let learningAlgorithmModuleObject = TS.projects.superalgos.botModules.tradingStages.newSuperalgosBotModulesTradingStages(processIndex) + return thisObject function initialize() { @@ -98,6 +121,19 @@ exports.newSuperalgosBotModulesLearningSystem = function (processIndex) { learningSystem.infos.push(infoDataArray) } + /* + Initialize the Learning Algorithm + */ + learningAlgorithmModuleObject.initialize(onInitializeReady) + + function onInitializeReady(err) { + if (err.result === TS.projects.superalgos.globals.standardResponses.DEFAULT_OK_RESPONSE.result) { + + } else { + + } + } + function isItInside(elementWithTimestamp, elementWithBeginEnd) { /* The function is to allow in Conditions and Formulas to easily @@ -115,6 +151,9 @@ exports.newSuperalgosBotModulesLearningSystem = function (processIndex) { function finalize() { + learningAlgorithmModuleObject.finalize() + learningAlgorithmModuleObject = undefined + chart = undefined exchange = undefined market = undefined @@ -137,10 +176,11 @@ exports.newSuperalgosBotModulesLearningSystem = function (processIndex) { } function mantain() { - + learningAlgorithmModuleObject.mantain() } function reset() { + learningAlgorithmModuleObject.reset() learningSystem.highlights = [] learningSystem.errors = [] @@ -161,6 +201,8 @@ exports.newSuperalgosBotModulesLearningSystem = function (processIndex) { chart = pChart exchange = pExchange market = pMarket + + learningAlgorithmModuleObject.updateChart(pChart, pExchange, pMarket) } function buildDynamicIndicators() { @@ -183,6 +225,19 @@ exports.newSuperalgosBotModulesLearningSystem = function (processIndex) { /* Dynamic Indicators */ buildDynamicIndicators() + /* + Initialize the Learning Algorithm + */ + learningAlgorithmModuleObject.run(onRun) + + function onRun(err) { + if (err.result === TS.projects.superalgos.globals.standardResponses.DEFAULT_OK_RESPONSE.result) { + + } else { + + } + } + } catch (err) { /* If an error ocurred during execution, it was alreeady logged and diff --git a/Projects/Superalgos/TS/Bot-Modules/Learning-Bot/Low-Frequency-Learning/Time-Series-Forcasting/Tensor-Flow-JS/LearningAlgorithm.js b/Projects/Superalgos/TS/Bot-Modules/Learning-Bot/Low-Frequency-Learning/Time-Series-Forcasting/Tensor-Flow-JS/LearningAlgorithm.js new file mode 100644 index 000000000..16adde1ed --- /dev/null +++ b/Projects/Superalgos/TS/Bot-Modules/Learning-Bot/Low-Frequency-Learning/Time-Series-Forcasting/Tensor-Flow-JS/LearningAlgorithm.js @@ -0,0 +1,105 @@ +exports.newSuperalgosAlgorithmModulesTimeSeriesForcastingTensorFlowJS = function (processIndex) { + /* + This module implements a learning algorithm based on Time Series Forcasting with Tensor Flow js, + as described at the folliwing web page: + + https://jinglescode.github.io/time-series-forecasting-tensorflowjs/ + */ + const MODULE_NAME = 'Time Series Forcasting Tensor Flow JS Learning Algorithm' + let thisObject = { + updateChart: updateChart, + mantain: mantain, + reset: reset, + run: run, + initialize: initialize, + finalize: finalize + } + + let learningEngine + let learningSystem + let sessionParameters + + /* + These 3 are the main data structures available to users + when writing conditions and formulas. + */ + let chart + let exchange + let market + + return thisObject + + function initialize(callbackFunction) { + learningSystem = TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SIMULATION_STATE.learningSystem + learningEngine = TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SIMULATION_STATE.learningEngine + sessionParameters = TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE.learningParameters + + callbackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_OK_RESPONSE) + } + + function finalize() { + learningEngine = undefined + learningSystem = undefined + sessionParameters = undefined + + chart = undefined + exchange = undefined + market = undefined + } + + function updateChart(pChart, pExchange, pMarket) { + /* + We need these 3 data structures to be a local objects + accessible while evaluating conditions and formulas. + */ + chart = pChart + exchange = pExchange + market = pMarket + } + + function mantain() { + updateCounters() + updateEnds() + } + + function reset() { + } + + function run(callbackFunction) { + /* + Do your magic here... + */ + + /* + if all is good, return this... + */ + callbackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_OK_RESPONSE) + + /* + if something is bad, return this... + */ + //callbackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_FAIL_RESPONSE) + + } + + function badDefinitionUnhandledException(err, message, node) { + + let docs = { + project: 'Superalgos', + category: 'Topic', + type: 'TS LF Learning Bot Error - ' + message, + placeholder: {} + } + + learningSystem.addError([node.id, message, docs]) + + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] -> " + message); + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] -> node.name = " + node.name); + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] -> node.type = " + node.type); + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] -> node.config = " + JSON.stringify(node.config)); + if (err !== undefined) { + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE_OBJECT.write(MODULE_NAME, "[ERROR] -> err.stack = " + err.stack); + } + throw 'Error Already Recorded' + } +} \ No newline at end of file diff --git a/TaskServer/MultiProject.js b/TaskServer/MultiProject.js index 2b26abcb9..e12a68d62 100644 --- a/TaskServer/MultiProject.js +++ b/TaskServer/MultiProject.js @@ -30,6 +30,7 @@ exports.newMultiProject = function () { projectInstance.taskModules = {} projectInstance.processModules = {} projectInstance.botModules = {} + projectInstance.algorithmModules = {} /* Set up Utilities of this Project */ if (projectDefinition.TS.utilities !== undefined) { @@ -93,12 +94,23 @@ exports.newMultiProject = function () { if (projectDefinition.TS.botModules !== undefined) { for (let j = 0; j < projectDefinition.TS.botModules.length; j++) { let botModuleDefinition = projectDefinition.TS.botModules[j] - let path = global.env.PATH_TO_PROJECTS_REQUIRED + '/' + projectDefinition.name + '/' + 'TS' + '/' + 'Bot-Modules' + '/' + botModuleDefinition.folderName + '/' + botModuleDefinition.fileName + let path = global.env.PATH_TO_PROJECTS_REQUIRED + '/' + projectDefinition.name + '/' + 'TS' + '/' + 'Bot-Modules' + '/' + botModuleDefinition.folderName + '/' + botModuleDefinition.fileName let requiredObject = require(path) projectInstance.botModules[botModuleDefinition.propertyName] = requiredObject } } + + /* Set up Algorithm Modules for this Project */ + if (projectDefinition.TS.algorithmModules !== undefined) { + for (let j = 0; j < projectDefinition.TS.algorithmModules.length; j++) { + let algorithmModuleDefinition = projectDefinition.TS.algorithmModules[j] + let path = global.env.PATH_TO_PROJECTS_REQUIRED + '/' + projectDefinition.name + '/' + 'TS' + '/' + 'Bot-Modules' + '/' + algorithmModuleDefinition.folderName + '/' + algorithmModuleDefinition.fileName + + let requiredObject = require(path) + projectInstance.algorithmModules[algorithmModuleDefinition.propertyName] = requiredObject + } + } } TS.projects.superalgos.globals.taskConstants.PROJECTS_SCHEMA = PROJECTS_SCHEMA