TRADING_BOT MAIN LOOP introduced
This commit is contained in:
1 parent
0bbcc4588c
commit
91dfa45b61
9 files changed
+70
-34
No files matched your search
Binary file not shown.
@@ -38,7 +38,7 @@
|
||||
<Compile Include="Interval Executor.js" />
|
||||
<Compile Include="Poloniex API Client.js" />
|
||||
<Compile Include="Run.js" />
|
||||
<Compile Include="TradingBot.js">
|
||||
<Compile Include="Trading Bot Main Loop.js">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Utilities.js" />
|
||||
|
||||
@@ -252,6 +252,23 @@ exports.newAzureFileStorage = function newAzureFileStorage(BOT) {
|
||||
}
|
||||
}
|
||||
|
||||
if (err.code = "ParentNotFound") {
|
||||
|
||||
/* This is how Azure tell us the folder where the file was supposed to be does not exist. We map this to our own response. */
|
||||
|
||||
let customResponse = {
|
||||
result: global.DEFAULT_FAIL_RESPONSE.result,
|
||||
message: "Folder does not exist."
|
||||
};
|
||||
|
||||
logger.write("[ERROR] getTextFile -> onFileReceived -> Custom Response -> message = " + customResponse.message);
|
||||
|
||||
callBackFunction(customResponse);
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
logger.write("[ERROR] getTextFile -> onFileReceived -> Dont know what to do here. Cancelling operation. ");
|
||||
callBackFunction(global.DEFAULT_FAIL_RESPONSE);
|
||||
|
||||
|
||||
+20
-21
@@ -139,15 +139,22 @@
|
||||
|
||||
if (FULL_LOG === true) { logger.write("[INFO] initialize -> getStatusReport -> Entering function."); }
|
||||
|
||||
/* If the process run and was interrupted, there should be a status report that allows us to resume execution. */
|
||||
|
||||
let fileName = "Status.Report.json"
|
||||
let filePath = EXCHANGE_NAME + "/" + bot.codeName + "/" + bot.dataSetVersion + "/Processes/" + bot.process;
|
||||
let filePath = bot.devTeam + "/" + bot.codeName + "/" + bot.version + "/" + bot.dataSetVersion + "/Processes/" + bot.process + "/" + EXCHANGE_NAME;
|
||||
|
||||
cloudStorage.getTextFile(filePath, fileName, onFileReceived);
|
||||
|
||||
function onFileReceived(err, text) {
|
||||
|
||||
if (err.result === global.DEFAULT_FAIL_RESPONSE.result && err.message === 'Folder does not exist.') {
|
||||
logger.write("[INFO] initialize -> getStatusReport -> onFileReceived -> err = " + err.message);
|
||||
|
||||
/* In this case we can assume that this is the first execution ever of this bot.*/
|
||||
|
||||
createConext(callBack);
|
||||
return;
|
||||
}
|
||||
|
||||
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
|
||||
logger.write("[ERROR] initialize -> getStatusReport -> onFileReceived -> err = " + err.message);
|
||||
callBack(err);
|
||||
@@ -161,25 +168,17 @@
|
||||
try {
|
||||
|
||||
thisObject.statusReport = JSON.parse(text);
|
||||
|
||||
if (thisObject.statusReport.lastExecution === undefined) {
|
||||
createConext(callBack);
|
||||
} else {
|
||||
getExecutionHistory(callBack);
|
||||
}
|
||||
getExecutionHistory(callBack);
|
||||
|
||||
} catch (err) {
|
||||
|
||||
/*
|
||||
|
||||
It might happen that the file content is corrupt or it does not exist. The bot can not run without an Execution Context,
|
||||
since it is risky to ignore its own context, so even for first time execution, a file with the right format
|
||||
is needed.
|
||||
|
||||
It might happen that the file content is corrupt. We will consider this as a temporary situation, since sometimes the file
|
||||
is being updated at the moment of the read. The bot can not run without a valid Status Report but we can request the platform to retry later.
|
||||
*/
|
||||
|
||||
logger.write("[ERROR] initialize -> getStatusReport -> onFileReceived -> Bot cannot execute without the Status report. -> Err = " + err.message);
|
||||
callBack(global.DEFAULT_FAIL_RESPONSE);
|
||||
logger.write("[ERROR] initialize -> getStatusReport -> onFileReceived -> Bot cannot execute without a valid Status report. -> Err = " + err.message);
|
||||
callBack(global.DEFAULT_RETRY_RESPONSE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,7 +194,7 @@
|
||||
if (FULL_LOG === true) { logger.write("[INFO] initialize -> getExecutionHistory -> Entering function."); }
|
||||
|
||||
let fileName = "Execution.History.json"
|
||||
let filePath = EXCHANGE_NAME + "/" + bot.codeName + "/" + bot.dataSetVersion + "/Output/" + bot.process;
|
||||
let filePath = bot.devTeam + "/" + bot.codeName + "/" + bot.version + "/" + bot.dataSetVersion + "/Output/" + bot.process + "/" + EXCHANGE_NAME;
|
||||
|
||||
cloudStorage.getTextFile(filePath, fileName, onFileReceived);
|
||||
|
||||
@@ -246,7 +245,7 @@
|
||||
|
||||
let fileName = "Execution.Context.json"
|
||||
let dateForPath = date.getUTCFullYear() + '/' + utilities.pad(date.getUTCMonth() + 1, 2) + '/' + utilities.pad(date.getUTCDate(), 2) + '/' + utilities.pad(date.getUTCHours(), 2) + '/' + utilities.pad(date.getUTCMinutes(), 2);
|
||||
let filePath = EXCHANGE_NAME + "/" + bot.codeName + "/" + bot.dataSetVersion + "/Output/" + bot.process + "/" + dateForPath;
|
||||
let filePath = bot.devTeam + "/" + bot.codeName + "/" + bot.version + "/" + bot.dataSetVersion + "/Output/" + bot.process + "/" + EXCHANGE_NAME + "/" + dateForPath;
|
||||
|
||||
cloudStorage.getTextFile(filePath, fileName, onFileReceived);
|
||||
|
||||
@@ -374,7 +373,7 @@
|
||||
|
||||
let fileName = "Execution.Context.json"
|
||||
let dateForPath = processDatetime.getUTCFullYear() + '/' + utilities.pad(processDatetime.getUTCMonth() + 1, 2) + '/' + utilities.pad(processDatetime.getUTCDate(), 2) + '/' + utilities.pad(processDatetime.getUTCHours(), 2) + '/' + utilities.pad(processDatetime.getUTCMinutes(), 2);
|
||||
let filePath = EXCHANGE_NAME + "/" + bot.codeName + "/" + bot.dataSetVersion + "/Output/" + bot.process + "/" + dateForPath;
|
||||
let filePath = bot.devTeam + "/" + bot.codeName + "/" + bot.version + "/" + bot.dataSetVersion + "/Output/" + bot.process + "/" + EXCHANGE_NAME + "/" + dateForPath;
|
||||
|
||||
utilities.createFolderIfNeeded(filePath, cloudStorage, onFolderCreated);
|
||||
|
||||
@@ -429,7 +428,7 @@
|
||||
try {
|
||||
|
||||
let fileName = "Execution.History.json"
|
||||
let filePath = EXCHANGE_NAME + "/" + bot.codeName + "/" + bot.dataSetVersion + "/Output/" + bot.process;
|
||||
let filePath = bot.devTeam + "/" + bot.codeName + "/" + bot.version + "/" + bot.dataSetVersion + "/Output/" + bot.process + "/" + EXCHANGE_NAME;
|
||||
|
||||
utilities.createFolderIfNeeded(filePath, cloudStorage, onFolderCreated);
|
||||
|
||||
@@ -494,7 +493,7 @@
|
||||
try {
|
||||
|
||||
let fileName = "Status.Report.json"
|
||||
let filePath = EXCHANGE_NAME + "/" + bot.codeName + "/" + bot.dataSetVersion + "/Processes/" + bot.process;
|
||||
let filePath = bot.devTeam + "/" + bot.codeName + "/" + bot.version + "/" + bot.dataSetVersion + "/Processes/" + bot.process + "/" + EXCHANGE_NAME;
|
||||
|
||||
utilities.createFolderIfNeeded(filePath, cloudStorage, onFolderCreated);
|
||||
|
||||
|
||||
+2
-2
@@ -68,7 +68,7 @@
|
||||
|
||||
function initializeOliviaStorage() {
|
||||
|
||||
oliviaAzureFileStorage.initialize("Olivia", onInizialized);
|
||||
oliviaAzureFileStorage.initialize("AAOlivia", onInizialized);
|
||||
|
||||
function onInizialized(err) {
|
||||
if (err.result === global.DEFAULT_OK_RESPONSE.result) {
|
||||
@@ -82,7 +82,7 @@
|
||||
|
||||
function initializeTomStorage() {
|
||||
|
||||
tomAzureFileStorage.initialize("Tom", onInizialized);
|
||||
tomAzureFileStorage.initialize("AATom", onInizialized);
|
||||
|
||||
function onInizialized(err) {
|
||||
if (err.result === global.DEFAULT_OK_RESPONSE.result) {
|
||||
|
||||
+9
-5
@@ -24,23 +24,27 @@
|
||||
|
||||
try {
|
||||
|
||||
folderPath = thisObject.bot.devTeam;
|
||||
folderPath = '../../Logs';
|
||||
|
||||
createFolderSync(folderPath);
|
||||
|
||||
folderPath = thisObject.bot.devTeam + "/" + thisObject.bot.type;
|
||||
folderPath = '../../Logs/' + thisObject.bot.devTeam;
|
||||
|
||||
createFolderSync(folderPath);
|
||||
|
||||
folderPath = thisObject.bot.devTeam + "/" + thisObject.bot.type + "/" + thisObject.bot.codeName;
|
||||
folderPath = '../../Logs/' + thisObject.bot.devTeam + "/" + thisObject.bot.type;
|
||||
|
||||
createFolderSync(folderPath);
|
||||
|
||||
folderPath = thisObject.bot.devTeam + "/" + thisObject.bot.type + "/" + thisObject.bot.codeName + "/" + thisObject.bot.version;
|
||||
folderPath = '../../Logs/' + thisObject.bot.devTeam + "/" + thisObject.bot.type + "/" + thisObject.bot.codeName;
|
||||
|
||||
createFolderSync(folderPath);
|
||||
|
||||
folderPath = thisObject.bot.devTeam + "/" + thisObject.bot.type + "/" + thisObject.bot.codeName + "/" + thisObject.bot.version + "/" + thisObject.bot.process;
|
||||
folderPath = '../../Logs/' + thisObject.bot.devTeam + "/" + thisObject.bot.type + "/" + thisObject.bot.codeName + "/" + thisObject.bot.version;
|
||||
|
||||
createFolderSync(folderPath);
|
||||
|
||||
folderPath = '../../Logs/' + thisObject.bot.devTeam + "/" + thisObject.bot.type + "/" + thisObject.bot.codeName + "/" + thisObject.bot.version + "/" + thisObject.bot.process;
|
||||
|
||||
createFolderSync(folderPath);
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ const START_ALL_MONTHS = false;
|
||||
const START_ONE_MONTH = true;
|
||||
|
||||
const INTERVAL_EXECUTOR_MODULE = require('./Interval Executor');
|
||||
const TRADING_BOT_MODULE = require('./TradingBot');
|
||||
const TRADING_BOT_MAIN_LOOP_MODULE = require('./Trading Bot Main Loop');
|
||||
|
||||
/* Now we will see which are the script Arguments. We expect the name of the process to run, since that is what it should be defined at the Task Scheduller. */
|
||||
|
||||
@@ -194,14 +194,14 @@ function runTradingBot(pProcessConfig) {
|
||||
try {
|
||||
if (FULL_LOG === true) { logger.write("[INFO] runTradingBot -> Entering function."); }
|
||||
|
||||
let tradingBot = TRADING_BOT_MODULE.newTradingBot(botConfig);
|
||||
tradingBot.initialize(vmConfig.bot.path, pProcessConfig, onInitializeReady);
|
||||
let tradingBotMainLoop = TRADING_BOT_MAIN_LOOP_MODULE.newTradingBotMainLoop(botConfig);
|
||||
tradingBotMainLoop.initialize(vmConfig.bot.path, pProcessConfig, onInitializeReady);
|
||||
|
||||
function onInitializeReady(err) {
|
||||
|
||||
if (err.result === global.DEFAULT_OK_RESPONSE.result) {
|
||||
|
||||
tradingBot.run(whenRunFinishes);
|
||||
tradingBotMainLoop.run(whenRunFinishes);
|
||||
|
||||
function whenRunFinishes(err) {
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
exports.newTradingBot = function newTradingBot(BOT) {
|
||||
exports.newTradingBotMainLoop = function newTradingBotMainLoop(BOT) {
|
||||
|
||||
let bot = BOT;
|
||||
const ROOT_DIR = './';
|
||||
@@ -80,6 +80,8 @@
|
||||
|
||||
function initializeContext() {
|
||||
|
||||
if (FULL_LOG === true) { logger.write("[INFO] run -> loop -> initializeContext -> Entering function."); }
|
||||
|
||||
context = CONTEXT.newContext(bot, DEBUG_MODULE, AZURE_FILE_STORAGE);
|
||||
context.initialize(processDatetime, onInizialized);
|
||||
|
||||
@@ -96,6 +98,8 @@
|
||||
|
||||
function initializeDatasource() {
|
||||
|
||||
if (FULL_LOG === true) { logger.write("[INFO] run -> loop -> initializeDatasource -> Entering function."); }
|
||||
|
||||
datasource = DATASOURCE.newDatasource(bot, DEBUG_MODULE, AZURE_FILE_STORAGE);
|
||||
datasource.initialize(onInizialized);
|
||||
|
||||
@@ -112,6 +116,8 @@
|
||||
|
||||
function initializeExchangeAPI() {
|
||||
|
||||
if (FULL_LOG === true) { logger.write("[INFO] run -> loop -> initializeExchangeAPI -> Entering function."); }
|
||||
|
||||
exchangeAPI = EXCHANGE_API.newExchangeAPI(bot, DEBUG_MODULE, POLONIEX_CLIENT_MODULE);
|
||||
exchangeAPI.initialize(onInizialized);
|
||||
|
||||
@@ -128,6 +134,8 @@
|
||||
|
||||
function initializeAssistant() {
|
||||
|
||||
if (FULL_LOG === true) { logger.write("[INFO] run -> loop -> initializeAssistant -> Entering function."); }
|
||||
|
||||
assistant = ASSISTANT.newAssistant(bot, DEBUG_MODULE);
|
||||
assistant.initialize(processDatetime, context, exchangeAPI, onInizialized);
|
||||
|
||||
@@ -144,6 +152,8 @@
|
||||
|
||||
function initializeUserBot() {
|
||||
|
||||
if (FULL_LOG === true) { logger.write("[INFO] run -> loop -> initializeUserBot -> Entering function."); }
|
||||
|
||||
usertBot = USER_BOT_MODULE.newUserBot(bot, DEBUG_MODULE);
|
||||
|
||||
let platform = {
|
||||
@@ -168,6 +178,8 @@
|
||||
|
||||
function startUserBot() {
|
||||
|
||||
if (FULL_LOG === true) { logger.write("[INFO] run -> loop -> startUserBot -> Entering function."); }
|
||||
|
||||
usertBot.run(platform, onFinished);
|
||||
|
||||
function onFinished(err) {
|
||||
@@ -197,6 +209,8 @@
|
||||
|
||||
function saveContext() {
|
||||
|
||||
if (FULL_LOG === true) { logger.write("[INFO] run -> loop -> saveContext -> Entering function."); }
|
||||
|
||||
context.saveThemAll(onFinished);
|
||||
|
||||
function onFinished(err) {
|
||||
@@ -218,6 +232,8 @@
|
||||
|
||||
function loopControl(nextWaitTime) {
|
||||
|
||||
if (FULL_LOG === true) { logger.write("[INFO] run -> loopControl -> nextWaitTime = " + nextWaitTime); }
|
||||
|
||||
/* Trading bots are going to be executed after a configured period of time after the last execution ended. This is to avoid overlapping executions. */
|
||||
|
||||
switch (nextWaitTime) {
|
||||
Whitespace-only changes.
Reference in new issue
Block a user