Polishing
This commit is contained in:
1 parent
569a1a129d
commit
61fb1bb053
10 files changed
+212
-90
No files matched your search
+14
-2
@@ -6656,6 +6656,18 @@
|
||||
"type": "node",
|
||||
"childType": "Strategy",
|
||||
"autoAdd": true
|
||||
},
|
||||
{
|
||||
"name": "limitOrders",
|
||||
"type": "node",
|
||||
"childType": "Limit Order",
|
||||
"autoAdd": true
|
||||
},
|
||||
{
|
||||
"name": "marketOrders",
|
||||
"type": "node",
|
||||
"childType": "Market Order",
|
||||
"autoAdd": true
|
||||
}
|
||||
]
|
||||
},
|
||||
@@ -6907,7 +6919,7 @@
|
||||
"addLeftIcons": true,
|
||||
"level": 3,
|
||||
"chainAttachesTo": {
|
||||
"compatibleTypes": "->Market Buy Orders->Market Sell Orders->"
|
||||
"compatibleTypes": "->Market Buy Orders->Market Sell Orders->Last->"
|
||||
},
|
||||
"icon": "market-order",
|
||||
"propertyNameAtParent": "marketOrders",
|
||||
@@ -7137,7 +7149,7 @@
|
||||
"addLeftIcons": true,
|
||||
"level": 3,
|
||||
"chainAttachesTo": {
|
||||
"compatibleTypes": "->Limit Buy Orders->Limit Sell Orders->"
|
||||
"compatibleTypes": "->Limit Buy Orders->Limit Sell Orders->Last->"
|
||||
},
|
||||
"icon": "menu-mobility-unfreeze",
|
||||
"propertyNameAtParent": "limitOrders",
|
||||
|
||||
@@ -215,6 +215,9 @@ function newEventsServerClient(networkNode) {
|
||||
let config = JSON.parse(networkNode.config)
|
||||
host = config.host
|
||||
port = config.webSocketsPort
|
||||
|
||||
/* Check if we really have to stablish the connection. */
|
||||
if (config.autoConnect === false) {return}
|
||||
} catch (err) {
|
||||
networkNode.payload.uiObject.setErrorMessage('Bad configuration. Cannot know the host name or the webSocketsPort.')
|
||||
return
|
||||
|
||||
@@ -23,6 +23,7 @@ function newUiObject() {
|
||||
isRunning: undefined,
|
||||
shortcutKey: undefined,
|
||||
isShowing: undefined,
|
||||
hasError: undefined,
|
||||
run: run,
|
||||
stop: stop,
|
||||
heartBeat: heartBeat,
|
||||
@@ -74,7 +75,6 @@ function newUiObject() {
|
||||
let isRunningAtBackend
|
||||
let runningAtBackendCounter = 0
|
||||
|
||||
let hasError
|
||||
let errorMessageCounter = 0
|
||||
|
||||
let hasValue
|
||||
@@ -690,7 +690,7 @@ function newUiObject() {
|
||||
errorMessageCounter--
|
||||
if (errorMessageCounter < 0) {
|
||||
errorMessageCounter = 0
|
||||
hasError = false
|
||||
thisObject.hasError = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -735,7 +735,7 @@ function newUiObject() {
|
||||
function setErrorMessage(message, duration) {
|
||||
if (message !== undefined) {
|
||||
errorMessage = message
|
||||
hasError = true
|
||||
thisObject.hasError = true
|
||||
if (duration === undefined) { duration = 1 }
|
||||
errorMessageCounter = 100 * duration
|
||||
}
|
||||
@@ -743,7 +743,7 @@ function newUiObject() {
|
||||
|
||||
function resetErrorMessage() {
|
||||
errorMessage = undefined
|
||||
hasError = false
|
||||
thisObject.hasError = false
|
||||
}
|
||||
|
||||
function setValue(value, counter, minDecimals) {
|
||||
@@ -1054,7 +1054,7 @@ function newUiObject() {
|
||||
if (thisObject.isOnFocus === false) {
|
||||
drawReferenceLine()
|
||||
drawChainLine()
|
||||
drawBodyAndPicture()
|
||||
|
||||
if (isDragging === false && thisObject.isOnFocus === true) {
|
||||
thisObject.menu.drawBackground()
|
||||
}
|
||||
@@ -1067,6 +1067,7 @@ function newUiObject() {
|
||||
|
||||
function drawMiddleground() {
|
||||
if (thisObject.isOnFocus === false) {
|
||||
drawBodyAndPicture()
|
||||
drawErrorMessage()
|
||||
drawValue()
|
||||
drawPercentage()
|
||||
@@ -1366,7 +1367,7 @@ function newUiObject() {
|
||||
}
|
||||
|
||||
function drawErrorMessage() {
|
||||
if (hasError === false) { return }
|
||||
if (thisObject.hasError === false) { return }
|
||||
if (canvas.floatingSpace.inMapMode === true) {
|
||||
return
|
||||
}
|
||||
@@ -1712,7 +1713,7 @@ function newUiObject() {
|
||||
browserCanvasContext.fillStyle = 'rgba(' + UI_COLOR.BLACK + ', 0.70)'
|
||||
browserCanvasContext.fill()
|
||||
/* Border when node is in focus */
|
||||
if (hasError !== true && nodeDefinition.isHierarchyHead !== true && thisObject.circularProgressBar === undefined) {
|
||||
if (thisObject.hasError !== true && nodeDefinition.isHierarchyHead !== true && thisObject.circularProgressBar === undefined) {
|
||||
browserCanvasContext.beginPath()
|
||||
browserCanvasContext.arc(visiblePosition.x, visiblePosition.y, VISIBLE_RADIUS, 0, Math.PI * 2, true)
|
||||
browserCanvasContext.closePath()
|
||||
@@ -1732,7 +1733,7 @@ function newUiObject() {
|
||||
}
|
||||
}
|
||||
|
||||
if (hasError === true) {
|
||||
if (thisObject.hasError === true) {
|
||||
VISIBLE_RADIUS = thisObject.container.frame.radius * 1.5
|
||||
if (canvas.floatingSpace.inMapMode === true) {
|
||||
VISIBLE_RADIUS = canvas.floatingSpace.transformRadiusToMap(VISIBLE_RADIUS)
|
||||
|
||||
@@ -53,10 +53,10 @@ function newRecordValues() {
|
||||
The Product Root Node is the root of the node hiriarchy from where we are going to extract the record values.
|
||||
*/
|
||||
let productRootNode = productRoot
|
||||
scanProperties(productRootNode)
|
||||
scanProperties(productDefinition, productRootNode)
|
||||
}
|
||||
|
||||
function scanProperties(productRootNode, index) {
|
||||
function scanProperties(productDefinition, productRootNode, index) {
|
||||
for (let i = 0; i < productDefinition.record.properties.length; i++) {
|
||||
let property = productDefinition.record.properties[i]
|
||||
let key = property.id
|
||||
@@ -64,7 +64,17 @@ function newRecordValues() {
|
||||
key = property.id + '-' + index
|
||||
}
|
||||
if (property.config.nodePath !== undefined) {
|
||||
let propertyRoot = eval(property.config.nodePath)
|
||||
let propertyRoot
|
||||
try {
|
||||
propertyRoot = eval(property.config.nodePath)
|
||||
} catch(err) {
|
||||
console.log('Definition Error at Product ' + productDefinition.name)
|
||||
console.log('productDefinition.config = ' + JSON.stringify(productDefinition.config))
|
||||
console.log('Definition Error at Property ' + property.name)
|
||||
console.log('property.config = ' + JSON.stringify(property.config))
|
||||
console.log('property.config.nodePath = ' + property.config.nodePath)
|
||||
return
|
||||
}
|
||||
propertyTargetNodeMap.set(key, propertyRoot)
|
||||
} else {
|
||||
propertyTargetNodeMap.set(key, productRootNode)
|
||||
|
||||
@@ -1 +1 @@
|
||||
{"type":"Data Mine","name":"Masters-Trading","config":"{ \n\"codeName\": \"Masters-Trading\"\n}","sensorBots":[],"indicatorBots":[],"tradingBots":[{"type":"Trading Bot","name":"Low Frequency","config":"{\n\"codeName\": \"Low-Frequency\"\n}","processes":[{"type":"Process Definition","name":"Trading","config":"{\n \"codeName\": \"Trading\",\n \"sessionRunWaitTime\": 5000,\n \"retryWaitTime\": 10000,\n \"framework\": {\n \"name\": \"Low-Frequency-Trading-Process\"\n }\n }","processOutput":{"type":"Process Output","name":"New Process Output","outputDatasets":[{"type":"Output Dataset","name":"Multi-Period-Market Episode Counters","id":"6fc3054e-5034-4fc7-bddf-02360e0a6acf","savedPayload":{"position":{"x":15126.709841795062,"y":14115.844903867037},"targetPosition":{"x":19338.91372893721,"y":12256.290080293493},"floatingObject":{"isPinned":false,"isFrozen":false,"isCollapsed":false,"angleToParent":2,"distanceToParent":3,"arrangementStyle":0},"uiObject":{},"referenceParent":{"type":"Dataset Definition","name":"Multi-Period-Market","id":"db063d7b-db05-4bf6-941f-999e1ccb38fc"}}},{"type":"Output Dataset","name":"Multi-Period-Daily Episode Counters","id":"c3c2240f-514b-40de-825f-674037117d0b","savedPayload":{"position":{"x":15060.50960648652,"y":13958.543096746436},"targetPosition":{"x":19338.91372893721,"y":12256.290080293493},"floatingObject":{"isPinned":false,"isFrozen":false,"isCollapsed":false,"angleToParent":2,"distanceToParent":3,"arrangementStyle":0},"uiObject":{},"referenceParent":{"type":"Dataset Definition","name":"Multi-Period-Daily","id":"0764aaaf-d970-4f23-9df8-55fe5b7dd273"}}},{"type":"Output Dataset","name":"Multi-Period-Market Episode Statistics","id":"1dfdfc0d-842f-4e63-a591-256ac8a06b0f","savedPayload":{"position":{"x":15000.170158581612,"y":13798.909615340754},"targetPosition":{"x":19338.91372893721,"y":12256.290080293493},"floatingObject":{"isPinned":false,"isFrozen":false,"isCollapsed":false,"angleToParent":2,"distanceToParent":3,"arrangementStyle":0},"uiObject":{},"referenceParent":{"type":"Dataset Definition","name":"Multi-Period-Market","id":"fc9dc0f7-35e0-4548-bb91-f920825d694f"}}},{"type":"Output Dataset","name":"Multi-Period-Daily Episode Statistics","id":"39360c55-5ba8-413b-b0bd-cac3789ebf5f","savedPayload":{"position":{"x":14945.771166150153,"y":13637.156840896287},"targetPosition":{"x":19338.91372893721,"y":12256.290080293493},"floatingObject":{"isPinned":false,"isFrozen":false,"isCollapsed":false,"angleToParent":2,"distanceToParent":3,"arrangementStyle":0},"uiObject":{},"referenceParent":{"type":"Dataset Definition","name":"Multi-Period-Daily","id":"91953362-8751-4440-a898-21245cd47c15"}}},{"type":"Output Dataset","name":"Multi-Period-Market Current Candle","id":"ba614415-6e4f-4979-97a5-34c72a59e67c","savedPayload":{"position":{"x":14897.386505301713,"y":13473.814926987687},"targetPosition":{"x":19338.91372893721,"y":12256.290080293493},"floatingObject":{"isPinned":false,"isFrozen":false,"isCollapsed":false,"angleToParent":2,"distanceToParent":3,"arrangementStyle":0},"uiObject":{},"referenceParent":{"type":"Dataset Definition","name":"Multi-Period-Market","id":"17fb41f5-61b5-4433-9a10-950ff8216a7e"}}},{"type":"Output Dataset","name":"Multi-Period-Daily Current Candle","id":"1a2e3461-33e6-45b1-bcc1-c56a8c5d275c","savedPayload":{"position":{"x":14855.08212849534,"y":13309.762335468542},"targetPosition":{"x":19338.91372893721,"y":12256.290080293493},"floatingObject":{"isPinned":false,"isFrozen":false,"isCollapsed":false,"angleToParent":2,"distanceToParent":3,"arrangementStyle":0},"uiObject":{},"referenceParent":{"type":"Dataset Definition","name":"Multi-Period-Daily","id":"bbc9467d-28bf-432b-9e49-582d1bcabfb9"}}},{"type":"Output Dataset","name":"Multi-Period-Market Episode Balances","id":"567e4dc9-4deb-41e4-9ac9-dde943fb91ba","savedPayload":{"position":{"x":14818.915848183393,"y":13144.277773603595},"targetPosition":{"x":19338.91372893721,"y":12256.290080293493},"floatingObject":{"isPinned":false,"isFrozen":false,"isCollapsed":false,"angleToParent":2,"distanceToParent":3,"arrangementStyle":0},"uiObject":{},"referenceParent":{"type":"Dataset Definition","name":"Multi-Period-Market","id":"acddff59-b51b-4b45-b58f-71d4077755ee"}}},{"type":"Output Dataset","name":"Multi-Period-Daily Episode Balances","id":"e94b9bb3-9f0c-4550-94e1-09de444780a8","savedPayload":{"position":{"x":14788.936996026083,"y":12977.582492132336},"targetPosition":{"x":19338.91372893721,"y":12256.290080293493},"floatingObject":{"isPinned":false,"isFrozen":false,"isCollapsed":false,"angleToParent":2,"distanceToParent":3,"arrangementStyle":0},"uiObject":{},"referenceParent":{"type":"Dataset Definition","name":"Multi-Period-Daily","id":"213b8fe8-f772-41ea-a3f5-97867be555f7"}}},{"type":"Output Dataset","name":"Multi-Period-Market Distance To Event","id":"a30e7306-7be9-41b6-a465-558fdfe2a903","savedPayload":{"position":{"x":14765.186501949274,"y":12809.903263371778},"targetPosition":{"x":19338.91372893721,"y"Line truncated
|
||||
{"type":"Data Mine","name":"Masters-Trading","config":"{ \n\"codeName\": \"Masters-Trading\"\n}","sensorBots":[],"indicatorBots":[],"tradingBots":[{"type":"Trading Bot","name":"Low Frequency","config":"{\n\"codeName\": \"Low-Frequency\"\n}","processes":[{"type":"Process Definition","name":"Trading","config":"{\n \"codeName\": \"Trading\",\n \"sessionRunWaitTime\": 5000,\n \"retryWaitTime\": 10000,\n \"framework\": {\n \"name\": \"Low-Frequency-Trading-Process\"\n }\n }","processOutput":{"type":"Process Output","name":"New Process Output","outputDatasets":[{"type":"Output Dataset","name":"Multi-Period-Market Episode Counters","id":"6fc3054e-5034-4fc7-bddf-02360e0a6acf","savedPayload":{"position":{"x":15123.41163050094,"y":14131.6654223834},"targetPosition":{"x":19329.624549119937,"y":12258.776357515448},"floatingObject":{"isPinned":false,"isFrozen":false,"isCollapsed":false,"angleToParent":2,"distanceToParent":3,"arrangementStyle":0},"uiObject":{},"referenceParent":{"type":"Dataset Definition","name":"Multi-Period-Market","id":"db063d7b-db05-4bf6-941f-999e1ccb38fc"}}},{"type":"Output Dataset","name":"Multi-Period-Daily Episode Counters","id":"c3c2240f-514b-40de-825f-674037117d0b","savedPayload":{"position":{"x":15059.583922735308,"y":13981.6860113914},"targetPosition":{"x":19329.624549119937,"y":12258.776357515448},"floatingObject":{"isPinned":false,"isFrozen":false,"isCollapsed":false,"angleToParent":2,"distanceToParent":3,"arrangementStyle":0},"uiObject":{},"referenceParent":{"type":"Dataset Definition","name":"Multi-Period-Daily","id":"0764aaaf-d970-4f23-9df8-55fe5b7dd273"}}},{"type":"Output Dataset","name":"Multi-Period-Market Episode Statistics","id":"1dfdfc0d-842f-4e63-a591-256ac8a06b0f","savedPayload":{"position":{"x":15001.0923286648,"y":13829.555221724748},"targetPosition":{"x":19329.624549119937,"y":12258.776357515448},"floatingObject":{"isPinned":false,"isFrozen":false,"isCollapsed":false,"angleToParent":2,"distanceToParent":3,"arrangementStyle":0},"uiObject":{},"referenceParent":{"type":"Dataset Definition","name":"Multi-Period-Market","id":"fc9dc0f7-35e0-4548-bb91-f920825d694f"}}},{"type":"Output Dataset","name":"Multi-Period-Daily Episode Statistics","id":"39360c55-5ba8-413b-b0bd-cac3789ebf5f","savedPayload":{"position":{"x":14948.006652598842,"y":13676.050968178995},"targetPosition":{"x":19329.624549119937,"y":12258.776357515448},"floatingObject":{"isPinned":false,"isFrozen":false,"isCollapsed":false,"angleToParent":2,"distanceToParent":3,"arrangementStyle":0},"uiObject":{},"referenceParent":{"type":"Dataset Definition","name":"Multi-Period-Daily","id":"91953362-8751-4440-a898-21245cd47c15"}}},{"type":"Output Dataset","name":"Multi-Period-Market Episode Candle","id":"ba614415-6e4f-4979-97a5-34c72a59e67c","savedPayload":{"position":{"x":14900.392637325853,"y":13521.179581728626},"targetPosition":{"x":19329.624549119937,"y":12258.776357515448},"floatingObject":{"isPinned":false,"isFrozen":false,"isCollapsed":false,"angleToParent":2,"distanceToParent":3,"arrangementStyle":0},"uiObject":{},"referenceParent":{"type":"Dataset Definition","name":"Multi-Period-Market","id":"17fb41f5-61b5-4433-9a10-950ff8216a7e"}}},{"type":"Output Dataset","name":"Multi-Period-Daily Episode Candle","id":"1a2e3461-33e6-45b1-bcc1-c56a8c5d275c","savedPayload":{"position":{"x":14858.309491924105,"y":13364.741589835976},"targetPosition":{"x":19329.624549119937,"y":12258.776357515448},"floatingObject":{"isPinned":false,"isFrozen":false,"isCollapsed":false,"angleToParent":2,"distanceToParent":3,"arrangementStyle":0},"uiObject":{},"referenceParent":{"type":"Dataset Definition","name":"Multi-Period-Daily","id":"bbc9467d-28bf-432b-9e49-582d1bcabfb9"}}},{"type":"Output Dataset","name":"Multi-Period-Market Episode Balances","id":"567e4dc9-4deb-41e4-9ac9-dde943fb91ba","savedPayload":{"position":{"x":14821.809568281462,"y":13206.928494123413},"targetPosition":{"x":19329.624549119937,"y":12258.776357515448},"floatingObject":{"isPinned":false,"isFrozen":false,"isCollapsed":false,"angleToParent":2,"distanceToParent":3,"arrangementStyle":0},"uiObject":{},"referenceParent":{"type":"Dataset Definition","name":"Multi-Period-Market","id":"acddff59-b51b-4b45-b58f-71d4077755ee"}}},{"type":"Output Dataset","name":"Multi-Period-Daily Episode Balances","id":"e94b9bb3-9f0c-4550-94e1-09de444780a8","savedPayload":{"position":{"x":14790.93831241929,"y":13047.935697824543},"targetPosition":{"x":19329.624549119937,"y":12258.776357515448},"floatingObject":{"isPinned":false,"isFrozen":false,"isCollapsed":false,"angleToParent":2,"distanceToParent":3,"arrangementStyle":0},"uiObject":{},"referenceParent":{"type":"Dataset Definition","name":"Multi-Period-Daily","id":"213b8fe8-f772-41ea-a3f5-97867be555f7"}}},{"type":"Output Dataset","name":"Multi-Period-Market Distance To Event","id":"a30e7306-7be9-41b6-a465-558fdfe2a903","savedPayload":{"position":{"x":14765.734171531782,"y":12887.960675732134},"targetPosition":{"x":19329.624549119937,Line truncated
|
||||
@@ -128,7 +128,7 @@ exports.newTradingOrders = function newTradingOrders(bot, logger, tradingEngineM
|
||||
at the Trading Engine Order the lock is Open or Closed.
|
||||
*/
|
||||
if (tradingSystemOrder.config.spawnMultipleOrders !== true) {
|
||||
if (tradingEngineOrder.identifier.value === 'Closed') {
|
||||
if (tradingEngineOrder.lock.value === 'Closed') {
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -151,25 +151,23 @@ exports.newTradingOrders = function newTradingOrders(bot, logger, tradingEngineM
|
||||
|
||||
/* During the Second cycle we can not cancel orders. That is reserved for the First cycle. */
|
||||
if (tradingEngine.current.episode.cycle.value === 'Second') { continue }
|
||||
/*
|
||||
In the previous steps, we might have discovered that the order was cancelled
|
||||
at the exchange, or filled, so the order might still not be Open.
|
||||
If the stage is closing or the order is not Open, we wont be cancelling orders
|
||||
based on defined events.
|
||||
|
||||
/* Check if we need to cancel the order */
|
||||
await checkCancelOrderEvent(tradingEngineStage, executionAlgorithm, executionNode, tradingEngineOrder, tradingSystemOrder)
|
||||
/*
|
||||
If by this time the order is closed, we need to clone it and get the close
|
||||
to the Last node at the Trading Engine data structure.
|
||||
*/
|
||||
if (tradingEngineStage.status.value !== 'Closing' && tradingEngineOrder.status.value === 'Open') {
|
||||
|
||||
/* Check if we need to Cancel this Order */
|
||||
let situationName = checkOrderEvent(tradingSystemOrder.cancelOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode)
|
||||
if (situationName !== undefined) {
|
||||
|
||||
/* Simulate Order Cancelation, if needed. */
|
||||
simulateCancelOrder(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, 'Cancel Event')
|
||||
|
||||
/* Cancel the order at the Exchange, if needed. */
|
||||
await exchangeCancelOrder(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, 'Cancel Event')
|
||||
|
||||
updateEndsWithCycle(tradingEngineOrder)
|
||||
if (tradingEngineOrder.status.value === 'Closed') {
|
||||
switch (tradingEngineOrder.type) {
|
||||
case 'Market Order': {
|
||||
tradingEngineModule.cloneValues(tradingEngineOrder, tradingEngine.last.marketOrders)
|
||||
break
|
||||
}
|
||||
case 'Limit Order': {
|
||||
tradingEngineModule.cloneValues(tradingEngineOrder, tradingEngine.last.limitOrders)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -177,6 +175,30 @@ exports.newTradingOrders = function newTradingOrders(bot, logger, tradingEngineM
|
||||
}
|
||||
}
|
||||
|
||||
async function checkCancelOrderEvent(tradingEngineStage, executionAlgorithm, executionNode, tradingEngineOrder, tradingSystemOrder) {
|
||||
/*
|
||||
In the previous steps, we might have discovered that the order was cancelled
|
||||
at the exchange, or filled, so the order might still not be Open.
|
||||
If the stage is closing or the order is not Open, we wont be cancelling orders
|
||||
based on defined events.
|
||||
*/
|
||||
if (tradingEngineStage.status.value !== 'Closing' && tradingEngineOrder.status.value === 'Open') {
|
||||
|
||||
/* Check if we need to Cancel this Order */
|
||||
let situationName = checkOrderEvent(tradingSystemOrder.cancelOrderEvent, tradingSystemOrder, executionAlgorithm, executionNode)
|
||||
if (situationName !== undefined) {
|
||||
|
||||
/* Simulate Order Cancelation, if needed. */
|
||||
simulateCancelOrder(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, 'Cancel Event')
|
||||
|
||||
/* Cancel the order at the Exchange, if needed. */
|
||||
await exchangeCancelOrder(tradingEngineStage, tradingSystemOrder, tradingEngineOrder, 'Cancel Event')
|
||||
|
||||
updateEndsWithCycle(tradingEngineOrder)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function tradingSystemValidations(tradingSystemOrder) {
|
||||
/* Trading System Validations */
|
||||
if (tradingSystemOrder.config.percentageOfAlgorithmSize === undefined) { badDefinitionUnhandledException(undefined, 'tradingSystemOrder.config.percentageOfAlgorithmSize === undefined', tradingSystemOrder) }
|
||||
@@ -397,65 +419,104 @@ exports.newTradingOrders = function newTradingOrders(bot, logger, tradingEngineM
|
||||
/* Actual Rate Simulation */
|
||||
let calculatedBasedOnTradingSystem = false
|
||||
|
||||
/* Based on the Trading System Definition */
|
||||
if (tradingSystemOrder.simulatedExchangeEvents !== undefined) {
|
||||
if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate !== undefined) {
|
||||
if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula !== undefined) {
|
||||
/* Calculate this only once for this order */
|
||||
if (tradingEngineOrder.orderStatistics.actualRate.value === tradingEngineOrder.orderStatistics.actualRate.config.initialValue) {
|
||||
tradingEngineOrder.orderStatistics.actualRate.value = tradingSystem.formulas.get(tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula.id)
|
||||
if (tradingEngineOrder.orderStatistics.actualRate.value !== undefined) {
|
||||
calculatedBasedOnTradingSystem = true
|
||||
basedOnTradingSystem()
|
||||
basedOnSessionParameters()
|
||||
considerBestMatchWithOrderBook()
|
||||
recalculateQuotedAsset()
|
||||
recalculateSizePlaced()
|
||||
|
||||
function basedOnTradingSystem() {
|
||||
/* Based on the Trading System Definition */
|
||||
if (tradingSystemOrder.simulatedExchangeEvents !== undefined) {
|
||||
if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate !== undefined) {
|
||||
if (tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula !== undefined) {
|
||||
/* Calculate this only once for this order */
|
||||
if (tradingEngineOrder.orderStatistics.actualRate.value === tradingEngineOrder.orderStatistics.actualRate.config.initialValue) {
|
||||
tradingEngineOrder.orderStatistics.actualRate.value = tradingSystem.formulas.get(tradingSystemOrder.simulatedExchangeEvents.simulatedActualRate.formula.id)
|
||||
if (tradingEngineOrder.orderStatistics.actualRate.value !== undefined) {
|
||||
calculatedBasedOnTradingSystem = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Based on the Session Parameters Definition */
|
||||
if (calculatedBasedOnTradingSystem === false) {
|
||||
switch (tradingEngineOrder.type) {
|
||||
case 'Market Order': {
|
||||
/* Actual Rate is simulated based on the Session Paremeters */
|
||||
let slippageAmount = tradingEngineOrder.rate.value * sessionParameters.slippage.config.positionRate / 100
|
||||
switch (tradingSystemOrder.type) {
|
||||
case 'Market Sell Order': {
|
||||
tradingEngineOrder.orderStatistics.actualRate.value = tradingEngineOrder.rate.value - slippageAmount
|
||||
break
|
||||
}
|
||||
case 'Market Buy Order': {
|
||||
tradingEngineOrder.orderStatistics.actualRate.value = tradingEngineOrder.rate.value + slippageAmount
|
||||
break
|
||||
function basedOnSessionParameters() {
|
||||
/* Based on the Session Parameters Definition */
|
||||
if (calculatedBasedOnTradingSystem === false) {
|
||||
switch (tradingEngineOrder.type) {
|
||||
case 'Market Order': {
|
||||
/* Actual Rate is simulated based on the Session Paremeters */
|
||||
let slippageAmount = tradingEngineOrder.rate.value * sessionParameters.slippage.config.positionRate / 100
|
||||
switch (tradingSystemOrder.type) {
|
||||
case 'Market Sell Order': {
|
||||
tradingEngineOrder.orderStatistics.actualRate.value = tradingEngineOrder.rate.value - slippageAmount
|
||||
break
|
||||
}
|
||||
case 'Market Buy Order': {
|
||||
tradingEngineOrder.orderStatistics.actualRate.value = tradingEngineOrder.rate.value + slippageAmount
|
||||
break
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'Limit Order': {
|
||||
/* In Limit Orders the actual rate is the rate of the order, there is no slippage */
|
||||
tradingEngineOrder.orderStatistics.actualRate.value = tradingEngineOrder.rate.value
|
||||
break
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'Limit Order': {
|
||||
/* In Limit Orders the actual rate is the rate of the order, there is no slippage */
|
||||
tradingEngineOrder.orderStatistics.actualRate.value = tradingEngineOrder.rate.value
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
tradingEngineOrder.orderStatistics.actualRate.value = global.PRECISE(tradingEngineOrder.orderStatistics.actualRate.value, 10)
|
||||
|
||||
/*
|
||||
Recalculate the Quouted Asset Size based on the newly calculated Actual Rate.
|
||||
*/
|
||||
tradingEngineOrder.orderQuotedAsset.size.value = tradingEngineOrder.orderBaseAsset.size.value * tradingEngineOrder.orderStatistics.actualRate.value
|
||||
tradingEngineOrder.orderQuotedAsset.size.value = global.PRECISE(tradingEngineOrder.orderQuotedAsset.size.value, 10)
|
||||
/*
|
||||
Recaulculate the Size Placed Based on the newly calculated Quoted Asset Size.
|
||||
*/
|
||||
tradingEngineStage.stageQuotedAsset.sizePlaced.value =
|
||||
tradingEngineStage.stageQuotedAsset.sizePlaced.value -
|
||||
previousQuotedAssetSize
|
||||
function considerBestMatchWithOrderBook() {
|
||||
/*
|
||||
Until here we have got the rate based on the formula definition or based on session parameters slippage.
|
||||
THe last check is about watching what happened in the market. Let's remember that the exchange will
|
||||
fill the order with the best possible matches at it's order book. That means that if the rate
|
||||
we set for the order was too low (for a sale order) or too hight (for a buy order), the actual rate
|
||||
should be better than expected.
|
||||
*/
|
||||
switch (true) {
|
||||
case tradingSystemOrder.type === 'Market Buy Order' || tradingSystemOrder.type === 'Limit Buy Order': {
|
||||
if (tradingEngineOrder.orderStatistics.actualRate.value > tradingEngine.current.episode.candle.max.value) {
|
||||
tradingEngineOrder.orderStatistics.actualRate.value = tradingEngine.current.episode.candle.max.value
|
||||
}
|
||||
break
|
||||
}
|
||||
case tradingSystemOrder.type === 'Market Sell Order' || tradingSystemOrder.type === 'Limit Sell Order': {
|
||||
if (tradingEngineOrder.orderStatistics.actualRate.value < tradingEngine.current.episode.candle.min.value) {
|
||||
tradingEngineOrder.orderStatistics.actualRate.value = tradingEngine.current.episode.candle.min.value
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
tradingEngineOrder.orderStatistics.actualRate.value = global.PRECISE(tradingEngineOrder.orderStatistics.actualRate.value, 10)
|
||||
}
|
||||
|
||||
tradingEngineStage.stageQuotedAsset.sizePlaced.value =
|
||||
tradingEngineStage.stageQuotedAsset.sizePlaced.value +
|
||||
tradingEngineOrder.orderQuotedAsset.size.value
|
||||
function recalculateQuotedAsset() {
|
||||
/*
|
||||
Recalculate the Quouted Asset Size based on the newly calculated Actual Rate.
|
||||
*/
|
||||
tradingEngineOrder.orderQuotedAsset.size.value = tradingEngineOrder.orderBaseAsset.size.value * tradingEngineOrder.orderStatistics.actualRate.value
|
||||
tradingEngineOrder.orderQuotedAsset.size.value = global.PRECISE(tradingEngineOrder.orderQuotedAsset.size.value, 10)
|
||||
}
|
||||
|
||||
tradingEngineStage.stageQuotedAsset.sizePlaced.value = global.PRECISE(tradingEngineStage.stageQuotedAsset.sizePlaced.value, 10)
|
||||
function recalculateSizePlaced(){
|
||||
/*
|
||||
Recaulculate the Size Placed Based on the newly calculated Quoted Asset Size.
|
||||
*/
|
||||
tradingEngineStage.stageQuotedAsset.sizePlaced.value =
|
||||
tradingEngineStage.stageQuotedAsset.sizePlaced.value -
|
||||
previousQuotedAssetSize
|
||||
|
||||
tradingEngineStage.stageQuotedAsset.sizePlaced.value =
|
||||
tradingEngineStage.stageQuotedAsset.sizePlaced.value +
|
||||
tradingEngineOrder.orderQuotedAsset.size.value
|
||||
|
||||
tradingEngineStage.stageQuotedAsset.sizePlaced.value = global.PRECISE(tradingEngineStage.stageQuotedAsset.sizePlaced.value, 10)
|
||||
}
|
||||
}
|
||||
|
||||
function percentageFilledSimulation() {
|
||||
|
||||
@@ -43,10 +43,34 @@ exports.newTradingOutput = function newTradingOutput(bot, logger, tradingEngineM
|
||||
let outputDatasets = bot.processNode.referenceParent.processOutput.outputDatasets
|
||||
let outputDatasetsMap = new Map()
|
||||
|
||||
if (bot.FIRST_EXECUTION === true && bot.RESUME === false) {
|
||||
await initializeOutputs()
|
||||
if (bot.processingDailyFiles === true) {
|
||||
/*
|
||||
For Daily Files, we are going to initialize the outputs at the first execution
|
||||
if we are not resuming, but also at every following execution except when
|
||||
we are at the head of the market, because at the head of the market we need to
|
||||
append more data to the same output files that already exists.
|
||||
*/
|
||||
if (bot.FIRST_EXECUTION === true && bot.RESUME === false) {
|
||||
await initializeOutputs()
|
||||
} else {
|
||||
if (bot.simulationState.tradingEngine.current.episode.headOfTheMarket.value === true) {
|
||||
await readFiles()
|
||||
} else {
|
||||
await initializeOutputs()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
await readFiles()
|
||||
/*
|
||||
Form Market Files, we are only going to initialize the outputs at the first run
|
||||
and if we are not resuming execution. Subsequent runs means that we are at the
|
||||
head of the market. Remember that backtests finishes the session once the reach
|
||||
the final datetime.
|
||||
*/
|
||||
if (bot.FIRST_EXECUTION === true && bot.RESUME === false) {
|
||||
await initializeOutputs()
|
||||
} else {
|
||||
await readFiles()
|
||||
}
|
||||
}
|
||||
|
||||
await tradingSimulation.runSimulation(
|
||||
|
||||
@@ -557,7 +557,12 @@
|
||||
tradingProcessDate
|
||||
)
|
||||
|
||||
bot.FIRST_EXECUTION = false // From here on, all other loops executions wont be the first execution.
|
||||
/*
|
||||
From here on, all other loops executions wont be the first execution and also
|
||||
we will consider that it is not resuming a previous execution as well.
|
||||
*/
|
||||
bot.FIRST_EXECUTION = false
|
||||
bot.RESUME = false
|
||||
}
|
||||
|
||||
async function writeProcessFiles() {
|
||||
|
||||
@@ -148,6 +148,15 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, tradin
|
||||
tradingEngineModule.setCurrentCycle('First')
|
||||
await runCycle()
|
||||
|
||||
/*
|
||||
We check if we need to stop before appending the records so that the stop
|
||||
reason is also propery recorded. Note also that we check this after the first
|
||||
cycle, where orders have not been submitted to the exchange yet, but we
|
||||
had the chance to check for the status of placed orders or even cancel
|
||||
the ones that needed cancellation.
|
||||
*/
|
||||
checkIfWeNeedToStop()
|
||||
|
||||
/* Add new records to the process output */
|
||||
tradingRecordsModule.appendRecords()
|
||||
|
||||
@@ -163,12 +172,6 @@ exports.newTradingSimulation = function newTradingSimulation(bot, logger, tradin
|
||||
tradingEngineModule.setCurrentCycle('Second')
|
||||
await runCycle()
|
||||
|
||||
/*
|
||||
We check if we need to stop before appending the records so that the stop
|
||||
reason is also propery recorded.
|
||||
*/
|
||||
checkIfWeNeedToStop()
|
||||
|
||||
/* Add new records to the process output */
|
||||
tradingRecordsModule.appendRecords()
|
||||
|
||||
|
||||
@@ -891,11 +891,14 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM
|
||||
either on Base Asset or Quoted Asset.
|
||||
This can happens at any time when we update the sizeFilled and the feesPaid values
|
||||
when we see at the exchange that orders were filled.
|
||||
Note that the comparison is made with a Rounding Factor in order to avoid rounding problems.
|
||||
*/
|
||||
const ROUNDING_ERROR_CORRECTION_FACTOR = 1.0000001
|
||||
|
||||
switch (tradingEngineStage.stageDefinedIn.value) {
|
||||
case 'Base Asset': {
|
||||
if (
|
||||
tradingEngineStage.stageBaseAsset.sizeFilled.value
|
||||
tradingEngineStage.stageBaseAsset.sizeFilled.value * ROUNDING_ERROR_CORRECTION_FACTOR
|
||||
>=
|
||||
tradingEngineStage.stageBaseAsset.targetSize.value
|
||||
) {
|
||||
@@ -907,7 +910,7 @@ exports.newTradingStages = function newTradingStages(bot, logger, tradingEngineM
|
||||
}
|
||||
case 'Quoted Asset': {
|
||||
if (
|
||||
tradingEngineStage.stageQuotedAsset.sizeFilled.value
|
||||
tradingEngineStage.stageQuotedAsset.sizeFilled.value * ROUNDING_ERROR_CORRECTION_FACTOR
|
||||
>=
|
||||
tradingEngineStage.stageQuotedAsset.targetSize.value
|
||||
) {
|
||||
|
||||
Reference in new issue
Block a user