Beta 6 without Execution
This commit is contained in:
1 parent
4033d4d07f
commit
d40b985f5e
123 files changed
+8114
-12392
No files matched your search
+242
-350
@@ -2,32 +2,29 @@
|
||||
|
||||
const MODULE_NAME = "Session"
|
||||
const FULL_LOG = true;
|
||||
const ONE_YEAR_IN_MILISECONDS = 365 * 24 * 60 * 60 * 1000
|
||||
|
||||
let thisObject = {
|
||||
initialize: initialize
|
||||
}
|
||||
|
||||
const SOCIAL_BOTS_MODULE = require('./SocialBots.js')
|
||||
let socialBotsModule = SOCIAL_BOTS_MODULE.newSocialBots(bot, parentLogger)
|
||||
|
||||
return thisObject;
|
||||
|
||||
function initialize(pProcessConfig, callBackFunction) {
|
||||
try {
|
||||
if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); }
|
||||
|
||||
let telegramAPI
|
||||
|
||||
/* Initialize this info so that everything is logged propeerly */
|
||||
|
||||
bot.SESSION = {
|
||||
name: bot.processNode.session.name,
|
||||
id: bot.processNode.session.id
|
||||
}
|
||||
|
||||
/* Set the folderName for early logging */
|
||||
if (bot.processNode.session.code.folderName === undefined) {
|
||||
if (bot.processNode.session.config.folderName === undefined) {
|
||||
bot.SESSION.folderName = bot.SESSION.id
|
||||
} else {
|
||||
bot.SESSION.folderName = bot.processNode.session.code.folderName + "-" + bot.SESSION.id
|
||||
bot.SESSION.folderName = bot.processNode.session.config.folderName + "-" + bot.SESSION.id
|
||||
}
|
||||
|
||||
/* Check if there is a session */
|
||||
@@ -44,35 +41,35 @@
|
||||
global.EVENT_SERVER_CLIENT.listenToEvent(bot.sessionKey, 'Run Session', undefined, bot.sessionKey, undefined, runSession)
|
||||
global.EVENT_SERVER_CLIENT.listenToEvent(bot.sessionKey, 'Stop Session', undefined, bot.sessionKey, undefined, stopSession)
|
||||
|
||||
/* Errors sent to the UI */
|
||||
bot.sessionError = sessionError
|
||||
|
||||
/* Heartbeats sent to the UI */
|
||||
bot.sessionHeartBeat = sessionHeartBeat
|
||||
|
||||
callBackFunction(global.DEFAULT_OK_RESPONSE)
|
||||
return
|
||||
|
||||
function runSession(message) {
|
||||
|
||||
try {
|
||||
if (bot.SESSION_STATUS === 'Idle' || bot.SESSION_STATUS === 'Running') { return } // This happens when the UI is reloaded, the session was running and tries to run it again.
|
||||
|
||||
/* We are going to run the Definition comming at the event. */
|
||||
bot.APP_SCHEMA_ARRAY = JSON.parse(message.event.appSchema)
|
||||
bot.TRADING_SYSTEM = JSON.parse(message.event.tradingSystem)
|
||||
bot.TRADING_ENGINE = JSON.parse(message.event.tradingEngine)
|
||||
bot.SESSION = JSON.parse(message.event.session)
|
||||
bot.DEPENDENCY_FILTER = JSON.parse(message.event.dependencyFilter)
|
||||
bot.RESUME = message.event.resume
|
||||
bot.FIRST_EXECUTION = true
|
||||
bot.SESSION.stop = stopSession // stop function
|
||||
|
||||
/* Set the folderName for logging, reports, context and data output */
|
||||
let code
|
||||
if (bot.SESSION.code !== undefined) {
|
||||
code = bot.SESSION.code
|
||||
if (code.folderName === undefined) {
|
||||
bot.SESSION.folderName = bot.SESSION.id
|
||||
} else {
|
||||
bot.SESSION.folderName = code.folderName + "-" + bot.SESSION.id
|
||||
}
|
||||
}
|
||||
setUpAppSchema()
|
||||
setUpSessionFolderName()
|
||||
|
||||
/* Extract values from different sources and consolidate them under one structure that is going to be used later on. */
|
||||
setValuesToUse(message)
|
||||
if (checkParemeters() === false) { return }
|
||||
|
||||
/* Set up Social Bots */
|
||||
initializeSocialBots()
|
||||
socialBotsModule.initialize()
|
||||
|
||||
let allGood
|
||||
switch (bot.SESSION.type) {
|
||||
@@ -99,105 +96,253 @@
|
||||
} else {
|
||||
bot.STOP_SESSION = true
|
||||
}
|
||||
|
||||
socialBotsModule.sendMessage(bot.SESSION.type + " '" + bot.SESSION.name + "' is starting.")
|
||||
} catch (err) {
|
||||
parentLogger.write(MODULE_NAME, "[ERROR] initialize -> runSession -> err = " + err.stack);
|
||||
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
|
||||
}
|
||||
}
|
||||
|
||||
function stopSession(message) {
|
||||
function stopSession(commandOrigin) {
|
||||
try {
|
||||
if (commandOrigin === undefined) { commandOrigin = ' from the User Interface.' }
|
||||
socialBotsModule.sendMessage(bot.SESSION.type + " '" + bot.SESSION.name + "' is stopping " + commandOrigin)
|
||||
socialBotsModule.finalize()
|
||||
bot.STOP_SESSION = true
|
||||
finalizeSocialBots()
|
||||
} catch (err) {
|
||||
parentLogger.write(MODULE_NAME, "[ERROR] initialize -> stopSession -> err = " + err.stack);
|
||||
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
|
||||
}
|
||||
}
|
||||
|
||||
function checkDatetimes() {
|
||||
if (bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf() < (new Date()).valueOf()) {
|
||||
if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> checkDatetimes -> Overriding initialDatetime with present datetime because " + bot.VALUES_TO_USE.timeRange.initialDatetime + " is in the past."); }
|
||||
bot.VALUES_TO_USE.timeRange.initialDatetime = new Date()
|
||||
function setUpAppSchema() {
|
||||
/* Setup the APP_SCHEMA_MAP based on the APP_SCHEMA_ARRAY */
|
||||
bot.APP_SCHEMA_MAP = new Map()
|
||||
for (let i = 0; i < bot.APP_SCHEMA_ARRAY.length; i++) {
|
||||
let nodeDefinition = bot.APP_SCHEMA_ARRAY[i]
|
||||
let key = nodeDefinition.type
|
||||
bot.APP_SCHEMA_MAP.set(key, nodeDefinition)
|
||||
}
|
||||
if (bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf() < (new Date()).valueOf()) {
|
||||
if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> checkDatetimes -> Overriding finalDatetime with present datetime plus one year because " + bot.VALUES_TO_USE.timeRange.finalDatetime + " is in the past."); }
|
||||
bot.VALUES_TO_USE.timeRange.finalDatetime = new Date() + ONE_YEAR_IN_MILISECONDS
|
||||
}
|
||||
|
||||
function setUpSessionFolderName() {
|
||||
/* Set the folderName for logging, reports, context and data output */
|
||||
let config
|
||||
if (bot.SESSION.config !== undefined) {
|
||||
config = bot.SESSION.config
|
||||
if (config.folderName === undefined) {
|
||||
bot.SESSION.folderName = bot.SESSION.id
|
||||
} else {
|
||||
bot.SESSION.folderName = config.folderName + "-" + bot.SESSION.id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function checkParemeters() {
|
||||
/*
|
||||
Here we check all the Session Parameters received. If something critical is missing we abort returning false. If something
|
||||
non critical is missing, we complete it with a default value.
|
||||
*/
|
||||
|
||||
if (bot.SESSION.parameters === undefined) {
|
||||
let errorMessage = "Session Node with no Parameters."
|
||||
parentLogger.write(MODULE_NAME, "[ERROR] initialize -> checkParemeters -> " + errorMessage)
|
||||
bot.sessionError(bot.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 = {
|
||||
name: 'Missing Time Range',
|
||||
type: 'Time Range',
|
||||
config: {
|
||||
initialDatetime: (new Date()).valueOf(),
|
||||
finalDatetime: (new Date()).valueOf()
|
||||
}
|
||||
}
|
||||
}
|
||||
if (bot.SESSION.parameters.timeRange.config.initialDatetime === undefined) { // if the initialDatetime is missing we create a default one.
|
||||
bot.SESSION.parameters.timeRange.config.initialDatetime = (new Date()).valueOf()
|
||||
} else {
|
||||
bot.SESSION.parameters.timeRange.config.initialDatetime = (new Date(bot.SESSION.parameters.timeRange.config.initialDatetime)).valueOf()
|
||||
}
|
||||
if (bot.SESSION.parameters.timeRange.config.finalDatetime === undefined) { // if the finalDatetime is missing we create a default one.
|
||||
bot.SESSION.parameters.timeRange.config.finalDatetime = (new Date()).valueOf()
|
||||
} else {
|
||||
bot.SESSION.parameters.timeRange.config.finalDatetime = (new Date(bot.SESSION.parameters.timeRange.config.finalDatetime)).valueOf()
|
||||
}
|
||||
/* Session Type Forced Values */
|
||||
switch (bot.SESSION.type) {
|
||||
case 'Live Trading Session': {
|
||||
if (bot.SESSION.parameters.timeRange.config.finalDatetime.valueOf() > (new Date()).valueOf()) {
|
||||
bot.SESSION.parameters.timeRange.config.finalDatetime = new Date()
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'Live Trading Session': {
|
||||
bot.SESSION.parameters.timeRange.config.initialDatetime = new Date()
|
||||
bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + global.ONE_YEAR_IN_MILISECONDS)
|
||||
break
|
||||
}
|
||||
case 'Fordward Testing Session': {
|
||||
bot.SESSION.parameters.timeRange.config.initialDatetime = new Date()
|
||||
bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + global.ONE_YEAR_IN_MILISECONDS)
|
||||
break
|
||||
}
|
||||
case 'Paper Trading Session': {
|
||||
bot.SESSION.parameters.timeRange.config.initialDatetime = new Date()
|
||||
bot.SESSION.parameters.timeRange.config.finalDatetime = new Date((new Date()).valueOf() + global.ONE_YEAR_IN_MILISECONDS)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
/* Time Frame */
|
||||
if (bot.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)
|
||||
return false
|
||||
}
|
||||
if (bot.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)
|
||||
return false
|
||||
}
|
||||
bot.SESSION.parameters.timeFrame.config.value = getTimeFrameFromLabel(bot.SESSION.parameters.timeFrame.config.label)
|
||||
|
||||
/* Session Base Asset */
|
||||
if (bot.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)
|
||||
return false
|
||||
}
|
||||
if (bot.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)
|
||||
return false
|
||||
}
|
||||
if (bot.SESSION.parameters.sessionBaseAsset.config.minimumBalance === undefined) {
|
||||
bot.SESSION.parameters.sessionBaseAsset.config.minimumBalance = 0
|
||||
}
|
||||
if (bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance === undefined) {
|
||||
bot.SESSION.parameters.sessionBaseAsset.config.maximumBalance = Number.MAX_SAFE_INTEGER
|
||||
}
|
||||
|
||||
/* Session Quoted Asset */
|
||||
if (bot.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)
|
||||
return false
|
||||
}
|
||||
if (bot.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)
|
||||
return false
|
||||
}
|
||||
if (bot.SESSION.parameters.sessionQuotedAsset.config.minimumBalance === undefined) {
|
||||
bot.SESSION.parameters.sessionQuotedAsset.config.minimumBalance = 0
|
||||
}
|
||||
if (bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance === undefined) {
|
||||
bot.SESSION.parameters.sessionQuotedAsset.config.maximumBalance = Number.MAX_SAFE_INTEGER
|
||||
}
|
||||
|
||||
/* Slippage */
|
||||
if (bot.SESSION.parameters.slippage === undefined) { // if the Slippage is missing we create a default one.
|
||||
bot.SESSION.parameters.timeRange = {
|
||||
name: 'Missing Slippage',
|
||||
type: 'Slippage',
|
||||
config: {
|
||||
positionRate: 0,
|
||||
stopLoss: 0,
|
||||
takeProfit: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
if (bot.SESSION.parameters.slippage.config.positionRate === undefined) { // if the positionRate is missing we create a default one.
|
||||
bot.SESSION.parameters.slippage.config.positionRate = 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.SESSION.parameters.slippage.config.takeProfit === undefined) { // if the takeProfit is missing we create a default one.
|
||||
bot.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 = {
|
||||
name: 'Missing Fee Structure',
|
||||
type: 'Fee Structure',
|
||||
config: {
|
||||
maker: 0,
|
||||
taker: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
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.SESSION.parameters.feeStructure.config.taker === undefined) { // if the taker is missing we create a default one.
|
||||
bot.SESSION.parameters.feeStructure.config.taker = 0
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
function startBackTesting(message) {
|
||||
bot.startMode = "Backtest"
|
||||
if (bot.VALUES_TO_USE.timeRange.finalDatetime.valueOf() > (new Date()).valueOf()) {
|
||||
bot.VALUES_TO_USE.timeRange.finalDatetime = new Date()
|
||||
}
|
||||
bot.resumeExecution = false;
|
||||
bot.hasTheBotJustStarted = true
|
||||
bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf())
|
||||
bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf()) // Here we initialize the date of the process.
|
||||
return true
|
||||
}
|
||||
|
||||
function startLiveTrading(message) {
|
||||
|
||||
if (process.env.KEY === undefined || process.env.SECRET === undefined) {
|
||||
if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> startLiveTrading -> Key name or Secret not provided, not possible to run the process in Live mode."); }
|
||||
console.log("Key 'codeName' or 'secret' not provided. Plese check that and try again.")
|
||||
return
|
||||
if (process.env.KEY === undefined || process.env.SECRET === undefined) { // TODO Remove this from Process .env
|
||||
if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[ERROR] initialize -> startLiveTrading -> Key 'codeName' or 'secret' not provided. Plese check that and try again."); }
|
||||
return false
|
||||
}
|
||||
|
||||
bot.startMode = "Live"
|
||||
checkDatetimes()
|
||||
bot.resumeExecution = false;
|
||||
bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf())
|
||||
bot.hasTheBotJustStarted = true
|
||||
pProcessConfig.liveWaitTime = getTimeFrameFromLabel(bot.VALUES_TO_USE.timeFrame)
|
||||
bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf())
|
||||
pProcessConfig.liveWaitTime = sessionParameters.timeFrame.config.value
|
||||
return true
|
||||
}
|
||||
|
||||
function startFordwardTesting(message) {
|
||||
|
||||
if (process.env.KEY === undefined || process.env.SECRET === undefined) {
|
||||
if (FULL_LOG === true) { parentLogger.write(MODULE_NAME, "[WARN] initialize -> startFordwardTesting -> Key name or Secret not provided, not possible to run the process in Forward Testing mode."); }
|
||||
console.log("Key 'codeName' or 'secret' not provided. Plese check that and try again.")
|
||||
return
|
||||
return false
|
||||
}
|
||||
|
||||
bot.startMode = "Live"
|
||||
checkDatetimes()
|
||||
bot.resumeExecution = false;
|
||||
bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf())
|
||||
bot.hasTheBotJustStarted = true
|
||||
bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf())
|
||||
|
||||
/* Reduce the balance */
|
||||
|
||||
let balancePercentage = 1 // This is the default value
|
||||
|
||||
if (bot.SESSION.code.balancePercentage !== undefined) {
|
||||
balancePercentage = bot.SESSION.code.balancePercentage
|
||||
if (bot.SESSION.config.balancePercentage !== undefined) {
|
||||
balancePercentage = bot.SESSION.config.balancePercentage
|
||||
}
|
||||
|
||||
bot.VALUES_TO_USE.initialBalanceA = bot.VALUES_TO_USE.initialBalanceA * balancePercentage / 100
|
||||
bot.VALUES_TO_USE.initialBalanceB = bot.VALUES_TO_USE.initialBalanceB * balancePercentage / 100
|
||||
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.VALUES_TO_USE.minimumBalanceA = bot.VALUES_TO_USE.minimumBalanceA * balancePercentage / 100
|
||||
bot.VALUES_TO_USE.minimumBalanceB = bot.VALUES_TO_USE.minimumBalanceB * 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.VALUES_TO_USE.maximumBalanceA = bot.VALUES_TO_USE.maximumBalanceA * balancePercentage / 100
|
||||
bot.VALUES_TO_USE.maximumBalanceB = bot.VALUES_TO_USE.maximumBalanceB * 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
|
||||
|
||||
pProcessConfig.normalWaitTime = getTimeFrameFromLabel(bot.VALUES_TO_USE.timeFrame)
|
||||
pProcessConfig.normalWaitTime = sessionParameters.timeFrame.config.value
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
function startPaperTrading(message) {
|
||||
bot.startMode = "Paper"
|
||||
|
||||
checkDatetimes()
|
||||
bot.resumeExecution = false;
|
||||
bot.hasTheBotJustStarted = true
|
||||
bot.multiPeriodProcessDatetime = new Date(bot.VALUES_TO_USE.timeRange.initialDatetime.valueOf())
|
||||
pProcessConfig.normalWaitTime = getTimeFrameFromLabel(bot.VALUES_TO_USE.timeFrame)
|
||||
bot.tradingProcessDate = new Date(bot.SESSION.parameters.timeRange.config.initialDatetime.valueOf())
|
||||
pProcessConfig.normalWaitTime = sessionParameters.timeFrame.config.value
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -222,290 +367,37 @@
|
||||
}
|
||||
}
|
||||
|
||||
function setValuesToUse(message) {
|
||||
/*
|
||||
Base on the information received we will determined which values ultimatelly are going to be used,
|
||||
and once we do, they will become constants across the multiple loops executions.
|
||||
*/
|
||||
|
||||
/* Set all default values */
|
||||
bot.VALUES_TO_USE = {
|
||||
slippage: {
|
||||
positionRate: 0,
|
||||
stopLoss: 0,
|
||||
takeProfit: 0
|
||||
},
|
||||
feeStructure: {
|
||||
maker: 0,
|
||||
taker: 0
|
||||
},
|
||||
timeRange: {
|
||||
initialDatetime: new Date(),
|
||||
finalDatetime: new Date()
|
||||
}
|
||||
function sessionHeartBeat(processingDate) {
|
||||
let event = {
|
||||
seconds: (new Date()).getSeconds(),
|
||||
processingDate: processingDate
|
||||
}
|
||||
global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Heartbeat', event)
|
||||
|
||||
/* Session Type Dependant Default Values */
|
||||
|
||||
switch (bot.SESSION.type) {
|
||||
case 'Backtesting Session': {
|
||||
bot.VALUES_TO_USE.timeRange.initialDatetime = new Date((new Date()).valueOf() - ONE_YEAR_IN_MILISECONDS)
|
||||
bot.VALUES_TO_USE.timeRange.finalDatetime = new Date()
|
||||
break
|
||||
}
|
||||
case 'Live Trading Session': {
|
||||
bot.VALUES_TO_USE.timeRange.initialDatetime = new Date()
|
||||
bot.VALUES_TO_USE.timeRange.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS)
|
||||
break
|
||||
}
|
||||
case 'Fordward Testing Session': {
|
||||
bot.VALUES_TO_USE.timeRange.initialDatetime = new Date()
|
||||
bot.VALUES_TO_USE.timeRange.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS)
|
||||
break
|
||||
}
|
||||
case 'Paper Trading Session': {
|
||||
bot.VALUES_TO_USE.timeRange.initialDatetime = new Date()
|
||||
bot.VALUES_TO_USE.timeRange.finalDatetime = new Date((new Date()).valueOf() + ONE_YEAR_IN_MILISECONDS)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
let tradingSystem = bot.TRADING_SYSTEM
|
||||
|
||||
if (tradingSystem !== undefined) {
|
||||
|
||||
/* Applying Session Level Parameters */
|
||||
if (bot.SESSION !== undefined) {
|
||||
if (bot.SESSION.parameters !== undefined) {
|
||||
|
||||
/* Base Asset and Initial Balances. */
|
||||
{
|
||||
if (bot.SESSION.parameters.baseAsset !== undefined) {
|
||||
if (bot.SESSION.parameters.baseAsset.referenceParent !== undefined) {
|
||||
if (bot.SESSION.parameters.baseAsset.referenceParent.referenceParent !== undefined) {
|
||||
|
||||
let code = bot.SESSION.parameters.baseAsset.referenceParent.referenceParent.code
|
||||
|
||||
if (code.codeName !== undefined) {
|
||||
bot.VALUES_TO_USE.baseAsset = code.codeName;
|
||||
}
|
||||
|
||||
code = bot.SESSION.parameters.baseAsset.code
|
||||
|
||||
if (bot.VALUES_TO_USE.baseAsset === bot.market.baseAsset) {
|
||||
if (code.initialBalance !== undefined) {
|
||||
bot.VALUES_TO_USE.initialBalanceA = code.initialBalance;
|
||||
bot.VALUES_TO_USE.initialBalanceB = 0
|
||||
}
|
||||
if (code.minimumBalance !== undefined) {
|
||||
bot.VALUES_TO_USE.minimumBalanceA = code.minimumBalance;
|
||||
bot.VALUES_TO_USE.minimumBalanceB = 0
|
||||
}
|
||||
if (code.maximumBalance !== undefined) {
|
||||
bot.VALUES_TO_USE.maximumBalanceA = code.maximumBalance;
|
||||
bot.VALUES_TO_USE.maximumBalanceB = 0
|
||||
}
|
||||
} else {
|
||||
if (code.initialBalance !== undefined) {
|
||||
bot.VALUES_TO_USE.initialBalanceB = code.initialBalance;
|
||||
bot.VALUES_TO_USE.initialBalanceA = 0
|
||||
}
|
||||
if (code.minimumBalance !== undefined) {
|
||||
bot.VALUES_TO_USE.minimumBalanceB = code.minimumBalance;
|
||||
bot.VALUES_TO_USE.minimumBalanceA = 0
|
||||
}
|
||||
if (code.maximumBalance !== undefined) {
|
||||
bot.VALUES_TO_USE.maximumBalanceB = code.maximumBalance;
|
||||
bot.VALUES_TO_USE.maximumBalanceA = 0
|
||||
}
|
||||
}
|
||||
|
||||
/* Solve when values are missing. */
|
||||
if (bot.VALUES_TO_USE.minimumBalanceA === undefined) {
|
||||
bot.VALUES_TO_USE.minimumBalanceA = 0
|
||||
}
|
||||
|
||||
if (bot.VALUES_TO_USE.minimumBalanceB === undefined) {
|
||||
bot.VALUES_TO_USE.minimumBalanceB = 0
|
||||
}
|
||||
|
||||
if (bot.VALUES_TO_USE.maximumBalanceA === undefined) {
|
||||
bot.VALUES_TO_USE.maximumBalanceA = 10000000000000000
|
||||
}
|
||||
|
||||
if (bot.VALUES_TO_USE.maximumBalanceB === undefined) {
|
||||
bot.VALUES_TO_USE.maximumBalanceB = 10000000000000000
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Quoted Asset. */
|
||||
{
|
||||
if (bot.SESSION.parameters.quotedAsset !== undefined) {
|
||||
if (bot.SESSION.parameters.quotedAsset.referenceParent !== undefined) {
|
||||
if (bot.SESSION.parameters.quotedAsset.referenceParent.referenceParent !== undefined) {
|
||||
|
||||
let code = bot.SESSION.parameters.quotedAsset.referenceParent.referenceParent.code
|
||||
|
||||
if (code.codeName !== undefined) {
|
||||
bot.VALUES_TO_USE.quotedAsset = code.codeName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Time Frame */
|
||||
if (bot.SESSION.parameters.timeFrame !== undefined) {
|
||||
bot.VALUES_TO_USE.timeFrame = bot.SESSION.parameters.timeFrame.code.value
|
||||
}
|
||||
|
||||
/* Slippage */
|
||||
if (bot.SESSION.parameters.slippage !== undefined) {
|
||||
if (bot.SESSION.parameters.slippage.code !== undefined) {
|
||||
|
||||
let code = bot.SESSION.parameters.slippage.code
|
||||
|
||||
if (code.positionRate !== undefined) {
|
||||
bot.VALUES_TO_USE.slippage.positionRate = code.positionRate
|
||||
}
|
||||
if (code.stopLoss !== undefined) {
|
||||
bot.VALUES_TO_USE.slippage.stopLoss = code.stopLoss
|
||||
}
|
||||
if (code.takeProfit !== undefined) {
|
||||
bot.VALUES_TO_USE.slippage.takeProfit = code.takeProfit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Fee Structure */
|
||||
if (bot.SESSION.parameters.feeStructure !== undefined) {
|
||||
if (bot.SESSION.parameters.feeStructure.code !== undefined) {
|
||||
|
||||
let code = bot.SESSION.parameters.feeStructure.code
|
||||
|
||||
if (code.maker !== undefined) {
|
||||
bot.VALUES_TO_USE.feeStructure.maker = code.maker
|
||||
}
|
||||
if (code.taker !== undefined) {
|
||||
bot.VALUES_TO_USE.feeStructure.taker = code.taker
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Time Range */
|
||||
if (bot.SESSION.parameters.timeRange !== undefined) {
|
||||
if (bot.SESSION.parameters.timeRange.code !== undefined) {
|
||||
|
||||
let code = bot.SESSION.parameters.timeRange.code
|
||||
if (code.initialDatetime !== undefined) {
|
||||
if (isNaN(Date.parse(code.initialDatetime)) === true) {
|
||||
parentLogger.write(MODULE_NAME, "[WARN] initialize -> runSession -> setValuesToUse -> Cannot use initialDatatime provided at Session Parameters because it is not a valid Date.");
|
||||
} else {
|
||||
bot.VALUES_TO_USE.timeRange.initialDatetime = new Date(code.initialDatetime)
|
||||
}
|
||||
}
|
||||
if (code.finalDatetime !== undefined) {
|
||||
if (isNaN(Date.parse(code.finalDatetime)) === true) {
|
||||
parentLogger.write(MODULE_NAME, "[WARN] initialize -> runSession -> setValuesToUse -> Cannot use finalDatetime provided at Session Parameters because it is not a valid Date.");
|
||||
} else {
|
||||
bot.VALUES_TO_USE.timeRange.finalDatetime = new Date(code.finalDatetime)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (global.STOP_TASK_GRACEFULLY === true) {
|
||||
bot.STOP_SESSION = true
|
||||
}
|
||||
}
|
||||
|
||||
function initializeSocialBots() {
|
||||
|
||||
if (bot.SESSION.socialBots !== undefined) {
|
||||
if (bot.SESSION.socialBots.bots !== undefined) {
|
||||
for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) {
|
||||
let socialBot = bot.SESSION.socialBots.bots[i]
|
||||
if (socialBot.type === "Telegram Bot") {
|
||||
let code = socialBot.code
|
||||
socialBot.botInstance = initializeTelegramBot(code.botToken, code.chatId)
|
||||
}
|
||||
}
|
||||
function sessionError(node, errorMessage) {
|
||||
let event
|
||||
if (node !== undefined) {
|
||||
event = {
|
||||
nodeName: node.name,
|
||||
nodeType: node.type,
|
||||
nodeId: node.id,
|
||||
errorMessage: errorMessage
|
||||
}
|
||||
|
||||
function announce(announcement) {
|
||||
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 code = announcement.code
|
||||
|
||||
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, code.text).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> Telegram API error -> err = " + err))
|
||||
}
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeSocialBots -> announce -> err = " + err.stack);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bot.SESSION.socialBots.announce = announce
|
||||
}
|
||||
}
|
||||
|
||||
function finalizeSocialBots() {
|
||||
if (bot.SESSION.socialBots === undefined) { return }
|
||||
if (bot.SESSION.socialBots.bots !== undefined) {
|
||||
for (let i = 0; i < bot.SESSION.socialBots.bots.length; i++) {
|
||||
let socialBot = bot.SESSION.socialBots.bots[i]
|
||||
if (socialBot.type === "Telegram Bot") {
|
||||
let code = socialBot.code
|
||||
socialBot.botInstance = finalizeTelegramBot(code.chatId)
|
||||
}
|
||||
} else {
|
||||
event = {
|
||||
errorMessage: errorMessage
|
||||
}
|
||||
}
|
||||
}
|
||||
global.EVENT_SERVER_CLIENT.raiseEvent(bot.sessionKey, 'Error', event)
|
||||
|
||||
function initializeTelegramBot(botToken, chatId) {
|
||||
/* Telegram Bot Initialization */
|
||||
try {
|
||||
const Telegraf = require('telegraf')
|
||||
let telegramBot
|
||||
telegramBot = new Telegraf(botToken)
|
||||
telegramBot.start((ctx) => ctx.reply('Hi! I am the Telegram Bot that will tell you what happens with your trading session.'))
|
||||
telegramBot.help((ctx) => ctx.reply('I can stop the session if you say STOP.'))
|
||||
telegramBot.hears('STOP', (ctx) => stopSession())
|
||||
telegramBot.launch()
|
||||
|
||||
const Telegram = require('telegraf/telegram')
|
||||
|
||||
telegramAPI = new Telegram(botToken)
|
||||
|
||||
const messge = bot.SESSION.type + " '" + bot.SESSION.name + "' was started with an initial balance of " + " " + bot.VALUES_TO_USE.initialBalanceA + " " + bot.VALUES_TO_USE.baseAsset + "."
|
||||
telegramAPI.sendMessage(chatId, messge).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeTelegramBot -> Telegram API error -> err = " + err))
|
||||
|
||||
let botInstance = {
|
||||
telegramBot: telegramBot,
|
||||
telegramAPI: telegramAPI,
|
||||
chatId: chatId
|
||||
}
|
||||
return botInstance
|
||||
} catch (err) {
|
||||
parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeTelegramBot -> err = " + err.stack);
|
||||
if (global.STOP_TASK_GRACEFULLY === true) {
|
||||
bot.STOP_SESSION = true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function finalizeTelegramBot(chatId) {
|
||||
const messge = bot.SESSION.type + " '" + bot.SESSION.name + "' was stopped."
|
||||
telegramAPI.sendMessage(chatId, messge).catch(err => parentLogger.write(MODULE_NAME, "[WARN] initialize -> initializeTelegramBot -> Telegram API error -> err = " + err))
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
|
||||
Reference in new issue
Block a user