Draft Code of Indicator Bots and Sensor Refactoring

This commit is contained in:
Luis Fernando Molina committed 2019-10-26 19:41:09 +02:00
1 parent 136be168a5
commit e3f2e6f06b
8 files changed
+190 -275

No files matched your search

+92 -1
View File
@@ -12,11 +12,102 @@
buildChannels: buildChannels,
buildStandardChannels: buildStandardChannels,
buildSubChannels: buildSubChannels,
buildStandardSubChannels: buildStandardSubChannels
buildStandardSubChannels: buildStandardSubChannels,
buildRecords: buildRecords
};
return thisObject;
function buildDependecyArray(dataFile, recordDefinition) {
let dependecyArray = []
for (let i = 0; i < dataFile.length; i++) {
let record = {}
for (let j = 0; j < recordDefinition.length; j++) {
let property = recordDefinition[j]
record[property.name] = dataFile[i][j]
}
dependecyArray.push(record);
}
return dependecyArray
}
function addCalculatedProperties(dependencyArray, variableName, timePeriod) {
let dependecyArray = []
/* This is Initialization Code */
let lastMovingAverage = 0;
const SIDE_TOLERANCE = 0.5 * timePeriod / ONE_DAY_IN_MILISECONDS;
const SMALL_SLOPE = 1.0 * timePeriod / ONE_DAY_IN_MILISECONDS;
const MEDIUM_SLOPE = 2.0 * timePeriod / ONE_DAY_IN_MILISECONDS;
const HIGH_SLOPE = 4.0 * timePeriod / ONE_DAY_IN_MILISECONDS;
/* This is Initialization Code */
for (let i = 0; i < dataFile.length; i++) {
let indicator = {}
indicator[variableName] = dataFile[i]
/* This is Add Properties Code */
if (lastMovingAverage > indicator.bollingerBand.movingAverage) { indicator.bollingerBand.direction = 'Down'; }
if (lastMovingAverage < indicator.bollingerBand.movingAverage) { indicator.bollingerBand.direction = 'Up'; }
if (lastMovingAverage === indicator.bollingerBand.movingAverage) { indicator.bollingerBand.direction = 'Side'; }
let delta = Math.abs(indicator.bollingerBand.movingAverage - lastMovingAverage);
indicator.bollingerBand.slope = 'Extreme';
if (delta < indicator.bollingerBand.movingAverage * HIGH_SLOPE / 100) { indicator.bollingerBand.slope = 'Steep'; }
if (delta < indicator.bollingerBand.movingAverage * MEDIUM_SLOPE / 100) { indicator.bollingerBand.slope = 'Medium'; }
if (delta < indicator.bollingerBand.movingAverage * SMALL_SLOPE / 100) { indicator.bollingerBand.slope = 'Gentle'; }
if (delta < indicator.bollingerBand.movingAverage * SIDE_TOLERANCE / 100) { indicator.bollingerBand.slope = 'Side'; }
lastMovingAverage = indicator.bollingerBand.movingAverage;
/* This is Add Properties Code */
dependecyArray.push(indicator.bollingerBand);
}
return dependecyArray
}
function buildRecords(dataDependency, recordDefinition) {
let records = []
/* Initialization Code */
let period = 1
/* Initialization Code */
for (let i = 0; i < dataDependency.bollinerBands.length; i++) {
/* Calculations Code*/
if (i === 0) {continue}
let currentBand = dataDependency.bollinerBands[i];
let previousBand = dataDependency.bollinerBands[i - 1];
if (currentBand.direction === previousBand.direction) {
period++
} else {
period = 1
}
/* Calculations Code*/
let record = {
begin: currentBand.begin, /* Property Formula Code */
end: currentBand.end, /* Property Formula Code */
direction: currentBand.direction, /* Property Formula Code */
period: period /* Property Formula Code */
};
records.push(record)
}
return records
}
function buildBandsArray(dataFile, bands, timePeriod, callBackFunction) {
try {
+4 -4
View File
@@ -20,12 +20,12 @@
let utilities = UTILITIES.newCloudUtilities(bot, logger);
let dataDependencies;
let fileStorage = FILE_STORAGE.newFileStorage(logger);
let processConfig;
return thisObject;
function initialize(pDataDependencies, callBackFunction) {
function initialize(pProcessConfig, callBackFunction) {
try {
@@ -34,7 +34,7 @@
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); }
dataDependencies = pDataDependencies;
processConfig = pProcessConfig;
callBackFunction(global.DEFAULT_OK_RESPONSE);
} catch (err) {
@@ -79,7 +79,7 @@
let fileContent = "";
for (i = 0; i < channels.length; i++) {
for (let i = 0; i < channels.length; i++) {
let channel = channels[i];
+80 -258
View File
@@ -18,12 +18,12 @@
start: start
};
let dataDependencies;
let fileStorage = FILE_STORAGE.newFileStorage(logger);
let processConfig;
return thisObject;
function initialize(pDataDependencies, callBackFunction) {
function initialize(pProcessConfig, callBackFunction) {
try {
@@ -32,7 +32,7 @@
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); }
dataDependencies = pDataDependencies;
processConfig = pProcessConfig;
callBackFunction(global.DEFAULT_OK_RESPONSE);
} catch (err) {
@@ -64,46 +64,88 @@
commons.buildSubChannels(bands, subChannels, callBackFunction);
commons.buildStandardSubChannels(bands, standardSubChannels, callBackFunction);
writeChannelsFile();
let dataDependency = {}
for (let i = 0; i < processConfig.dataDependencies.length; i++) {
let dependency = processConfig.dataDependencies[i]
let recordDefinition = processConfig.recordDefinition
let dependencyArray = commons.buildDependencyArray(dataFiles[i], recordDefinition);
dependencyArray = commons.addCalculatedProperties(dependencyArray, dependency.singularVariableName, timePeriod);
dataDependency[dependency.pluralVariableName] = dependencyArray
}
function writeChannelsFile() {
for (let i = 0; i < processConfig.updatesDatasets.length; i++) {
let product = processConfig.updatesDatasets[i].product
let dataset = processConfig.updatesDatasets[i].dataset
let recordDefinition = getRecordDefinition(product)
let records = commons.buildRecords(dataDependency, recordDefinition)
let fileContent = generateFileContent(indicatorData, recordDefinition)
writeFile(product, dataset, fileContent)
}
let totalFilesWritten = 0
function anotherFileWritten() {
totalFilesWritten++
if (totalFilesWritten === processConfig.updatesDatasets.length) {
callBackFunction(global.DEFAULT_OK_RESPONSE);
}
}
function getRecordDefinition(product) {
for (let j = 0; j < bot.products.length; j++) {
if (bot.products[j].codeName === product) {
return bot.products[j].recordDefinition
}
}
}
function generateFileContent(records, recordDefinition) {
try {
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeChannelsFile -> Entering function."); }
let separator = "";
let fileRecordCounter = 0;
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> generateFileContent -> Entering function."); }
let fileContent = "";
let recordSeparator = "";
for (i = 0; i < channels.length; i++) {
let channel = channels[i];
fileContent = fileContent + separator + '[' +
channel.begin + "," +
channel.end + "," +
'"' + channel.direction + '"' + "," +
channel.period + "," +
channel.firstMovingAverage + "," +
channel.lastMovingAverage + "," +
channel.firstDeviation + "," +
channel.lastDeviation + "]";
if (separator === "") { separator = ","; }
fileRecordCounter++;
for (let i = 0; i < records.length; i++) {
let record = records[i];
fileContent = fileContent + recordSeparator + '['
let propertySeparator = ""
for (let j = 0; j < recordDefinition.length; j++) {
let property = recordDefinition[j]
fileContent = fileContent + propertySeparator
if (property.isNumeric !== true) {
fileContent = fileContent + '"'
}
fileContent = fileContent + record[property.name]
if (property.isNumeric !== true) {
fileContent = fileContent + '"'
}
if (recordSeparator === "") { propertySeparator = ","; }
}
fileContent = fileContent + ']'
if (recordSeparator === "") { recordSeparator = ","; }
}
fileContent = "[" + fileContent + "]";
return fileContent
}
catch (err) {
logger.write(MODULE_NAME, "[ERROR] start -> generateFileContent -> err = " + err.stack);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
function writeFile(product, dataset, fileContent) {
try {
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeFile -> Entering function."); }
let fileName = '' + market.assetA + '_' + market.assetB + '.json';
let filePathRoot = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + global.CLONE_EXECUTOR.codeName + "." + global.CLONE_EXECUTOR.version + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion;
let filePath = filePathRoot + "/Output/" + BOLLINGER_CHANNELS_FOLDER_NAME + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel;
let filePath = filePathRoot + "/Output/" + product + "/" + dataset + "/" + outputPeriodLabel;
filePath += '/' + fileName
fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated);
@@ -112,254 +154,34 @@
try {
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeChannelsFile -> onFileCreated -> Entering function."); }
if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> writeChannelsFile -> onFileCreated -> fileContent = " + fileContent); }
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeFile -> onFileCreated -> Entering function."); }
if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> writeFile -> onFileCreated -> fileContent = " + fileContent); }
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
logger.write(MODULE_NAME, "[ERROR] start -> writeChannelsFile -> onFileCreated -> err = " + err.stack);
logger.write(MODULE_NAME, "[ERROR] start -> writeChannelsFile -> onFileCreated -> filePath = " + filePath);
logger.write(MODULE_NAME, "[ERROR] start -> writeChannelsFile -> onFileCreated -> market = " + market.assetA + "_" + market.assetB);
logger.write(MODULE_NAME, "[ERROR] start -> writeFile -> onFileCreated -> err = " + err.stack);
logger.write(MODULE_NAME, "[ERROR] start -> writeFile -> onFileCreated -> filePath = " + filePath);
logger.write(MODULE_NAME, "[ERROR] start -> writeFile -> onFileCreated -> market = " + market.assetA + "_" + market.assetB);
callBackFunction(err);
return;
}
writeStandardChannelsFile();
anotherFileWritten();
}
catch (err) {
logger.write(MODULE_NAME, "[ERROR] start -> writeChannelsFile -> onFileCreated -> err = " + err.stack);
logger.write(MODULE_NAME, "[ERROR] start -> writeFile -> onFileCreated -> err = " + err.stack);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
}
catch (err) {
logger.write(MODULE_NAME, "[ERROR] start -> writeChannelsFile -> err = " + err.stack);
logger.write(MODULE_NAME, "[ERROR] start -> writeFile -> err = " + err.stack);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
function writeStandardChannelsFile() {
try {
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStandardChannelsFile -> Entering function."); }
let separator = "";
let fileRecordCounter = 0;
let fileContent = "";
for (i = 0; i < standardChannels.length; i++) {
let channel = standardChannels[i];
fileContent = fileContent + separator + '[' +
channel.begin + "," +
channel.end + "," +
'"' + channel.direction + '"' + "," +
channel.period + "]";
if (separator === "") { separator = ","; }
fileRecordCounter++;
}
fileContent = "[" + fileContent + "]";
let fileName = '' + market.assetA + '_' + market.assetB + '.json';
let filePathRoot = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + global.CLONE_EXECUTOR.codeName + "." + global.CLONE_EXECUTOR.version + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion;
let filePath = filePathRoot + "/Output/" + BOLLINGER_STANDARD_CHANNELS_FOLDER_NAME + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel;
filePath += '/' + fileName
fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated);
function onFileCreated(err) {
try {
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStandardChannelsFile -> onFileCreated -> Entering function."); }
if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStandardChannelsFile -> onFileCreated -> fileContent = " + fileContent); }
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
logger.write(MODULE_NAME, "[ERROR] start -> writeStandardChannelsFile -> onFileCreated -> err = " + err.stack);
logger.write(MODULE_NAME, "[ERROR] start -> writeStandardChannelsFile -> onFileCreated -> filePath = " + filePath);
logger.write(MODULE_NAME, "[ERROR] start -> writeStandardChannelsFile -> onFileCreated -> market = " + market.assetA + "_" + market.assetB);
callBackFunction(err);
return;
}
writeSubChannelsFile();
}
catch (err) {
logger.write(MODULE_NAME, "[ERROR] start -> writeStandardChannelsFile -> onFileCreated -> err = " + err.stack);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
}
catch (err) {
logger.write(MODULE_NAME, "[ERROR] start -> writeStandardChannelsFile -> err = " + err.stack);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
function writeSubChannelsFile() {
try {
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeSubChannelsFile -> Entering function."); }
let separator = "";
let fileRecordCounter = 0;
let fileContent = "";
for (i = 0; i < subChannels.length; i++) {
let channel = subChannels[i];
fileContent = fileContent + separator + '[' +
channel.begin + "," +
channel.end + "," +
'"' + channel.direction + '"' + "," +
'"' + channel.slope + '"' + "," +
channel.period + "," +
channel.firstMovingAverage + "," +
channel.lastMovingAverage + "," +
channel.firstDeviation + "," +
channel.lastDeviation + "]";
if (separator === "") { separator = ","; }
fileRecordCounter++;
}
fileContent = "[" + fileContent + "]";
let fileName = '' + market.assetA + '_' + market.assetB + '.json';
let filePathRoot = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + global.CLONE_EXECUTOR.codeName + "." + global.CLONE_EXECUTOR.version + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion;
let filePath = filePathRoot + "/Output/" + BOLLINGER_SUB_CHANNELS_FOLDER_NAME + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel;
filePath += '/' + fileName
fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated);
function onFileCreated(err) {
try {
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeSubChannelsFile -> onFileCreated -> Entering function."); }
if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> writeSubChannelsFile -> onFileCreated -> fileContent = " + fileContent); }
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
logger.write(MODULE_NAME, "[ERROR] start -> writeSubChannelsFile -> onFileCreated -> err = " + err.stack);
logger.write(MODULE_NAME, "[ERROR] start -> writeSubChannelsFile -> onFileCreated -> filePath = " + filePath);
logger.write(MODULE_NAME, "[ERROR] start -> writeSubChannelsFile -> onFileCreated -> market = " + market.assetA + "_" + market.assetB);
callBackFunction(err);
return;
}
writeStandardSubChannelsFile();
}
catch (err) {
logger.write(MODULE_NAME, "[ERROR] start -> writeSubChannelsFile -> onFileCreated -> err = " + err.stack);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
}
catch (err) {
logger.write(MODULE_NAME, "[ERROR] start -> writeSubChannelsFile -> err = " + err.stack);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
function writeStandardSubChannelsFile() {
try {
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStandardSubChannelsFile -> Entering function."); }
let separator = "";
let fileRecordCounter = 0;
let fileContent = "";
for (i = 0; i < standardSubChannels.length; i++) {
let channel = standardSubChannels[i];
fileContent = fileContent + separator + '[' +
channel.begin + "," +
channel.end + "," +
'"' + channel.direction + '"' + "," +
'"' + channel.slope + '"' + "," +
channel.period + "]";
if (separator === "") { separator = ","; }
fileRecordCounter++;
}
fileContent = "[" + fileContent + "]";
let fileName = '' + market.assetA + '_' + market.assetB + '.json';
let filePathRoot = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + global.CLONE_EXECUTOR.codeName + "." + global.CLONE_EXECUTOR.version + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion;
let filePath = filePathRoot + "/Output/" + BOLLINGER_STANDARD_SUB_CHANNELS_FOLDER_NAME + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel;
filePath += '/' + fileName
fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated);
function onFileCreated(err) {
try {
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStandardSubChannelsFile -> onFileCreated -> Entering function."); }
if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStandardSubChannelsFile -> onFileCreated -> fileContent = " + fileContent); }
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
logger.write(MODULE_NAME, "[ERROR] start -> writeStandardSubChannelsFile -> onFileCreated -> err = " + err.stack);
logger.write(MODULE_NAME, "[ERROR] start -> writeStandardSubChannelsFile -> onFileCreated -> filePath = " + filePath);
logger.write(MODULE_NAME, "[ERROR] start -> writeStandardSubChannelsFile -> onFileCreated -> market = " + market.assetA + "_" + market.assetB);
callBackFunction(err);
return;
}
callBackFunction(global.DEFAULT_OK_RESPONSE);
}
catch (err) {
logger.write(MODULE_NAME, "[ERROR] start -> writeSubChannelsFile -> onFileCreated -> err = " + err.stack);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
}
catch (err) {
logger.write(MODULE_NAME, "[ERROR] start -> writeSubChannelsFile -> err = " + err.stack);
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
}
}
}
catch (err) {
logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack);
+1 -1
View File
@@ -568,7 +568,7 @@
if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] run -> loop -> intitializeProcessFramework -> Entering function."); }
processFramework.initialize(processConfig, statusDependencies, dataDependencies, undefined, onInizialized);
processFramework.initialize(processConfig, statusDependencies, dataDependencies, onInizialized);
function onInizialized(err) {
+2 -2
View File
@@ -29,7 +29,7 @@
return thisObject;
function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, pAssistant, callBackFunction) {
function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, callBackFunction) {
try {
@@ -64,7 +64,7 @@
let USER_BOT_MODULE = require("./IndicatorBotDaily")
usertBot = USER_BOT_MODULE.newIndicatorBotDaily(bot, logger, COMMONS_MODULE, UTILITIES, FILE_STORAGE);
usertBot.initialize(dataDependencies, callBackFunction, pAssistant);
usertBot.initialize(processConfig, callBackFunction);
} catch (err) {
logger.write(MODULE_NAME, "[ERROR] initialize -> err = "+ err.stack);
+2 -2
View File
@@ -23,7 +23,7 @@
return thisObject;
function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, pAssistant, callBackFunction) {
function initialize(pProcessConfig, pStatusDependencies, pDataDependencies, callBackFunction) {
try {
@@ -58,7 +58,7 @@
let USER_BOT_MODULE = require("./IndicatorBotMarket")
usertBot = USER_BOT_MODULE.newIndicatorBotMarket(bot, logger, COMMONS_MODULE, UTILITIES, FILE_STORAGE);
usertBot.initialize(dataDependencies, callBackFunction, pAssistant);
usertBot.initialize(processConfig, callBackFunction);
} catch (err) {
logger.write(MODULE_NAME, "[ERROR] initialize -> err = "+ err.stack);
+6 -6
View File
@@ -146,7 +146,8 @@
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
console.log(logDisplace + "Root : [ERROR] start -> getBotConfig -> onInizialized -> onFileReceived -> err = " + JSON.stringify(err));
console.log(logDisplace + "Root : [ERROR] start -> getBotConfig -> onInizialized -> onFileReceived -> filePath = " + filePath);
console.log(logDisplace + "Root : [ERROR] start -> getBotConfig -> onInizialized -> onFileReceived -> team = " + global.TASK_NODE.bot.code.team);
return;
}
@@ -173,7 +174,6 @@
botConfig.process = global.TASK_NODE.bot.processes[processIndex].code.process;
botConfig.debug = {};
botConfig.processIndex = processIndex
botConfig.processNode = global.TASK_NODE.bot.processes[processIndex]
/* Loop Counter */
@@ -256,7 +256,7 @@
if (FULL_LOG === true) { console.log(logDisplace + "Root : [INFO] start -> findProcess -> startProcess -> Ready to start process."); }
switch (botConfig.type) {
case 'Sensor': {
case 'Sensor Bot': {
runSensorBot(newInstanceBotConfig, processConfig, padMonth, year);
break;
}
@@ -295,7 +295,7 @@
};
switch (botConfig.type) {
case 'Sensor': {
case 'Sensor Bot': {
runSensorBot(botConfig, processConfig, month, year);
break;
}
@@ -325,7 +325,7 @@
}
switch (botConfig.type) {
case 'Sensor': {
case 'Sensor Bot': {
runSensorBot(botConfig, processConfig, month, year);
break;
}
@@ -355,7 +355,7 @@
let year = (new Date()).getUTCFullYear();
switch (botConfig.type) {
case 'Sensor': {
case 'Sensor Bot': {
runSensorBot(botConfig, processConfig, month, year);
break;
}
+3 -1
View File
@@ -44,7 +44,9 @@
return
function runSession(message) {
console.log(message.event.definition)
/* We are going to run the Definition comming at the event. */
bot.DEFINITION = JSON.parse(message.event.definition)
bot.SESSION = JSON.parse(message.event.session)