diff --git a/LowFrequencyTrading/Announcements.js b/LowFrequencyTrading/Announcements.js index 751836a78..b9112dd7e 100644 --- a/LowFrequencyTrading/Announcements.js +++ b/LowFrequencyTrading/Announcements.js @@ -3,7 +3,6 @@ exports.newAnnouncements = function newAnnouncements(bot, logger) { const MODULE_NAME = 'Announcements' let thisObject = { - checkAnnouncements: checkAnnouncements, makeAnnoucements: makeAnnoucements, initialize: initialize, finalize: finalize @@ -20,93 +19,40 @@ exports.newAnnouncements = function newAnnouncements(bot, logger) { function finalize() { tradingEngine = undefined + announcementsToBeMade = undefined } - function checkAnnouncements(node, value) { - /* - Value is an optional parameter that represents the value that the announcement is monitoring for change (for numeric values only). - If we do receive this value, we will only make the annoucement if the variance is grater than the user pre-defined value - for this variance. - */ - + function makeAnnoucements(node) { if (node.announcements !== undefined) { for (let i = 0; i < node.announcements.length; i++) { let announcement = node.announcements[i] - let key = node.type + '-' + announcement.name + '-' + announcement.id - - let lastPeriodAnnounced = -1 - let newAnnouncementRecord = {} - - for (let j = 0; j < variable.announcements.length; j++) { - let announcementRecord = variable.announcements[j] - if (announcementRecord.key === key) { - lastPeriodAnnounced = announcementRecord.periods - newAnnouncementRecord = announcementRecord - break + let canAnnounce = true + if (announcement.announcementCondition !== undefined) { + let conditionValue + try { + conditionValue = eval(announcement.announcementCondition.code) + } catch (err) { + tradingSystem.errors.push([announcement.announcementCondition.id, err.message]) } + if (conditionValue !== true) { canAnnounce = false } } - - if (tradingEngine.episode.episodeCounters.periods.value > lastPeriodAnnounced) { - if (isNaN(value) === false) { - /* The Value Variation is what tells us how much the value already announced must change in order to annouce it again. */ - let valueVariation - - let config = announcement.config - valueVariation = config.valueVariation - - if (newAnnouncementRecord.value !== undefined && valueVariation !== undefined) { - let upperLimit = newAnnouncementRecord.value + newAnnouncementRecord.value * valueVariation / 100 - let lowerLimit = newAnnouncementRecord.value - newAnnouncementRecord.value * valueVariation / 100 - if (value > lowerLimit && value < upperLimit) { - /* There is not enough variation to announce this again. */ - return - } + if (canAnnounce === true) { + let text = announcement.config.text + if (announcement.formula !== undefined) { + try { + text = eval(announcement.formula.code) + } catch (err) { + tradingSystem.errors.push([announcement.formula.id, err.message]) } } - /* - We store the announcement temporarily at an Array to differ its execution, becasue we need to evaulate its formula - and at this point in time the potential variables used at the formula are still not set. - */ - announcement.value = value - announcementsToBeMade.push(announcement) - - /* Next, we will remmeber this announcement was already done, so that it is not announced again in further processing of the same day. */ - if (newAnnouncementRecord.periods !== undefined) { - newAnnouncementRecord.periods = tradingEngine.episode.episodeCounters.periods.value - newAnnouncementRecord.value = value + if (bot.SESSION.socialBots !== undefined) { + bot.SESSION.socialBots.announce(text) } else { - newAnnouncementRecord = { - key: key, - periods: tradingEngine.episode.episodeCounters.periods.value, - value: value - } - variable.announcements.push(newAnnouncementRecord) + tradingSystem.errors.push([announcement.id, 'Could not announce because session does not have Social Bots.']) } } } } } - - function makeAnnoucements() { - /* Here we go through all the annoucements that need to be done during this loop, and we just do them. */ - for (let i = 0; i < announcementsToBeMade.length; i++) { - announcement = announcementsToBeMade[i] - /* Here we check if there is a formula attached to the annoucement, we evaluate it to get the annoucement text. */ - let formulaValue - if (announcement.formula !== undefined) { - try { - let value = announcement.value - formulaValue = eval(announcement.formula.code) - } catch (err) { - announcement.formula.error = err.message - } - } - announcement.formulaValue = formulaValue - - if (bot.SESSION.socialBots !== undefined) { - bot.SESSION.socialBots.announce(announcement) - } - } - } } \ No newline at end of file diff --git a/LowFrequencyTrading/TradingSimulation.js b/LowFrequencyTrading/TradingSimulation.js index a8a7cc891..1cde9f2a0 100644 --- a/LowFrequencyTrading/TradingSimulation.js +++ b/LowFrequencyTrading/TradingSimulation.js @@ -68,10 +68,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT let snapshotsModule = SNAPSHOTS_MODULE.newSnapshots(bot, logger) snapshotsModule.initialize() - const ANNOUNCEMENTS_MODULE = require('./Announcements.js') - let announcementsModule = ANNOUNCEMENTS_MODULE.newAnnouncements(bot, logger) - announcementsModule.initialize() - const TRADING_SYSTEM_MODULE = require('./TradingSystem.js') let tradingSystemModule = TRADING_SYSTEM_MODULE.newTradingSystem(bot, logger) tradingSystemModule.initialize() @@ -224,7 +220,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT snapshotsModule.manageSnapshots() tradingRecordsModule.appendRecords() - announcementsModule.makeAnnoucements() // After everything at the simulation level was done, we will do the annoucements that are pending. controlLoop() } @@ -259,7 +254,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, UTILIT tradingSystemModule.finalize() tradingRecordsModule.finalize() snapshotsModule.finalize() - announcementsModule.finalize() callback() } diff --git a/LowFrequencyTrading/TradingSystem.js b/LowFrequencyTrading/TradingSystem.js index 2e7b5d980..27463eee4 100644 --- a/LowFrequencyTrading/TradingSystem.js +++ b/LowFrequencyTrading/TradingSystem.js @@ -44,6 +44,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { const TRADING_POSITION_MODULE = require('./TradingPosition.js') let tradingPositionModule = TRADING_POSITION_MODULE.newTradingPosition(bot, logger) + const ANNOUNCEMENTS_MODULE = require('./Announcements.js') + let announcementsModule = ANNOUNCEMENTS_MODULE.newAnnouncements(bot, logger) + return thisObject function initialize() { @@ -53,6 +56,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingStrategyModule.initialize() tradingPositionModule.initialize() + announcementsModule.initialize() } function finalize() { @@ -62,6 +66,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingPositionModule.finalize() tradingPositionModule = undefined + announcementsModule.finalize() + announcementsModule = undefined + chart = undefined conditions = undefined @@ -296,9 +303,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.distanceToEvent.triggerOn.value = 1 + announcementsModule.makeAnnoucements(triggerStage.triggerOn) + announcementsModule.makeAnnoucements(triggerStage) /* TODO See what to do with this: - checkAnnouncements(triggerStage.triggerOn) - checkAnnouncements(triggerStage) saveAsLastTriggerOnSnapshot = true */ @@ -346,9 +353,7 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingEngine.current.distanceToEvent.triggerOff.value = 1 - /* TODO See what to do with this: - checkAnnouncements(triggerStage.triggerOff) - */ + announcementsModule.makeAnnoucements(triggerStage.triggerOff) logger.write(MODULE_NAME, '[INFO] checkTriggerOff -> Closing Strategy: ' + strategy.name) return true // This Means that we have just met the conditions to trigger off. @@ -397,9 +402,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingStrategyModule.updateStageType('Open Stage') tradingPositionModule.openPosition(situation.name) + announcementsModule.makeAnnoucements(triggerStage.takePosition) + announcementsModule.makeAnnoucements(strategy.openStage) /* TODO See what to do with this: - checkAnnouncements(triggerStage.takePosition) - checkAnnouncements(strategy.openStage) saveAsLastTakePositionSnapshot = true */ @@ -481,14 +486,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { tradingStrategyModule.updateStageType('Manage Stage') - /* TODO ANNOUNCEMENT - checkAnnouncements(manageStage, 'Take Profit') - */ + announcementsModule.makeAnnoucements(manageStage) } - - /* TODO ANNOUNCEMENT - checkAnnouncements(nextPhaseEvent) - */ + announcementsModule.makeAnnoucements(nextPhaseEvent) return // only one event can pass at the time } } @@ -538,13 +538,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (tradingEngine.current.position.takeProfit.takeProfitPhase.value > 0) { tradingStrategyModule.updateStageType('Manage Stage') - /* TODO ANNOUNCEMENT - checkAnnouncements(manageStage, 'Take Profit') - */ + announcementsModule.makeAnnoucements(manageStage) } - /* - checkAnnouncements(moveToPhaseEvent) - */ + announcementsModule.makeAnnoucements(moveToPhaseEvent) return // only one event can pass at the time } } @@ -580,11 +576,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let previousValue = tradingEngine.current.position.stopLoss.value tradingPositionModule.applyStopLossFormula(formulas, phase.formula.id) - /* TODO ANNOUNCEMENTS if (tradingEngine.current.position.stopLoss.value !== previousValue) { - checkAnnouncements(phase, tradingEngine.current.position.stopLoss.value) + announcementsModule.makeAnnoucements(phase) } - */ } } } @@ -658,13 +652,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { tradingStrategyModule.updateStageType('Manage Stage') - /* TODO ANNOUNCEMENT - checkAnnouncements(manageStage, 'Stop') - */ + announcementsModule.makeAnnoucements(manageStage) } - /* TODO ANNOUNCEMENT - checkAnnouncements(nextPhaseEvent) - */ + announcementsModule.makeAnnoucements(nextPhaseEvent) return // only one event can pass at the time } } @@ -714,13 +704,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { if (tradingEngine.current.position.stopLoss.stopLossPhase.value > 0) { tradingStrategyModule.updateStageType('Manage Stage') - /* TODO ANNOUNCEMENT - checkAnnouncements(manageStage, 'Stop') - */ + announcementsModule.makeAnnoucements(manageStage) } - /* TODO ANNOUNCEMENT - checkAnnouncements(moveToPhaseEvent) - */ + announcementsModule.makeAnnoucements(moveToPhaseEvent) return // only one event can pass at the time } } @@ -756,11 +742,9 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { let previousValue = tradingEngine.current.position.takeProfit.value tradingPositionModule.applyTakeProfitFormula(formulas, phase.formula.id) - /* TODO ANNOUNCEMENTS if (tradingEngine.current.position.takeProfit.value !== previousValue) { - checkAnnouncements(phase, tradingEngine.current.position.takeProfit.value) + announcementsModule.makeAnnoucements(phase) } - */ } } } @@ -785,12 +769,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingPositionModule.preventStopLossDistortion() tradingPositionModule.applySlippageToStopLoss() tradingPositionModule.closePosition('Stop Loss') - tradingStrategyModule.updateStageType('Close Stage') - - /* TODO ANNOUNCEMENT - checkAnnouncements(strategy.closeStage, 'Stop') - */ + announcementsModule.makeAnnoucements(strategy.closeStage) return true // This means that the STOP was hit. } @@ -804,12 +784,8 @@ exports.newTradingSystem = function newTradingSystem(bot, logger) { tradingPositionModule.preventTakeProfitDistortion() tradingPositionModule.applySlippageToTakeProfit() tradingPositionModule.closePosition('Take Profit') - tradingStrategyModule.updateStageType('Close Stage') - - /* TODO ANNOUNCEMENT - checkAnnouncements(strategy.closeStage, 'Take Profit') - */ + announcementsModule.makeAnnoucements(strategy.closeStage) return true // This means that the Take Profit was hit. } } diff --git a/SocialBots.js b/SocialBots.js index 736f50fd9..9ae190727 100644 --- a/SocialBots.js +++ b/SocialBots.js @@ -19,29 +19,22 @@ exports.newSocialBots = function newSocialBots(bot, logger) { let socialBot = bot.SESSION.socialBots.bots[i] if (socialBot.type === "Telegram Bot") { let config = socialBot.config - socialBot.botInstance = SOCIAL_BOTS_MODULE.newTelegramBot(bot, logger) + socialBot.botInstance = TELEGRAM_BOT_MODULE.newTelegramBot(bot, logger) socialBot.botInstance.initialize(config.botToken, config.chatId) } } } - function announce(announcement) { + function announce(text) { if (bot.SESSION.socialBots.bots !== undefined) { for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) { let socialBot = bot.SESSION.socialBots.bots[i] try { - let config = announcement.config - if (socialBot.type === "Telegram Bot") { - if (announcement.formulaValue !== undefined) { - socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, announcement.formulaValue).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> Telegram API error -> err = " + err)) - } else { - socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, config.text).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> Telegram API error -> err = " + err)) - } + socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, text).catch(err => logger.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> Telegram API error -> err = " + err)) } - } catch (err) { - parentLogger.write(MODULE_NAME, "[WARN] initialize -> announce -> err = " + err.stack); + logger.write(MODULE_NAME, "[WARN] initialize -> announce -> err = " + err.stack); } } }