Ai Bot Node implemented

This commit is contained in:
Luis Fernando Molina committed 2020-11-03 12:16:59 +01:00
1 parent 3b9dc370b9
commit b2ed704745
30 files changed
+393 -299

No files matched your search

+74 -74
View File
@@ -15,16 +15,16 @@ exports.newTrainingSession = function newTrainingSession(bot, parentLogger) {
function initialize(pProcessConfig, callBackFunction) {
try {
/* Initialize this info so that everything is logged propeerly */
bot.SESSION = {
bot.AI_SESSION = {
name: bot.processNode.session.name,
id: bot.processNode.session.id
}
/* Set the folderName for early logging */
if (bot.processNode.session.config.folderName === undefined) {
bot.SESSION.folderName = bot.processNode.session.type.replace(' ', '-').replace(' ', '-') + '-' + bot.SESSION.id
bot.AI_SESSION.folderName = bot.processNode.session.type.replace(' ', '-').replace(' ', '-') + '-' + bot.AI_SESSION.id
} else {
bot.SESSION.folderName = bot.processNode.session.type.replace(' ', '-').replace(' ', '-') + '-' + bot.processNode.session.config.folderName
bot.AI_SESSION.folderName = bot.processNode.session.type.replace(' ', '-').replace(' ', '-') + '-' + bot.processNode.session.config.folderName
}
/* Check if there is a session */
@@ -79,11 +79,11 @@ exports.newTrainingSession = function newTrainingSession(bot, parentLogger) {
/* We are going to run the Definition comming at the event. */
bot.TRADING_SYSTEM = JSON.parse(message.event.tradingSystem)
bot.TRADING_ENGINE = JSON.parse(message.event.tradingEngine)
bot.SESSION = JSON.parse(message.event.session)
bot.AI_SESSION = JSON.parse(message.event.session)
bot.DEPENDENCY_FILTER = JSON.parse(message.event.dependencyFilter)
bot.RESUME = false
bot.FIRST_EXECUTION = true
bot.SESSION.stop = stopSession // stop function
bot.AI_SESSION.stop = stopSession // stop function
setUpSessionFolderName()
@@ -93,7 +93,7 @@ exports.newTrainingSession = function newTrainingSession(bot, parentLogger) {
socialBotsModule.initialize()
let allGood
switch (bot.SESSION.type) {
switch (bot.AI_SESSION.type) {
case 'Backtesting Session': {
allGood = startBackTesting(message)
break
@@ -119,7 +119,7 @@ exports.newTrainingSession = function newTrainingSession(bot, parentLogger) {
if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, '[IMPORTANT] onSessionRun -> Stopping the Session now. ') }
}
socialBotsModule.sendMessage(bot.SESSION.type + " '" + bot.SESSION.name + "' is starting.")
socialBotsModule.sendMessage(bot.AI_SESSION.type + " '" + bot.AI_SESSION.name + "' is starting.")
} catch (err) {
parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onSessionRun -> err = " + err.stack);
}
@@ -131,7 +131,7 @@ exports.newTrainingSession = function newTrainingSession(bot, parentLogger) {
function onSessionResume(message) {
try {
if (bot.SESSION.stop === undefined) {
if (bot.AI_SESSION.stop === undefined) {
parentLogger.write(MODULE_NAME, "[WARN] onSessionResume -> Event received to resume the Session that have never be ran before. ")
return
}
@@ -145,7 +145,7 @@ exports.newTrainingSession = function newTrainingSession(bot, parentLogger) {
bot.RESUME = true
bot.STOP_SESSION = false
socialBotsModule.sendMessage(bot.SESSION.type + " '" + bot.SESSION.name + "' is resuming.")
socialBotsModule.sendMessage(bot.AI_SESSION.type + " '" + bot.AI_SESSION.name + "' is resuming.")
} catch (err) {
parentLogger.write(MODULE_NAME, "[ERROR] initialize -> onSessionResume -> err = " + err.stack);
}
@@ -153,11 +153,11 @@ exports.newTrainingSession = function newTrainingSession(bot, parentLogger) {
function stopSession(commandOrigin) {
socialBotsModule.sendMessage(bot.SESSION.type + " '" + bot.SESSION.name + "' is stopping " + commandOrigin)
socialBotsModule.sendMessage(bot.AI_SESSION.type + " '" + bot.AI_SESSION.name + "' is stopping " + commandOrigin)
socialBotsModule.finalize()
bot.STOP_SESSION = true
if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, '[IMPORTANT] stopSession -> Stopping the Session now. ') }
sessionInfo(bot.SESSION, commandOrigin)
sessionInfo(bot.AI_SESSION, commandOrigin)
}
function setUpSessionFolderName() {
@@ -167,12 +167,12 @@ exports.newTrainingSession = function newTrainingSession(bot, parentLogger) {
Set the folderName for logging, reports, context and data output
*/
let config
if (bot.SESSION.config !== undefined) {
config = bot.SESSION.config
if (bot.AI_SESSION.config !== undefined) {
config = bot.AI_SESSION.config
if (config.folderName === undefined) {
bot.SESSION.folderName = bot.processNode.session.type.replace(' ', '-').replace(' ', '-') + '-' + bot.SESSION.id
bot.AI_SESSION.folderName = bot.processNode.session.type.replace(' ', '-').replace(' ', '-') + '-' + bot.AI_SESSION.id
} else {
bot.SESSION.folderName = bot.processNode.session.type.replace(' ', '-').replace(' ', '-') + '-' + bot.processNode.session.config.folderName
bot.AI_SESSION.folderName = bot.processNode.session.type.replace(' ', '-').replace(' ', '-') + '-' + bot.processNode.session.config.folderName
}
}
}
@@ -183,16 +183,16 @@ exports.newTrainingSession = function newTrainingSession(bot, parentLogger) {
non critical is missing, we complete it with a default value.
*/
if (bot.SESSION.parameters === undefined) {
if (bot.AI_SESSION.parameters === undefined) {
let errorMessage = "Session Node with no Parameters."
parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage)
bot.sessionError(bot.SESSION, errorMessage)
bot.sessionError(bot.AI_SESSION, errorMessage)
return false
}
/* Time Range */
if (bot.SESSION.parameters.timeRange === undefined) { // if the Time Range is missing we create a default one.
bot.SESSION.parameters.timeRange = {
if (bot.AI_SESSION.parameters.timeRange === undefined) { // if the Time Range is missing we create a default one.
bot.AI_SESSION.parameters.timeRange = {
name: 'Missing Time Range',
type: 'Time Range',
config: {
@@ -202,18 +202,18 @@ exports.newTrainingSession = function newTrainingSession(bot, parentLogger) {
}
} else {
/* Check that we received valid dates */
if (bot.SESSION.type === 'Backtesting Session') {
if (isNaN(new Date(bot.SESSION.parameters.timeRange.config.initialDatetime)).valueOf()) {
if (bot.AI_SESSION.type === 'Backtesting Session') {
if (isNaN(new Date(bot.AI_SESSION.parameters.timeRange.config.initialDatetime)).valueOf()) {
let errorMessage = "sessionParameters.timeRange.config.initialDatetime is not a valid date."
parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage)
bot.sessionError(bot.SESSION.parameters, errorMessage)
bot.sessionError(bot.AI_SESSION.parameters, errorMessage)
return false
}
}
if (isNaN(new Date(bot.SESSION.parameters.timeRange.config.finalDatetime)).valueOf()) {
if (isNaN(new Date(bot.AI_SESSION.parameters.timeRange.config.finalDatetime)).valueOf()) {
let errorMessage = "sessionParameters.timeRange.config.initialDatetime is not a valid date."
parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage)
bot.sessionError(bot.SESSION.parameters, errorMessage)
bot.sessionError(bot.AI_SESSION.parameters, errorMessage)
return false
}
}
@@ -222,7 +222,7 @@ exports.newTrainingSession = function newTrainingSession(bot, parentLogger) {
let today = (new Date()).valueOf()
let aYearAgo = today - global.ONE_YEAR_IN_MILISECONDS
let aYearFromNow = today + global.ONE_YEAR_IN_MILISECONDS
switch (bot.SESSION.type) {
switch (bot.AI_SESSION.type) {
case 'Backtesting Session': {
useDefaultDatetimes(aYearAgo, today)
break
@@ -248,81 +248,81 @@ exports.newTrainingSession = function newTrainingSession(bot, parentLogger) {
*/
/* Initial Datetime */
if (bot.SESSION.type === 'Backtesting Session') {
if (bot.SESSION.parameters.timeRange.config.initialDatetime === undefined) {
bot.SESSION.parameters.timeRange.config.initialDatetime = initialDefault
if (bot.AI_SESSION.type === 'Backtesting Session') {
if (bot.AI_SESSION.parameters.timeRange.config.initialDatetime === undefined) {
bot.AI_SESSION.parameters.timeRange.config.initialDatetime = initialDefault
} else {
bot.SESSION.parameters.timeRange.config.initialDatetime = (new Date(bot.SESSION.parameters.timeRange.config.initialDatetime)).valueOf()
bot.AI_SESSION.parameters.timeRange.config.initialDatetime = (new Date(bot.AI_SESSION.parameters.timeRange.config.initialDatetime)).valueOf()
}
} else {
/* Non backtest session can start from the past only if explicitly configured that way */
if (bot.SESSION.parameters.timeRange.config.initialDatetime === undefined || bot.SESSION.parameters.timeRange.config.allowStartingFromThePast !== true) {
bot.SESSION.parameters.timeRange.config.initialDatetime = initialDefault
if (bot.AI_SESSION.parameters.timeRange.config.initialDatetime === undefined || bot.AI_SESSION.parameters.timeRange.config.allowStartingFromThePast !== true) {
bot.AI_SESSION.parameters.timeRange.config.initialDatetime = initialDefault
} else {
bot.SESSION.parameters.timeRange.config.initialDatetime = (new Date(bot.SESSION.parameters.timeRange.config.initialDatetime)).valueOf()
bot.AI_SESSION.parameters.timeRange.config.initialDatetime = (new Date(bot.AI_SESSION.parameters.timeRange.config.initialDatetime)).valueOf()
}
}
/* Final Datetime */
if (bot.SESSION.parameters.timeRange.config.finalDatetime === undefined) {
bot.SESSION.parameters.timeRange.config.finalDatetime = finalDefault
if (bot.AI_SESSION.parameters.timeRange.config.finalDatetime === undefined) {
bot.AI_SESSION.parameters.timeRange.config.finalDatetime = finalDefault
} else {
bot.SESSION.parameters.timeRange.config.finalDatetime = (new Date(bot.SESSION.parameters.timeRange.config.finalDatetime)).valueOf()
bot.AI_SESSION.parameters.timeRange.config.finalDatetime = (new Date(bot.AI_SESSION.parameters.timeRange.config.finalDatetime)).valueOf()
}
}
/* Time Frame */
if (bot.SESSION.parameters.timeFrame === undefined) {
if (bot.AI_SESSION.parameters.timeFrame === undefined) {
let errorMessage = "Session Parameters Node with no Time Frame."
parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage)
bot.sessionError(bot.SESSION.parameters, errorMessage)
bot.sessionError(bot.AI_SESSION.parameters, errorMessage)
return false
}
if (bot.SESSION.parameters.timeFrame.config.label === undefined) {
if (bot.AI_SESSION.parameters.timeFrame.config.label === undefined) {
let errorMessage = "Session Parameters Node with no Time Frame Label configuration."
parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage)
bot.sessionError(bot.SESSION.parameters.timeFrame, errorMessage)
bot.sessionError(bot.AI_SESSION.parameters.timeFrame, errorMessage)
return false
}
bot.SESSION.parameters.timeFrame.config.value = getTimeFrameFromLabel(bot.SESSION.parameters.timeFrame.config.label)
if (bot.SESSION.parameters.timeFrame.config.value === undefined) {
bot.AI_SESSION.parameters.timeFrame.config.value = getTimeFrameFromLabel(bot.AI_SESSION.parameters.timeFrame.config.label)
if (bot.AI_SESSION.parameters.timeFrame.config.value === undefined) {
let errorMessage = "Config error: label value not recognized. Try 01-min or 01-hs for example."
parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage)
bot.sessionError(bot.SESSION.parameters.timeFrame, errorMessage)
bot.sessionError(bot.AI_SESSION.parameters.timeFrame, errorMessage)
return false
}
/* Session Base Asset */
if (bot.SESSION.parameters.sessionBaseAsset === undefined) {
if (bot.AI_SESSION.parameters.sessionBaseAsset === undefined) {
let errorMessage = "Session Parameters Node with no Session Base Asset."
parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage)
bot.sessionError(bot.SESSION.parameters, errorMessage)
bot.sessionError(bot.AI_SESSION.parameters, errorMessage)
return false
}
if (bot.SESSION.parameters.sessionBaseAsset.config.initialBalance === undefined) {
if (bot.AI_SESSION.parameters.sessionBaseAsset.config.initialBalance === undefined) {
let errorMessage = "Session Parameters Session Base Asset with no initialBalance configuration."
parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage)
bot.sessionError(bot.SESSION.parameters.sessionBaseAsset, errorMessage)
bot.sessionError(bot.AI_SESSION.parameters.sessionBaseAsset, errorMessage)
return false
}
/* Session Quoted Asset */
if (bot.SESSION.parameters.sessionQuotedAsset === undefined) {
if (bot.AI_SESSION.parameters.sessionQuotedAsset === undefined) {
let errorMessage = "Session Parameters Node with no Session Quoted Asset."
parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage)
bot.sessionError(bot.SESSION.parameters, errorMessage)
bot.sessionError(bot.AI_SESSION.parameters, errorMessage)
return false
}
if (bot.SESSION.parameters.sessionQuotedAsset.config.initialBalance === undefined) {
if (bot.AI_SESSION.parameters.sessionQuotedAsset.config.initialBalance === undefined) {
let errorMessage = "Session Parameters Session Quoted Asset with no initialBalance configuration."
parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage)
bot.sessionError(bot.SESSION.parameters.sessionQuotedAsset, errorMessage)
bot.sessionError(bot.AI_SESSION.parameters.sessionQuotedAsset, errorMessage)
return false
}
/* Slippage */
if (bot.SESSION.parameters.slippage === undefined) { // if the Slippage is missing we create a default one.
bot.SESSION.parameters.slippage = {
if (bot.AI_SESSION.parameters.slippage === undefined) { // if the Slippage is missing we create a default one.
bot.AI_SESSION.parameters.slippage = {
name: 'Missing Slippage',
type: 'Slippage',
config: {
@@ -332,19 +332,19 @@ exports.newTrainingSession = function newTrainingSession(bot, parentLogger) {
}
}
}
if (bot.SESSION.parameters.slippage.config.marketOrderRate === undefined) { // if the marketOrderRate is missing we create a default one.
bot.SESSION.parameters.slippage.config.marketOrderRate = 0
if (bot.AI_SESSION.parameters.slippage.config.marketOrderRate === undefined) { // if the marketOrderRate is missing we create a default one.
bot.AI_SESSION.parameters.slippage.config.marketOrderRate = 0
}
if (bot.SESSION.parameters.slippage.config.stopLoss === undefined) { // if the stopLoss is missing we create a default one.
bot.SESSION.parameters.slippage.config.stopLoss = 0
if (bot.AI_SESSION.parameters.slippage.config.stopLoss === undefined) { // if the stopLoss is missing we create a default one.
bot.AI_SESSION.parameters.slippage.config.stopLoss = 0
}
if (bot.SESSION.parameters.slippage.config.takeProfit === undefined) { // if the takeProfit is missing we create a default one.
bot.SESSION.parameters.slippage.config.takeProfit = 0
if (bot.AI_SESSION.parameters.slippage.config.takeProfit === undefined) { // if the takeProfit is missing we create a default one.
bot.AI_SESSION.parameters.slippage.config.takeProfit = 0
}
/* Fee Structure */
if (bot.SESSION.parameters.feeStructure === undefined) { // if the Fee Structure is missing we create a default one.
bot.SESSION.parameters.feeStructure = {
if (bot.AI_SESSION.parameters.feeStructure === undefined) { // if the Fee Structure is missing we create a default one.
bot.AI_SESSION.parameters.feeStructure = {
name: 'Missing Fee Structure',
type: 'Fee Structure',
config: {
@@ -353,11 +353,11 @@ exports.newTrainingSession = function newTrainingSession(bot, parentLogger) {
}
}
}
if (bot.SESSION.parameters.feeStructure.config.maker === undefined) { // if the maker is missing we create a default one.
bot.SESSION.parameters.feeStructure.config.maker = 0
if (bot.AI_SESSION.parameters.feeStructure.config.maker === undefined) { // if the maker is missing we create a default one.
bot.AI_SESSION.parameters.feeStructure.config.maker = 0
}
if (bot.SESSION.parameters.feeStructure.config.taker === undefined) { // if the taker is missing we create a default one.
bot.SESSION.parameters.feeStructure.config.taker = 0
if (bot.AI_SESSION.parameters.feeStructure.config.taker === undefined) { // if the taker is missing we create a default one.
bot.AI_SESSION.parameters.feeStructure.config.taker = 0
}
return true
@@ -371,7 +371,7 @@ exports.newTrainingSession = function newTrainingSession(bot, parentLogger) {
if (bot.KEY === undefined || bot.SECRET === undefined) {
let errorMessage = "Key 'codeName' or 'secret' not provided. Plese check that and try again."
if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> startLiveTrading -> " + errorMessage); }
bot.sessionError(bot.SESSION, errorMessage)
bot.sessionError(bot.AI_SESSION, errorMessage)
return false
}
return true
@@ -387,20 +387,20 @@ exports.newTrainingSession = function newTrainingSession(bot, parentLogger) {
/* Reduce the balance */
let balancePercentage = 1 // This is the default value
if (bot.SESSION.config.balancePercentage !== undefined) {
balancePercentage = bot.SESSION.config.balancePercentage
if (bot.AI_SESSION.config.balancePercentage !== undefined) {
balancePercentage = bot.AI_SESSION.config.balancePercentage
}
bot.SESSION.parameters.sessionBaseAsset.config.initialBalance = bot.SESSION.parameters.sessionBaseAsset.config.initialBalance * balancePercentage / 100
bot.SESSION.parameters.sessionQuotedAsset.config.initialBalance = bot.SESSION.parameters.sessionQuotedAsset.config.initialBalance * balancePercentage / 100
bot.AI_SESSION.parameters.sessionBaseAsset.config.initialBalance = bot.AI_SESSION.parameters.sessionBaseAsset.config.initialBalance * balancePercentage / 100
bot.AI_SESSION.parameters.sessionQuotedAsset.config.initialBalance = bot.AI_SESSION.parameters.sessionQuotedAsset.config.initialBalance * balancePercentage / 100
bot.SESSION.parameters.sessionBaseAsset.config.minimumBalance = bot.SESSION.parameters.sessionBaseAsset.config.minimumBalance * balancePercentage / 100
bot.SESSION.parameters.sessionQuotedAsset.config.minimumBalance = bot.SESSION.parameters.sessionQuotedAsset.config.minimumBalance * balancePercentage / 100
bot.AI_SESSION.parameters.sessionBaseAsset.config.minimumBalance = bot.AI_SESSION.parameters.sessionBaseAsset.config.minimumBalance * balancePercentage / 100
bot.AI_SESSION.parameters.sessionQuotedAsset.config.minimumBalance = bot.AI_SESSION.parameters.sessionQuotedAsset.config.minimumBalance * balancePercentage / 100
bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance = bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance * balancePercentage / 100
bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance = bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance * balancePercentage / 100
bot.AI_SESSION.parameters.sessionBaseAsset.config.maximumBalance = bot.AI_SESSION.parameters.sessionBaseAsset.config.maximumBalance * balancePercentage / 100
bot.AI_SESSION.parameters.sessionQuotedAsset.config.maximumBalance = bot.AI_SESSION.parameters.sessionQuotedAsset.config.maximumBalance * balancePercentage / 100
pProcessConfig.normalWaitTime = bot.SESSION.parameters.timeFrame.config.value
pProcessConfig.normalWaitTime = bot.AI_SESSION.parameters.timeFrame.config.value
return true
}