File storage dependency

This commit is contained in:
Matias committed 2019-06-22 13:22:49 -03:00
1 parent acb0475c10
commit 61dbd15cf1
2 files changed
+185 -256

No files matched your search

+177 -218
View File
@@ -5,29 +5,29 @@ var LRCIndicator = function (settings) {
this.age = 0
this.history = []
this.x = []
/*
* Do not use array(depth) as it might not be implemented
*/
/*
* Do not use array(depth) as it might not be implemented
*/
for (var i = 0; i < this.depth; i++) {
this.history.push(0.0)
this.x.push(i)
}
// log.debug("Created LRC indicator with h: ", this.depth);
// log.debug("Created LRC indicator with h: ", this.depth);
}
LRCIndicator.prototype.update = function (price) {
// We need sufficient history to get the right result.
// We need sufficient history to get the right result.
if (this.result === false && this.age < this.depth) {
this.history[this.age] = price
this.age++
this.result = false
// log.debug("Waiting for sufficient age: ", this.age, " out of ", this.depth);
// log.debug("Waiting for sufficient age: ", this.age, " out of ", this.depth);
return
}
this.age++
// shift history
// shift history
for (var i = 0; i < (this.depth - 1); i++) {
this.history[i] = this.history[i + 1]
}
@@ -35,23 +35,23 @@ LRCIndicator.prototype.update = function (price) {
this.calculate(price)
// log.debug("Checking LRC: ", this.result.toFixed(8), "\tH: ", this.age);
// log.debug("Checking LRC: ", this.result.toFixed(8), "\tH: ", this.age);
return
}
/*
* Least squares linear regression fitting.
*/
function linreg(values_x, values_y) {
function linreg (values_x, values_y) {
var sum_x = 0
var sum_y = 0
var sum_xy = 0
var sum_xx = 0
var count = 0
/*
* We'll use those variables for faster read/write access.
*/
/*
* We'll use those variables for faster read/write access.
*/
var x = 0
var y = 0
var values_length = values_x.length
@@ -60,16 +60,16 @@ function linreg(values_x, values_y) {
throw new Error('The parameters values_x and values_y need to have same size!')
}
/*
* Nothing to do.
*/
/*
* Nothing to do.
*/
if (values_length === 0) {
return [[], []]
}
/*
* Calculate the sum for each of the parts necessary.
*/
/*
* Calculate the sum for each of the parts necessary.
*/
for (var v = 0; v < values_length; v++) {
x = values_x[v]
y = values_y[v]
@@ -80,10 +80,10 @@ function linreg(values_x, values_y) {
count++
}
/*
* Calculate m and b for the formula: y = x * m + b
*
*/
/*
* Calculate m and b for the formula: y = x * m + b
*
*/
var m = (count * sum_xy - sum_x * sum_y) / (count * sum_xx - sum_x * sum_x)
var b = (sum_y / count) - (m * sum_x) / count
@@ -94,14 +94,14 @@ function linreg(values_x, values_y) {
* Handle calculations
*/
LRCIndicator.prototype.calculate = function (price) {
// get the reg
// get the reg
var reg = linreg(this.x, this.history)
// y = a * x + b
// y = a * x + b
this.result = ((this.depth - 1) * reg[0]) + reg[1]
}
exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_STORAGE) {
exports.newUserBot = function newUserBot (bot, logger, COMMONS, UTILITIES, fileStorage) {
const FULL_LOG = false
const LOG_FILE_CONTENT = false
const USE_PARTIAL_LAST_CANDLE = true // When running live the last candle generated is a partial candle.
@@ -117,63 +117,40 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
start: start
}
let gaussStorage = BLOB_STORAGE.newBlobStorage(bot, logger)
let oliviaStorage = BLOB_STORAGE.newBlobStorage(bot, logger)
let utilities = UTILITIES.newCloudUtilities(bot, logger)
let statusDependencies
return thisObject
function initialize(pStatusDependencies, pMonth, pYear, callBackFunction) {
function initialize (pStatusDependencies, pMonth, pYear, callBackFunction) {
try {
logger.fileName = MODULE_NAME
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] initialize -> Entering function.') }
statusDependencies = pStatusDependencies
callBackFunction(global.DEFAULT_OK_RESPONSE)
gaussStorage.initialize(bot.devTeam, onGaussInizialized)
function onGaussInizialized(err) {
if (err.result === global.DEFAULT_OK_RESPONSE.result) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] initialize -> onGaussInizialized -> Initialization Succeed.') }
oliviaStorage.initialize('AAMasters', onOliviaInizialized)
} else {
logger.write(MODULE_NAME, '[ERROR] initialize -> onGaussInizialized -> err = ' + err.message)
callBackFunction(err)
}
}
function onOliviaInizialized(err) {
if (err.result === global.DEFAULT_OK_RESPONSE.result) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] initialize -> onOliviaInizialized -> Initialization Succeed.') }
callBackFunction(global.DEFAULT_OK_RESPONSE)
} else {
logger.write(MODULE_NAME, '[ERROR] initialize -> onOliviaInizialized -> err = ' + err.message)
callBackFunction(err)
}
}
} catch (err) {
logger.write(MODULE_NAME, '[ERROR] initialize -> err = ' + err.message)
callBackFunction(global.DEFAULT_FAIL_RESPONSE)
}
}
/*
This process will do the following:
For every period on the market:
a. Get olivia files for the current execution time
i. If the file exist, validate we can get 63 candles
1) 63 candles exist, proceed
2) 63 candles doesn't exist, call for previous execution day until have 63
ii. File doesn't exist, wait until get the files
b. Once we have the 63 candles, calculate LRC
c. Save LRC points
*/
/*
This process will do the following:
For every period on the market:
a. Get olivia files for the current execution time
i. If the file exist, validate we can get 63 candles
1) 63 candles exist, proceed
2) 63 candles doesn't exist, call for previous execution day until have 63
ii. File doesn't exist, wait until get the files
b. Once we have the 63 candles, calculate LRC
c. Save LRC points
*/
function start(callBackFunction) {
function start (callBackFunction) {
try {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> Entering function.') }
@@ -187,7 +164,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
getContextVariables()
function getContextVariables() {
function getContextVariables () {
try {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> getContextVariables -> Entering function.') }
@@ -256,14 +233,14 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
}
}
function buildLRCPoints() {
function buildLRCPoints () {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> buildLRCPoints -> Entering function.') }
let writeReport = false
advanceTime()
function advanceTime() {
function advanceTime () {
try {
logger.newInternalLoop(bot.codeName, bot.process)
executionTime = new Date(lastCandles[10] + ONE_MIN_IN_MILISECONDS)
@@ -282,7 +259,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
}
}
function isOnHeadOfMarket(timeToValidate, period) {
function isOnHeadOfMarket (timeToValidate, period) {
let candleBeginIndex = 4
let lastCandlePeriodOnCache
let lastDayIndex = period + '/' + timeToValidate.getUTCFullYear() + '/' + utilities.pad(timeToValidate.getUTCMonth() + 1, 2) + '/' + utilities.pad(timeToValidate.getUTCDate(), 2)
@@ -313,12 +290,12 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
}
}
function periodsLoop() {
function periodsLoop () {
let n = 0 // loop Variable representing each possible period as defined at the periods array.
loopBody()
function loopBody() {
function loopBody () {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> buildLRCPoints -> periodsLoop -> loopBody -> Entering function. Loop: ' + n) }
const outputPeriod = global.dailyFilePeriods[n][0]
@@ -327,7 +304,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
isTimeToRun()
function isTimeToRun() {
function isTimeToRun () {
if (executionTime.valueOf() >= nextExecution) {
getLRCPoints()
} else {
@@ -335,7 +312,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
}
}
function getLRCPoints() {
function getLRCPoints () {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> getLRCPoints -> Entering function.') }
const maxLRCDepth = 63
@@ -347,19 +324,19 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
let queryDate = new Date(executionTime)
let candleFile = getDailyFile(queryDate, onDailyFileReceived)
function onDailyFileReceived(err, candleFile) {
function onDailyFileReceived (err, candleFile) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> getLRCPoints -> onDailyFileReceived.') }
if (err.result === global.DEFAULT_OK_RESPONSE.result) {
for (let i = 0; i < candleFile.length; i++) {
let candle = {
open: undefined,
close: undefined,
min: 10000000000000,
max: 0,
begin: undefined,
end: undefined
}
open: undefined,
close: undefined,
min: 10000000000000,
max: 0,
begin: undefined,
end: undefined
}
candle.min = candleFile[i][0]
candle.max = candleFile[i][1]
@@ -375,8 +352,8 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
let lastCandle = executionTime.valueOf() - (outputPeriod * maxLRCDepth)
if (candleArray.length < maxLRCDepth && candle.begin > lastCandle && candle.begin <= executionTime.valueOf()) {
candleArray.push(candle)
}
candleArray.push(candle)
}
}
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> getLRCPoints -> Candle Array Length: ' + candleArray.length) }
@@ -387,32 +364,32 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
let lrcPoint = performLRCCalculations(candleArray)
savePoint(lrcPoint)
} else if (backwardsCount <= maxBackwardsCount) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> getLRCPoints -> Getting file for day before.') }
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> getLRCPoints -> Getting file for day before.') }
queryDate.setDate(queryDate.getDate() - 1)
getDailyFile(queryDate, onDailyFileReceived)
backwardsCount++
} else {
logger.write(MODULE_NAME, '[WARN] start -> getLRCPoints -> Not enough history to calculate LRC.')
queryDate.setDate(queryDate.getDate() - 1)
getDailyFile(queryDate, onDailyFileReceived)
backwardsCount++
} else {
logger.write(MODULE_NAME, '[WARN] start -> getLRCPoints -> Not enough history to calculate LRC.')
// If the control candle is not ready, advance the execution.
if (n === 10) {
lastCandles[10] += ONE_MIN_IN_MILISECONDS
// If the control candle is not ready, advance the execution.
if (n === 10) {
lastCandles[10] += ONE_MIN_IN_MILISECONDS
}
setTimeout(function () { controlLoop() }, 0)
}
setTimeout(function () { controlLoop() }, 0)
}
}
}
function getDailyFile(dateTime, onDailyFileReceived) {
function getDailyFile (dateTime, onDailyFileReceived) {
try {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> getLRCPoints -> getDailyFile -> Entering function.') }
/**
* Let's first check if we can use our own process cache in order to reduce network requests.
* Otherwise, continue with the one from the servers.
*
*/
/**
* Let's first check if we can use our own process cache in order to reduce network requests.
* Otherwise, continue with the one from the servers.
*
*/
let datePath = dateTime.getUTCFullYear() + '/' + utilities.pad(dateTime.getUTCMonth() + 1, 2) + '/' + utilities.pad(dateTime.getUTCDate(), 2)
let cachePosition = n + '/' + datePath
@@ -425,27 +402,26 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
} else {
let filePath = 'AAMasters/AAOlivia.1.0/AACloud.1.1/Poloniex/dataSet.V1/Output/Candles/Multi-Period-Daily/' + folderName + '/' + datePath
let fileName = market.assetA + '_' + market.assetB + '.json'
oliviaStorage.getTextFile(filePath, fileName, onFileReceived)
fileStorage.getTextFile(bot.devTeam, filePath + '/' + fileName, onFileReceived)
}
function onFileReceived(err, text) {
function onFileReceived (err, text) {
if (err.result === global.DEFAULT_OK_RESPONSE.result) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> getLRCPoints -> getDailyFile -> onFileReceived > Entering Function.') }
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> getLRCPoints -> getDailyFile -> onFileReceived > Entering Function.') }
let candleFile = JSON.parse(text)
let candleFile = JSON.parse(text)
dailyFileCache.set(cachePosition, candleFile)
onDailyFileReceived(global.DEFAULT_OK_RESPONSE, candleFile)
} else {
logger.write(MODULE_NAME, '[WARN] start -> getLRCPoints -> getDailyFile -> onFileReceived -> Failed to get the file. Dependency not ready.')
dailyFileCache.set(cachePosition, candleFile)
onDailyFileReceived(global.DEFAULT_OK_RESPONSE, candleFile)
} else {
logger.write(MODULE_NAME, '[WARN] start -> getLRCPoints -> getDailyFile -> onFileReceived -> Failed to get the file. Dependency not ready.')
// If the control candle is not ready, advance the execution.
if (n === 10) {
lastCandles[10] += ONE_MIN_IN_MILISECONDS
// If the control candle is not ready, advance the execution.
if (n === 10) {
lastCandles[10] += ONE_MIN_IN_MILISECONDS
}
setTimeout(function () { controlLoop() }, 0)
}
setTimeout(function () { controlLoop() }, 0)
}
}
} catch (err) {
logger.write(MODULE_NAME, '[ERROR] start -> getLRCPoints -> getDailyFile -> err = ' + err.message)
@@ -453,12 +429,12 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
}
}
function performLRCCalculations(candleArray) {
function performLRCCalculations (candleArray) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> performLRCCalculations -> Entering function.') }
/*
* It's needed to order since it's possible that we need to get another file and it will put an older candle at the end of the array.
*/
/*
* It's needed to order since it's possible that we need to get another file and it will put an older candle at the end of the array.
*/
candleArray.sort(function (a, b) {
return a.begin - b.begin
})
@@ -488,7 +464,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
return lrcPoints
}
function calculateLRC(candlesArray, lrcPoints) {
function calculateLRC (candlesArray, lrcPoints) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> getLRCPoints -> calculateLRC -> Entering function.') }
let lrcMinIndicator = new LRCIndicator(15)
@@ -504,9 +480,9 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
lrcMaxIndicator.update(averagePrice)
}
/*
* Only if there is enough history the result will be calculated
*/
/*
* Only if there is enough history the result will be calculated
*/
if (lrcMinIndicator.result != false && lrcMidIndicator.result != false && lrcMaxIndicator.result != false) {
lrcPoints[2] = lrcMinIndicator.result
lrcPoints[3] = lrcMidIndicator.result
@@ -518,15 +494,15 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
}
}
function savePoint(lrcPoint) {
function savePoint (lrcPoint) {
try {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> savePoint -> Entering function.') }
/**
* Let's first check if we can use our own process cache in order to reduce network requests.
* Otherwise, continue with the one from the servers.
*
*/
/**
* Let's first check if we can use our own process cache in order to reduce network requests.
* Otherwise, continue with the one from the servers.
*
*/
let datePath = executionTime.getUTCFullYear() + '/' + utilities.pad(executionTime.getUTCMonth() + 1, 2) + '/' + utilities.pad(executionTime.getUTCDate(), 2)
let filePath = bot.filePathRoot + '/Output/LRC-Points/Multi-Period-Daily/' + folderName + '/' + datePath
let fileName = market.assetA + '_' + market.assetB + '.json'
@@ -543,31 +519,29 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
let timeToValidate = new Date(executionTime.valueOf() + outputPeriod)
if (isOnHeadOfMarket(timeToValidate, n) || isLastDayExecution()) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> savePoint -> getCurrentContent -> file = ' + fileName + filePath) }
gaussStorage.getTextFile(filePath, fileName, onFileRetrieved)
fileStorage.getTextFile(bot.devTeam, filePath + '/' + fileName, onFileRetrieved)
} else {
// Move to the next period without writing report
// Move to the next period without writing report
setTimeout(function () { controlLoop() }, 0)
}
function onFileRetrieved(err, text) {
function onFileRetrieved (err, text) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> savePoint -> onFileRetrieved -> Entering function.') }
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
if (err.message === 'File does not exist.') {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> savePoint -> onFileRetrieved -> Daily file does not exist, creating folders.') }
if (err.message === 'File does not exist.') {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> savePoint -> onFileRetrieved -> Daily file does not exist, creating folders.') }
createFile()
return
} else {
logger.write(MODULE_NAME, '[ERROR] start -> savePoint -> onFileRetrieved -> err = ' + err.message)
callBackFunction(err)
return
createFile()
return
} else {
logger.write(MODULE_NAME, '[ERROR] start -> savePoint -> onFileRetrieved -> err = ' + err.message)
callBackFunction(err)
return
}
}
}
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> savePoint -> onFileRetrieved -> Daily file exist, appending content.') }
@@ -576,95 +550,94 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
appendToFile(finalContent)
}
function createFile() {
function createFile () {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> savePoint -> createFile -> Entering function.') }
let fileContent = JSON.stringify(lrcPointsFileCache.get(cachePosition))
fileStorage.createTextFile(bot.devTeam, filePath + '/' + fileName, fileContent, onFileCreated)
gaussStorage.createTextFile(filePath, fileName, fileContent, onFileCreated)
function onFileCreated (err) {
function onFileCreated(err) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> savePoint -> onFileCreated -> Entering function.') }
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> savePoint -> onFileCreated -> Entering function.') }
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
logger.write(MODULE_NAME, '[ERROR] start -> savePoint -> onFileCreated -> err = ' + err.message)
callBackFunction(err)
return
}
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
logger.write(MODULE_NAME, '[ERROR] start -> savePoint -> onFileCreated -> err = ' + err.message)
callBackFunction(err)
return
if (LOG_FILE_CONTENT === true) {
logger.write(MODULE_NAME, '[INFO] start -> savePoint -> onFileCreated -> Content written = ' + JSON.stringify(fileContent))
}
saveCleanAndMoveForward()
}
if (LOG_FILE_CONTENT === true) {
logger.write(MODULE_NAME, '[INFO] start -> savePoint -> onFileCreated -> Content written = ' + JSON.stringify(fileContent))
}
saveCleanAndMoveForward()
}
}
function appendToFile(fileContent) {
function appendToFile (fileContent) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> savePoint -> appendToFile -> Entering function.') }
gaussStorage.createTextFile(filePath, fileName, JSON.stringify(fileContent), onExistingFileUpdated)
fileStorage.createTextFile(bot.devTeam, filePath + '/' + fileName, JSON.stringify(fileContent), onExistingFileUpdated)
function onExistingFileUpdated(err) {
function onExistingFileUpdated (err) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> savePoint -> onExistingFileUpdated -> Entering function.') }
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> savePoint -> onExistingFileUpdated -> Entering function.') }
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
logger.write(MODULE_NAME, '[ERROR] start -> savePoint -> onExistingFileUpdated -> err = ' + err.message)
callBackFunction(err)
return
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
logger.write(MODULE_NAME, '[ERROR] start -> savePoint -> onExistingFileUpdated -> err = ' + err.message)
callBackFunction(err)
return
}
if (LOG_FILE_CONTENT === true) {
logger.write(MODULE_NAME, '[INFO] start -> savePoint -> onExistingFileUpdated -> Content written = ' + JSON.stringify(fileContent))
}
saveCleanAndMoveForward()
}
if (LOG_FILE_CONTENT === true) {
logger.write(MODULE_NAME, '[INFO] start -> savePoint -> onExistingFileUpdated -> Content written = ' + JSON.stringify(fileContent))
}
saveCleanAndMoveForward()
}
}
function appendLRCPoints(existingLRCPoints) {
function appendLRCPoints (existingLRCPoints) {
if (existingLRCPoints === undefined) {
return lrcPointsFileCache.get(cachePosition)
}
return lrcPointsFileCache.get(cachePosition)
}
for (i = 0; i < lrcPointsFileCache.get(cachePosition).length; i++) {
let lrcPoint = lrcPointsFileCache.get(cachePosition)[i]
let lastCandleOnFile = existingLRCPoints[existingLRCPoints.length - 1]
let lrcPoint = lrcPointsFileCache.get(cachePosition)[i]
let lastCandleOnFile = existingLRCPoints[existingLRCPoints.length - 1]
if (lastCandleOnFile[0] < lrcPoint[0]) {
existingLRCPoints.push(lrcPoint)
} else {
logger.write(MODULE_NAME, '[WARN] start -> savePoint -> appendLRCPoints -> The LCR Point calculated existed on the file storage period ' + folderName + ', not saving: ' + JSON.stringify(lrcPoint))
if (lastCandleOnFile[0] < lrcPoint[0]) {
existingLRCPoints.push(lrcPoint)
} else {
logger.write(MODULE_NAME, '[WARN] start -> savePoint -> appendLRCPoints -> The LCR Point calculated existed on the file storage period ' + folderName + ', not saving: ' + JSON.stringify(lrcPoint))
}
}
}
return existingLRCPoints
}
function saveCleanAndMoveForward() {
function saveCleanAndMoveForward () {
writeReport = true
cleanUpCache(lrcPointsFileCache, executionTime)
cleanUpCache(dailyFileCache, executionTime)
setTimeout(function () { controlLoop() }, 0)
}
function cleanUpCache(fileCache, dateTime) {
function cleanUpCache (fileCache, dateTime) {
let date = new Date(dateTime)
let datePath = date.getUTCFullYear() + '/' + utilities.pad(date.getUTCMonth() + 1, 2) + '/' + utilities.pad(date.getUTCDate(), 2)
let cachePosition = n + '/' + datePath
// Remove and go back one day
// Remove and go back one day
if (fileCache.has(cachePosition)) {
fileCache.delete(cachePosition)
date.setDate(date.getDate() - 1)
cleanUpCache(fileCache, date)
}
fileCache.delete(cachePosition)
date.setDate(date.getDate() - 1)
cleanUpCache(fileCache, date)
}
}
function isLastDayExecution() {
function isLastDayExecution () {
let date = new Date(executionTime.valueOf())
date.setUTCHours(23)
@@ -674,11 +647,11 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
date.setUTCMilliseconds(0)
if (executionTime >= date.valueOf()) {
return true
}
return true
}
else {
return false
}
return false
}
}
} catch (err) {
logger.write(MODULE_NAME, '[ERROR] start -> savePoint -> err = ' + err.message)
@@ -688,7 +661,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
}
}
function controlLoop() {
function controlLoop () {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> buildLRCPoints -> periodsLoop -> controlLoop -> Entering function. Checking Period: ' + global.dailyFilePeriods[n][1]) }
n++
@@ -703,7 +676,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
let lastOneMinuteCandle = lastCandles[10]
writeDataRange(lastOneMinuteCandle, onWritten)
function onWritten(err) {
function onWritten (err) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> buildLRCPoints -> periodsLoop -> controlLoop -> onWritten -> Entering function.') }
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
@@ -723,7 +696,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
}
}
function writeDataRange(lastOneMinuteCandle, callBack) {
function writeDataRange (lastOneMinuteCandle, callBack) {
try {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDataRange -> Entering function.') }
@@ -737,45 +710,31 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
let fileName = 'Data.Range.' + market.assetA + '_' + market.assetB + '.json'
let filePath = bot.filePathRoot + '/Output/LRC-Points/' + bot.process
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDataRange -> fileName = ' + fileName) }
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDataRange -> filePath = ' + filePath) }
fileStorage.createTextFile(bot.devTeam, filePath + '/' + fileName, fileContentDataRange + '\n', onFileCreated)
utilities.createFolderIfNeeded(filePath, gaussStorage, onFolderCreated)
function onFolderCreated(err) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDataRange -> onFolderCreated -> Entering function.') }
function onFileCreated (err) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDataRange -> onFolderCreated -> onFileCreated -> Entering function.') }
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
logger.write(MODULE_NAME, '[ERROR] start -> writeDataRange -> onFolderCreated -> err = ' + err.message)
logger.write(MODULE_NAME, '[ERROR] start -> writeDataRange -> onFolderCreated -> onFileCreated -> err = ' + err.message)
callBack(err)
return
}
gaussStorage.createTextFile(filePath, fileName, fileContentDataRange + '\n', onFileCreated)
function onFileCreated(err) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeDataRange -> onFolderCreated -> onFileCreated -> Entering function.') }
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
logger.write(MODULE_NAME, '[ERROR] start -> writeDataRange -> onFolderCreated -> onFileCreated -> err = ' + err.message)
callBack(err)
return
}
if (LOG_FILE_CONTENT === true) {
logger.write(MODULE_NAME, '[INFO] start -> writeDataRange -> onFolderCreated -> onFileCreated -> Content written = ' + fileContentDataRange)
}
callBack(global.DEFAULT_OK_RESPONSE)
if (LOG_FILE_CONTENT === true) {
logger.write(MODULE_NAME, '[INFO] start -> writeDataRange -> onFolderCreated -> onFileCreated -> Content written = ' + fileContentDataRange)
}
callBack(global.DEFAULT_OK_RESPONSE)
}
} catch (err) {
logger.write(MODULE_NAME, '[ERROR] start -> writeDataRange -> err = ' + err.message)
callBack(global.DEFAULT_FAIL_RESPONSE)
}
}
function writeStatusReport(callBack) {
function writeStatusReport (callBack) {
try {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> writeStatusReport -> Entering function.') }
+8 -38
View File
@@ -101,7 +101,7 @@ LRCIndicator.prototype.calculate = function (price) {
this.result = ((this.depth - 1) * reg[0]) + reg[1]
}
exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_STORAGE, FILE_STORAGE) {
exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, fileStorage) {
const FULL_LOG = false
const LOG_FILE_CONTENT = false
const USE_PARTIAL_LAST_CANDLE = true // When running live the last candle generated is a partial candle.
@@ -110,18 +110,11 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
const MODULE_NAME = 'User Bot'
const EXCHANGE_NAME = 'Poloniex'
thisObject = {
initialize: initialize,
start: start
}
let gaussStorage = BLOB_STORAGE.newBlobStorage(bot, logger)
let oliviaStorage = BLOB_STORAGE.newBlobStorage(bot, logger)
let utilities = UTILITIES.newCloudUtilities(bot, logger)
let statusDependencies
return thisObject
@@ -133,28 +126,8 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] initialize -> Entering function.') }
statusDependencies = pStatusDependencies
callBackFunction(global.DEFAULT_OK_RESPONSE)
gaussStorage.initialize(bot.devTeam, onGaussInizialized)
function onGaussInizialized(err) {
if (err.result === global.DEFAULT_OK_RESPONSE.result) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] initialize -> onGaussInizialized -> Initialization Succeed.') }
oliviaStorage.initialize('AAMasters', onOliviaInizialized)
} else {
logger.write(MODULE_NAME, '[ERROR] initialize -> onGaussInizialized -> err = ' + err.message)
callBackFunction(err)
}
}
function onOliviaInizialized(err) {
if (err.result === global.DEFAULT_OK_RESPONSE.result) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] initialize -> onOliviaInizialized -> Initialization Succeed.') }
callBackFunction(global.DEFAULT_OK_RESPONSE)
} else {
logger.write(MODULE_NAME, '[ERROR] initialize -> onOliviaInizialized -> err = ' + err.message)
callBackFunction(err)
}
}
} catch (err) {
logger.write(MODULE_NAME, '[ERROR] initialize -> err = ' + err.message)
callBackFunction(global.DEFAULT_FAIL_RESPONSE)
@@ -178,7 +151,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> Entering function.') }
let market = global.MARKET
let reportFilePath = EXCHANGE_NAME + '/Processes/' + bot.process
let reportFilePath = global.EXCHANGE_NAME + '/Processes/' + bot.process
let executionTime
let lastCandles
let marketFileCache = new Map()
@@ -402,10 +375,10 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
onMarketFileReceived(global.DEFAULT_OK_RESPONSE, marketFileCache.get(n))
} else {
let filePath = 'AAMasters/AAOlivia.1.0/AACloud.1.1/Poloniex/dataSet.V1/Output/Candles/Multi-Period-Market/' + folderName
let filePath = 'AAMasters/AAOlivia.1.0/AACloud.1.1/' + global.EXCHANGE_NAME + '/dataSet.V1/Output/Candles/Multi-Period-Market/' + folderName
let fileName = market.assetA + '_' + market.assetB + '.json'
oliviaStorage.getTextFile(filePath, fileName, onFileReceived)
fileStorage.getTextFile(bot.devTeam, filePath + '/' + fileName, onFileReceived)
}
function onFileReceived(err, text) {
@@ -513,9 +486,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
let timeToValidate = new Date(executionTime.valueOf() + outputPeriod)
if (isPeriodOnHeadOfMarket(timeToValidate)) {
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> savePoint -> getCurrentContent -> file = ' + fileName + filePath) }
gaussStorage.getTextFile(filePath, fileName, onFileRetrieved)
fileStorage.getTextFile(bot.devTeam, filePath + '/' + fileName, onFileRetrieved)
} else {
// Move to the next period without writing report
setTimeout(function () { controlLoop() }, 0)
@@ -551,8 +522,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> savePoint -> createFile -> Entering function.') }
let fileContent = JSON.stringify(lrcPointsFileCache.get(n))
gaussStorage.createTextFile(filePath, fileName, fileContent, onFileCreated)
fileStorage.createTextFile(bot.devTeam, filePath + '/' + fileName, fileContent, onFileCreated)
function onFileCreated(err) {
@@ -577,7 +547,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_S
if (FULL_LOG === true) { logger.write(MODULE_NAME, '[INFO] start -> savePoint -> appendToFile -> Entering function.') }
gaussStorage.createTextFile(filePath, fileName, JSON.stringify(fileContent), onExistingFileUpdated)
fileStorage.createTextFile(bot.devTeam, filePath + '/' + fileName, JSON.stringify(fileContent), onExistingFileUpdated)
function onExistingFileUpdated(err) {