From f00b1b0cb19d0351dcf9ff5c9033e88a43414284 Mon Sep 17 00:00:00 2001 From: Smidy13 <77650800+Smidy13@users.noreply.github.com> Date: Tue, 4 May 2021 22:27:02 +1200 Subject: [PATCH] Improvement: Interpreting Multiple Telegram Commands --- .../Learning-Bot/SingleMarketLearningBot.js | 1 + .../Trading-Bot/Announcements/TelegramBot.js | 104 +++++++++++++++++- .../Trading-Bot/SingleMarketTradingBot.js | 1 + .../Superalgos/TS/Globals/ProcessVariables.js | 1 + 4 files changed, 101 insertions(+), 6 deletions(-) diff --git a/Projects/Superalgos/TS/Bot-Modules/Learning-Bot/SingleMarketLearningBot.js b/Projects/Superalgos/TS/Bot-Modules/Learning-Bot/SingleMarketLearningBot.js index 011ed9f72..91cab877e 100644 --- a/Projects/Superalgos/TS/Bot-Modules/Learning-Bot/SingleMarketLearningBot.js +++ b/Projects/Superalgos/TS/Bot-Modules/Learning-Bot/SingleMarketLearningBot.js @@ -72,6 +72,7 @@ /* Checking if we need to need to emit any event */ if (TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_STATUS === 'Idle' && TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_STOPPING === false) { TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_STATUS = 'Running' + TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_BEGIN = new Date() } if (TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_STATUS === 'Running' && TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_STOPPING === true) { diff --git a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Announcements/TelegramBot.js b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Announcements/TelegramBot.js index 308ff37d7..2d47ba3bc 100644 --- a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Announcements/TelegramBot.js +++ b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/Announcements/TelegramBot.js @@ -8,7 +8,8 @@ exports.newSuperalgosBotModulesTelegramBot = function (processIndex) { chatId: undefined, sendMessage: sendMessage, initialize: initialize, - finalize: finalize + finalize: finalize, + interpretCommand: interpretCommand } return thisObject @@ -22,8 +23,18 @@ exports.newSuperalgosBotModulesTelegramBot = function (processIndex) { thisObject.telegramBot = new Telegraf(botToken) thisObject.telegramBot.start((ctx) => ctx.reply('Hi! I am the Telegram Bot that will tell you what happens with your trading session.')) - thisObject.telegramBot.help((ctx) => ctx.replyWithHTML('SUPERALGOS BOT HELP\n\n• Use /stop to stop the session.\n• Use /help to receive this help message.')) + thisObject.telegramBot.help((ctx) => ctx.replyWithHTML('SUPERALGOS BOT HELP\n\n' + + '• Use /stop to stop the session.\n' + + '• Use /help to receive this help message.\n' + + '• Use /balances for episode balances.\n' + + '• Use /counters for episode counters.\n' + + '• Use /statistics for episode statistics.\n' + + '• Use /uptime for episode uptime.')) thisObject.telegramBot.command('stop', (ctx) => TS.projects.superalgos.functionLibraries.sessionFunctions.stopSession(processIndex, 'by command from Telegram.')) + thisObject.telegramBot.command('balances', (ctx) => interpretCommand('balances', processIndex)) + thisObject.telegramBot.command('uptime', (ctx) => interpretCommand('uptime', processIndex)) + thisObject.telegramBot.command('statistics', (ctx) => interpretCommand('statistics', processIndex)) + thisObject.telegramBot.command('counters', (ctx) => interpretCommand('counters', processIndex)) thisObject.telegramBot.launch() const Telegram = require('telegraf/telegram') @@ -31,21 +42,102 @@ exports.newSuperalgosBotModulesTelegramBot = function (processIndex) { thisObject.telegramAPI = new Telegram(botToken) const message = "Telegram bot is starting. For assistance type /help." - thisObject.telegramAPI.sendMessage(thisObject.chatId, message).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initializeTelegramBot -> Telegram API error -> err = " + err)) + thisObject.telegramAPI.sendMessage(thisObject.chatId, message) } catch (err) { - parentLogger.write(MODULE_NAME, "[WARN] initialize -> err = " + err.stack); + TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).PROCESS_INSTANCE_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[WARN] initialize -> Telegram API error -> err = " + err.stack) } } function finalize() { const message = "Telegram bot is signing off." - thisObject.telegramAPI.sendMessage(thisObject.chatId, message).catch(err => parentLogger.write(MODULE_NAME, "[WARN] finalize -> Telegram API error -> err = " + err)) + thisObject.telegramAPI.sendMessage(thisObject.chatId, message).catch(err => TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).PROCESS_INSTANCE_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[WARN] finalize -> Telegram API error -> err = " + err.stack)) } function sendMessage(message) { - thisObject.telegramAPI.sendMessage(thisObject.chatId, message).catch(err => parentLogger.write(MODULE_NAME, "[WARN] announce -> Telegram API error -> err = " + err)) + thisObject.telegramAPI.sendMessage(thisObject.chatId, message).catch(err => TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).PROCESS_INSTANCE_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[WARN] sendMessage -> Telegram API error -> err = " + err.stack)) + } + + + function interpretCommand(command, processIndex) { + + let message + let tradingEngine + let tradingEpisode + let sessionNode + let sessionName + + tradingEngine = TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SIMULATION_STATE.tradingEngine + tradingEpisode = tradingEngine.tradingCurrent.tradingEpisode + + sessionNode = TS.projects.superalgos.globals.processConstants.CONSTANTS_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_NODE + sessionName = sessionNode.name + + + switch (command) { + case 'balances': { + + let baseAsset = TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + let quotedAsset = TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName + + message = "SUPERALGOS BALANCES\n\nTrading Session: " + sessionName + + "\nPair: " + baseAsset + "/" + quotedAsset + + "\nBase Asset: " + tradingEpisode.episodeBaseAsset.balance.value + " " + baseAsset + + "\nQuoted Asset: " + tradingEpisode.episodeQuotedAsset.balance.value + " " + quotedAsset + break + } + case 'uptime': { + + let sessionBegin = TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_BEGIN + + let dateBegin = new Date(sessionBegin) + let dateNow = new Date(); + + let seconds = Math.floor((dateNow - (dateBegin))/1000) + let minutes = Math.floor(seconds/60) + let hours = Math.floor(minutes/60) + let days = Math.floor(hours/24) + + hours = hours-(days*24) + minutes = minutes-(days*24*60)-(hours*60) + seconds = seconds-(days*24*60*60)-(hours*60*60)-(minutes*60) + + let uptime = days + "D, " + hours + "H, " + minutes + "M, " + seconds + "S" + + message = "SUPERALGOS UPTIME\n\nTrading Session: " + sessionName + + "\nUptime: " + uptime + break + } + case 'statistics': { + + message = "SUPERALGOS STATISTICS\n\nTrading Session: " + sessionName + + "\nDays: " + (tradingEpisode.tradingEpisodeStatistics.days.value).toFixed(2) + + "\nProfit Loss: " + tradingEpisode.tradingEpisodeStatistics.profitLoss.value + + "\nROI: " + (tradingEpisode.tradingEpisodeStatistics.ROI.value).toFixed(2) + + "\nAnnualized Rate Of Return: " + (tradingEpisode.tradingEpisodeStatistics.annualizedRateOfReturn.value).toFixed(2) + + "\nHit Fail: " + tradingEpisode.tradingEpisodeStatistics.hitFail.value + break + } + case 'counters': { + + message = "SUPERALGOS COUNTERS\n\nTrading Session: " + sessionName + + "\nPeriods: " + tradingEpisode.tradingEpisodeCounters.periods.value + + "\nStrategies: " + tradingEpisode.tradingEpisodeCounters.strategies.value + + "\nPositions: " + tradingEpisode.tradingEpisodeCounters.positions.value + + "\nOrders: " + tradingEpisode.tradingEpisodeCounters.orders.value + + "\nHits: " + tradingEpisode.tradingEpisodeCounters.hits.value + + "\nFails: " + tradingEpisode.tradingEpisodeCounters.fails.value + break + } + } + + thisObject.telegramAPI.sendMessage(thisObject.chatId, message, {parse_mode: 'HTML'}).catch(err => TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).PROCESS_INSTANCE_LOGGER_MODULE_OBJECT.write(MODULE_NAME, + "[WARN] sendMessage -> Telegram API error -> err = " + err.stack)) + } } \ No newline at end of file diff --git a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/SingleMarketTradingBot.js b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/SingleMarketTradingBot.js index be3e36417..4708cb894 100644 --- a/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/SingleMarketTradingBot.js +++ b/Projects/Superalgos/TS/Bot-Modules/Trading-Bot/SingleMarketTradingBot.js @@ -72,6 +72,7 @@ /* Checking if we need to need to emit any event */ if (TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_STATUS === 'Idle' && TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_STOPPING === false) { TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_STATUS = 'Running' + TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_BEGIN = new Date() } if (TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).SESSION_STATUS === 'Running' && TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_STOPPING === true) { diff --git a/Projects/Superalgos/TS/Globals/ProcessVariables.js b/Projects/Superalgos/TS/Globals/ProcessVariables.js index 2438cc927..5ff9b3c58 100644 --- a/Projects/Superalgos/TS/Globals/ProcessVariables.js +++ b/Projects/Superalgos/TS/Globals/ProcessVariables.js @@ -22,6 +22,7 @@ exports.newSuperalgosGlobalsProcessVariables = function () { MAIN_LOOP_COUNTER This is the counter of main loops for a bot process. SOCIAL_BOTS_MODULE This is the module that controls social bots. SESSION_STATUS This tell us the status of the session at any given time. + SESSION_BEGIN This tell us the date time the session was begun. IS_SESSION_STOPPING This tell us if a session is in the process of being stopped. IS_SESSION_RESUMING This tell us if a session is resuming from the state at the previous execution. IS_SESSION_FIRST_LOOP This tell us if we are at the execution of the first loop of the bot related to this session.