From a1ecaafd2f9a60590579a8d44c9ac615058dd50e Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 10 Aug 2019 11:16:46 +0200 Subject: [PATCH] All Working --- Commons.js | 198 ++++++++++++++++++++++---------- Multi-Period-Daily/User.Bot.js | 14 +-- Multi-Period-Market/User.Bot.js | 13 ++- this.bot.config.json | 22 ---- 4 files changed, 150 insertions(+), 97 deletions(-) diff --git a/Commons.js b/Commons.js index c37bc1bd3..c03e873f7 100644 --- a/Commons.js +++ b/Commons.js @@ -1424,64 +1424,115 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> putOpeningOrder -> Entering function."); } - /* Mechanism to avoid putting the same order over and over again at different executions of the simulation engine. */ - - if (interExecutionMemory.executionContext.period !== undefined) { - if (period <= interExecutionMemory.executionContext.period) { + /* We wont take a position unless we are withing the startDate and the endDate range */ + if (startDate !== undefined) { + if (candle.begin < startDate.valueOf()) { takePositionAtSimulation() return; } } + if (endDate !== undefined) { + if (candle.begin > endDate.valueOf()) { + takePositionAtSimulation() + return; + } + } + + /* Checking the status of current positions */ + let positions = assistant.getPositions(); + if (positions.length > 0) { + let position = positions[positions.length - 1] // We are allways checking the the last position is not open. + if (position.status === 'open') { + afterLoop(); + return + } + } + + /* Mechanism to avoid putting the same order over and over again at different executions of the simulation engine. */ + + if (interExecutionMemory.executionContext !== undefined) { + if (interExecutionMemory.executionContext.periods !== undefined) { + if (periods <= interExecutionMemory.executionContext.periods) { + takePositionAtSimulation() + return; + } + } + } + /* We are not going to place orders based on outdated information. The next filter prevents firing orders when backtesting. */ let today = new Date(Math.trunc((new Date().valueOf()) / ONE_DAY_IN_MILISECONDS) * ONE_DAY_IN_MILISECONDS) let processDay = new Date(Math.trunc(currentDay.valueOf() / ONE_DAY_IN_MILISECONDS) * ONE_DAY_IN_MILISECONDS) - if (today !== processDay) { - //takePositionAtSimulation() - //return; + if (today.valueOf() !== processDay.valueOf()) { + takePositionAtSimulation() + return; } + let openPositionRate + let amountA + let amountB let positionDirection + if (baseAsset === 'BTC') { positionDirection = "sell" + + openPositionRate = tradePositionRate - 100 // Provisional stuff to simulate a market order. Put the price lower to get it executed quickly + + amountA = tradePositionSize * openPositionRate + amountB = tradePositionSize } else { positionDirection = "buy" + + openPositionRate = tradePositionRate + 100 // Provisional stuff to simulate a market order. Put the price higher to get it executed quickly + + amountA = tradePositionSize + amountB = tradePositionSize / openPositionRate } interExecutionMemory.executionContext = { status: "Taking Position" } - assistant.putPosition(positionDirection, tradePositionRate, tradePositionSize * tradePositionRate, tradePositionSize, onOrderPut) + assistant.putPosition(positionDirection, openPositionRate, amountA, amountB, onOrderPut) function onOrderPut(err) { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> putOpeningOrder -> onOrderPut -> Entering function."); } - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> putOpeningOrder -> onOrderPut -> DEFAULT_OK_RESPONSE "); } - interExecutionMemory.executionContext = { - status: "In a Position", - period: period + try { + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> putOpeningOrder -> onOrderPut -> DEFAULT_OK_RESPONSE "); } + interExecutionMemory.executionContext = { + status: "In a Position", + periods: periods, + amountA: amountA, + amountB: amountB + } + takePositionAtSimulation() + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> putOpeningOrder -> onOrderPut -> DEFAULT_FAIL_RESPONSE "); } + strategy.openStage.openExecution.error = err.message + afterLoop() + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> putOpeningOrder -> onOrderPut -> DEFAULT_RETRY_RESPONSE "); } + strategy.openStage.openExecution.error = err.message + afterLoop() + return; } - takePositionAtSimulation() - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> putOpeningOrder -> onOrderPut -> DEFAULT_FAIL_RESPONSE "); } - strategy.openStage.openExecution.error = err.message - afterLoop() - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> putOpeningOrder -> onOrderPut -> DEFAULT_RETRY_RESPONSE "); } - strategy.openStage.openExecution.error = err.message - afterLoop() - return; } + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[ERROR] runSimulation -> putOpeningOrder -> onOrderPut -> Unexpected Response -> Message = " + err.message); } + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] runSimulation -> putOpeningOrder -> onOrderPut -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> putOpeningOrder -> onOrderPut -> Unexpected Response -> Message = " + err.message); } - strategy.openStage.openExecution.error = err.message } } @@ -1533,9 +1584,6 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { /* Position size and rate */ let strategy = tradingSystem.strategies[currentStrategyIndex]; - tradePositionSize = strategy.positionSize; - tradePositionRate = strategy.positionRate; - /* We see if we need to put the actual order at the exchange. */ if (interExecutionMemory.executionContext !== undefined) { @@ -1578,7 +1626,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { /* Checking the status of current positions */ let positions = assistant.getPositions(); if (positions.length > 0) { - let position = positions[0] + let position = positions[positions.length - 1] // We are allways checking the the last position is not open. if (position.status === 'open') { afterLoop(); return @@ -1587,55 +1635,81 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { /* Mechanism to avoid putting the same order over and over again at different executions of the simulation engine. */ - if (interExecutionMemory.executionContext.period !== undefined) { - if (period <= interExecutionMemory.executionContext.period) { - closePositionAtSimulation() - return; + if (interExecutionMemory.executionContext !== undefined) { + if (interExecutionMemory.executionContext.periods !== undefined) { + if (periods <= interExecutionMemory.executionContext.periods) { + closePositionAtSimulation() + return; + } } } + let closePositionRate + let amountA + let amountB let positionDirection + if (baseAsset === 'BTC') { positionDirection = "buy" + + closePositionRate = ticker.last + 100; // This is provisional and totally arbitrary, until we have a formula on the designer that defines this stuff. + + amountA = interExecutionMemory.executionContext.amountA + amountB = interExecutionMemory.executionContext.amountA / closePositionRate } else { positionDirection = "sell" + + closePositionRate = ticker.last - 100; // This is provisional and totally arbitrary, until we have a formula on the designer that defines this stuff. + + amountA = interExecutionMemory.executionContext.amountB * closePositionRate + amountB = interExecutionMemory.executionContext.amountB } interExecutionMemory.executionContext = { status: "Closing Position" } - assistant.putPosition(positionDirection, tradePositionRate, tradePositionSize * tradePositionRate, tradePositionSize, onOrderPut) + assistant.putPosition(positionDirection, closePositionRate, amountA, amountB, onOrderPut) function onOrderPut(err) { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> putClosingOrder -> onOrderPut -> Entering function."); } - switch (err.result) { - case global.DEFAULT_OK_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> putClosingOrder -> onOrderPut -> DEFAULT_OK_RESPONSE "); } - interExecutionMemory.executionContext = { - status: "Position Closed", - period: period + try { + switch (err.result) { + case global.DEFAULT_OK_RESPONSE.result: { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> putClosingOrder -> onOrderPut -> DEFAULT_OK_RESPONSE "); } + interExecutionMemory.executionContext = { + status: "Position Closed", + periods: periods, + amountA: amountA, + amountB: amountB + } + closePositionAtSimulation() + return; + } + case global.DEFAULT_FAIL_RESPONSE.result: { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> putClosingOrder -> onOrderPut -> DEFAULT_FAIL_RESPONSE "); } + /* We will assume that the problem is temporary, and expect that it will work at the next execution.*/ + strategy.closeStage.closeExecution.error = err.message + afterLoop() + return; + } + case global.DEFAULT_RETRY_RESPONSE.result: { + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> putOpeningOrder -> onOrderPut -> DEFAULT_RETRY_RESPONSE "); } + strategy.closeStage.closeExecution.error = err.message + afterLoop() + return; } - closePositionAtSimulation() - return; - } - case global.DEFAULT_FAIL_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> putClosingOrder -> onOrderPut -> DEFAULT_FAIL_RESPONSE "); } - /* We will assume that the problem is temporary, and expect that it will work at the next execution.*/ - strategy.closeStage.closeExecution.error = err.message - afterLoop() - return; - } - case global.DEFAULT_RETRY_RESPONSE.result: { - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> putOpeningOrder -> onOrderPut -> DEFAULT_RETRY_RESPONSE "); } - strategy.closeStage.closeExecution.error = err.message - afterLoop() - return; } + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[ERROR] runSimulation -> putClosingOrder -> onOrderPut -> Unexpected Response -> Message = " + err.message); } + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] runSimulation -> putClosingOrder -> onOrderPut -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return } - if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> putClosingOrder -> onOrderPut -> Unexpected Response -> Message = " + err.message); } - strategy.openStage.openExecution.error = err.message } } diff --git a/Multi-Period-Daily/User.Bot.js b/Multi-Period-Daily/User.Bot.js index 1e457f5a7..ca0130408 100644 --- a/Multi-Period-Daily/User.Bot.js +++ b/Multi-Period-Daily/User.Bot.js @@ -70,28 +70,28 @@ dataFile = dataFiles[i]; switch (i) { - + /* case 0: { commons.buildLRC(dataFile, callBackFunction); break; - } - case 1: { + }*/ + case 0: { commons.buildPercentageBandwidthMap(dataFile, callBackFunction); break; } - case 2: { + case 1: { commons.buildBollingerBandsMap(dataFile, callBackFunction); break; } - case 3: { + case 2: { commons.buildBollingerChannelsArray(dataFile, callBackFunction); break; } - case 4: { + case 3: { commons.buildBollingerSubChannelsArray(dataFile, callBackFunction); break; } - case 5: { + case 4: { commons.buildCandles(dataFile, callBackFunction); break; } diff --git a/Multi-Period-Market/User.Bot.js b/Multi-Period-Market/User.Bot.js index 19a134dea..5bac69a1f 100644 --- a/Multi-Period-Market/User.Bot.js +++ b/Multi-Period-Market/User.Bot.js @@ -71,27 +71,28 @@ switch (i) { + /* case 0: { commons.buildLRC(dataFile, callBackFunction); break; - } - case 1: { + }*/ + case 0: { commons.buildPercentageBandwidthMap(dataFile, callBackFunction); break; } - case 2: { + case 1: { commons.buildBollingerBandsMap(dataFile, callBackFunction); break; } - case 3: { + case 2: { commons.buildBollingerChannelsArray(dataFile, callBackFunction); break; } - case 4: { + case 3: { commons.buildBollingerSubChannelsArray(dataFile, callBackFunction); break; } - case 5: { + case 4: { commons.buildCandles(dataFile, callBackFunction); break; } diff --git a/this.bot.config.json b/this.bot.config.json index d9d7926df..a7d7de407 100644 --- a/this.bot.config.json +++ b/this.bot.config.json @@ -39,17 +39,6 @@ } ], "dataDependencies": [ - { - "devTeam": "AAVikings", - "bot": "AAGauss", - "botVersion": { - "major": 1, - "minor": 0 - }, - "product": "LRC-Points", - "dataSetVersion": "dataSet.V1", - "dataSet": "Multi-Period-Market" - }, { "devTeam": "AAMasters", "bot": "AAChris", @@ -157,17 +146,6 @@ } ], "dataDependencies": [ - { - "devTeam": "AAVikings", - "bot": "AAGauss", - "botVersion": { - "major": 1, - "minor": 0 - }, - "product": "LRC-Points", - "dataSetVersion": "dataSet.V1", - "dataSet": "Multi-Period-Daily" - }, { "devTeam": "AAMasters", "bot": "AAChris",