From 2a0a5a44cac710a64eaa7c1ff3a787be573925df Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 1 Apr 2020 17:18:35 +0200 Subject: [PATCH] Announcements Improved --- AppSchema.json | 49 ++++++++++++++++-- .../FunctionLibraries/SessionFunctions.js | 6 +-- TaskServer/Session.js | 16 +++--- TaskServer/TradingSimulation.js | 50 +++++++++++++------ 4 files changed, 89 insertions(+), 32 deletions(-) diff --git a/AppSchema.json b/AppSchema.json index 924bde796..17f71a47a 100644 --- a/AppSchema.json +++ b/AppSchema.json @@ -3680,6 +3680,12 @@ "relatedUiObject": "Trigger Stage", "actionFunction": "payload.onMenuItemClick" }, + { + "action": "Add UI Object", + "label": "Add Announcement", + "relatedUiObject": "Announcement", + "actionFunction": "payload.onMenuItemClick" + }, { "action": "Delete UI Object", "askConfirmation": true, @@ -3714,6 +3720,11 @@ "type": "node", "childType": "Take Position Event", "autoAdd": true + }, + { + "name": "announcements", + "type": "array", + "childType": "Announcement" } ], "propertyNameAtParent": "triggerStage" @@ -3879,6 +3890,12 @@ "relatedUiObject": "Open Execution", "actionFunction": "payload.onMenuItemClick" }, + { + "action": "Add UI Object", + "label": "Add Announcement", + "relatedUiObject": "Announcement", + "actionFunction": "payload.onMenuItemClick" + }, { "action": "Delete UI Object", "askConfirmation": true, @@ -3907,6 +3924,11 @@ "type": "node", "childType": "Open Execution", "autoAdd": true + }, + { + "name": "announcements", + "type": "array", + "childType": "Announcement" } ], "propertyNameAtParent": "openStage" @@ -4325,6 +4347,12 @@ "relatedUiObject": "Manage Stage", "actionFunction": "payload.onMenuItemClick" }, + { + "action": "Add UI Object", + "label": "Add Announcement", + "relatedUiObject": "Announcement", + "actionFunction": "payload.onMenuItemClick" + }, { "action": "Delete UI Object", "askConfirmation": true, @@ -4353,6 +4381,11 @@ "type": "node", "childType": "Take Profit", "autoAdd": true + }, + { + "name": "announcements", + "type": "array", + "childType": "Announcement" } ], "propertyNameAtParent": "manageStage" @@ -4369,6 +4402,12 @@ "relatedUiObject": "Close Execution", "actionFunction": "payload.onMenuItemClick" }, + { + "action": "Add UI Object", + "label": "Add Announcement", + "relatedUiObject": "Announcement", + "actionFunction": "payload.onMenuItemClick" + }, { "action": "Delete UI Object", "askConfirmation": true, @@ -4391,6 +4430,11 @@ "type": "node", "childType": "Close Execution", "autoAdd": true + }, + { + "name": "announcements", + "type": "array", + "childType": "Announcement" } ], "propertyNameAtParent": "closeStage" @@ -4523,11 +4567,8 @@ }, "addLeftIcons": true, "level": 4, - "referenceAttachesTo": { - "compatibleTypes": "->Telegram Bot->" - }, "chainAttachesTo": { - "compatibleTypes": "->Trigger On Event->Trigger Off Event->Take Position Event->Next Phase Event->Phase->" + "compatibleTypes": "->Trigger On Event->Trigger Off Event->Take Position Event->Next Phase Event->Phase->Trigger Stage->Open Stage->Manage Stage->Close Stage->" }, "properties": [ { diff --git a/CanvasApp/DesignSpace/Workspace/FunctionLibraries/SessionFunctions.js b/CanvasApp/DesignSpace/Workspace/FunctionLibraries/SessionFunctions.js index 3d393d8ab..33542151d 100644 --- a/CanvasApp/DesignSpace/Workspace/FunctionLibraries/SessionFunctions.js +++ b/CanvasApp/DesignSpace/Workspace/FunctionLibraries/SessionFunctions.js @@ -37,7 +37,7 @@ function newSessionFunctions () { 'Exchange Account Asset->Asset->' + 'Strategy->' + 'Trigger Stage->Trigger On Event->Trigger Off Event->Take Position Event->' + - 'Announcement->Telegram Bot->' + + 'Announcement->Formula->' + 'Open Stage->Initial Definition->Open Execution->' + 'Position Size->Position Rate->Formula->' + 'Initial Stop->Initial Take Profit->' + @@ -45,8 +45,8 @@ function newSessionFunctions () { 'Stop->Take Profit->' + 'Phase->Formula->Next Phase Event->' + 'Situation->Condition->Javascript Code->' + - 'Announcement->Telegram Bot->Formula->' + - 'Close Stage->Close Execution->' + 'Close Stage->Close Execution->' + + 'Announcement->Formula->' let tradingSystem = functionLibraryProtocolNode.getProtocolNode(node.payload.referenceParent, false, true, true, false, false, lightingPath) diff --git a/TaskServer/Session.js b/TaskServer/Session.js index f671d62aa..7d0035356 100644 --- a/TaskServer/Session.js +++ b/TaskServer/Session.js @@ -513,17 +513,15 @@ let socialBot = bot.SESSION.socialBots.bots[i] try { let code = announcement.code - if (announcement.referenceParent !== undefined) { - if (socialBot.id === announcement.referenceParent.id) { - 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 -> setUpSocialBots -> announce -> Telegram API error -> err = " + err)) - } else { - socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, code.text).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> setUpSocialBots -> announce -> Telegram API error -> err = " + err)) - } - } + + 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 -> setUpSocialBots -> announce -> Telegram API error -> err = " + err)) + } else { + socialBot.botInstance.telegramAPI.sendMessage(socialBot.botInstance.chatId, code.text).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> setUpSocialBots -> announce -> Telegram API error -> err = " + err)) } } + } catch (err) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> setUpSocialBots -> announce -> err = " + err.stack); } diff --git a/TaskServer/TradingSimulation.js b/TaskServer/TradingSimulation.js index 6ec01a595..2356e2d0b 100644 --- a/TaskServer/TradingSimulation.js +++ b/TaskServer/TradingSimulation.js @@ -1167,6 +1167,8 @@ if (passed) { strategyStage = 'Trigger Stage'; + checkAnnouncements(triggerStage) + currentStrategyIndex = j; currentStrategy.begin = candle.begin; currentStrategy.beginRate = candle.min; @@ -1313,6 +1315,8 @@ type = '"Take Position"'; strategyStage = 'Open Stage'; + checkAnnouncements(strategy.openStage) + stopLossStage = 'Open Stage'; takeProfitStage = 'Open Stage'; stopLossPhase = 0; @@ -1330,7 +1334,7 @@ takePositionNow = true currentTrade.takePositionSituation = situation.name - + checkAnnouncements(triggerStage.takePosition) if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> loop -> Conditions at the Take Position Event were met."); } @@ -1411,7 +1415,10 @@ stopLossPhase++; stopLossStage = 'Manage Stage' - if (takeProfitPhase > 0) { strategyStage = 'Manage Stage' } + if (takeProfitPhase > 0) { + strategyStage = 'Manage Stage' + checkAnnouncements(manageStage, 'Take Profit') + } if (processingDailyFiles) { if (positionedAtYesterday) { @@ -1543,7 +1550,10 @@ takeProfitPhase++; takeProfitStage = 'Manage Stage' - if (stopLossPhase > 0) { strategyStage = 'Manage Stage' } + if (stopLossPhase > 0) { + strategyStage = 'Manage Stage' + checkAnnouncements(manageStage, 'Stop') + } if (processingDailyFiles) { if (positionedAtYesterday) { @@ -1745,6 +1755,8 @@ marketRate = closeRate; type = '"Close@StopLoss"'; strategyStage = 'Close Stage'; + checkAnnouncements(strategy.closeStage, 'Stop') + stopLossStage = 'No Stage'; takeProfitStage = 'No Stage'; currentTrade.end = candle.end; @@ -1814,6 +1826,8 @@ marketRate = closeRate; type = '"Close@TakeProfit"'; strategyStage = 'Close Stage'; + checkAnnouncements(strategy.closeStage, 'Take Profit') + stopLossStage = 'No Stage'; takeProfitStage = 'No Stage'; @@ -2669,7 +2683,7 @@ function checkAnnouncements(node, value) { /* - Value is an optional parameter that represents the value that the announcement is monitoring for change. + 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. */ @@ -2693,18 +2707,20 @@ if (periods > lastPeriodAnnounced) { - /* The Value Variation is what tells us how much the value already announced must change in order to annouce it again. */ - let valueVariation - - let code = announcement.code - valueVariation = code.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 (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 code = announcement.code + valueVariation = code.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 + } } } @@ -2712,6 +2728,7 @@ 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. */ @@ -2739,6 +2756,7 @@ let formulaValue if (announcement.formula !== undefined) { try { + let value = announcement.value formulaValue = eval(announcement.formula.code); } catch (err) { announcement.formula.error = err.message