Merge pull request #879 from Smidy13/develop

Improvement: Interpreting Multiple Telegram Commands
This commit is contained in:
Luis Fernando Molina authored and GitHub committed 2021-05-06 13:51:48 -04:00
commit 70a346ccc9
4 files changed
+101 -6

No files matched your search

@@ -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) {
@@ -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('<b><u>SUPERALGOS BOT HELP</u></b>\n\n• Use /stop to stop the session.\n• Use /help to receive this help message.'))
thisObject.telegramBot.help((ctx) => ctx.replyWithHTML('<b><u>SUPERALGOS BOT HELP</u></b>\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 = "<b><u>SUPERALGOS BALANCES</u></b>\n\n<b>Trading Session:</b> " + sessionName +
"\n<b>Pair:</b> " + baseAsset + "/" + quotedAsset +
"\n<b>Base Asset:</b> " + tradingEpisode.episodeBaseAsset.balance.value + " " + baseAsset +
"\n<b>Quoted Asset:</b> " + 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 = "<b><u>SUPERALGOS UPTIME</u></b>\n\n<b>Trading Session:</b> " + sessionName +
"\n<b>Uptime:</b> " + uptime
break
}
case 'statistics': {
message = "<b><u>SUPERALGOS STATISTICS</u></b>\n\n<b>Trading Session:</b> " + sessionName +
"\n<b>Days:</b> " + (tradingEpisode.tradingEpisodeStatistics.days.value).toFixed(2) +
"\n<b>Profit Loss:</b> " + tradingEpisode.tradingEpisodeStatistics.profitLoss.value +
"\n<b>ROI:</b> " + (tradingEpisode.tradingEpisodeStatistics.ROI.value).toFixed(2) +
"\n<b>Annualized Rate Of Return:</b> " + (tradingEpisode.tradingEpisodeStatistics.annualizedRateOfReturn.value).toFixed(2) +
"\n<b>Hit Fail:</b> " + tradingEpisode.tradingEpisodeStatistics.hitFail.value
break
}
case 'counters': {
message = "<b><u>SUPERALGOS COUNTERS</u></b>\n\n<b>Trading Session:</b> " + sessionName +
"\n<b>Periods:</b> " + tradingEpisode.tradingEpisodeCounters.periods.value +
"\n<b>Strategies:</b> " + tradingEpisode.tradingEpisodeCounters.strategies.value +
"\n<b>Positions:</b> " + tradingEpisode.tradingEpisodeCounters.positions.value +
"\n<b>Orders:</b> " + tradingEpisode.tradingEpisodeCounters.orders.value +
"\n<b>Hits:</b> " + tradingEpisode.tradingEpisodeCounters.hits.value +
"\n<b>Fails:</b> " + 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))
}
}
@@ -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) {
@@ -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.