Trading Engine Module migrated

This commit is contained in:
Luis Fernando Molina committed 2020-12-04 15:14:15 +01:00
1 parent 89a8864486
commit e87d3bf3ce
11 files changed
+42 -42

No files matched your search

@@ -1,4 +1,4 @@
exports.newSuperalgosBotModulesTradingEpisode = function (processIndex, tradingEngineModuleObject) {
exports.newSuperalgosBotModulesTradingEpisode = function (processIndex) {
/*
This module packages all functions related to Episodes.
*/
@@ -83,7 +83,7 @@ exports.newSuperalgosBotModulesTradingEpisode = function (processIndex, tradingE
}
function resetEpisode() {
tradingEngineModuleObject.initializeNode(tradingEngine.current.episode)
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.initializeNode(tradingEngine.current.episode)
}
function updateEnds() {
@@ -1,4 +1,4 @@
exports.newSuperalgosBotModulesTradingExecution = function (processIndex, tradingEngineModuleObject) {
exports.newSuperalgosBotModulesTradingExecution = function (processIndex) {
/*
The Trading Execution modules manages the execution nodes and execution algoritms.
*/
@@ -16,7 +16,7 @@ exports.newSuperalgosBotModulesTradingExecution = function (processIndex, tradin
let tradingSystem
let sessionParameters
let tradingOrdersModuleObject = TS.projects.superalgos.botModules.tradingOrders.newSuperalgosBotModulesTradingOrders(processIndex, tradingEngineModuleObject)
let tradingOrdersModuleObject = TS.projects.superalgos.botModules.tradingOrders.newSuperalgosBotModulesTradingOrders(processIndex)
return thisObject
@@ -1,4 +1,4 @@
exports.newSuperalgosBotModulesTradingOrders = function (processIndex, tradingEngineModule) {
exports.newSuperalgosBotModulesTradingOrders = function (processIndex) {
/*
The Trading Orders modules manages the execution of orders against the exchanges.
*/
@@ -87,7 +87,7 @@ exports.newSuperalgosBotModulesTradingOrders = function (processIndex, tradingEn
if (tradingSystemOrder.referenceParent === undefined) {
badDefinitionUnhandledException(undefined, 'tradingSystemOrder.referenceParent === undefined', tradingSystemOrder)
}
let tradingEngineOrder = tradingEngineModule.getNodeById(tradingSystemOrder.referenceParent.id)
let tradingEngineOrder = TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.getNodeById(tradingSystemOrder.referenceParent.id)
if (tradingEngineOrder.status === undefined) {
badDefinitionUnhandledException(undefined, 'tradingEngineOrder.status === undefined', tradingEngineOrder)
@@ -111,7 +111,7 @@ exports.newSuperalgosBotModulesTradingOrders = function (processIndex, tradingEn
let tradingSystemOrder = orders[i]
tradingSystemValidations(tradingSystemOrder)
let tradingEngineOrder = tradingEngineModule.getNodeById(tradingSystemOrder.referenceParent.id)
let tradingEngineOrder = TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.getNodeById(tradingSystemOrder.referenceParent.id)
tradingEngineValidations(tradingEngineOrder)
switch (tradingEngineOrder.status.value) {
@@ -177,11 +177,11 @@ exports.newSuperalgosBotModulesTradingOrders = function (processIndex, tradingEn
if (tradingEngineOrder.status.value === 'Closed') {
switch (tradingEngineOrder.type) {
case 'Market Order': {
tradingEngineModule.cloneValues(tradingEngineOrder, tradingEngine.last.marketOrders)
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.cloneValues(tradingEngineOrder, tradingEngine.last.marketOrders)
break
}
case 'Limit Order': {
tradingEngineModule.cloneValues(tradingEngineOrder, tradingEngine.last.limitOrders)
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.cloneValues(tradingEngineOrder, tradingEngine.last.limitOrders)
break
}
}
@@ -1040,7 +1040,7 @@ exports.newSuperalgosBotModulesTradingOrders = function (processIndex, tradingEn
function resetTradingEngineDataStructure(tradingEngineOrder, tradingSystemOrder, stageStatus) {
if (tradingEngineOrder.status.value === 'Closed') {
/* We reset the order data structure inside the Trading Engine to its initial value */
tradingEngineModule.initializeNode(tradingEngineOrder)
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.initializeNode(tradingEngineOrder)
if (tradingSystemOrder.config.spawnMultipleOrders !== true) {
/*
We close the lock so as to prevent this data structure to be used again during this same stage execution.
@@ -1,4 +1,4 @@
exports.newSuperalgosBotModulesTradingOutput = function (processIndex, tradingEngineModuleObject) {
exports.newSuperalgosBotModulesTradingOutput = function (processIndex) {
/*
This module will load if necesary all the data outputs so that they can be appended with new
records if needed. After running the simulation, it will save all the data outputs.
@@ -37,7 +37,7 @@ exports.newSuperalgosBotModulesTradingOutput = function (processIndex, tradingEn
}
/* Preparing everything for the Simulation */
let tradingSimulationModuleObject = TS.projects.superalgos.botModules.tradingSimulation.newSuperalgosBotModulesTradingSimulation(processIndex, tradingEngineModuleObject)
let tradingSimulationModuleObject = TS.projects.superalgos.botModules.tradingSimulation.newSuperalgosBotModulesTradingSimulation(processIndex)
let outputDatasets = TS.projects.superalgos.utilities.nodeFunctions.nodeBranchToArray(TS.projects.superalgos.globals.taskConstants.TASK_NODE.bot.processes[processIndex].referenceParent.processOutput, 'Output Dataset')
let outputDatasetsMap = new Map()
@@ -1,4 +1,4 @@
exports.newSuperalgosBotModulesTradingPosition = function (processIndex, tradingEngineModuleObject) {
exports.newSuperalgosBotModulesTradingPosition = function (processIndex) {
/*
This module packages all functions related to Positions.
*/
@@ -103,7 +103,7 @@ exports.newSuperalgosBotModulesTradingPosition = function (processIndex, trading
/*
Now that the position is closed, it is the right time to move this position from current to last at the Trading Engine data structure.
*/
tradingEngineModuleObject.cloneValues(tradingEngine.current.position, tradingEngine.last.position)
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.cloneValues(tradingEngine.current.position, tradingEngine.last.position)
cycleBasedStatistics()
@@ -314,7 +314,7 @@ exports.newSuperalgosBotModulesTradingPosition = function (processIndex, trading
function resetTradingEngineDataStructure() {
if (tradingEngine.current.position.status.value === 'Closed') {
tradingEngineModuleObject.initializeNode(tradingEngine.current.position)
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.initializeNode(tradingEngine.current.position)
}
}
@@ -17,9 +17,9 @@
let dataDependenciesModule;
let dataFiles = new Map();
let multiPeriodDataFiles = new Map();
let fileStorage = TS.projects.superalgos.taskModules.fileStorage.newFileStorage(processIndex);
TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE = TS.projects.superalgos.botModules.tradingEngine.newSuperalgosBotModulesTradingEngine(processIndex)
let tradingOutputModuleObject = TS.projects.superalgos.botModules.tradingOutput.newSuperalgosBotModulesTradingOutput(processIndex, tradingEngineModuleObject)
let fileStorage = TS.projects.superalgos.taskModules.fileStorage.newFileStorage(processIndex)
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT = TS.projects.superalgos.botModules.tradingEngine.newSuperalgosBotModulesTradingEngine(processIndex)
let tradingOutputModuleObject = TS.projects.superalgos.botModules.tradingOutput.newSuperalgosBotModulesTradingOutput(processIndex)
return thisObject;
@@ -34,14 +34,14 @@
} catch (err) {
TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack);
callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_FAIL_RESPONSE);
callBackFunction(TS.projects.superalgos.globals.standardResponses.DEFAULT_FAIL_RESPONSE)
}
}
function finalize() {
tradingEngineModuleObject.finalize()
tradingEngineModuleObject = undefined
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.finalize()
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT = undefined
tradingOutputModuleObject.finalize()
tradingOutputModuleObject = undefined
@@ -77,7 +77,7 @@
}
/* We set up the Trading Engine Module. */
tradingEngineModuleObject.initialize()
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.initialize()
/* Initializing the Trading Process Date */
if (TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_FIRST_LOOP === true && TS.projects.superalgos.globals.processVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).IS_SESSION_RESUMING === false) {
@@ -1,4 +1,4 @@
exports.newSuperalgosBotModulesTradingSimulation = function (processIndex, tradingEngineModuleObject) {
exports.newSuperalgosBotModulesTradingSimulation = function (processIndex) {
/*
This Module represents the trading simulacion. Escentially a loop through a set of candles and
the execution at each loop cycle of the Trading System Protocol.
@@ -37,10 +37,10 @@ exports.newSuperalgosBotModulesTradingSimulation = function (processIndex, tradi
let tradingRecordsModuleObject = TS.projects.superalgos.botModules.tradingRecords.newSuperalgosBotModulesTradingRecords(processIndex)
tradingRecordsModuleObject.initialize(outputDatasetsMap)
let tradingSystemModuleObject = TS.projects.superalgos.botModules.tradingSystem.newSuperalgosBotModulesTradingSystem(processIndex, tradingEngineModuleObject)
let tradingSystemModuleObject = TS.projects.superalgos.botModules.tradingSystem.newSuperalgosBotModulesTradingSystem(processIndex)
tradingSystemModuleObject.initialize()
let tradingEpisodeModuleObject = TS.projects.superalgos.botModules.tradingEpisode.newSuperalgosBotModulesTradingEpisode(processIndex, tradingEngineModuleObject)
let tradingEpisodeModuleObject = TS.projects.superalgos.botModules.tradingEpisode.newSuperalgosBotModulesTradingEpisode(processIndex)
tradingEpisodeModuleObject.initialize()
/* Setting up the candles array: The whole simulation is based on the array of candles at the time-frame defined at the session parameters. */
@@ -108,7 +108,7 @@ exports.newSuperalgosBotModulesTradingSimulation = function (processIndex, tradi
if (FULL_LOG === true) { TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Candle Begin @ ' + (new Date(candle.begin)).toLocaleString()) }
if (FULL_LOG === true) { TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE.write(MODULE_NAME, '[INFO] runSimulation -> loop -> Candle End @ ' + (new Date(candle.end)).toLocaleString()) }
tradingEngineModuleObject.setCurrentCandle(candle) // We move the current candle we are standing at, to the trading engine data structure to make it available to anyone, including conditions and formulas.
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.setCurrentCandle(candle) // We move the current candle we are standing at, to the trading engine data structure to make it available to anyone, including conditions and formulas.
/* We emit a heart beat so that the UI can now where we are at the overal process.*/
heartBeat()
@@ -133,7 +133,7 @@ exports.newSuperalgosBotModulesTradingSimulation = function (processIndex, tradi
*/
tradingSystemModuleObject.mantain()
tradingEpisodeModuleObject.mantain()
tradingEngineModuleObject.mantain()
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.mantain()
/*
Run the first cycle of the Trading System. In this first cycle we
@@ -142,7 +142,7 @@ exports.newSuperalgosBotModulesTradingSimulation = function (processIndex, tradi
orders can not be created, since otherwise the could be cancelled at
the second cycle without spending real time at the order book.
*/
tradingEngineModuleObject.setCurrentCycle('First')
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.setCurrentCycle('First')
await runCycle()
/*
@@ -166,7 +166,7 @@ exports.newSuperalgosBotModulesTradingSimulation = function (processIndex, tradi
without the need to wait until the next candle. Orders can not be cancelled
during the second cycle.
*/
tradingEngineModuleObject.setCurrentCycle('Second')
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.setCurrentCycle('Second')
await runCycle()
checkIfWeNeedToStopAfterBothCycles()
@@ -180,7 +180,7 @@ exports.newSuperalgosBotModulesTradingSimulation = function (processIndex, tradi
/* Reset Data Structures */
tradingSystemModuleObject.reset()
tradingEpisodeModuleObject.reset()
tradingEngineModuleObject.reset()
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.reset()
let infoMessage = 'Processing candle # ' + tradingEngine.current.episode.candle.index.value + ' @ the ' + tradingEngine.current.episode.cycle.value + ' cycle.'
if (FULL_LOG === true) { TS.projects.superalgos.globals.loggerVariables.VARIABLES_BY_PROCESS_INDEX_MAP.get(processIndex).BOT_MAIN_LOOP_LOGGER_MODULE.write(MODULE_NAME, '[INFO] runSimulation -> loop -> ' + infoMessage) }
@@ -1,4 +1,4 @@
exports.newSuperalgosBotModulesTradingStages = function (processIndex, tradingEngineModuleObject) {
exports.newSuperalgosBotModulesTradingStages = function (processIndex) {
/*
This module packages all functions related to Stages.
*/
@@ -21,9 +21,9 @@ exports.newSuperalgosBotModulesTradingStages = function (processIndex, tradingEn
finalize: finalize
}
let tradingStrategyModuleObject = TS.projects.superalgos.botModules.tradingStrategy.newSuperalgosBotModulesTradingStrategy(processIndex, tradingEngineModuleObject)
let tradingPositionModuleObject = TS.projects.superalgos.botModules.tradingPosition.newSuperalgosBotModulesTradingPosition(processIndex, tradingEngineModuleObject)
let tradingExecutionModuleObject = TS.projects.superalgos.botModules.tradingExecution.newSuperalgosBotModulesTradingExecution(processIndex, tradingEngineModuleObject)
let tradingStrategyModuleObject = TS.projects.superalgos.botModules.tradingStrategy.newSuperalgosBotModulesTradingStrategy(processIndex)
let tradingPositionModuleObject = TS.projects.superalgos.botModules.tradingPosition.newSuperalgosBotModulesTradingPosition(processIndex)
let tradingExecutionModuleObject = TS.projects.superalgos.botModules.tradingExecution.newSuperalgosBotModulesTradingExecution(processIndex)
let announcementsModuleObject = TS.projects.superalgos.botModules.announcements.newSuperalgosBotModulesAnnouncements(processIndex)
let snapshotsModuleObject = TS.projects.superalgos.botModules.snapshots.newSuperalgosBotModulesSnapshots(processIndex)
let tradingEpisodeModuleObject = TS.projects.superalgos.botModules.tradingEpisode.newSuperalgosBotModulesTradingEpisode(processIndex)
@@ -282,7 +282,7 @@ exports.newSuperalgosBotModulesTradingStages = function (processIndex, tradingEn
let tradingEngineStage = tradingEngine.current.strategyOpenStage
/* Reset the Exchange Orders data structure to its initial value */
tradingEngineModuleObject.initializeNode(tradingEngine.exchangeOrders)
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.initializeNode(tradingEngine.exchangeOrders)
/* Entry Position size and rate */
tradingSystem.evalFormulas(tradingSystemStage, 'Initial Targets')
@@ -1081,7 +1081,7 @@ exports.newSuperalgosBotModulesTradingStages = function (processIndex, tradingEn
}
function resetStage(stage) {
tradingEngineModuleObject.initializeNode(stage)
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.initializeNode(stage)
}
}
@@ -1,4 +1,4 @@
exports.newSuperalgosBotModulesTradingStrategy = function (processIndex, tradingEngineModuleObject) {
exports.newSuperalgosBotModulesTradingStrategy = function (processIndex) {
/*
This module packages all functions related to Strategies.
*/
@@ -63,7 +63,7 @@ exports.newSuperalgosBotModulesTradingStrategy = function (processIndex, trading
/*
Now that the strategy is closed, it is the right time to move this strategy from current to last at the Trading Engine data structure.
*/
tradingEngineModuleObject.cloneValues(tradingEngine.current.strategy, tradingEngine.last.strategy)
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.cloneValues(tradingEngine.current.strategy, tradingEngine.last.strategy)
}
function updateEnds() {
@@ -75,7 +75,7 @@ exports.newSuperalgosBotModulesTradingStrategy = function (processIndex, trading
function resetTradingEngineDataStructure() {
if (tradingEngine.current.strategy.status.value === 'Closed') {
tradingEngineModuleObject.initializeNode(tradingEngine.current.strategy)
TS.projects.superalgos.globals.processModuleObjects.MODULE_OBJECTS_BY_PROCESS_INDEX_MAP.get(processIndex).TRADING_ENGINE_MODULE_OBJECT.initializeNode(tradingEngine.current.strategy)
}
}
@@ -1,4 +1,4 @@
exports.newSuperalgosBotModulesTradingSystem = function (processIndex, tradingEngineModuleObject) {
exports.newSuperalgosBotModulesTradingSystem = function (processIndex) {
/*
The Trading System is the user defined set of rules compliant with the Trading Protocol that
defines the trading logic to be applied during each cycle of the Simulation.
@@ -19,7 +19,7 @@ exports.newSuperalgosBotModulesTradingSystem = function (processIndex, tradingEn
let sessionParameters
let dynamicIndicators
let tradingStagesModuleObject = TS.projects.superalgos.botModules.tradingStages.newSuperalgosBotModulesTradingStages(processIndex, tradingEngineModuleObject)
let tradingStagesModuleObject = TS.projects.superalgos.botModules.tradingStages.newSuperalgosBotModulesTradingStages(processIndex)
return thisObject
@@ -11,7 +11,7 @@ exports.newSuperalgosGlobalsModuleObjects = function () {
This is a list of possible Module Objects you might find here.
TRADING_ENGINE This is the module that manages the Trading Engine data structure.
TRADING_ENGINE_MODULE_OBJECT This is the module that manages the Trading Engine data structure.
*/
return thisObject
}