diff --git a/.env.local b/.env.local index ab3193771..fb69d8efa 100644 --- a/.env.local +++ b/.env.local @@ -1,6 +1,21 @@ NODE_ENV=production -PORT=4011 +PORT=4004 + +# LOG_LEVEL=info MONGO_URL=mongodb://mongo/saweb +GATEWAY_ENDPOINT=http://master-app-api:4100/graphql PRESHARED_GATEWAY_KEY=saweb_key + +# The NODE_ENDPOINT is used at team creation and is where other components will search for the team +NODE_ENDPOINT=http://docker.for.win.localhost:4100/ + +# Storage Configuration +STORAGE_PROVIDER=Minio +# Minio +MINIO_END_POINT=minio +MINIO_PORT=9000 +MINIO_USE_SSL=false +MINIO_ACCESS_KEY=minio +MINIO_SECRET_KEY=minio123 diff --git a/package-lock.json b/package-lock.json index 52d8bf200..689905ac4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -38,6 +38,17 @@ "tslib": "^1.9.2", "uuid": "^3.2.1", "xml2js": "^0.4.19" + }, + "dependencies": { + "axios": { + "version": "0.18.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.18.1.tgz", + "integrity": "sha512-0BfJq4NSfQXd+SkFdrvFbG7addhYSBA2mQwISr46pD6E5iqkWg02RAs8vyTT/j0RTnoYmeXauBuSv1qKwR179g==", + "requires": { + "follow-redirects": "1.5.10", + "is-buffer": "^2.0.2" + } + } } }, "@azure/storage-blob": { @@ -228,6 +239,27 @@ "uri-js": "^4.2.2" } }, + "ansi-colors": { + "version": "3.2.3", + "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-3.2.3.tgz", + "integrity": "sha512-LEHHyuhlPY3TmuUYMh2oz89lTShfvgbmzaBcxve9t/9Wuy7Dwf4yoAKcND7KFT1HAQfqZ12qtc+DUrBMeKF9nw==", + "dev": true + }, + "ansi-regex": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz", + "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=", + "dev": true + }, + "ansi-styles": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", + "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", + "dev": true, + "requires": { + "color-convert": "^1.9.0" + } + }, "apollo-cache-control": { "version": "0.6.0", "resolved": "https://registry.npmjs.org/apollo-cache-control/-/apollo-cache-control-0.6.0.tgz", @@ -446,6 +478,15 @@ } } }, + "argparse": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", + "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", + "dev": true, + "requires": { + "sprintf-js": "~1.0.2" + } + }, "array-flatten": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", @@ -464,6 +505,12 @@ "resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz", "integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=" }, + "assertion-error": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", + "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==", + "dev": true + }, "async": { "version": "2.6.1", "resolved": "https://registry.npmjs.org/async/-/async-2.6.1.tgz", @@ -501,12 +548,12 @@ "integrity": "sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==" }, "axios": { - "version": "0.18.0", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.18.0.tgz", - "integrity": "sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=", + "version": "0.19.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.0.tgz", + "integrity": "sha512-1uvKqKQta3KBxIz14F2v06AEHZ/dIoeKfbTRkK1E5oqjDnuEerLmYTgJB5AiQZHJcljpg1TuRzdjDR06qNk0DQ==", "requires": { - "follow-redirects": "^1.3.0", - "is-buffer": "^1.1.5" + "follow-redirects": "1.5.10", + "is-buffer": "^2.0.2" } }, "azure-storage": { @@ -570,6 +617,12 @@ "resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz", "integrity": "sha1-MasayLEpNjRj41s+u2n038+6eUc=" }, + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true + }, "bcrypt-pbkdf": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz", @@ -578,6 +631,45 @@ "tweetnacl": "^0.14.3" } }, + "block-stream2": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/block-stream2/-/block-stream2-1.1.0.tgz", + "integrity": "sha1-xzjjqRupd+u14f70MeE8oR2GOeI=", + "requires": { + "defined": "^1.0.0", + "inherits": "^2.0.1", + "readable-stream": "^2.0.4" + }, + "dependencies": { + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, + "readable-stream": { + "version": "2.3.6", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", + "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "~5.1.0" + } + } + } + }, "bluebird": { "version": "3.5.1", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz", @@ -600,6 +692,22 @@ "type-is": "~1.6.17" } }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "browser-stdout": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz", + "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==", + "dev": true + }, "browserify-mime": { "version": "1.2.9", "resolved": "https://registry.npmjs.org/browserify-mime/-/browserify-mime-1.2.9.tgz", @@ -610,6 +718,11 @@ "resolved": "https://registry.npmjs.org/bson/-/bson-1.0.9.tgz", "integrity": "sha512-IQX9/h7WdMBIW/q/++tGd+emQr0XMdeZ6icnT/74Xk9fnabWn+gZgpE+9V+gujL3hhJOoNrnDVY7tWdzc7NUTg==" }, + "buffer-from": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", + "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==" + }, "busboy": { "version": "0.2.14", "resolved": "https://registry.npmjs.org/busboy/-/busboy-0.2.14.tgz", @@ -624,11 +737,76 @@ "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==" }, + "camelcase": { + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", + "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==", + "dev": true + }, "caseless": { "version": "0.12.0", "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz", "integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=" }, + "chai": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/chai/-/chai-4.2.0.tgz", + "integrity": "sha512-XQU3bhBukrOsQCuwZndwGcCVQHyZi53fQ6Ys1Fym7E4olpIqqZZhhoFJoaKVvV17lWQoXYwgWN2nF5crA8J2jw==", + "dev": true, + "requires": { + "assertion-error": "^1.1.0", + "check-error": "^1.0.2", + "deep-eql": "^3.0.1", + "get-func-name": "^2.0.0", + "pathval": "^1.1.0", + "type-detect": "^4.0.5" + } + }, + "chalk": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "dev": true, + "requires": { + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" + }, + "dependencies": { + "supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "dev": true, + "requires": { + "has-flag": "^3.0.0" + } + } + } + }, + "check-error": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz", + "integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=", + "dev": true + }, + "cliui": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-4.1.0.tgz", + "integrity": "sha512-4FG+RSG9DL7uEwRUZXZn3SS34DiDPfzP0VOiEwtUWlE+AR2EIg+hSyvrIgUUfhdgR/UkAeW2QHgeP+hWrXs7jQ==", + "dev": true, + "requires": { + "string-width": "^2.1.1", + "strip-ansi": "^4.0.0", + "wrap-ansi": "^2.0.0" + } + }, + "code-point-at": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz", + "integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=", + "dev": true + }, "color": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/color/-/color-3.0.0.tgz", @@ -687,6 +865,52 @@ "delayed-stream": "~1.0.0" } }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true + }, + "concat-stream": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz", + "integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==", + "requires": { + "buffer-from": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^2.2.2", + "typedarray": "^0.0.6" + }, + "dependencies": { + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, + "readable-stream": { + "version": "2.3.6", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", + "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "~5.1.0" + } + } + } + }, "content-disposition": { "version": "0.5.2", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.2.tgz", @@ -726,6 +950,19 @@ "vary": "^1" } }, + "cross-spawn": { + "version": "6.0.5", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz", + "integrity": "sha512-eTVLrBSt7fjbDygz805pMnstIs2VTBNkRm0qxZd+M7A5XDdxVRWO5MxGBXZhjY4cqLYLdtrGqRf8mBPmzwSpWQ==", + "dev": true, + "requires": { + "nice-try": "^1.0.4", + "path-key": "^2.0.1", + "semver": "^5.5.0", + "shebang-command": "^1.2.0", + "which": "^1.2.9" + } + }, "dashdash": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz", @@ -742,6 +979,21 @@ "ms": "2.0.0" } }, + "decamelize": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz", + "integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=", + "dev": true + }, + "deep-eql": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-3.0.1.tgz", + "integrity": "sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==", + "dev": true, + "requires": { + "type-detect": "^4.0.0" + } + }, "define-properties": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz", @@ -750,6 +1002,11 @@ "object-keys": "^1.0.12" } }, + "defined": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/defined/-/defined-1.0.0.tgz", + "integrity": "sha1-yY2bzvdWdBiOEQlpFRGZ45sfppM=" + }, "delayed-stream": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", @@ -789,10 +1046,21 @@ "streamsearch": "0.1.2" } }, + "diff": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz", + "integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==", + "dev": true + }, "dotenv": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-6.0.0.tgz", - "integrity": "sha512-FlWbnhgjtwD+uNLUGHbMykMOYQaTivdHEmYwAKFjn6GKe/CqY0fNae93ZHTd20snh9ZLr8mTzIL9m0APQ1pjQg==" + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.0.0.tgz", + "integrity": "sha512-30xVGqjLjiUOArT4+M5q9sYdvuR4riM6yK9wMcas9Vbp6zZa+ocC9dp6QoftuhTPhFAiLK/0C5Ni2nou/Bk8lg==" + }, + "double-ended-queue": { + "version": "2.1.0-0", + "resolved": "https://registry.npmjs.org/double-ended-queue/-/double-ended-queue-2.1.0-0.tgz", + "integrity": "sha1-ED01J/0xUo9AGIEwyEHv3XgmTlw=" }, "ecc-jsbn": { "version": "0.1.2", @@ -808,6 +1076,12 @@ "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" }, + "emoji-regex": { + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz", + "integrity": "sha512-CwBLREIQ7LvYFB0WyRvwhq5N5qPhc6PMjD6bYggFlI5YyDgl+0vxq5VHbMOFqLg7hfWzmu8T5Z1QofhmTIhItA==", + "dev": true + }, "enabled": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/enabled/-/enabled-1.0.2.tgz", @@ -821,6 +1095,15 @@ "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", "integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=" }, + "end-of-stream": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.1.tgz", + "integrity": "sha512-1MkrZNvWTKCaigbn+W15elq2BB/L22nqrSY5DKlo3X6+vclJm8Bb5djXJBmEX6fS3+zCh/F4VBK5Z2KxJt4s2Q==", + "dev": true, + "requires": { + "once": "^1.4.0" + } + }, "env-variable": { "version": "0.0.5", "resolved": "https://registry.npmjs.org/env-variable/-/env-variable-0.0.5.tgz", @@ -849,16 +1132,33 @@ "is-symbol": "^1.0.2" } }, + "es6-error": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/es6-error/-/es6-error-2.1.1.tgz", + "integrity": "sha1-kThDAexe0cmnJH0RKCRyFvA1R80=" + }, "escape-html": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", "integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=" }, + "escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", + "dev": true + }, "esm": { "version": "3.2.25", "resolved": "https://registry.npmjs.org/esm/-/esm-3.2.25.tgz", "integrity": "sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==" }, + "esprima": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", + "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==", + "dev": true + }, "etag": { "version": "1.8.1", "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", @@ -874,6 +1174,21 @@ "resolved": "https://registry.npmjs.org/events/-/events-3.0.0.tgz", "integrity": "sha512-Dc381HFWJzEOhQ+d8pkNon++bk9h6cdAoAj4iE6Q4y6xgTzySWXlKn05/TVNpjnfRqi/X0EpJEJohPjNI3zpVA==" }, + "execa": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/execa/-/execa-1.0.0.tgz", + "integrity": "sha512-adbxcyWV46qiHyvSp50TKt05tB4tK3HcmF7/nxfAdhnox83seTDbwnaqKO4sXRy7roHAIFqJP/Rw/AuEbX61LA==", + "dev": true, + "requires": { + "cross-spawn": "^6.0.0", + "get-stream": "^4.0.0", + "is-stream": "^1.1.0", + "npm-run-path": "^2.0.0", + "p-finally": "^1.0.0", + "signal-exit": "^3.0.0", + "strip-eof": "^1.0.0" + } + }, "express": { "version": "4.16.3", "resolved": "https://registry.npmjs.org/express/-/express-4.16.3.tgz", @@ -1056,26 +1371,47 @@ } } }, - "follow-redirects": { - "version": "1.7.0", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.7.0.tgz", - "integrity": "sha512-m/pZQy4Gj287eNy94nivy5wchN3Kp+Q5WgUPNy5lJSZ3sgkVKSYV/ZChMAQVIgx1SqfZ2zBZtPA2YlXIWxxJOQ==", + "find-up": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz", + "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==", + "dev": true, "requires": { - "debug": "^3.2.6" + "locate-path": "^3.0.0" + } + }, + "flat": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/flat/-/flat-4.1.0.tgz", + "integrity": "sha512-Px/TiLIznH7gEDlPXcUD4KnBusa6kR6ayRUVcnEAbreRIuhkqow/mun59BuRXwoYk7ZQOLW1ZM05ilIvK38hFw==", + "dev": true, + "requires": { + "is-buffer": "~2.0.3" + }, + "dependencies": { + "is-buffer": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.3.tgz", + "integrity": "sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw==", + "dev": true + } + } + }, + "follow-redirects": { + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", + "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", + "requires": { + "debug": "=3.1.0" }, "dependencies": { "debug": { - "version": "3.2.6", - "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz", - "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", "requires": { - "ms": "^2.1.1" + "ms": "2.0.0" } - }, - "ms": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", - "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==" } } }, @@ -1109,11 +1445,38 @@ "resolved": "https://registry.npmjs.org/fs-capacitor/-/fs-capacitor-2.0.4.tgz", "integrity": "sha512-8S4f4WsCryNw2mJJchi46YgB6CR5Ze+4L1h8ewl9tEpL4SJ3ZO+c/bS4BWhB8bK+O3TMqhuZarTitd0S0eh2pA==" }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true + }, "function-bind": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" }, + "get-caller-file": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", + "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==", + "dev": true + }, + "get-func-name": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.0.tgz", + "integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=", + "dev": true + }, + "get-stream": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz", + "integrity": "sha512-GMat4EJ5161kIy2HevLlr4luNjBgvmj413KaQA7jt4V8B4RDsfpHk7WQ9GVqfYyyx8OS/L66Kox+rJRNklLK7w==", + "dev": true, + "requires": { + "pump": "^3.0.0" + } + }, "getpass": { "version": "0.1.7", "resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz", @@ -1122,6 +1485,20 @@ "assert-plus": "^1.0.0" } }, + "glob": { + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.3.tgz", + "integrity": "sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ==", + "dev": true, + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + }, "graphql": { "version": "0.13.2", "resolved": "https://registry.npmjs.org/graphql/-/graphql-0.13.2.tgz", @@ -1192,6 +1569,12 @@ } } }, + "growl": { + "version": "1.10.5", + "resolved": "https://registry.npmjs.org/growl/-/growl-1.10.5.tgz", + "integrity": "sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==", + "dev": true + }, "har-schema": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz", @@ -1214,6 +1597,12 @@ "function-bind": "^1.1.1" } }, + "has-flag": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", + "dev": true + }, "has-symbols": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.0.tgz", @@ -1228,6 +1617,12 @@ "safe-buffer": "^5.0.1" } }, + "he": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", + "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==", + "dev": true + }, "http-errors": { "version": "1.7.2", "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", @@ -1258,11 +1653,27 @@ "safer-buffer": ">= 2.1.2 < 3" } }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "dev": true, + "requires": { + "once": "^1.3.0", + "wrappy": "1" + } + }, "inherits": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" }, + "invert-kv": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/invert-kv/-/invert-kv-2.0.0.tgz", + "integrity": "sha512-wPVv/y/QQ/Uiirj/vh3oP+1Ww+AWehmi1g5fFWGPF6IpCBCDVrhgHRMvrLfdYcwDh3QJbGXDW4JAuzxElLSqKA==", + "dev": true + }, "ipaddr.js": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.0.tgz", @@ -1274,9 +1685,9 @@ "integrity": "sha512-eVRqCvVlZbuw3GrM63ovNSNAeA1K16kaR/LRY/92w0zxQ5/1YzwblUX652i4Xs9RwAGjW9d9y6X88t8OaAJfWQ==" }, "is-buffer": { - "version": "1.1.6", - "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz", - "integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==" + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.3.tgz", + "integrity": "sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw==" }, "is-callable": { "version": "1.1.4", @@ -1288,6 +1699,12 @@ "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.1.tgz", "integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY=" }, + "is-fullwidth-code-point": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", + "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=", + "dev": true + }, "is-regex": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz", @@ -1319,6 +1736,12 @@ "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=" }, + "isexe": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", + "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=", + "dev": true + }, "isstream": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", @@ -1329,6 +1752,16 @@ "resolved": "https://registry.npmjs.org/iterall/-/iterall-1.2.2.tgz", "integrity": "sha512-yynBb1g+RFUPY64fTrFv7nsjRrENBQJaX2UL+2Szc9REFrSNm1rpSXHGzhmAy7a9uv3vlvgBlXnf9RqmPH1/DA==" }, + "js-yaml": { + "version": "3.13.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz", + "integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==", + "dev": true, + "requires": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + } + }, "jsbn": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz", @@ -1357,6 +1790,11 @@ "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==" }, + "json-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/json-stream/-/json-stream-1.0.0.tgz", + "integrity": "sha1-GjhU4o0rvuqzHMfd9oPS3cVlJwg=" + }, "json-stringify-safe": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz", @@ -1391,6 +1829,25 @@ "colornames": "^1.1.1" } }, + "lcid": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/lcid/-/lcid-2.0.0.tgz", + "integrity": "sha512-avPEb8P8EGnwXKClwsNUgryVjllcRqtMYa49NTsbQagYuT1DcXnl1915oxWjoyGrXR6zH/Y0Zc96xWsPcoDKeA==", + "dev": true, + "requires": { + "invert-kv": "^2.0.0" + } + }, + "locate-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", + "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==", + "dev": true, + "requires": { + "p-locate": "^3.0.0", + "path-exists": "^3.0.0" + } + }, "lodash": { "version": "4.17.11", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", @@ -1406,6 +1863,15 @@ "resolved": "https://registry.npmjs.org/lodash.sortby/-/lodash.sortby-4.7.0.tgz", "integrity": "sha1-7dFMgk4sycHgsKG0K7UhBRakJDg=" }, + "log-symbols": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-2.2.0.tgz", + "integrity": "sha512-VeIAFslyIerEJLXHziedo2basKbMKtTw3vfn5IzG0XTjhAVEJyNHnL2p7vc+wBDSdQuUpNw3M2u6xb9QsAY5Eg==", + "dev": true, + "requires": { + "chalk": "^2.0.1" + } + }, "logform": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/logform/-/logform-2.1.2.tgz", @@ -1438,6 +1904,15 @@ "yallist": "^3.0.2" } }, + "map-age-cleaner": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/map-age-cleaner/-/map-age-cleaner-0.1.3.tgz", + "integrity": "sha512-bJzx6nMoP6PDLPBFmg7+xRKeFZvFboMrGlxmNj9ClvX53KrmvM5bXFXEWjbz4cz1AFn+jWJ9z/DJSz7hrs0w3w==", + "dev": true, + "requires": { + "p-defer": "^1.0.0" + } + }, "md5.js": { "version": "1.3.4", "resolved": "https://registry.npmjs.org/md5.js/-/md5.js-1.3.4.tgz", @@ -1452,6 +1927,17 @@ "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" }, + "mem": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/mem/-/mem-4.3.0.tgz", + "integrity": "sha512-qX2bG48pTqYRVmDB37rn/6PT7LcR8T7oAX3bf99u1Tt1nzxYfxkgqDwUwolPlXweM0XzBOBFzSx4kfp7KP1s/w==", + "dev": true, + "requires": { + "map-age-cleaner": "^0.1.1", + "mimic-fn": "^2.0.0", + "p-is-promise": "^2.0.0" + } + }, "memory-pager": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", @@ -1486,6 +1972,109 @@ "mime-db": "1.40.0" } }, + "mimic-fn": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", + "integrity": "sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==", + "dev": true + }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + }, + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=" + }, + "minio": { + "version": "7.0.8", + "resolved": "https://registry.npmjs.org/minio/-/minio-7.0.8.tgz", + "integrity": "sha512-nPVmtms5cARaaqCCH3n0n0jIMaaG+y8Ka5s0/YA8X5/fvZoHmDmaIoJtvSwZc72L/gQgqEJqcIvBMrbjW7NkHQ==", + "requires": { + "async": "^1.5.2", + "block-stream2": "^1.0.0", + "concat-stream": "^1.4.8", + "es6-error": "^2.0.2", + "json-stream": "^1.0.0", + "lodash": "^4.14.2", + "mime-types": "^2.1.14", + "mkdirp": "^0.5.1", + "querystring": "0.2.0", + "through2": "^0.6.5", + "uuid": "^3.1.0", + "xml": "^1.0.0", + "xml2js": "^0.4.15" + }, + "dependencies": { + "async": { + "version": "1.5.2", + "resolved": "https://registry.npmjs.org/async/-/async-1.5.2.tgz", + "integrity": "sha1-7GphrlZIDAw8skHJVhjiCJL5Zyo=" + } + } + }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "requires": { + "minimist": "0.0.8" + } + }, + "mocha": { + "version": "6.1.4", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-6.1.4.tgz", + "integrity": "sha512-PN8CIy4RXsIoxoFJzS4QNnCH4psUCPWc4/rPrst/ecSJJbLBkubMiyGCP2Kj/9YnWbotFqAoeXyXMucj7gwCFg==", + "dev": true, + "requires": { + "ansi-colors": "3.2.3", + "browser-stdout": "1.3.1", + "debug": "3.2.6", + "diff": "3.5.0", + "escape-string-regexp": "1.0.5", + "find-up": "3.0.0", + "glob": "7.1.3", + "growl": "1.10.5", + "he": "1.2.0", + "js-yaml": "3.13.1", + "log-symbols": "2.2.0", + "minimatch": "3.0.4", + "mkdirp": "0.5.1", + "ms": "2.1.1", + "node-environment-flags": "1.0.5", + "object.assign": "4.1.0", + "strip-json-comments": "2.0.1", + "supports-color": "6.0.0", + "which": "1.3.1", + "wide-align": "1.1.3", + "yargs": "13.2.2", + "yargs-parser": "13.0.0", + "yargs-unparser": "1.5.0" + }, + "dependencies": { + "debug": { + "version": "3.2.6", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz", + "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==", + "dev": true, + "requires": { + "ms": "^2.1.1" + } + }, + "ms": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", + "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==", + "dev": true + } + } + }, "mongodb": { "version": "3.1.6", "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.1.6.tgz", @@ -1575,11 +2164,42 @@ "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" }, + "nice-try": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz", + "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==", + "dev": true + }, + "node-environment-flags": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/node-environment-flags/-/node-environment-flags-1.0.5.tgz", + "integrity": "sha512-VNYPRfGfmZLx0Ye20jWzHUjyTW/c+6Wq+iLhDzUI4XmhrDd9l/FozXV3F2xOaXjvp0co0+v1YSR3CMP6g+VvLQ==", + "dev": true, + "requires": { + "object.getownpropertydescriptors": "^2.0.3", + "semver": "^5.7.0" + } + }, "node-fetch": { "version": "2.6.0", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" }, + "npm-run-path": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-2.0.2.tgz", + "integrity": "sha1-NakjLfo11wZ7TLLd8jV7GHFTbF8=", + "dev": true, + "requires": { + "path-key": "^2.0.0" + } + }, + "number-is-nan": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz", + "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=", + "dev": true + }, "oauth-sign": { "version": "0.9.0", "resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz", @@ -1600,6 +2220,18 @@ "resolved": "https://registry.npmjs.org/object-path/-/object-path-0.11.4.tgz", "integrity": "sha1-NwrnUvvzfePqcKhhwju6iRVpGUk=" }, + "object.assign": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/object.assign/-/object.assign-4.1.0.tgz", + "integrity": "sha512-exHJeq6kBKj58mqGyTQ9DFvrZC/eR6OwxzoM9YRoGBqrXYonaFyGiFMuc9VZrXf7DarreEwMpurG3dd+CNyW5w==", + "dev": true, + "requires": { + "define-properties": "^1.1.2", + "function-bind": "^1.1.1", + "has-symbols": "^1.0.0", + "object-keys": "^1.0.11" + } + }, "object.getownpropertydescriptors": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/object.getownpropertydescriptors/-/object.getownpropertydescriptors-2.0.3.tgz", @@ -1617,21 +2249,107 @@ "ee-first": "1.1.1" } }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "dev": true, + "requires": { + "wrappy": "1" + } + }, "one-time": { "version": "0.0.4", "resolved": "https://registry.npmjs.org/one-time/-/one-time-0.0.4.tgz", "integrity": "sha1-+M33eISCb+Tf+T46nMN7HkSAdC4=" }, + "os-locale": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/os-locale/-/os-locale-3.1.0.tgz", + "integrity": "sha512-Z8l3R4wYWM40/52Z+S265okfFj8Kt2cC2MKY+xNi3kFs+XGI7WXu/I309QQQYbRW4ijiZ+yxs9pqEhJh0DqW3Q==", + "dev": true, + "requires": { + "execa": "^1.0.0", + "lcid": "^2.0.0", + "mem": "^4.0.0" + } + }, + "p-defer": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/p-defer/-/p-defer-1.0.0.tgz", + "integrity": "sha1-n26xgvbJqozXQwBKfU+WsZaw+ww=", + "dev": true + }, + "p-finally": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz", + "integrity": "sha1-P7z7FbiZpEEjs0ttzBi3JDNqLK4=", + "dev": true + }, + "p-is-promise": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/p-is-promise/-/p-is-promise-2.1.0.tgz", + "integrity": "sha512-Y3W0wlRPK8ZMRbNq97l4M5otioeA5lm1z7bkNkxCka8HSPjR0xRWmpCmc9utiaLP9Jb1eD8BgeIxTW4AIF45Pg==", + "dev": true + }, + "p-limit": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.0.tgz", + "integrity": "sha512-pZbTJpoUsCzV48Mc9Nh51VbwO0X9cuPFE8gYwx9BTCt9SF8/b7Zljd2fVgOxhIF/HDTKgpVzs+GPhyKfjLLFRQ==", + "dev": true, + "requires": { + "p-try": "^2.0.0" + } + }, + "p-locate": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz", + "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==", + "dev": true, + "requires": { + "p-limit": "^2.0.0" + } + }, + "p-try": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", + "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==", + "dev": true + }, "parseurl": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==" }, + "path-exists": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=", + "dev": true + }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true + }, + "path-key": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/path-key/-/path-key-2.0.1.tgz", + "integrity": "sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=", + "dev": true + }, "path-to-regexp": { "version": "0.1.7", "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, + "pathval": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.0.tgz", + "integrity": "sha1-uULm1L3mUwBe9rcTYd74cn0GReA=", + "dev": true + }, "performance-now": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", @@ -1683,6 +2401,16 @@ "resolved": "https://registry.npmjs.org/psl/-/psl-1.1.31.tgz", "integrity": "sha512-/6pt4+C+T+wZUieKR620OpzN/LlnNKuWjy1iFLQ/UG35JqHlR/89MP1d96dUfkf6Dne3TuLQzOYEYshJ+Hx8mw==" }, + "pump": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", + "integrity": "sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==", + "dev": true, + "requires": { + "end-of-stream": "^1.1.0", + "once": "^1.3.1" + } + }, "punycode": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", @@ -1693,6 +2421,11 @@ "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==" }, + "querystring": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz", + "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=" + }, "range-parser": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", @@ -1720,6 +2453,26 @@ "string_decoder": "~0.10.x" } }, + "redis": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/redis/-/redis-2.8.0.tgz", + "integrity": "sha512-M1OkonEQwtRmZv4tEWF2VgpG0JWJ8Fv1PhlgT5+B+uNq2cA3Rt1Yt/ryoR+vQNOQcIEgdCdfH0jr3bDpihAw1A==", + "requires": { + "double-ended-queue": "^2.1.0-0", + "redis-commands": "^1.2.0", + "redis-parser": "^2.6.0" + } + }, + "redis-commands": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.5.0.tgz", + "integrity": "sha512-6KxamqpZ468MeQC3bkWmCB1fp56XL64D4Kf0zJSwDZbVLLm7KFkoIcHrgRvQ+sk8dnhySs7+yBg94yIkAK7aJg==" + }, + "redis-parser": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-2.6.0.tgz", + "integrity": "sha1-Uu0J2srBCPGmMcB+m2mUHnoZUEs=" + }, "regenerator-runtime": { "version": "0.13.2", "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.2.tgz", @@ -1778,6 +2531,18 @@ } } }, + "require-directory": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", + "integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I=", + "dev": true + }, + "require-main-filename": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-2.0.0.tgz", + "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==", + "dev": true + }, "require_optional": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz", @@ -1880,6 +2645,12 @@ "send": "0.16.2" } }, + "set-blocking": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz", + "integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc=", + "dev": true + }, "setprototypeof": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz", @@ -1894,6 +2665,27 @@ "safe-buffer": "^5.0.1" } }, + "shebang-command": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-1.2.0.tgz", + "integrity": "sha1-RKrGW2lbAzmJaMOfNj/uXer98eo=", + "dev": true, + "requires": { + "shebang-regex": "^1.0.0" + } + }, + "shebang-regex": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-1.0.0.tgz", + "integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=", + "dev": true + }, + "signal-exit": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz", + "integrity": "sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=", + "dev": true + }, "simple-swizzle": { "version": "0.2.2", "resolved": "https://registry.npmjs.org/simple-swizzle/-/simple-swizzle-0.2.2.tgz", @@ -1916,6 +2708,12 @@ "memory-pager": "^1.0.2" } }, + "sprintf-js": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", + "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", + "dev": true + }, "sshpk": { "version": "1.16.1", "resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.16.1.tgz", @@ -1947,11 +2745,42 @@ "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-0.1.2.tgz", "integrity": "sha1-gIudDlb8Jz2Am6VzOOkpkZoanxo=" }, + "string-width": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", + "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==", + "dev": true, + "requires": { + "is-fullwidth-code-point": "^2.0.0", + "strip-ansi": "^4.0.0" + } + }, "string_decoder": { "version": "0.10.31", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=" }, + "strip-ansi": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz", + "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=", + "dev": true, + "requires": { + "ansi-regex": "^3.0.0" + } + }, + "strip-eof": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", + "integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=", + "dev": true + }, + "strip-json-comments": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", + "integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo=", + "dev": true + }, "subscriptions-transport-ws": { "version": "0.9.16", "resolved": "https://registry.npmjs.org/subscriptions-transport-ws/-/subscriptions-transport-ws-0.9.16.tgz", @@ -1974,6 +2803,15 @@ } } }, + "supports-color": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-6.0.0.tgz", + "integrity": "sha512-on9Kwidc1IUQo+bQdhi8+Tijpo0e1SS6RoGo2guUwn5vdaxw8RXOF9Vb2ws+ihWOmh4JnCJOvaziZWP1VABaLg==", + "dev": true, + "requires": { + "has-flag": "^3.0.0" + } + }, "symbol-observable": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/symbol-observable/-/symbol-observable-1.2.0.tgz", @@ -1984,6 +2822,28 @@ "resolved": "https://registry.npmjs.org/text-hex/-/text-hex-1.0.0.tgz", "integrity": "sha512-uuVGNWzgJ4yhRaNSiubPY7OjISw4sw4E5Uv0wbjp+OzcbmVU/rsT8ujgcXJhn9ypzsgr5vlzpPqP+MBBKcGvbg==" }, + "through2": { + "version": "0.6.5", + "resolved": "https://registry.npmjs.org/through2/-/through2-0.6.5.tgz", + "integrity": "sha1-QaucZ7KdVyCQcUEOHXp6lozTrUg=", + "requires": { + "readable-stream": ">=1.0.33-1 <1.1.0-0", + "xtend": ">=4.0.0 <4.1.0-0" + }, + "dependencies": { + "readable-stream": { + "version": "1.0.34", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.0.34.tgz", + "integrity": "sha1-Elgg40vIQtLyqq+v5MKRbuMsFXw=", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.1", + "isarray": "0.0.1", + "string_decoder": "~0.10.x" + } + } + } + }, "toidentifier": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", @@ -2029,6 +2889,12 @@ "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz", "integrity": "sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q=" }, + "type-detect": { + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz", + "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==", + "dev": true + }, "type-is": { "version": "1.6.18", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", @@ -2038,6 +2904,11 @@ "mime-types": "~2.1.24" } }, + "typedarray": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", + "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=" + }, "underscore": { "version": "1.8.3", "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz", @@ -2100,6 +2971,30 @@ "extsprintf": "^1.2.0" } }, + "which": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", + "integrity": "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==", + "dev": true, + "requires": { + "isexe": "^2.0.0" + } + }, + "which-module": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.0.tgz", + "integrity": "sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho=", + "dev": true + }, + "wide-align": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.3.tgz", + "integrity": "sha512-QGkOQc8XL6Bt5PwnsExKBPuMKBxnGxWWW3fU55Xt4feHozMUhdUMaBCk290qpm/wG5u/RSKzwdAC4i51YigihA==", + "dev": true, + "requires": { + "string-width": "^1.0.2 || 2" + } + }, "winston": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/winston/-/winston-3.2.1.tgz", @@ -2174,6 +3069,59 @@ } } }, + "wrap-ansi": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz", + "integrity": "sha1-2Pw9KE3QV5T+hJc8rs3Rz4JP3YU=", + "dev": true, + "requires": { + "string-width": "^1.0.1", + "strip-ansi": "^3.0.1" + }, + "dependencies": { + "ansi-regex": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", + "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=", + "dev": true + }, + "is-fullwidth-code-point": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz", + "integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=", + "dev": true, + "requires": { + "number-is-nan": "^1.0.0" + } + }, + "string-width": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", + "integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=", + "dev": true, + "requires": { + "code-point-at": "^1.0.0", + "is-fullwidth-code-point": "^1.0.0", + "strip-ansi": "^3.0.0" + } + }, + "strip-ansi": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", + "dev": true, + "requires": { + "ansi-regex": "^2.0.0" + } + } + } + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", + "dev": true + }, "ws": { "version": "6.2.1", "resolved": "https://registry.npmjs.org/ws/-/ws-6.2.1.tgz", @@ -2182,6 +3130,11 @@ "async-limiter": "~1.0.0" } }, + "xml": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/xml/-/xml-1.0.1.tgz", + "integrity": "sha1-eLpyAgApxbyHuKgaPPzXS0ovweU=" + }, "xml2js": { "version": "0.4.19", "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz", @@ -2196,11 +3149,134 @@ "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz", "integrity": "sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0=" }, + "xtend": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz", + "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=" + }, + "y18n": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz", + "integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==", + "dev": true + }, "yallist": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.0.3.tgz", "integrity": "sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==" }, + "yargs": { + "version": "13.2.2", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-13.2.2.tgz", + "integrity": "sha512-WyEoxgyTD3w5XRpAQNYUB9ycVH/PQrToaTXdYXRdOXvEy1l19br+VJsc0vcO8PTGg5ro/l/GY7F/JMEBmI0BxA==", + "dev": true, + "requires": { + "cliui": "^4.0.0", + "find-up": "^3.0.0", + "get-caller-file": "^2.0.1", + "os-locale": "^3.1.0", + "require-directory": "^2.1.1", + "require-main-filename": "^2.0.0", + "set-blocking": "^2.0.0", + "string-width": "^3.0.0", + "which-module": "^2.0.0", + "y18n": "^4.0.0", + "yargs-parser": "^13.0.0" + }, + "dependencies": { + "ansi-regex": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", + "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==", + "dev": true + }, + "string-width": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz", + "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==", + "dev": true, + "requires": { + "emoji-regex": "^7.0.1", + "is-fullwidth-code-point": "^2.0.0", + "strip-ansi": "^5.1.0" + } + }, + "strip-ansi": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", + "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", + "dev": true, + "requires": { + "ansi-regex": "^4.1.0" + } + } + } + }, + "yargs-parser": { + "version": "13.0.0", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-13.0.0.tgz", + "integrity": "sha512-w2LXjoL8oRdRQN+hOyppuXs+V/fVAYtpcrRxZuF7Kt/Oc+Jr2uAcVntaUTNT6w5ihoWfFDpNY8CPx1QskxZ/pw==", + "dev": true, + "requires": { + "camelcase": "^5.0.0", + "decamelize": "^1.2.0" + } + }, + "yargs-unparser": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/yargs-unparser/-/yargs-unparser-1.5.0.tgz", + "integrity": "sha512-HK25qidFTCVuj/D1VfNiEndpLIeJN78aqgR23nL3y4N0U/91cOAzqfHlF8n2BvoNDcZmJKin3ddNSvOxSr8flw==", + "dev": true, + "requires": { + "flat": "^4.1.0", + "lodash": "^4.17.11", + "yargs": "^12.0.5" + }, + "dependencies": { + "get-caller-file": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-1.0.3.tgz", + "integrity": "sha512-3t6rVToeoZfYSGd8YoLFR2DJkiQrIiUrGcjvFX2mDw3bn6k2OtwHN0TNCLbBO+w8qTvimhDkv+LSscbJY1vE6w==", + "dev": true + }, + "require-main-filename": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-1.0.1.tgz", + "integrity": "sha1-l/cXtp1IeE9fUmpsWqj/3aBVpNE=", + "dev": true + }, + "yargs": { + "version": "12.0.5", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-12.0.5.tgz", + "integrity": "sha512-Lhz8TLaYnxq/2ObqHDql8dX8CJi97oHxrjUcYtzKbbykPtVW9WB+poxI+NM2UIzsMgNCZTIf0AQwsjK5yMAqZw==", + "dev": true, + "requires": { + "cliui": "^4.0.0", + "decamelize": "^1.2.0", + "find-up": "^3.0.0", + "get-caller-file": "^1.0.1", + "os-locale": "^3.0.0", + "require-directory": "^2.1.1", + "require-main-filename": "^1.0.1", + "set-blocking": "^2.0.0", + "string-width": "^2.0.0", + "which-module": "^2.0.0", + "y18n": "^3.2.1 || ^4.0.0", + "yargs-parser": "^11.1.1" + } + }, + "yargs-parser": { + "version": "11.1.1", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-11.1.1.tgz", + "integrity": "sha512-C6kB/WJDiaxONLJQnF8ccx9SEeoTTLek8RVbaOIsrAUS8VrBEXfmeSnCZxygc+XC2sNMBIwOOnfcxiynjHsVSQ==", + "dev": true, + "requires": { + "camelcase": "^5.0.0", + "decamelize": "^1.2.0" + } + } + } + }, "zen-observable": { "version": "0.8.14", "resolved": "https://registry.npmjs.org/zen-observable/-/zen-observable-0.8.14.tgz", diff --git a/package.json b/package.json index 6dab68f4f..3e4f6357e 100644 --- a/package.json +++ b/package.json @@ -13,19 +13,23 @@ "test": "echo \"Error: no test specified\" && exit 1" }, "devDependencies": { - "express": "^4.16.4" + "express": "^4.16.4", + "chai": "^4.2.0", + "mocha": "^6.1.4" }, "dependencies": { "@azure/storage-blob": "^10.3.0", "apollo-server-express": "2.1.0", - "axios": "0.18.0", + "axios": "0.19.0", "azure-storage": "^2.10.3", - "dotenv": "6.0.0", + "dotenv": "8.0.0", "esm": "^3.2.22", "express": "4.16.3", "graphql": "0.13.2", "json-beautify": "^1.1.0", + "minio": "^7.0.8", "mongoose": "5.3.4", + "redis": "^2.8.0", "winston": "3.2.1" } } diff --git a/src/app.js b/src/app.js index 69f1f14ce..74855f021 100644 --- a/src/app.js +++ b/src/app.js @@ -5,9 +5,13 @@ import schema from './schema'; import wrongPreshared from './errors/notAllowed.json'; import db from './db'; import logger from './utils/logger' +import bodyParser from 'body-parser' const app = express(); +app.use(bodyParser.json({limit: '50mb'})); +app.use(bodyParser.urlencoded({limit: '50mb', extended: true})); + app.post('/graphql', (req, res, next) => { if (req.headers.preshared === process.env.PRESHARED_GATEWAY_KEY) { next(); diff --git a/src/config/ecosystem.json b/src/config/ecosystem.json index 656159ce7..3641c3fe7 100644 --- a/src/config/ecosystem.json +++ b/src/config/ecosystem.json @@ -5,10 +5,10 @@ "codeName": "AAMasters", "displayName": "Masters", "host": { - "url": "https://platform-api-dev.superalgos.org/", - "storage": "https://aadevelop.blob.core.windows.net", + "url": "https://demo-api-dev.superalgos.org/", + "storage": "localStorage", "container": "aamasters", - "accessKey": "?st=2019-05-21T12%3A00%3A00Z&se=2020-05-22T12%3A00%3A00Z&sp=rl&sv=2018-03-28&sr=c&sig=PsGM67klPfLjdfjymziEQEQcD5Wzy3SMcZWsTCQrhd8%3D" + "accessKey": "" }, "bots": [ { @@ -725,10 +725,10 @@ "codeName": "AAVikings", "displayName": "Vikings", "host": { - "url": "https://platform-api-dev.superalgos.org/", + "url": "https://demo-api-dev.superalgos.org/", "container": "aavikings", - "storage": "https://aadevelop.blob.core.windows.net", - "accessKey": "?st=2019-05-20T12%3A00%3A00Z&se=2020-05-21T12%3A00%3A00Z&sp=rl&sv=2018-03-28&sr=c&sig=hJuV9aEP4KVZo5z54FEF1BQtkbksfwkTJFv1FYkHFRg%3D" + "storage": "localStorage", + "accessKey": "" }, "bots": [ { @@ -832,10 +832,10 @@ "codeName": "AAMasters", "displayName": "AA Arena", "host": { - "url": "https://platform-api-dev.superalgos.org/", - "storage": "https://aadevelop.blob.core.windows.net", + "url": "https://demo-api-dev.superalgos.org/", + "storage": "localStorage", "container": "aaplatform", - "accessKey": "?sv=2018-03-28&ss=b&srt=co&sp=rw&se=2020-01-04T00:00:00Z&st=2019-01-08T11:25:04Z&spr=https&sig=xZU1PO%2BdtVilqlZp%2BVRWW55LYl8oGZU7x4JnP%2BhMc%2FY%3D" + "accessKey": "" }, "competitions": [ { @@ -849,10 +849,10 @@ "devTeam": "AAMasters", "codeName": "PlottersROI", "host": { - "url": "https://platform-api-dev.superalgos.org/", - "storage": "https://aadevelop.blob.core.windows.net", + "url": "https://demo-api-dev.superalgos.org/", + "storage": "localStorage", "container": "aamasters", - "accessKey": "?st=2019-05-21T12%3A00%3A00Z&se=2020-05-22T12%3A00%3A00Z&sp=rl&sv=2018-03-28&sr=c&sig=PsGM67klPfLjdfjymziEQEQcD5Wzy3SMcZWsTCQrhd8%3D" + "accessKey": "" }, "moduleName": "CombinedProfits", "repo": "Plotters-ROI" diff --git a/src/models/ecosystem.js b/src/models/ecosystem.js index 827a3ae42..1e6a2b9d8 100644 --- a/src/models/ecosystem.js +++ b/src/models/ecosystem.js @@ -30,7 +30,7 @@ const ecosystemSchema = new Schema({ }, accessKey: { type: String, - required: true + required: false } }, bots: [{ @@ -200,7 +200,7 @@ const ecosystemSchema = new Schema({ }, accessKey: { type: String, - required: true + required: false } }, competitions: [{ @@ -252,7 +252,7 @@ const ecosystemSchema = new Schema({ }, accessKey: { type: String, - required: true + required: false } }, repo: { diff --git a/src/schema/mutations/AddTeam.js b/src/schema/mutations/AddTeam.js index 81030d41f..94f557eb6 100644 --- a/src/schema/mutations/AddTeam.js +++ b/src/schema/mutations/AddTeam.js @@ -3,7 +3,9 @@ import { AuthenticationError, WrongArgumentsError } from '../../errors' import { TeamType } from '../types' import { Ecosystem } from '../../models' import { TeamInputType } from '../types/input' -import { copyBots } from '../../storage/CopyBots' +import { copyBot } from '../../storage/CopyBot' +import { copySimulator } from '../../storage/CopySimulator' +import { createContainer, writeFileContent } from '../../storage/providers/MinioStorage' export const args = { team: { type: TeamInputType } } @@ -34,23 +36,22 @@ const resolve = async (parent, { team }, context) => { } } - let storage = require ('../../storage/providers/AzureStorage') // Default value - if (process.env.STORAGE_PROVIDER === 'Minio') { - storage = require ('../../storage/providers/MinioStorage') - } + // Teams will be created on a local minio container + let botsConfigurations = [] + await createContainer(team.codeName) + let tradingBot = await copyBot(team.codeName, team.bot.codeName, team.bot.displayName, writeFileContent) + botsConfigurations.push(tradingBot) + let simulatorBot = await copySimulator(team.codeName, team.bot.codeName, team.bot.displayName, writeFileContent) + botsConfigurations.push(simulatorBot) - let containerName = team.codeName - await storage.createContainer(containerName) - let accessKey = storage.createPrivateAccessKey(containerName, "WRITE", 365) - - let botsConfigurations = await copyBots(storage, team) team.bots = botsConfigurations + if (!team.host) { let nodeEndpoint = { - url: process.env.SUPERALGOS_NODE_ENDPOINT, - storage: process.env.STORAGE_URL, + url: process.env.NODE_ENDPOINT, + storage: 'localStorage', container: team.codeName, - accessKey: accessKey, + accessKey: '', } team.host = nodeEndpoint } diff --git a/src/schema/mutations/CreateFile.js b/src/schema/mutations/CreateFile.js new file mode 100644 index 000000000..ab2992f8b --- /dev/null +++ b/src/schema/mutations/CreateFile.js @@ -0,0 +1,28 @@ +import logger from '../../utils/logger' +import { FileInputType } from '../types/input' +import { GraphQLString } from 'graphql' +import { writeFileContent } from '../../storage/providers/MinioStorage' + +export const args = { file: { type: FileInputType } } + +const resolve = async (parent, { file }, context) => { + logger.debug('createFile -> Entering Function.') + try { + //TODO Add permissions + await writeFileContent(file.container, file.filePath, file.fileContent) + return 'File created.' + } catch (err) { + logger.error('createFile -> Error: %s', err.message) + throw err + } +} + +const mutation = { + createFile: { + type: GraphQLString, + args, + resolve, + } +} + +export default mutation; diff --git a/src/schema/mutations/DeleteTeam.js b/src/schema/mutations/DeleteTeam.js index 9e8ee3626..f604bdac6 100644 --- a/src/schema/mutations/DeleteTeam.js +++ b/src/schema/mutations/DeleteTeam.js @@ -3,7 +3,7 @@ import { AuthenticationError, WrongArgumentsError } from '../../errors' import { TeamType } from '../types' import { Ecosystem } from '../../models' import { DeleteTeamInputType } from '../types/input' -import { deleteTeam } from '../../storage/DeleteTeam' +import { deleteContainer } from '../../storage/providers/MinioStorage' export const args = { team: { type: DeleteTeamInputType } } @@ -30,7 +30,7 @@ const resolve = async (parent, { team }, context) => { if (auxTeam.codeName === team.codeName) { logger.debug('deleteTeam -> Team found, removing.') teamFound = true - await deleteTeam(team) + await deleteContainer(team.codeName) userEcosystem.devTeams.splice(i, 1) await userEcosystem.save() break diff --git a/src/schema/mutations/index.js b/src/schema/mutations/index.js index 85ef11366..7e0b35711 100644 --- a/src/schema/mutations/index.js +++ b/src/schema/mutations/index.js @@ -1,16 +1,18 @@ import { GraphQLObjectType, -} from 'graphql'; +} from 'graphql' -import AddTeamMutation from './AddTeam'; -import DeleteTeamMutation from './DeleteTeam'; +import AddTeamMutation from './AddTeam' +import DeleteTeamMutation from './DeleteTeam' +import CreateFileMutation from './CreateFile' const Mutation = new GraphQLObjectType({ name: 'Mutation', fields: Object.assign( AddTeamMutation, - DeleteTeamMutation - ), -}); + DeleteTeamMutation, + CreateFileMutation + ) +}) -export default Mutation; +export default Mutation diff --git a/src/schema/queries/FileContent.js b/src/schema/queries/FileContent.js index 0c5ce7515..7c6e5e532 100644 --- a/src/schema/queries/FileContent.js +++ b/src/schema/queries/FileContent.js @@ -1,6 +1,8 @@ import logger from '../../utils/logger' import { FileInputType } from '../types/input' import { GraphQLString } from 'graphql' +import { getFileContent } from '../../storage/providers/MinioStorage' +import { getFileContentRemote } from '../../storage/providers/AzureStorage' export const args = { file: { type: FileInputType } } @@ -9,31 +11,18 @@ const resolve = async (parent, { file }, context) => { try { - let storage = require('../../storage/providers/AzureStorage') // Default value - if (process.env.STORAGE_PROVIDER === 'Minio') { - storage = require('../storage/providers/MinioStorage') + let fileContent + if (file.storage === 'localStorage') { + //TODO Add permissions + fileContent = await getFileContent(file.container, file.filePath) + } else { + fileContent = await getFileContentRemote(file.container, file.filePath, file.storage, file.accessKey) } - //Temporary fix for plotter and plotter pannels - if (file.storage === '' && file.accessKey === ''){ - let defaultEcosystem = require('../../config/ecosystem.json') - for (let index = 0; index < defaultEcosystem.devTeams.length; index++) { - const devTeam = defaultEcosystem.devTeams[index]; - if(devTeam.codeName.toLowerCase() === file.container){ - file.storage = devTeam.host.storage - file.accessKey = devTeam.host.accessKey - } - } - } - if (file.storage === '' || file.accessKey === ''){ - throw "Wrong parameters." - } - - let fileContent = await storage.getFileContentRemote(file.container, file.filePath, file.storage, file.accessKey) return fileContent } catch (err) { - logger.error('getFileContent -> Error: %s', err.stack) + logger.error('getFileContent -> Error: %s', err.message) throw err } } diff --git a/src/schema/queries/PlotterCode.js b/src/schema/queries/PlotterCode.js new file mode 100644 index 000000000..b16afd5c6 --- /dev/null +++ b/src/schema/queries/PlotterCode.js @@ -0,0 +1,53 @@ +import logger from '../../utils/logger' +import { FileInputType } from '../types/input' +import { GraphQLString } from 'graphql' +import { getFileContent } from '../../storage/providers/MinioStorage' +import { getFileContentRemote } from '../../storage/providers/AzureStorage' + +export const args = { file: { type: FileInputType } } + +const resolve = async (parent, { file }, context) => { + logger.debug('getPlotterCode -> Entering Function: ' + file.filePath) + + try { + + let fileContent + if (file.storage === '' || file.storage === undefined) { + //TODO Add permissions + fileContent = await getFileContent(file.container, file.filePath) + } else { + // TODO Temporary fix for plotter and plotter pannels + if (file.storage === '' && file.accessKey === '') { + let defaultEcosystem = require('../../config/ecosystem.json') + for (let index = 0; index < defaultEcosystem.devTeams.length; index++) { + const devTeam = defaultEcosystem.devTeams[index]; + if (devTeam.codeName.toLowerCase() === file.container) { + file.storage = devTeam.host.storage + file.accessKey = devTeam.host.accessKey + } + } + } + if (file.storage === '' || file.accessKey === '') { + throw "Wrong parameters." + } + fileContent = await getFileContentRemote(file.container, file.filePath, file.storage, file.accessKey) + } + + return fileContent + + } catch (err) { + logger.error('getPlotterCode -> Error: %s', err) + logger.error('getPlotterCode -> Stack: %s', err.stack) + throw err + } +} + +const query = { + plotterCode: { + type: GraphQLString, + args, + resolve, + } +} + +export default query diff --git a/src/schema/queries/index.js b/src/schema/queries/index.js index 1a9a0fbf6..2551f88ec 100644 --- a/src/schema/queries/index.js +++ b/src/schema/queries/index.js @@ -4,12 +4,14 @@ import { import EcosystemQuery from './Ecosystem'; import FileContentQuery from './FileContent'; +import PlotterCodeQuery from './PlotterCode'; const RootQuery = new GraphQLObjectType({ name: 'RootQueryType', fields: Object.assign( EcosystemQuery, - FileContentQuery + FileContentQuery, + PlotterCodeQuery ), }); diff --git a/src/schema/types/input/File.js b/src/schema/types/input/File.js index d92d4c6f0..7b69397ae 100644 --- a/src/schema/types/input/File.js +++ b/src/schema/types/input/File.js @@ -11,7 +11,7 @@ const Type = new GraphQLInputObjectType({ container: { type: new GraphQLNonNull(GraphQLString) }, filePath: { type: new GraphQLNonNull(GraphQLString) }, storage: { type: new GraphQLNonNull(GraphQLString) }, - accessKey: { type: new GraphQLNonNull(GraphQLString) }, + accessKey: { type: GraphQLString }, fileContent: { type: GraphQLString } }), }); diff --git a/src/storage/CopyBot.js b/src/storage/CopyBot.js index 309bb8808..76483c90f 100644 --- a/src/storage/CopyBot.js +++ b/src/storage/CopyBot.js @@ -1,14 +1,13 @@ import beautify from 'json-beautify' import logger from '../utils/logger' +import readFile from '../utils/readFile' -export const copyBot = async (storage, teamCodeName, botCodeName, botDisplayName) => { +export const copyBot = async (teamCodeName, botCodeName, botDisplayName, writeFileContent) => { try { logger.debug('copyBot started for bot %s', botCodeName) - - let containerName = 'aavikings' - let templatePath = 'AAVikings/bots/blumblebee-Trading-Bot' - let config = await storage.getFileContent(containerName, templatePath + '/this.bot.config.json') - let code = await storage.getFileContent(containerName, templatePath + '/Trading-Process/User.Bot.js') + // File imports + const botCode = await readFile('./src/storage/templates/TradingBot/User.Bot.js') + const config = await readFile('./src/storage/templates/TradingBot/this.bot.config.json') // Change config to adapt the new indicator let parsedConfig = JSON.parse(config) @@ -29,32 +28,14 @@ export const copyBot = async (storage, teamCodeName, botCodeName, botDisplayName parsedConfig.processes[0].dataDependencies[2].devTeam = teamCodeName parsedConfig.processes[0].dataDependencies[2].bot = 'simulator-' + botCodeName - // Write the new files + // Creating a new trading bot let newBotPath = teamCodeName + '/bots/' + botCodeName + '-Trading-Bot' - await storage.writeFileContent(teamCodeName, newBotPath + '/this.bot.config.json', beautify(parsedConfig, null, 2, 80)) - await storage.writeFileContent(teamCodeName, newBotPath + '/Trading-Bot/User.Bot.js', code) + await writeFileContent(teamCodeName, newBotPath + '/Trading-Process/User.Bot.js', botCode) + await writeFileContent(teamCodeName, newBotPath + '/this.bot.config.json', beautify(parsedConfig, null, 2, 80)) - logger.debug('copyBot completed for bot %s', botCodeName) return parsedConfig } catch (err) { logger.error('copyBot error for bot: %s. %s', botCodeName, err) throw err } } - -export const removeBot = async (storage, teamCodeName, botCodeName) => { - try { - logger.debug('removeBot started for bot %s', botCodeName) - - // Delete bot code - let newBotPath = teamCodeName + '/bots/' + 'bot-' + botCodeName - await storage.deleteBlob(teamCodeName, newBotPath + '/this.bot.config.json') - await storage.deleteBlob(teamCodeName, newBotPath + '/Trading-Bot/User.Bot.js') - - logger.debug('removeBot completed for bot %s', botCodeName) - - } catch (err) { - logger.error('copyBot error for bot %s: %s', botCodeName, err) - throw err - } -} diff --git a/src/storage/CopyBots.js b/src/storage/CopyBots.js deleted file mode 100644 index ede1b352b..000000000 --- a/src/storage/CopyBots.js +++ /dev/null @@ -1,22 +0,0 @@ -import logger from '../utils/logger' -import { copyBot } from './CopyBot' -import { copySimulator } from './CopySimulator' - -export const copyBots = async (storage, team) => { - try { - logger.debug('copyBots started for team %s', team.codeName) - let bots = [] - - let tradingBot = await copyBot(storage, team.codeName, team.bot.codeName, team.bot.displayName) - bots.push(tradingBot) - - let simulatorBot = await copySimulator(storage, team.codeName, team.bot.codeName, team.bot.displayName) - bots.push(simulatorBot) - - return bots - - } catch (err) { - logger.error('copyBots error for teamCodeName: %s, error: %s', team.bot.codeName, err.stack) - throw err - } -} diff --git a/src/storage/CopySimulator.js b/src/storage/CopySimulator.js index 1931ee9df..9bcb240f2 100644 --- a/src/storage/CopySimulator.js +++ b/src/storage/CopySimulator.js @@ -1,16 +1,16 @@ import beautify from 'json-beautify' import logger from '../utils/logger' +import readFile from '../utils/readFile' -export const copySimulator = async (storage, teamCodeName, botCodeName, botDisplayName) => { +export const copySimulator = async (teamCodeName, botCodeName, botDisplayName, writeFileContent) => { try { logger.debug('copySimulator started for bot %s', 'simulator-' + botCodeName) - let containerName = 'aamasters' - let templatePath = 'AAMasters/bots/AAJason-Indicator-Bot' - let commons = await storage.getFileContent(containerName, templatePath + '/Commons.js') - let config = await storage.getFileContent(containerName, templatePath + '/this.bot.config.json') - let codeDaily = await storage.getFileContent(containerName, templatePath + '/Multi-Period-Daily/User.Bot.js') - let codeMarket = await storage.getFileContent(containerName, templatePath + '/Multi-Period-Market/User.Bot.js') + // File imports + const commons = await readFile('./src/storage/templates/Simulator/Commons.js') + const config = await readFile('./src/storage/templates/Simulator/this.bot.config.json') + const codeDaily = await readFile('./src/storage/templates/Simulator/User.Bot.Daily.js') + const codeMarket = await readFile('./src/storage/templates/Simulator/User.Bot.Market.js') // Change config to adapt the new indicator let newBotCodeName = 'simulator-' + botCodeName @@ -47,12 +47,12 @@ export const copySimulator = async (storage, teamCodeName, botCodeName, botDispl parsedConfig.products[3].dataSets[1].dataRange.filePath = teamCodeName + '/' + parsedConfig.codeName + '.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Trades/Multi-Period-Daily' // Write the new files - let newSimulatorName = 'simulator-' + newBotCodeName + '-Indicator-Bot' + let newSimulatorName = newBotCodeName + '-Indicator-Bot' let newSimulatorPath = teamCodeName + '/bots/' + newSimulatorName - await storage.writeFileContent(teamCodeName, newSimulatorPath + '/Commons.js', commons) - await storage.writeFileContent(teamCodeName, newSimulatorPath + '/this.bot.config.json', beautify(parsedConfig, null, 2, 80)) - await storage.writeFileContent(teamCodeName, newSimulatorPath + '/Multi-Period-Daily/User.Bot.js', codeDaily) - await storage.writeFileContent(teamCodeName, newSimulatorPath + '/Multi-Period-Market/User.Bot.js', codeMarket) + await writeFileContent(teamCodeName, newSimulatorPath + '/Commons.js', commons) + await writeFileContent(teamCodeName, newSimulatorPath + '/this.bot.config.json', beautify(parsedConfig, null, 2, 80)) + await writeFileContent(teamCodeName, newSimulatorPath + '/Multi-Period-Daily/User.Bot.js', codeDaily) + await writeFileContent(teamCodeName, newSimulatorPath + '/Multi-Period-Market/User.Bot.js', codeMarket) logger.debug('copySimulator completed for bot %s', newBotCodeName) return parsedConfig diff --git a/src/storage/DeleteTeam.js b/src/storage/DeleteTeam.js deleted file mode 100644 index 1f984d634..000000000 --- a/src/storage/DeleteTeam.js +++ /dev/null @@ -1,18 +0,0 @@ -import logger from '../utils/logger' - -export const deleteTeam = async (team) => { - try { - logger.debug('deleteTeam started for team %s', team.codeName) - - let storage = require ('./providers/AzureStorage') // Default value - if (process.env.STORAGE_PROVIDER === 'Minio') { - storage = require ('./providers/MinioStorage') - } - - await storage.deleteContainer(team.codeName) - - } catch (err) { - logger.error('deleteTeam error for teamCodeName: %s, error: %s', team.codeName, err.stack) - throw err - } -} diff --git a/src/storage/providers/AzureStorage.js b/src/storage/providers/AzureStorage.js index 0992f7efb..d820fbbb7 100644 --- a/src/storage/providers/AzureStorage.js +++ b/src/storage/providers/AzureStorage.js @@ -1,4 +1,3 @@ -import logger from '../../utils/logger' import { Aborter, BlobURL, @@ -10,9 +9,9 @@ import { AnonymousCredential } from '@azure/storage-blob' -const AzureServiceURL = () => { +const getService = () => { // Use SharedKeyCredential with storage account and account key - const sharedKeyCredential = new SharedKeyCredential(process.env.STORAGE_BASE_URL, process.env.STORAGE_CONNECTION_STRING); + const sharedKeyCredential = new SharedKeyCredential(process.env.AZURE_BASE_URL, process.env.AZURE_CONNECTION_STRING); // Use sharedKeyCredential, tokenCredential or anonymousCredential to create a pipeline const pipeline = StorageURL.newPipeline(sharedKeyCredential, { @@ -22,18 +21,16 @@ const AzureServiceURL = () => { // List containers const serviceURL = new ServiceURL( // When using AnonymousCredential, following url should include a valid SAS or support public access - `https://${process.env.STORAGE_BASE_URL}.blob.core.windows.net`, + `https://${process.env.AZURE_BASE_URL}.blob.core.windows.net`, pipeline ) return serviceURL } -const serviceURL = AzureServiceURL() - export const getFileContentRemote = async (containerName, blobName, storage, accessKey) => { const pipeline = StorageURL.newPipeline(new AnonymousCredential(), { retryOptions: { maxTries: 10 } - }); + }) const serviceURLWithAccessKey = new ServiceURL( `${storage}${accessKey}`, pipeline @@ -45,14 +42,14 @@ export const getFileContentRemote = async (containerName, blobName, storage, acc } export const getFileContent = async (containerName, blobName) => { - const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName) + const containerURL = ContainerURL.fromServiceURL(getService(), containerName) const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName) let downloadResponse = await blockBlobURL.download(Aborter.none, 0) return await streamToString(downloadResponse.readableStreamBody) } export const writeFileContent = async (containerName, blobName, content) => { - const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName) + const containerURL = ContainerURL.fromServiceURL(getService(), containerName) const blobURL = BlobURL.fromContainerURL(containerURL, blobName) const blockBlobURL = BlockBlobURL.fromBlobURL(blobURL) await blockBlobURL.upload( @@ -63,77 +60,32 @@ export const writeFileContent = async (containerName, blobName, content) => { } export const deleteBlob = async (containerName, blobName) => { - try { - const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName) - const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName) - await blockBlobURL.delete(Aborter.none) - } catch (err) { - logger.warn('Error deleting the file: %s%s', containerName, blobName) - } + const containerURL = ContainerURL.fromServiceURL(getService(), containerName) + const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName) + await blockBlobURL.delete(Aborter.none) } export const deleteContainer = async (containerName) => { - const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName) + const containerURL = ContainerURL.fromServiceURL(getService(), containerName) await containerURL.delete(Aborter.none) - logger.debug('deleteContainer -> Successful.') } export const createContainer = async (containerName) => { - const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName) + const containerURL = ContainerURL.fromServiceURL(getService(), containerName) await containerURL.create(Aborter.none) - logger.debug('createContainer -> Successful.') } // A helper method used to read a Node.js readable stream into string async function streamToString(readableStream) { return new Promise((resolve, reject) => { - const chunks = []; + const chunks = [] readableStream.on('data', data => { chunks.push(data.toString()); - }); + }) readableStream.on('end', () => { - resolve(chunks.join('')); - }); + resolve(chunks.join('')) + }) readableStream.on('error', reject) - }); + }) } -export const createPrivateAccessKey = (container, type, days) => { - let azure = require('azure-storage'); - let blobService = azure.createBlobService(process.env.AZURE_STORAGE_CONNECTION_STRING); - - let startDate = new Date(); - startDate.setDate(startDate.getDate() - 1); - - let expiryDate = new Date(startDate); - expiryDate.setDate(startDate.getDate() + days); - - let azurePermissions; - - switch (type) { - case 'READ': { - azurePermissions = azure.BlobUtilities.SharedAccessPermissions.READ; - break; - } - case 'WRITE': { - azurePermissions = azure.BlobUtilities.SharedAccessPermissions.WRITE; - break; - } - case 'DELETE': { - azurePermissions = azure.BlobUtilities.SharedAccessPermissions.DELETE; - break; - } - } - - let blobName = undefined; - - let sharedAccessPolicy = { - AccessPolicy: { - Permissions: azurePermissions, - Start: startDate, - Expiry: expiryDate - } - }; - - return blobService.generateSharedAccessSignature(container, blobName, sharedAccessPolicy); -} diff --git a/src/storage/providers/MinioStorage.js b/src/storage/providers/MinioStorage.js new file mode 100644 index 000000000..1da17c11e --- /dev/null +++ b/src/storage/providers/MinioStorage.js @@ -0,0 +1,99 @@ +import { Client } from 'minio' + +const getService = () => { + let service = new Client({ + endPoint: process.env.MINIO_END_POINT, + port: JSON.parse(process.env.MINIO_PORT), + useSSL: JSON.parse(process.env.MINIO_USE_SSL), + accessKey: process.env.MINIO_ACCESS_KEY, + secretKey: process.env.MINIO_SECRET_KEY + }) + return service +} + +export const getFileContent = async (containerName, blobName) => { + let service = getService() + let chunks = [] + let fileContent = await new Promise(function (resolve, reject) { + service.getObject(containerName, blobName, function (error, dataStream) { + if (error) { + reject(error) + } + if (dataStream != undefined) { + dataStream.on('data', function (chunk) { + chunks.push(chunk.toString()) + }) + dataStream.on('end', function () { + resolve(chunks.join('')) + }) + dataStream.on('error', function (e) { + reject(e) + }) + } else { + resolve('') + } + }) + }) + return fileContent +} + +export const writeFileContent = async (containerName, blobName, content) => { + let service = getService() + await new Promise(function (resolve, reject) { + service.putObject(containerName, blobName, content, 'text/plain', function (error) { + if (error) { + reject(error) + } + resolve() + }) + }) +} + +export const createContainer = async (containerName) => { + let service = getService() + await new Promise(function (resolve, reject) { + service.makeBucket(containerName, '', function (error) { + if (error) { + reject(error) + } + resolve() + }) + }) +} + +export const deleteContainer = async (containerName) => { + let service = getService() + let objectsStream = service.listObjects(containerName, '', true) + let totalObjects = 0 + let removed = 0 + objectsStream.on('data', async function (obj) { + totalObjects++ + await new Promise(function (resolve, reject) { + service.removeObject(containerName, obj.name, function (e) { + if (e) reject(e) + removed++ + resolve() + }) + }) + }) + objectsStream.on('error', function (e) { + throw e + }) + objectsStream.on('end', async function (e) { + await removeBucket() + }) + + function removeBucket() { + if (totalObjects === removed) { + new Promise(function (resolve, reject) { + service.removeBucket(containerName, function (e) { + if (e) reject(e) + resolve() + }) + }) + } else { + setTimeout(removeBucket, 2000) + } + } +} + diff --git a/src/storage/templates/Simulator/Commons.js b/src/storage/templates/Simulator/Commons.js new file mode 100644 index 000000000..d0f98ae4b --- /dev/null +++ b/src/storage/templates/Simulator/Commons.js @@ -0,0 +1,1842 @@ +const strategy = require('./Integrations/Strategy') + +exports.newCommons = function newCommons(bot, logger, UTILITIES) { + + const { orderMessage } = require("@superalgos/mqservice") + + const { + MESSAGE_ENTITY, MESSAGE_TYPE, ORDER_CREATOR, ORDER_TYPE, + ORDER_OWNER, ORDER_DIRECTION, ORDER_STATUS, ORDER_EXIT_OUTCOME, + createMessage, getMessage, getExpandedMessage + } = orderMessage.newOrderMessage() + + + const FULL_LOG = true; + const LOG_FILE_CONTENT = false; + + const MODULE_NAME = "Commons"; + const ONE_DAY_IN_MILISECONDS = 24 * 60 * 60 * 1000; + const GMT_SECONDS = ':00.000 GMT+0000'; + + let thisObject = { + initializeData: initializeData, + runSimulation: runSimulation, + buildLRC: buildLRC, + buildPercentageBandwidthMap: buildPercentageBandwidthMap, + buildBollingerBandsMap: buildBollingerBandsMap, + buildBollingerChannelsArray: buildBollingerChannelsArray, + buildBollingerSubChannelsArray: buildBollingerSubChannelsArray, + buildCandles: buildCandles + }; + + let utilities = UTILITIES.newCloudUtilities(bot, logger); + + let LRCMap = new Map(); + let percentageBandwidthMap = new Map(); + let bollingerBandsMap = new Map(); + let bollingerChannelsArray = []; + let bollingerSubChannelsArray = []; + + let candles = []; + + return thisObject; + + function initializeData() { + + LRCMap = new Map(); + percentageBandwidthMap = new Map(); + bollingerBandsMap = new Map(); + bollingerChannelsArray = []; + bollingerSubChannelsArray = []; + + candles = []; + } + + async function runSimulation( + recordsArray, + conditionsArray, + strategiesArray, + tradesArray, + lastObjectsArray, + timePeriod, + currentDay, + startDate, + endDate, + interExecutionMemory, + callback) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> Entering function."); } + + let tradingSystem = await strategy.getStrategy(); + + /* Initial Default Values */ + + let initialDate = startDate; + + const DEFAULT_BASE_ASSET_BALANCE = 1 + const DEFAULT_BASE_ASSET_MINIMUN_BALANCE = 0.5 + const DEFAULT_BASE_ASSET_MAXIMUN_BALANCE = 2 + + let initialBalanceA = DEFAULT_BASE_ASSET_BALANCE + let minimunBalanceA = DEFAULT_BASE_ASSET_MINIMUN_BALANCE + let maximunBalanceA = DEFAULT_BASE_ASSET_MAXIMUN_BALANCE + let initialBalanceB = 0 + let minimunBalanceB = 0 + let maximunBalanceB = 0 + let baseAsset = 'BTC' + + /* Parameters Processing */ + + if (tradingSystem.parameters !== undefined) { + if (tradingSystem.parameters.baseAsset !== undefined) { + if (tradingSystem.parameters.baseAsset.formula !== undefined) { + let receivedParameters + try { + receivedParameters = JSON.parse(tradingSystem.parameters.baseAsset.formula.code); + + if (receivedParameters.name !== undefined) { + baseAsset = receivedParameters.name; + if (baseAsset !== 'BTC' && baseAsset !== 'USDT') { + tradingSystem.parameters.baseAsset.formula.error = baseAsset + ' is not supported. Using default: BTC.' + baseAsset = 'BTC' + } + } + + if (baseAsset === 'BTC') { + if (receivedParameters.initialBalance !== undefined) { + initialBalanceA = receivedParameters.initialBalance; + initialBalanceB = 0 + } else { + initialBalanceA = DEFAULT_BASE_ASSET_BALANCE; + initialBalanceB = 0 + } + if (receivedParameters.minimunBalance !== undefined) { + minimunBalanceA = receivedParameters.minimunBalance; + minimunBalanceB = 0 + } else { + minimunBalanceA = DEFAULT_BASE_ASSET_MINIMUN_BALANCE; + minimunBalanceB = 0 + } + if (receivedParameters.maximunBalance !== undefined) { + maximunBalanceA = receivedParameters.maximunBalance; + maximunBalanceB = 0 + } else { + maximunBalanceA = DEFAULT_BASE_ASSET_MAXIMUN_BALANCE; + maximunBalanceB = 0 + } + } else { + if (receivedParameters.initialBalance !== undefined) { + initialBalanceB = receivedParameters.initialBalance; + initialBalanceA = 0 + } else { + initialBalanceB = DEFAULT_BASE_ASSET_BALANCE; + initialBalanceA = 0 + } + if (receivedParameters.minimunBalance !== undefined) { + minimunBalanceB = receivedParameters.minimunBalance; + minimunBalanceA = 0 + } else { + minimunBalanceB = DEFAULT_BASE_ASSET_MINIMUN_BALANCE; + minimunBalanceA = 0 + } + if (receivedParameters.maximunBalance !== undefined) { + maximunBalanceB = receivedParameters.maximunBalance; + maximunBalanceA = 0 + } else { + maximunBalanceB = DEFAULT_BASE_ASSET_MAXIMUN_BALANCE; + maximunBalanceA = 0 + } + } + } catch (err) { + tradingSystem.parameters.baseAsset.formula.error = err.message + } + } + } + } + + /* Strategy and Phases */ + + let currentStrategyIndex = -1; + let strategyStage = 'No Stage'; + + /* Stop Loss Management */ + + const MIN_STOP_LOSS_VALUE = 1 // We can not let the stop be zero to avoid division by 0 error or infinity numbers as a result. + const MAX_STOP_LOSS_VALUE = Number.MAX_SAFE_INTEGER + let stopLoss = 0; + let stopLossPhase = 0; + let stopLossStage = 'No Stage'; + + /* Take Profit Management */ + + const MIN_TAKE_PROFIT_VALUE = 1 // We can not let the buy order be zero to avoid division by 0 error or infinity numbers as a result. + const MAX_TAKE_PROFIT_VALUE = Number.MAX_SAFE_INTEGER + let previousTakeProfit = 0; + let takeProfit = 0; + let takeProfitPhase = 0; + let takeProfitStage = 'No Stage'; + + /* Simulation Records */ + + let positionRate = 0; + let positionSize = 0; + let positionInstant; + + let previousBalanceAssetA = 0; + let previousBalanceAssetB = 0; + let hitRatio = 0; + let ROI = 0; + let days = 0; + let anualizedRateOfReturn = 0; + let type = '""'; + let marketRate = 0; + let takePositionNow = false + + /* In some cases we need to know if we are positioned at the last candle of the calendar day, for that we need thse variables. */ + + let lastInstantOfTheDay = currentDay.valueOf() + ONE_DAY_IN_MILISECONDS - 1; + let lastCandle = candles[candles.length - 1]; + + /* These 2 objects will allow us to create separate files for each one of them. */ + + let currentStrategy = { + begin: 0, + end: 0, + status: 0, + number: 0 + } + + let currentTrade = { + begin: 0, + end: 0, + status: 0, + profit: 0, + exitType: 0, + beginRate: 0, + endRate: 0 + } + + /* + The following counters need to survive multiple executions of the similator and keep themselves reliable. + This is challenging when the simulation is executed using Daily Files, since each execution means a new + day and these counters are meant to be kept along the whole market. + + To overcome this problem, we use the interExecutionMemory to record the values of the current execution + when finish. But there are a few details: + + 1. When the process is at the head of the market, it executes multple times at the same day. + 2. The same code serves execution from Market Files. + 3. In Daily Files we are receiving candles from the current day and previous day, so we need to take care of + not adding to the counters duplicate info when processing the day before candles. + + To overcome these challenges we record the values of the counters and variables on the interExecutionMemory only when + the day is complete and if we have a currentDay. That menas that for Market Files we will never use + interExecutionMemory. + */ + + let balanceAssetA = initialBalanceA; + let balanceAssetB = initialBalanceB; + + let lastProfit = 0; + let profit = 0; + let lastProfitPercent = 0; + + let roundtrips = 0; + let fails = 0; + let hits = 0; + let periods = 0; + + /* Message to the Simulation Executor */ + + let orderId = 0; + let messageId = 0; + + let yesterday = {}; + + yesterday.balanceAssetA = balanceAssetA; + yesterday.balanceAssetB = balanceAssetB; + + yesterday.lastProfit = 0; + yesterday.profit = 0; + yesterday.lastProfitPercent = 0; + + yesterday.Roundtrips = 0; + yesterday.fails = 0; + yesterday.hits = 0; + yesterday.Periods = 0; + + yesterday.orderId = 0; + yesterday.messageId = 0; + + yesterday.hitRatio = 0; + yesterday.ROI = 0; + yesterday.anualizedRateOfReturn = 0; + + if (interExecutionMemory.roundtrips === undefined) { + + /* Initialize the data structure we will use inter execution. */ + + interExecutionMemory.balanceAssetA = balanceAssetA; + interExecutionMemory.balanceAssetB = balanceAssetB; + + interExecutionMemory.lastProfit = lastProfit; + interExecutionMemory.profit = profit; + interExecutionMemory.lastProfitPercent = lastProfitPercent; + + interExecutionMemory.roundtrips = 0; + interExecutionMemory.fails = 0; + interExecutionMemory.hits = 0; + interExecutionMemory.periods = 0; + + interExecutionMemory.orderId = 0; + interExecutionMemory.messageId = 0; + + interExecutionMemory.hitRatio = 0; + interExecutionMemory.ROI = 0; + interExecutionMemory.anualizedRateOfReturn = 0; + + } else { + + /* We get the initial values from the day previous to the candles we receive at the current execution */ + + if (currentDay.valueOf() >= startDate.valueOf() + ONE_DAY_IN_MILISECONDS) { // Only after the first day we start grabbing the balance from this memory. + + balanceAssetA = interExecutionMemory.balanceAssetA; + balanceAssetB = interExecutionMemory.balanceAssetB; + + yesterday.balanceAssetA = balanceAssetA; + yesterday.balanceAssetB = balanceAssetB; + + } + + lastProfit = interExecutionMemory.lastProfit; + profit = interExecutionMemory.profit; + lastProfitPercent = interExecutionMemory.lastProfitPercent; + + roundtrips = interExecutionMemory.roundtrips; + fails = interExecutionMemory.fails; + hits = interExecutionMemory.hits; + periods = interExecutionMemory.periods; + + orderId = interExecutionMemory.orderId; + messageId = interExecutionMemory.messageId; + + hitRatio = interExecutionMemory.hitRatio; + ROI = interExecutionMemory.ROI; + anualizedRateOfReturn = interExecutionMemory.anualizedRateOfReturn; + + yesterday.hitRatio = hitRatio; + yesterday.ROI = ROI; + yesterday.anualizedRateOfReturn = anualizedRateOfReturn; + + } + + /* Main Simulation Loop: We go thourgh all the candles at this time period. */ + + for (let i = 0; i < candles.length; i++) { + + /* Update all the data objects available for the simulation. */ + + let candle = candles[i]; + let percentageBandwidth = percentageBandwidthMap.get(candle.begin); + let bollingerBand = bollingerBandsMap.get(candle.begin); + //let LRC = LRCMap.get(candle.begin); + + //if (LRC === undefined) { continue; } + if (percentageBandwidth === undefined) { continue; } // percentageBandwidth might start after the first few candles. + if (candle.begin < initialDate.valueOf()) { continue; } + + periods++; + days = periods * timePeriod / ONE_DAY_IN_MILISECONDS; + + if (currentDay !== undefined) { + if (candle.end < currentDay.valueOf()) { + yesterday.Periods++; + } + } + + let channel = getElement(bollingerChannelsArray, candle.begin, candle.end); + let subChannel = getElement(bollingerSubChannelsArray, candle.begin, candle.end); + + let lastObjects = { + candle: clone(candle), + //LRC: clone(LRC), + bollingerBand: clone(bollingerBand), + percentageBandwidth: clone(percentageBandwidth), + channel: clone(channel), + subChannel: clone(subChannel) + } + + function clone(obj) { + if (null == obj || "object" != typeof obj) return obj; + var copy = obj.constructor(); + for (var attr in obj) { + if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr]; + } + return copy; + } + + lastObjects.candle.previous = undefined; + //lastObjects.LRC.previous = undefined; + lastObjects.bollingerBand.previous = undefined; + lastObjects.percentageBandwidth.previous = undefined; + lastObjects.channel.previous = undefined; + lastObjects.subChannel.previous = undefined; + + lastObjectsArray.push(lastObjects); + + if (lastObjectsArray.length > 5) { + lastObjectsArray.splice(0,1); + } + + let conditions = new Map; // Here we store the conditions values that will be use in the simulator for decision making. + let formulas = new Map; + let conditionsArrayRecord = []; // These are the records that will be saved in a file for the plotter to consume. + let conditionsArrayValues = []; // Here we store the conditions values that will be written on file for the plotter. + let formulasErrors = []; // Here we store the errors produced by all phase formulas. + let formulasValues = []; // Here we store the values produced by all phase formulas. + + /* We define and evaluate all conditions to be used later during the simulation loop. */ + + conditionsArrayRecord.push(candle.begin); + conditionsArrayRecord.push(candle.end); + + evaluateConditionsAndFormulas(tradingSystem, conditions); + + function evaluateConditionsAndFormulas(tradingSystem, conditions) { + + for (let j = 0; j < tradingSystem.strategies.length; j++) { + + let strategy = tradingSystem.strategies[j]; + + let triggerStage = strategy.triggerStage + + if (triggerStage !== undefined) { + + if (triggerStage.triggerOn !== undefined) { + + for (let k = 0; k < triggerStage.triggerOn.situations.length; k++) { + + let situation = triggerStage.triggerOn.situations[k]; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = j + '-' + 'triggerStage' + '-' + 'triggerOn' + '-' + k + '-' + m; + + if (condition.code !== undefined) { + newCondition(key, condition.code); + } + } + } + } + + if (triggerStage.triggerOff !== undefined) { + + for (let k = 0; k < triggerStage.triggerOff.situations.length; k++) { + + let situation = triggerStage.triggerOff.situations[k]; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = j + '-' + 'triggerStage' + '-' + 'triggerOff' + '-' + k + '-' + m; + + if (condition.code !== undefined) { + newCondition(key, condition.code); + } + } + } + } + + if (triggerStage.takePosition !== undefined) { + + for (let k = 0; k < triggerStage.takePosition.situations.length; k++) { + + let situation = triggerStage.takePosition.situations[k]; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = j + '-' + 'triggerStage' + '-' + 'takePosition' + '-' + k + '-' + m; + + if (condition.code !== undefined) { + newCondition(key, condition.code); + } + } + } + } + } + + let openStage = strategy.openStage + + if (openStage !== undefined) { + + let initialDefinition = openStage.initialDefinition + + if (initialDefinition !== undefined) { + + if (initialDefinition.stopLoss !== undefined) { + + for (let p = 0; p < initialDefinition.stopLoss.phases.length; p++) { + + let phase = initialDefinition.stopLoss.phases[p]; + + /* Evaluate Formula */ + let formulaValue + let formulaError = '' + + if (phase.formula !== undefined) { + try { + formulaValue = eval(phase.formula.code); + if (formulaValue === Infinity) { + formulaError= "Formula evaluates to Infinity." + formulaValue = MAX_STOP_LOSS_VALUE + } + } catch (err) { + formulaError = err.message + } + if (isNaN(formulaValue)) { formulaValue = 0; } + if (formulaValue < MIN_STOP_LOSS_VALUE) { + formulaValue = MIN_STOP_LOSS_VALUE + } + } + + formulasErrors.push('"' + formulaError + '"') + formulasValues.push( formulaValue) + let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + p; + formulas.set(key, formulaValue) + + /* next phase event */ + let nextPhaseEvent = phase.nextPhaseEvent; + if (nextPhaseEvent !== undefined) { + + for (let k = 0; k < nextPhaseEvent.situations.length; k++) { + + let situation = nextPhaseEvent.situations[k]; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + p + '-' + k + '-' + m; + + if (condition.code !== undefined) { + newCondition(key, condition.code); + } + } + } + } + } + } + + if (initialDefinition.takeProfit !== undefined) { + + for (let p = 0; p < initialDefinition.takeProfit.phases.length; p++) { + + let phase = initialDefinition.takeProfit.phases[p]; + + /* Evaluate Formula */ + let formulaValue + let formulaError = '' + + if (phase.formula !== undefined) { + try { + formulaValue = eval(phase.formula.code); + if (formulaValue === Infinity) { + formulaError = "Formula evaluates to Infinity." + formulaValue = MAX_TAKE_PROFIT_VALUE + } + } catch (err) { + formulaError = err.message + } + if (isNaN(formulaValue)) { formulaValue = 0; } + if (formulaValue < MIN_TAKE_PROFIT_VALUE) { + formulaValue = MIN_TAKE_PROFIT_VALUE + } + } + + formulasErrors.push('"' + formulaError + '"') + formulasValues.push(formulaValue) + let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + p; + formulas.set(key, formulaValue) + + /* next phase event */ + let nextPhaseEvent = phase.nextPhaseEvent; + if (nextPhaseEvent !== undefined) { + + for (let k = 0; k < nextPhaseEvent.situations.length; k++) { + + let situation = nextPhaseEvent.situations[k]; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + p + '-' + k + '-' + m; + + if (condition.code !== undefined) { + newCondition(key, condition.code); + } + } + } + } + } + } + } + } + + let manageStage = strategy.manageStage + + if (manageStage !== undefined) { + + if (manageStage.stopLoss !== undefined) { + + for (let p = 0; p < manageStage.stopLoss.phases.length; p++) { + + let phase = manageStage.stopLoss.phases[p]; + + /* Evaluate Formula */ + let formulaValue + let formulaError = '' + + if (phase.formula !== undefined) { + try { + formulaValue = eval(phase.formula.code); + if (formulaValue === Infinity) { + formulaError = "Formula evaluates to Infinity." + formulaValue = MAX_STOP_LOSS_VALUE + } + } catch (err) { + formulaError = err.message + } + if (isNaN(formulaValue)) { formulaValue = 0; } + if (formulaValue < MIN_STOP_LOSS_VALUE) { + formulaValue = MIN_STOP_LOSS_VALUE + } + } + + formulasErrors.push('"' + formulaError + '"') + formulasValues.push(formulaValue) + let key = j + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + p; + formulas.set(key, formulaValue) + + /* next phase event */ + let nextPhaseEvent = phase.nextPhaseEvent; + if (nextPhaseEvent !== undefined) { + + for (let k = 0; k < nextPhaseEvent.situations.length; k++) { + + let situation = nextPhaseEvent.situations[k]; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = j + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + p + '-' + k + '-' + m; + + if (condition.code !== undefined) { + newCondition(key, condition.code); + } + } + } + } + } + } + + if (manageStage.takeProfit !== undefined) { + + for (let p = 0; p < manageStage.takeProfit.phases.length; p++) { + + let phase = manageStage.takeProfit.phases[p]; + + /* Evaluate Formula */ + let formulaValue + let formulaError = '' + + if (phase.formula !== undefined) { + try { + formulaValue = eval(phase.formula.code); + if (formulaValue === Infinity) { + formulaError = "Formula evaluates to Infinity." + formulaValue = MAX_TAKE_PROFIT_VALUE + } + } catch (err) { + formulaError = err.message + } + if (isNaN(formulaValue)) { formulaValue = 0; } + if (formulaValue < MIN_TAKE_PROFIT_VALUE) { + formulaValue = MIN_TAKE_PROFIT_VALUE + } + } + + formulasErrors.push('"' + formulaError + '"') + formulasValues.push(formulaValue) + let key = j + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + p; + formulas.set(key, formulaValue) + + /* next phase event */ + let nextPhaseEvent = phase.nextPhaseEvent; + if (nextPhaseEvent !== undefined) { + + for (let k = 0; k < nextPhaseEvent.situations.length; k++) { + + let situation = nextPhaseEvent.situations[k]; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = j + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + p + '-' + k + '-' + m; + + if (condition.code !== undefined) { + newCondition(key, condition.code); + } + } + } + } + } + } + } + } + + function newCondition(key, node) { + + let condition; + let error = '' + let value + + try { + value = eval(node.code); + } catch (err) { + /* + One possible error is that the conditions references a .previous that is undefined. For this + reason and others, we will simply set the value to false. + */ + value = false + node.error = err.message + } + + condition = { + key: key, + value: value + }; + + conditions.set(condition.key, condition); + + if (condition.value) { + conditionsArrayValues.push(1); + } else { + conditionsArrayValues.push(0); + } + } + + } + + + if ( + strategyStage === 'No Stage' && + currentStrategyIndex === -1 + ) { + let minimunBalance + let maximunBalance + let balance + + if (baseAsset === 'BTC') { + balance = balanceAssetA + minimunBalance = minimunBalanceA + maximunBalance = maximunBalanceA + } else { + balance = balanceAssetB + minimunBalance = minimunBalanceB + maximunBalance = maximunBalanceB + } + + if (balance > minimunBalance && balance < maximunBalance) { + /* Trigger On Conditions */ + + /* + Here we need to pick a strategy, or if there is not suitable strategy for the current + market conditions, we pass until the next period. + + To pick a new strategy we will evaluate what we call the trigger on. Once we enter + into one strategy, we will ignore market conditions for others. However there is also + a strategy trigger off which can be hit before taking a position. If hit, we would + be outside a strategy again and looking for the condition to enter all over again. + + */ + + for (let j = 0; j < tradingSystem.strategies.length; j++) { + + let strategy = tradingSystem.strategies[j]; + + let triggerStage = strategy.triggerStage + + if (triggerStage !== undefined) { + + if (triggerStage.triggerOn !== undefined) { + + for (let k = 0; k < triggerStage.triggerOn.situations.length; k++) { + + let situation = triggerStage.triggerOn.situations[k]; + let passed = true; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = j + '-' + 'triggerStage' + '-' + 'triggerOn' + '-' + k + '-' + m; + + let value = conditions.get(key).value; + + if (value === false) { passed = false; } + } + + if (passed) { + + strategyStage = 'Trigger Stage'; + currentStrategyIndex = j; + currentStrategy.begin = candle.begin; + currentStrategy.beginRate = candle.min; + currentStrategy.endRate = candle.min; // In case the strategy does not get exited + break; + } + } + } + } + } + } else { + tradingSystem.error = "Balance below the minimun. No more strategies will be executed." + } + } + + /* Trigger Off Condition */ + + if (strategyStage === 'Trigger Stage') { + + let strategy = tradingSystem.strategies[currentStrategyIndex]; + + let triggerStage = strategy.triggerStage + + if (triggerStage !== undefined) { + + if (triggerStage.triggerOff !== undefined) { + + for (let k = 0; k < triggerStage.triggerOff.situations.length; k++) { + + let situation = triggerStage.triggerOff.situations[k]; + let passed = true; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = currentStrategyIndex + '-' + 'triggerStage' + '-' + 'triggerOff' + '-' + k + '-' + m; + + let value = conditions.get(key).value; + + if (value === false) { passed = false; } + } + + if (passed) { + + currentStrategy.number = currentStrategyIndex + currentStrategy.end = candle.end; + currentStrategy.endRate = candle.min; + currentStrategy.status = 1; + strategyStage = 'No Stage'; + currentStrategyIndex = -1; + break; + } + } + } + } + } + + /* Checking if Stop or Take Profit were hit */ + + if ( + strategyStage === 'Open Stage' || + strategyStage === 'Manage Stage' + ) { + + /* Checking what happened since the last execution. We need to know if the Stop Loss + or our Take Profit were hit. */ + + /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ + + if ((baseAsset === 'BTC' && candle.max >= stopLoss) || (baseAsset !== 'BTC' && candle.min <= stopLoss)) { + + if (baseAsset === 'BTC') { + balanceAssetA = balanceAssetA + balanceAssetB / stopLoss; + balanceAssetB = 0; + } else { + balanceAssetB = balanceAssetB + balanceAssetA * stopLoss; + balanceAssetA = 0; + } + + if (currentDay !== undefined) { + if (positionInstant < currentDay.valueOf()) { + yesterday.balanceAssetA = balanceAssetA; + yesterday.balanceAssetB = balanceAssetB; + } + } + + marketRate = stopLoss; + type = '"Buy@StopLoss"'; + strategyStage = 'Close Stage'; + stopLossStage = 'No Stage'; + takeProfitStage = 'No Stage'; + currentTrade.end = candle.end; + currentTrade.status = 1; + currentTrade.exitType = 1; + currentTrade.endRate = stopLoss; + + currentStrategy.number = currentStrategyIndex + currentStrategy.end = candle.end; + currentStrategy.endRate = candle.min; + currentStrategy.status = 1; + } + + /* Take Profit condition: Here we verify if the Take Profit was filled or not. */ + + if ((baseAsset === 'BTC' && candle.min <= takeProfit) || (baseAsset !== 'BTC' && candle.max >= takeProfit)) { + + if (baseAsset === 'BTC') { + balanceAssetA = balanceAssetA + balanceAssetB / takeProfit; + balanceAssetB = 0; + } else { + balanceAssetB = balanceAssetB + balanceAssetA * takeProfit; + balanceAssetA = 0; + } + + if (currentDay !== undefined) { + if (positionInstant < currentDay.valueOf()) { + yesterday.balanceAssetA = balanceAssetA; + yesterday.balanceAssetB = balanceAssetB; + + } + } + + marketRate = takeProfit; + type = '"Buy@TakeProfit"'; + strategyStage = 'Close Stage'; + stopLossStage = 'No Stage'; + takeProfitStage = 'No Stage'; + currentTrade.end = candle.end; + currentTrade.status = 1; + currentTrade.exitType = 2; + currentTrade.endRate = takeProfit; + + currentStrategy.number = currentStrategyIndex + currentStrategy.end = candle.end; + currentStrategy.endRate = candle.min; + currentStrategy.status = 1; + } + } + + /* Take Position Condition */ + + if (strategyStage === 'Trigger Stage') { + + let strategy = tradingSystem.strategies[currentStrategyIndex]; + + let triggerStage = strategy.triggerStage + + if (triggerStage !== undefined) { + + if (triggerStage.takePosition !== undefined) { + + for (let k = 0; k < triggerStage.takePosition.situations.length; k++) { + + let situation = triggerStage.takePosition.situations[k]; + let passed = true; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = currentStrategyIndex + '-' + 'triggerStage' + '-' + 'takePosition' + '-' + k + '-' + m; + + let value = conditions.get(key).value; + + if (value === false) { passed = false; } + } + + if (passed) { + + type = '"Sell"'; + + strategyStage = 'Open Stage'; + stopLossStage = 'Open Stage'; + takeProfitStage = 'Open Stage'; + stopLossPhase = 1; + takeProfitPhase = 1; + currentTrade.begin = candle.begin; + currentTrade.beginRate = candle.close; + takePositionNow = true + break; + } + } + } + } + } + + /* Stop Loss Management */ + + if ( + strategyStage === 'Open Stage' || + strategyStage === 'Manage Stage' + ) { + + checkStopPhases() + calculateStopLoss(); + + } + + function checkStopPhases() { + + let strategy = tradingSystem.strategies[currentStrategyIndex]; + + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let parentNode + let j = currentStrategyIndex + let stageKey + let initialDefinitionKey = '' + let p + + if (stopLossStage === 'Open Stage' && openStage !== undefined) { + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.stopLoss !== undefined) { + parentNode = openStage.initialDefinition + initialDefinitionKey = '-' + 'initialDefinition' + stageKey = 'openStage' + p = stopLossPhase - 1 + } + } + } + + if (stopLossStage === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.stopLoss !== undefined) { + parentNode = manageStage + stageKey = 'manageStage' + p = stopLossPhase - 2 + } + } + + let phase = parentNode.stopLoss.phases[p]; + + let nextPhaseEvent = phase.nextPhaseEvent; + if (nextPhaseEvent !== undefined) { + + for (let k = 0; k < nextPhaseEvent.situations.length; k++) { + + let situation = nextPhaseEvent.situations[k]; + let passed = true; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = j + '-' + stageKey + initialDefinitionKey + '-' + 'stopLoss' + '-' + p + '-' + k + '-' + m; + + let value = conditions.get(key).value; + + if (value === false) { passed = false; } + } + + if (passed) { + + stopLossPhase++; + stopLossStage = 'Manage Stage' + if (takeProfitPhase > 1) { strategyStage = 'Manage Stage' } + return; + } + } + } + } + + function calculateStopLoss() { + + let strategy = tradingSystem.strategies[currentStrategyIndex]; + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let phase + let key + + if (stopLossStage === 'Open Stage' && openStage !== undefined) { + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.stopLoss !== undefined) { + phase = openStage.initialDefinition.stopLoss.phases[stopLossPhase - 1]; + key = currentStrategyIndex + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + (stopLossPhase - 1); + } + } + } + + if (stopLossStage === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.stopLoss !== undefined) { + phase = manageStage.stopLoss.phases[stopLossPhase - 2]; + key = currentStrategyIndex + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + (stopLossPhase - 2); + } + } + + if (phase.formula !== undefined) { + stopLoss = formulas.get(key) + } + } + + /* Take Profit Management */ + + if ( + strategyStage === 'Open Stage' || + strategyStage === 'Manage Stage' + ) { + + checkTakeProfitPhases(); + calculateTakeProfit(); + + } + + function checkTakeProfitPhases() { + + let strategy = tradingSystem.strategies[currentStrategyIndex]; + + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let parentNode + let j = currentStrategyIndex + let stageKey + let initialDefinitionKey = '' + let p + + if (takeProfitStage === 'Open Stage' && openStage !== undefined) { + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.takeProfit !== undefined) { + parentNode = openStage.initialDefinition + initialDefinitionKey = '-' + 'initialDefinition' + stageKey = 'openStage' + p = takeProfitPhase - 1 + } + } + } + + if (takeProfitStage === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.takeProfit !== undefined) { + parentNode = manageStage + stageKey = 'manageStage' + p = takeProfitPhase - 2 + } + } + + let phase = parentNode.takeProfit.phases[p]; + + let nextPhaseEvent = phase.nextPhaseEvent; + if (nextPhaseEvent !== undefined) { + + for (let k = 0; k < nextPhaseEvent.situations.length; k++) { + + let situation = nextPhaseEvent.situations[k]; + let passed = true; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = j + '-' + stageKey + initialDefinitionKey + '-' + 'takeProfit' + '-' + p + '-' + k + '-' + m; + + let value = conditions.get(key).value; + + if (value === false) { passed = false; } + } + + if (passed) { + + takeProfitPhase++; + takeProfitStage = 'Manage Stage' + if (stopLossPhase > 1) { strategyStage = 'Manage Stage' } + return; + } + } + } + } + + function calculateTakeProfit() { + + let strategy = tradingSystem.strategies[currentStrategyIndex]; + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let phase + let key + + if (takeProfitStage === 'Open Stage' && openStage !== undefined) { + if (openStage.initialDefinition !== undefined) { + if (openStage.initialDefinition.takeProfit !== undefined) { + phase = openStage.initialDefinition.takeProfit.phases[takeProfitPhase - 1]; + key = currentStrategyIndex + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + (takeProfitPhase - 1); + } + } + } + + if (takeProfitStage === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.takeProfit !== undefined) { + phase = manageStage.takeProfit.phases[takeProfitPhase - 2]; + key = currentStrategyIndex + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + (takeProfitPhase - 2); + } + } + + if (phase.formula !== undefined) { + takeProfit = formulas.get(key) + } + } + + /* Taking a Position */ + + if ( + takePositionNow === true + ) { + takePositionNow = false + + /* positionSize default is the whole balance if no size was defined by the user */ + if (baseAsset === 'BTC') { + positionSize = balanceAssetA; + } else { + positionSize = balanceAssetB; + } + + /* Check if the user defined a position size */ + let strategy = tradingSystem.strategies[currentStrategyIndex]; + let triggerStage = strategy.triggerStage + + if (triggerStage !== undefined) { + if (triggerStage.positionSize !== undefined) { + if (triggerStage.positionSize.formula !== undefined) { + try { + positionSize = eval(triggerStage.positionSize.formula.code); + } catch (err) { + triggerStage.positionSize.formula.error = err.message + } + if (isNaN(positionSize)) { + if (baseAsset === 'BTC') { + positionSize = balanceAssetA; + } else { + positionSize = balanceAssetB; + } + } + } + } + } + + marketRate = candle.close; + positionRate = marketRate; + + calculateStopLoss(); + calculateTakeProfit(); + + previousBalanceAssetA = balanceAssetA; + previousBalanceAssetB = balanceAssetB; + + lastProfit = 0; + lastProfitPercent = 0; + + if (baseAsset === 'BTC') { + balanceAssetB = balanceAssetB + positionSize * marketRate; + balanceAssetA = balanceAssetA - positionSize; + } else { + balanceAssetA = balanceAssetA + positionSize / marketRate; + balanceAssetB = balanceAssetB - positionSize; + } + + positionInstant = candle.end; + + if (currentDay !== undefined) { + if (positionInstant < currentDay.valueOf()) { + yesterday.balanceAssetA = balanceAssetA; + yesterday.balanceAssetB = balanceAssetB; + + yesterday.lastProfit = lastProfit; + yesterday.lastProfitPercent = lastProfitPercent; + } + } + + addRecord(); + continue; + } + + /* Closing a Position */ + + if (strategyStage === 'Close Stage') { + + roundtrips++; + + if (currentDay !== undefined) { + if (positionInstant < currentDay.valueOf()) { + yesterday.Roundtrips++; + } + } + + if (baseAsset === 'BTC') { + lastProfit = balanceAssetA - previousBalanceAssetA; + lastProfitPercent = lastProfit / previousBalanceAssetA * 100; + if (isNaN(lastProfitPercent)) { lastProfitPercent = 0; } + profit = balanceAssetA - initialBalanceA; + } else { + lastProfit = balanceAssetB - previousBalanceAssetB; + lastProfitPercent = lastProfit / previousBalanceAssetB * 100; + if (isNaN(lastProfitPercent)) { lastProfitPercent = 0; } + profit = balanceAssetB - initialBalanceB; + } + + if (currentDay !== undefined) { + if (positionInstant < currentDay.valueOf()) { + yesterday.lastProfit = lastProfit; + yesterday.profit = profit; + yesterday.lastProfitPercent = lastProfitPercent; + } + } + + currentTrade.lastProfitPercent = lastProfitPercent; + currentTrade.stopRate = stopLoss; + + if (lastProfit > 0) { + hits++; + + if (currentDay !== undefined) { + if (positionInstant < currentDay.valueOf()) { + yesterday.hits++; + } + } + + } else { + fails++; + + if (currentDay !== undefined) { + if (positionInstant < currentDay.valueOf()) { + yesterday.fails++; + } + } + } + + if (baseAsset === 'BTC') { + ROI = (initialBalanceA + profit) / initialBalanceA - 1; + hitRatio = hits / roundtrips; + anualizedRateOfReturn = ROI / days * 365; + } else { + ROI = (initialBalanceB + profit) / initialBalanceB - 1; + hitRatio = hits / roundtrips; + anualizedRateOfReturn = ROI / days * 365; + } + + if (currentDay !== undefined) { + if (positionInstant < currentDay.valueOf()) { + yesterday.ROI = ROI; + yesterday.hitRatio = hitRatio; + yesterday.anualizedRateOfReturn = anualizedRateOfReturn; + } + } + + + addRecord(); + + currentStrategyIndex = -1; + stopLoss = 0; + positionRate = 0; + positionSize = 0; + positionInstant = undefined; + takeProfit = 0; + strategyStage = 'No Stage'; + stopLossStage = 'No Stage'; + takeProfitStage = 'No Stage'; + stopLossPhase = 0; + takeProfitPhase = 0; + continue; + + } + + /* Not a buy or sell condition */ + + marketRate = candle.close; + addRecord(); + + function addRecord() { + + // Since we are going to write the message to a file that the Simulation Executor is going to read, we use the abbreviations. + let messageType; + let message; + let simulationRecord; + let orderRecord; + + messageId++; + + if (strategyStage === 'Open Stage' || strategyStage === 'Manage Stage') { + + if (strategyStage === 'Open Stage') { + messageType = MESSAGE_TYPE.Order; + orderId++; + } + if (strategyStage === 'Manage Stage') { + messageType = MESSAGE_TYPE.OrderUpdate; + } + + orderRecord = createMessage( + messageId, + MESSAGE_ENTITY.SimulationEngine, + MESSAGE_ENTITY.SimulationExecutor, + messageType, + (new Date()).valueOf(), + orderId.toString(), + ORDER_CREATOR.SimulationEngine, + (new Date()).valueOf(), + ORDER_OWNER.User, + global.EXCHANGE_NAME, + "BTC_USDT", + 0, + ORDER_TYPE.Limit, + marketRate, + stopLoss, + takeProfit, + ORDER_DIRECTION.Sell, + -1, + ORDER_STATUS.Signaled, + 0, + "") + + } + else { + + orderRecord = createMessage( + messageId, + MESSAGE_ENTITY.SimulationEngine, + MESSAGE_ENTITY.SimulationExecutor, + MESSAGE_TYPE.HeartBeat, + (new Date()).valueOf(), + "", + "", + 0, + "", + "", + "", + 0, + "", + 0, + 0, + 0, + "", + 0, + "", + 0, + "") + } + + let strategyStageNumber + switch (strategyStage) { + case 'No Stage': { + strategyStageNumber = 0 + break + } + case 'Trigger Stage': { + strategyStageNumber = 1 + break + } + case 'Open Stage': { + strategyStageNumber = 2 + break + } + case 'Manage Stage': { + strategyStageNumber = 3 + break + } + case 'Close Stage': { + strategyStageNumber = 4 + break + } + } + + simulationRecord = { + begin: candle.begin, + end: candle.end, + type: type, + marketRate: marketRate, + amount: 1, + balanceA: balanceAssetA, + balanceB: balanceAssetB, + profit: profit, + lastProfit: lastProfit, + stopLoss: stopLoss, + roundtrips: roundtrips, + hits: hits, + fails: fails, + hitRatio: hitRatio, + ROI: ROI, + periods: periods, + days: days, + anualizedRateOfReturn: anualizedRateOfReturn, + positionRate: positionRate, + lastProfitPercent: lastProfitPercent, + strategy: currentStrategyIndex, + strategyStageNumber: strategyStageNumber, + takeProfit: takeProfit, + stopLossPhase: stopLossPhase, + takeProfitPhase: takeProfitPhase, + orderRecord: orderRecord, + positionSize: positionSize, + initialBalanceA: initialBalanceA, + minimunBalanceA: minimunBalanceA, + maximunBalanceA: maximunBalanceA, + initialBalanceB: initialBalanceB, + minimunBalanceB: minimunBalanceB, + maximunBalanceB: maximunBalanceB + } + + recordsArray.push(simulationRecord); + + type = '""'; + + /* Prepare the information for the Conditions File */ + + conditionsArrayRecord.push(currentStrategyIndex); + conditionsArrayRecord.push(strategyStageNumber); + conditionsArrayRecord.push(stopLossPhase); + conditionsArrayRecord.push(takeProfitPhase); + conditionsArrayRecord.push(conditionsArrayValues); + conditionsArrayRecord.push(formulasErrors); + conditionsArrayRecord.push(formulasValues); + + conditionsArray.push(conditionsArrayRecord); + + /* Prepare the information for the Strategies File*/ + + if ( + (currentStrategy.begin !== 0 && currentStrategy.end !== 0) || + (currentStrategy.begin !== 0 && i === candles.length - 1 && lastCandle.end !== lastInstantOfTheDay) + ) { + + strategiesArray.push(currentStrategy); + + currentStrategy = { + begin: 0, + end: 0, + status: 0, + number: 0, + beginRate: 0, + endRate: 0 + } + } + + /* Prepare the information for the Trades File */ + + if ( + (currentTrade.begin !== 0 && currentTrade.end !== 0) || + (currentTrade.begin !== 0 && i === candles.length - 1 && lastCandle.end !== lastInstantOfTheDay) + ) { + + currentTrade.profit = lastProfit; + + tradesArray.push(currentTrade); + + currentTrade = { + begin: 0, + end: 0, + status: 0, + lastProfitPercent: 0, + exitType: 0, + beginRate: 0, + endRate: 0 + } + } + } + } + + /* + Before returning we need to see if we have to record some of our counters at the interExecutionMemory. + To do that, the condition to be met is that this execution must include all candles of the currentDay. + */ + + if (currentDay !== undefined) { + + if (lastCandle.end === lastInstantOfTheDay) { + + interExecutionMemory.balanceAssetA = yesterday.balanceAssetA; + interExecutionMemory.balanceAssetB = yesterday.balanceAssetB; + interExecutionMemory.lastProfit = yesterday.lastProfit; + interExecutionMemory.profit = yesterday.profit; + interExecutionMemory.lastProfitPercent = yesterday.lastProfitPercent; + + interExecutionMemory.roundtrips = interExecutionMemory.roundtrips + yesterday.Roundtrips; + interExecutionMemory.fails = interExecutionMemory.fails + yesterday.fails; + interExecutionMemory.hits = interExecutionMemory.hits + yesterday.hits; + interExecutionMemory.periods = interExecutionMemory.periods + yesterday.Periods; + + interExecutionMemory.messageId = interExecutionMemory.messageId + yesterday.messageId; + interExecutionMemory.orderId = interExecutionMemory.orderId + yesterday.orderId; + + interExecutionMemory.hitRatio = yesterday.hitRatio; + interExecutionMemory.ROI = yesterday.ROI; + interExecutionMemory.anualizedRateOfReturn = yesterday.anualizedRateOfReturn; + } + } + + callback(tradingSystem); + + function getElement(pArray, begin, end) { + + let element; + + for (let i = 0; i < pArray.length; i++) { + + element = pArray[i]; + + if (begin >= element.begin && end <= element.end) { + return element + } + } + + element = { + direction: 'unknown', + slope: 'unknown' + }; + return element; + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] runSimulation -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function buildLRC(dataFile, callBackFunction) { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] buildLRC -> Entering function."); } + + try { + + let previous; + + for (let i = 0; i < dataFile.length; i++) { + + let LRC = { + begin: dataFile[i][0], + end: dataFile[i][1], + _15: dataFile[i][2], + _30: dataFile[i][3], + _60: dataFile[i][4] + }; + + if (previous !== undefined) { + + if (previous._15 > LRC._15) { LRC.direction15 = 'down'; } + if (previous._15 < LRC._15) { LRC.direction15 = 'up'; } + if (previous._15 === LRC._15) { LRC.direction15 = 'side'; } + + if (previous._30 > LRC._30) { LRC.direction30 = 'down'; } + if (previous._30 < LRC._30) { LRC.direction30 = 'up'; } + if (previous._30 === LRC._30) { LRC.direction30 = 'side'; } + + if (previous._60 > LRC._60) { LRC.direction60 = 'down'; } + if (previous._60 < LRC._60) { LRC.direction60 = 'up'; } + if (previous._60 === LRC._60) { LRC.direction60 = 'side'; } + + } + + LRC.previous = previous; + + LRCMap.set(LRC.begin, LRC); + + previous = LRC; + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] buildLRC -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function buildPercentageBandwidthMap(dataFile, callBackFunction) { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] buildPercentageBandwidthMap -> Entering function."); } + + try { + + let previous; + + for (let i = 0; i < dataFile.length; i++) { + + let percentageBandwidth = { + begin: dataFile[i][0], + end: dataFile[i][1], + value: dataFile[i][2], + movingAverage: dataFile[i][3], + bandwidth: dataFile[i][4] + }; + + if (previous !== undefined) { + + if (previous.movingAverage > percentageBandwidth.movingAverage) { percentageBandwidth.direction = 'down'; } + if (previous.movingAverage < percentageBandwidth.movingAverage) { percentageBandwidth.direction = 'up'; } + if (previous.movingAverage === percentageBandwidth.movingAverage) { percentageBandwidth.direction = 'side'; } + + } + + percentageBandwidth.previous = previous; + + percentageBandwidthMap.set(percentageBandwidth.begin, percentageBandwidth); + + previous = percentageBandwidth; + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] buildPercentageBandwidthMap -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function buildBollingerBandsMap(dataFile, callBackFunction) { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] buildBollingerBandsMap -> Entering function."); } + + try { + + let previous; + + for (let i = 0; i < dataFile.length; i++) { + + let bollingerBand = { + begin: dataFile[i][0], + end: dataFile[i][1], + movingAverage: dataFile[i][2], + standardDeviation: dataFile[i][3], + deviation: dataFile[i][4] + }; + + if (previous !== undefined) { + + if (previous.movingAverage > bollingerBand.movingAverage) { bollingerBand.direction = 'down'; } + if (previous.movingAverage < bollingerBand.movingAverage) { bollingerBand.direction = 'up'; } + if (previous.movingAverage === bollingerBand.movingAverage) { bollingerBand.direction = 'side'; } + + } + + bollingerBand.previous = previous; + + bollingerBandsMap.set(bollingerBand.begin, bollingerBand); + + previous = bollingerBand; + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] buildBollingerBandsMap -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function buildBollingerChannelsArray(dataFile, callBackFunction) { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] buildBollingerChannelsArray -> Entering function."); } + + try { + + let previous; + + for (let i = 0; i < dataFile.length; i++) { + + let bollingerChannel = { + begin: dataFile[i][0], + end: dataFile[i][1], + direction: dataFile[i][2], + period: dataFile[i][3], + firstMovingAverage: dataFile[i][4], + lastMovingAverage: dataFile[i][5], + firstDeviation: dataFile[i][6], + lastDeviation: dataFile[i][7] + }; + + bollingerChannel.previous = previous; + + bollingerChannelsArray.push(bollingerChannel); + + previous = bollingerChannel; + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] buildBollingerChannelsArray -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function buildBollingerSubChannelsArray(dataFile, callBackFunction) { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] buildBollingerSubChannelsArray -> Entering function."); } + + try { + + let previous; + + for (let i = 0; i < dataFile.length; i++) { + + let bollingerSubChannel = { + begin: dataFile[i][0], + end: dataFile[i][1], + direction: dataFile[i][2], + slope: dataFile[i][3], + period: dataFile[i][4], + firstMovingAverage: dataFile[i][5], + lastMovingAverage: dataFile[i][6], + firstDeviation: dataFile[i][7], + lastDeviation: dataFile[i][8] + }; + + bollingerSubChannel.previous = previous; + + bollingerSubChannelsArray.push(bollingerSubChannel); + + previous = bollingerSubChannel; + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] buildBollingerSubChannelsArray -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function buildCandles(dataFile, callBackFunction) { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] buildCandles -> Entering function."); } + + try { + + let previous; + + for (let i = 0; i < dataFile.length; i++) { + + let candle = { + open: undefined, + close: undefined, + min: 10000000000000, + max: 0, + begin: undefined, + end: undefined, + direction: undefined + }; + + candle.min = dataFile[i][0]; + candle.max = dataFile[i][1]; + + candle.open = dataFile[i][2]; + candle.close = dataFile[i][3]; + + candle.begin = dataFile[i][4]; + candle.end = dataFile[i][5]; + + if (candle.open > candle.close) { candle.direction = 'down'; } + if (candle.open < candle.close) { candle.direction = 'up'; } + if (candle.open === candle.close) { candle.direction = 'side'; } + + candle.previous = previous; + + candles.push(candle); + + previous = candle; + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] buildCandles -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } +}; + + + diff --git a/src/storage/templates/Simulator/User.Bot.Daily.js b/src/storage/templates/Simulator/User.Bot.Daily.js new file mode 100644 index 000000000..7f2baca56 --- /dev/null +++ b/src/storage/templates/Simulator/User.Bot.Daily.js @@ -0,0 +1,472 @@ +exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, fileStorage) { + + const FULL_LOG = true; + const LOG_FILE_CONTENT = false; + + const MODULE_NAME = "User Bot"; + + const SIMULATED_RECORDS_FOLDER_NAME = "Trading-Simulation"; + const CONDITIONS_FOLDER_NAME = "Simulation-Conditions"; + const STRATEGIES_FOLDER_NAME = "Simulation-Strategies"; + const TRADES_FOLDER_NAME = "Simulation-Trades"; + + const commons = COMMONS.newCommons(bot, logger, UTILITIES); + + thisObject = { + initialize: initialize, + start: start + }; + + let utilities = UTILITIES.newCloudUtilities(bot, logger); + + let dataDependencies; + + return thisObject; + + function initialize(pDataDependencies, callBackFunction) { + + try { + + logger.fileName = MODULE_NAME; + logger.initialize(); + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } + + dataDependencies = pDataDependencies; + + callBackFunction(global.DEFAULT_OK_RESPONSE); + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function start(dataFiles, timePeriod, outputPeriodLabel, currentDay, startDate, endDate, interExecutionMemory, callBackFunction) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> Entering function."); } + + let market = global.MARKET; + let dataFile; + + let recordsArray = []; + let conditionsArray = []; + let strategiesArray = []; + let tradesArray = []; + + let tradingSystem = {}; + let lastObjectsArray = []; + + commons.initializeData(); + + for (let i = 0; i < dataDependencies.config.length; i++) { + + let dependency = dataDependencies.config[i]; + dataFile = dataFiles[i]; + + switch (i) { + + case 0: { + commons.buildLRC(dataFile, callBackFunction); + break; + } + case 1: { + commons.buildPercentageBandwidthMap(dataFile, callBackFunction); + break; + } + case 2: { + commons.buildBollingerBandsMap(dataFile, callBackFunction); + break; + } + case 3: { + commons.buildBollingerChannelsArray(dataFile, callBackFunction); + break; + } + case 4: { + commons.buildBollingerSubChannelsArray(dataFile, callBackFunction); + break; + } + case 5: { + commons.buildCandles(dataFile, callBackFunction); + break; + } + } + } + + commons.runSimulation( + recordsArray, + conditionsArray, + strategiesArray, + tradesArray, + lastObjectsArray, + timePeriod, + currentDay, + startDate, + endDate, + interExecutionMemory, + writeFiles) + + function writeFiles(pTradingSystem) { + tradingSystem = pTradingSystem + writeRecordsFile() + } + + function writeRecordsFile() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeRecordsFile -> Entering function."); } + + let separator = ""; + let fileRecordCounter = 0; + + let fileContent = ""; + + for (let i = 0; i < recordsArray.length; i++) { + + let record = recordsArray[i]; + + /* Will only add to the file the records of the current day */ + + if (record.begin < currentDay.valueOf()) { continue; } + + fileContent = fileContent + separator + '[' + + record.begin + "," + + record.end + "," + + record.type + "," + + record.marketRate + "," + + record.amount + "," + + record.balanceA + "," + + record.balanceB + "," + + record.profit + "," + + record.lastProfit + "," + + record.stopLoss + "," + + record.roundtrips + "," + + record.hits + "," + + record.fails + "," + + record.hitRatio + "," + + record.ROI + "," + + record.periods + "," + + record.days + "," + + record.anualizedRateOfReturn + "," + + record.positionRate + "," + + record.lastProfitPercent + "," + + record.strategy + "," + + record.strategyStageNumber + "," + + record.takeProfit + "," + + record.stopLossPhase + "," + + record.takeProfitPhase + "," + + JSON.stringify(record.orderRecord) + "," + + record.positionSize + "," + + record.initialBalanceA + "," + + record.minimunBalanceA + "," + + record.maximunBalanceA + "," + + record.initialBalanceB + "," + + record.minimunBalanceB + "," + + record.maximunBalanceB + "]"; + + if (separator === "") { separator = ","; } + + fileRecordCounter++; + + } + + fileContent = "[" + fileContent + "]"; + + let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2); + let fileName = '' + market.assetA + '_' + market.assetB + '.json'; + + let filePathRoot = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + global.CLONE_EXECUTOR.codeName + "." + global.CLONE_EXECUTOR.version + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + let filePath = filePathRoot + "/Output/" + SIMULATED_RECORDS_FOLDER_NAME + "/" + "Multi-Period-Daily" + "/" + outputPeriodLabel + "/" + dateForPath; + filePath += '/' + fileName + + fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); + + function onFileCreated(err) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeRecordsFile -> onFileCreated -> Entering function."); } + if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> writeRecordsFile -> onFileCreated -> fileContent = " + fileContent); } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + logger.write(MODULE_NAME, "[ERROR] start -> writeRecordsFile -> onFileCreated -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> writeRecordsFile -> onFileCreated -> filePath = " + filePath); + logger.write(MODULE_NAME, "[ERROR] start -> writeRecordsFile -> onFileCreated -> market = " + market.assetA + "_" + market.assetB); + + callBackFunction(err); + return; + + } + + writeConditionsFile(); + + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeRecordsFile -> onFileCreated -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeRecordsFile -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function writeConditionsFile() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeConditionsFile -> Entering function."); } + + let separator = ""; + let fileRecordCounter = 0; + + let fileContent = ""; + + for (let i = 0; i < conditionsArray.length; i++) { + + let record = conditionsArray[i]; + + /* Will only add to the file the records of the current day */ + + if (record.begin < currentDay.valueOf()) { continue; } + + let conditions = ""; + let conditionsSeparator = ""; + + /* Will only add to the file the records of the current day */ + + if (record.begin < currentDay.valueOf()) { continue; } + + for (let j = 0; j < record.length - 3; j++) { + conditions = conditions + conditionsSeparator + record[j]; + if (conditionsSeparator === "") { conditionsSeparator = ","; } + } + + conditions = conditions + conditionsSeparator + '[' + record[record.length - 3] + ']'; // The last item contains an Array of condition values. + conditions = conditions + conditionsSeparator + '[' + record[record.length - 2] + ']'; // The last item contains an Array of formulaErrors. + conditions = conditions + conditionsSeparator + '[' + record[record.length - 1] + ']'; // The last item contains an Array of formulaValues. + + fileContent = fileContent + separator + '[' + conditions + ']'; + + if (separator === "") { separator = ","; } + + fileRecordCounter++; + + } + + fileContent = "[" + fileContent + "]"; + fileContent = "[" + JSON.stringify(tradingSystem) + "," + JSON.stringify(lastObjectsArray) + "," + fileContent + "]"; + + let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2); + let fileName = '' + market.assetA + '_' + market.assetB + '.json'; + + let filePathRoot = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + global.CLONE_EXECUTOR.codeName + "." + global.CLONE_EXECUTOR.version + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + let filePath = filePathRoot + "/Output/" + CONDITIONS_FOLDER_NAME + "/" + "Multi-Period-Daily" + "/" + outputPeriodLabel + "/" + dateForPath; + filePath += '/' + fileName + + fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); + + function onFileCreated(err) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeConditionsFile -> onFileCreated -> Entering function."); } + if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> writeConditionsFile -> onFileCreated -> fileContent = " + fileContent); } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + logger.write(MODULE_NAME, "[ERROR] start -> writeConditionsFile -> onFileCreated -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> writeConditionsFile -> onFileCreated -> filePath = " + filePath); + logger.write(MODULE_NAME, "[ERROR] start -> writeConditionsFile -> onFileCreated -> market = " + market.assetA + "_" + market.assetB); + + callBackFunction(err); + return; + + } + + writeStrategiesFile(); + + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeConditionsFile -> onFileCreated -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeConditionsFile -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function writeStrategiesFile() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStrategiesFile -> Entering function."); } + + let separator = ""; + let fileRecordCounter = 0; + + let fileContent = ""; + + for (let i = 0; i < strategiesArray.length; i++) { + let record = strategiesArray[i]; + + /* Will only add to the file the records of the current day. In this case since objects can span more than one day, we add all of the objects that ends + at the current date. */ + + if (record.end < currentDay.valueOf()) { continue; } + + fileContent = fileContent + separator + '[' + + record.begin + "," + + record.end + "," + + record.status + "," + + record.number + "," + + record.beginRate + "," + + record.endRate + "]"; + + if (separator === "") { separator = ","; } + + fileRecordCounter++; + + } + + fileContent = "[" + fileContent + "]"; + + let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2); + let fileName = '' + market.assetA + '_' + market.assetB + '.json'; + + let filePathRoot = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + global.CLONE_EXECUTOR.codeName + "." + global.CLONE_EXECUTOR.version + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + let filePath = filePathRoot + "/Output/" + STRATEGIES_FOLDER_NAME + "/" + "Multi-Period-Daily" + "/" + outputPeriodLabel + "/" + dateForPath; + filePath += '/' + fileName + + fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); + + function onFileCreated(err) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStrategiesFile -> onFileCreated -> Entering function."); } + if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStrategiesFile -> onFileCreated -> fileContent = " + fileContent); } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + logger.write(MODULE_NAME, "[ERROR] start -> writeStrategiesFile -> onFileCreated -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> writeStrategiesFile -> onFileCreated -> filePath = " + filePath); + logger.write(MODULE_NAME, "[ERROR] start -> writeStrategiesFile -> onFileCreated -> market = " + market.assetA + "_" + market.assetB); + + callBackFunction(err); + return; + + } + + writeTradesFile(); + + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeStrategiesFile -> onFileCreated -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeStrategiesFile -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function writeTradesFile() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeTradesFile -> Entering function."); } + + let separator = ""; + let fileRecordCounter = 0; + + let fileContent = ""; + + for (let i = 0; i < tradesArray.length; i++) { + + let record = tradesArray[i]; + + /* Will only add to the file the records of the current day. In this case since objects can span more than one day, we add all of the objects that ends + at the current date. */ + + if (record.end < currentDay.valueOf()) { continue; } + + fileContent = fileContent + separator + '[' + + record.begin + "," + + record.end + "," + + record.status + "," + + record.lastProfitPercent + "," + + record.beginRate + "," + + record.endRate + "," + + record.exitType + "," + + record.stopRate + "]"; + + if (separator === "") { separator = ","; } + + fileRecordCounter++; + + } + + fileContent = "[" + fileContent + "]"; + + let dateForPath = currentDay.getUTCFullYear() + '/' + utilities.pad(currentDay.getUTCMonth() + 1, 2) + '/' + utilities.pad(currentDay.getUTCDate(), 2); + let fileName = '' + market.assetA + '_' + market.assetB + '.json'; + + let filePathRoot = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + global.CLONE_EXECUTOR.codeName + "." + global.CLONE_EXECUTOR.version + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + let filePath = filePathRoot + "/Output/" + TRADES_FOLDER_NAME + "/" + "Multi-Period-Daily" + "/" + outputPeriodLabel + "/" + dateForPath; + filePath += '/' + fileName + + fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); + + function onFileCreated(err) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeTradesFile -> onFileCreated -> Entering function."); } + if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> writeTradesFile -> onFileCreated -> fileContent = " + fileContent); } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + logger.write(MODULE_NAME, "[ERROR] start -> writeTradesFile -> onFileCreated -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> writeTradesFile -> onFileCreated -> filePath = " + filePath); + logger.write(MODULE_NAME, "[ERROR] start -> writeTradesFile -> onFileCreated -> market = " + market.assetA + "_" + market.assetB); + + callBackFunction(err); + return; + + } + + callBackFunction(global.DEFAULT_OK_RESPONSE); + + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeTradesFile -> onFileCreated -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeTradesFile -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } +}; diff --git a/src/storage/templates/Simulator/User.Bot.Market.js b/src/storage/templates/Simulator/User.Bot.Market.js new file mode 100644 index 000000000..52658e30e --- /dev/null +++ b/src/storage/templates/Simulator/User.Bot.Market.js @@ -0,0 +1,442 @@ +exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, fileStorage) { + + const FULL_LOG = true; + const LOG_FILE_CONTENT = false; + + const MODULE_NAME = "User Bot"; + + const SIMULATED_RECORDS_FOLDER_NAME = "Trading-Simulation"; + const CONDITIONS_FOLDER_NAME = "Simulation-Conditions"; + const STRATEGIES_FOLDER_NAME = "Simulation-Strategies"; + const TRADES_FOLDER_NAME = "Simulation-Trades"; + + const commons = COMMONS.newCommons(bot, logger, UTILITIES); + + thisObject = { + initialize: initialize, + start: start + }; + + let dataDependencies; + + return thisObject; + + function initialize(pDataDependencies, callBackFunction) { + + try { + + logger.fileName = MODULE_NAME; + logger.initialize(); + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] initialize -> Entering function."); } + + dataDependencies = pDataDependencies; + + callBackFunction(global.DEFAULT_OK_RESPONSE); + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function start(dataFiles, timePeriod, outputPeriodLabel, startDate, endDate, callBackFunction) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> Entering function."); } + + let market = global.MARKET; + let dataFile; + + let recordsArray = []; + let conditionsArray = []; + let strategiesArray = []; + let tradesArray = []; + let lastObjectsArray = []; + + let tradingSystem = {}; + let interExecutionMemory = {}; + let currentDay = bot.processDatetime; + + commons.initializeData(); + + for (let i = 0; i < dataDependencies.config.length; i++) { + + let dependency = dataDependencies.config[i]; + dataFile = dataFiles[i]; + + switch (i) { + + case 0: { + commons.buildLRC(dataFile, callBackFunction); + break; + } + case 1: { + commons.buildPercentageBandwidthMap(dataFile, callBackFunction); + break; + } + case 2: { + commons.buildBollingerBandsMap(dataFile, callBackFunction); + break; + } + case 3: { + commons.buildBollingerChannelsArray(dataFile, callBackFunction); + break; + } + case 4: { + commons.buildBollingerSubChannelsArray(dataFile, callBackFunction); + break; + } + case 5: { + commons.buildCandles(dataFile, callBackFunction); + break; + } + } + } + + commons.runSimulation( + recordsArray, + conditionsArray, + strategiesArray, + tradesArray, + lastObjectsArray, + timePeriod, + currentDay, + startDate, + endDate, + interExecutionMemory, + writeFiles) + + function writeFiles(pTradingSystem) { + tradingSystem = pTradingSystem + writeRecordsFile() + } + + function writeRecordsFile() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeRecordsFile -> Entering function."); } + + let separator = ""; + let fileRecordCounter = 0; + + let fileContent = ""; + + for (let i = 0; i < recordsArray.length; i++) { + + let record = recordsArray[i]; + + fileContent = fileContent + separator + '[' + + record.begin + "," + + record.end + "," + + record.type + "," + + record.marketRate + "," + + record.amount + "," + + record.balanceA + "," + + record.balanceB + "," + + record.profit + "," + + record.lastProfit + "," + + record.stopLoss + "," + + record.roundtrips + "," + + record.hits + "," + + record.fails + "," + + record.hitRatio + "," + + record.ROI + "," + + record.periods + "," + + record.days + "," + + record.anualizedRateOfReturn + "," + + record.positionRate + "," + + record.lastProfitPercent + "," + + record.strategy + "," + + record.strategyStageNumber + "," + + record.takeProfit + "," + + record.stopLossPhase + "," + + record.takeProfitPhase + "," + + JSON.stringify(record.orderRecord) + "," + + record.positionSize + "," + + record.initialBalanceA + "," + + record.minimunBalanceA + "," + + record.maximunBalanceA + "," + + record.initialBalanceB + "," + + record.minimunBalanceB + "," + + record.maximunBalanceB + "]"; + + if (separator === "") { separator = ","; } + + fileRecordCounter++; + + } + + fileContent = "[" + fileContent + "]"; + + let fileName = '' + market.assetA + '_' + market.assetB + '.json'; + + let filePathRoot = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + global.CLONE_EXECUTOR.codeName + "." + global.CLONE_EXECUTOR.version + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + let filePath = filePathRoot + "/Output/" + SIMULATED_RECORDS_FOLDER_NAME + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel; + filePath += '/' + fileName + + fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); + + function onFileCreated(err) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeRecordsFile -> onFileCreated -> Entering function."); } + if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> writeRecordsFile -> onFileCreated -> fileContent = " + fileContent); } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + logger.write(MODULE_NAME, "[ERROR] start -> writeRecordsFile -> onFileCreated -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> writeRecordsFile -> onFileCreated -> filePath = " + filePath); + logger.write(MODULE_NAME, "[ERROR] start -> writeRecordsFile -> onFileCreated -> market = " + market.assetA + "_" + market.assetB); + + callBackFunction(err); + return; + + } + + writeConditionsFile(); + + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeRecordsFile -> onFileCreated -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeRecordsFile -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function writeConditionsFile() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeConditionsFile -> Entering function."); } + + let separator = ""; + let fileRecordCounter = 0; + + let fileContent = ""; + + for (let i = 0; i < conditionsArray.length; i++) { + + let record = conditionsArray[i]; + + let conditions = ""; + let conditionsSeparator = ""; + + for (let j = 0; j < record.length - 3; j++) { + conditions = conditions + conditionsSeparator + record[j]; + if (conditionsSeparator === "") { conditionsSeparator = ","; } + } + + conditions = conditions + conditionsSeparator + '[' + record[record.length - 3] + ']'; // The last item contains an Array of condition values. + conditions = conditions + conditionsSeparator + '[' + record[record.length - 2] + ']'; // The last item contains an Array of formulaErrors. + conditions = conditions + conditionsSeparator + '[' + record[record.length - 1] + ']'; // The last item contains an Array of formulaValues. + + fileContent = fileContent + separator + '[' + conditions + ']'; + + if (separator === "") { separator = ","; } + + fileRecordCounter++; + + } + + fileContent = "[" + fileContent + "]"; + fileContent = "[" + JSON.stringify(tradingSystem) + "," + JSON.stringify(lastObjectsArray) + "," + fileContent + "]"; + let fileName = '' + market.assetA + '_' + market.assetB + '.json'; + + let filePathRoot = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + global.CLONE_EXECUTOR.codeName + "." + global.CLONE_EXECUTOR.version + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + let filePath = filePathRoot + "/Output/" + CONDITIONS_FOLDER_NAME + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel; + filePath += '/' + fileName + + fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); + + function onFileCreated(err) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeConditionsFile -> onFileCreated -> Entering function."); } + if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> writeConditionsFile -> onFileCreated -> fileContent = " + fileContent); } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + logger.write(MODULE_NAME, "[ERROR] start -> writeConditionsFile -> onFileCreated -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> writeConditionsFile -> onFileCreated -> filePath = " + filePath); + logger.write(MODULE_NAME, "[ERROR] start -> writeConditionsFile -> onFileCreated -> market = " + market.assetA + "_" + market.assetB); + + callBackFunction(err); + return; + + } + + writeStrategiesFile(); + + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeConditionsFile -> onFileCreated -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeConditionsFile -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function writeStrategiesFile() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStrategiesFile -> Entering function."); } + + let separator = ""; + let fileRecordCounter = 0; + + let fileContent = ""; + + for (let i = 0; i < strategiesArray.length; i++) { + let record = strategiesArray[i]; + + fileContent = fileContent + separator + '[' + + record.begin + "," + + record.end + "," + + record.status + "," + + record.number + "," + + record.beginRate + "," + + record.endRate + "]"; + + if (separator === "") { separator = ","; } + + fileRecordCounter++; + + } + + fileContent = "[" + fileContent + "]"; + let fileName = '' + market.assetA + '_' + market.assetB + '.json'; + + let filePathRoot = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + global.CLONE_EXECUTOR.codeName + "." + global.CLONE_EXECUTOR.version + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + let filePath = filePathRoot + "/Output/" + STRATEGIES_FOLDER_NAME + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel; + filePath += '/' + fileName + + fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); + + function onFileCreated(err) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStrategiesFile -> onFileCreated -> Entering function."); } + if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> writeStrategiesFile -> onFileCreated -> fileContent = " + fileContent); } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + logger.write(MODULE_NAME, "[ERROR] start -> writeStrategiesFile -> onFileCreated -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> writeStrategiesFile -> onFileCreated -> filePath = " + filePath); + logger.write(MODULE_NAME, "[ERROR] start -> writeStrategiesFile -> onFileCreated -> market = " + market.assetA + "_" + market.assetB); + + callBackFunction(err); + return; + + } + + writeTradesFile(); + + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeStrategiesFile -> onFileCreated -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeStrategiesFile -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function writeTradesFile() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeTradesFile -> Entering function."); } + + let separator = ""; + let fileRecordCounter = 0; + + let fileContent = ""; + + for (let i = 0; i < tradesArray.length; i++) { + + let record = tradesArray[i]; + + fileContent = fileContent + separator + '[' + + record.begin + "," + + record.end + "," + + record.status + "," + + record.lastProfitPercent + "," + + record.beginRate + "," + + record.endRate + "," + + record.exitType + "," + + record.stopRate + "]"; + + if (separator === "") { separator = ","; } + + fileRecordCounter++; + + } + + fileContent = "[" + fileContent + "]"; + let fileName = '' + market.assetA + '_' + market.assetB + '.json'; + + let filePathRoot = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + global.CLONE_EXECUTOR.codeName + "." + global.CLONE_EXECUTOR.version + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + let filePath = filePathRoot + "/Output/" + TRADES_FOLDER_NAME + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel; + filePath += '/' + fileName + + fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); + + function onFileCreated(err) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeTradesFile -> onFileCreated -> Entering function."); } + if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> writeTradesFile -> onFileCreated -> fileContent = " + fileContent); } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + logger.write(MODULE_NAME, "[ERROR] start -> writeTradesFile -> onFileCreated -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> writeTradesFile -> onFileCreated -> filePath = " + filePath); + logger.write(MODULE_NAME, "[ERROR] start -> writeTradesFile -> onFileCreated -> market = " + market.assetA + "_" + market.assetB); + + callBackFunction(err); + return; + + } + + callBackFunction(global.DEFAULT_OK_RESPONSE); + + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeTradesFile -> onFileCreated -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeTradesFile -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } +}; diff --git a/src/storage/templates/Simulator/this.bot.config.json b/src/storage/templates/Simulator/this.bot.config.json new file mode 100644 index 000000000..b6e0d1b8f --- /dev/null +++ b/src/storage/templates/Simulator/this.bot.config.json @@ -0,0 +1,391 @@ +{ + "displayName": "Jason", + "codeName": "AAJason", + "type": "Indicator", + "version": { + "major": 1, + "minor": 0, + "patch": 0 + }, + "devTeam": "AAMasters", + "profilePicture": "Jason.png", + "dataSetVersion": "dataSet.V1", + "processes": [ + { + "name": "Multi-Period-Market", + "description": "Produces a single file for each time period covering all the market.", + "normalWaitTime": 60000, + "retryWaitTime": 10000, + "sleepWaitTime": 3600000, + "comaWaitTime": 86400000, + "framework": { + "name": "Multi-Period-Market", + "startDate": { + "fixedDate": "2018-07-01" + }, + "endDate": { + } + }, + "statusDependencies": [ + { + "devTeam": "AAMasters", + "bot": "AAJason", + "botVersion": { + "major": 1, + "minor": 0 + }, + "process": "Multi-Period-Market", + "dataSetVersion": "dataSet.V1" + } + ], + "dataDependencies": [ + { + "devTeam": "AAVikings", + "bot": "AAGauss", + "botVersion": { + "major": 1, + "minor": 0 + }, + "product": "LRC-Points", + "dataSetVersion": "dataSet.V1", + "dataSet": "Multi-Period-Market" + }, + { + "devTeam": "AAMasters", + "bot": "AAChris", + "botVersion": { + "major": 1, + "minor": 0 + }, + "product": "Percentage-Bandwidth", + "dataSetVersion": "dataSet.V1", + "dataSet": "Multi-Period-Market" + }, + { + "devTeam": "AAMasters", + "bot": "AAChris", + "botVersion": { + "major": 1, + "minor": 0 + }, + "product": "Bollinger-Bands", + "dataSetVersion": "dataSet.V1", + "dataSet": "Multi-Period-Market" + }, + { + "devTeam": "AAMasters", + "bot": "AAPaula", + "botVersion": { + "major": 1, + "minor": 0 + }, + "product": "Bollinger-Channels", + "dataSetVersion": "dataSet.V1", + "dataSet": "Multi-Period-Market" + }, + { + "devTeam": "AAMasters", + "bot": "AAPaula", + "botVersion": { + "major": 1, + "minor": 0 + }, + "product": "Bollinger-Sub-Channels", + "dataSetVersion": "dataSet.V1", + "dataSet": "Multi-Period-Market" + }, + { + "devTeam": "AAMasters", + "bot": "AAOlivia", + "botVersion": { + "major": 1, + "minor": 0 + }, + "product": "Candles", + "dataSetVersion": "dataSet.V1", + "dataSet": "Multi-Period-Market" + } + ] + }, + { + "name": "Multi-Period-Daily", + "description": "Produces a Daily File at each time period.", + "normalWaitTime": 60000, + "retryWaitTime": 10000, + "sleepWaitTime": 3600000, + "comaWaitTime": 86400000, + "framework": { + "name": "Multi-Period-Daily", + "validPeriods": [ "15-min", "10-min", "05-min"], + "startDate": { + "fixedDate": "2018-07-01" + }, + "endDate": { + "takeItFromStatusDependency": 1 + } + }, + "statusDependencies": [ + { + "devTeam": "AAMasters", + "bot": "AACharly", + "botVersion": { + "major": 1, + "minor": 0 + }, + "process": "Historic-Trades", + "dataSetVersion": "dataSet.V1" + }, + { + "devTeam": "AAMasters", + "bot": "AAOlivia", + "botVersion": { + "major": 1, + "minor": 0 + }, + "process": "Multi-Period-Daily", + "dataSetVersion": "dataSet.V1" + }, + { + "devTeam": "AAMasters", + "bot": "AAJason", + "botVersion": { + "major": 1, + "minor": 0 + }, + "process": "Multi-Period-Daily", + "dataSetVersion": "dataSet.V1" + } + ], + "dataDependencies": [ + { + "devTeam": "AAVikings", + "bot": "AAGauss", + "botVersion": { + "major": 1, + "minor": 0 + }, + "product": "LRC-Points", + "dataSetVersion": "dataSet.V1", + "dataSet": "Multi-Period-Market" + }, + { + "devTeam": "AAMasters", + "bot": "AAChris", + "botVersion": { + "major": 1, + "minor": 0 + }, + "product": "Percentage-Bandwidth", + "dataSetVersion": "dataSet.V1", + "dataSet": "Multi-Period-Market" + }, + { + "devTeam": "AAMasters", + "bot": "AAChris", + "botVersion": { + "major": 1, + "minor": 0 + }, + "product": "Bollinger-Bands", + "dataSetVersion": "dataSet.V1", + "dataSet": "Multi-Period-Market" + }, + { + "devTeam": "AAMasters", + "bot": "AAPaula", + "botVersion": { + "major": 1, + "minor": 0 + }, + "product": "Bollinger-Channels", + "dataSetVersion": "dataSet.V1", + "dataSet": "Multi-Period-Market" + }, + { + "devTeam": "AAMasters", + "bot": "AAPaula", + "botVersion": { + "major": 1, + "minor": 0 + }, + "product": "Bollinger-Sub-Channels", + "dataSetVersion": "dataSet.V1", + "dataSet": "Multi-Period-Market" + }, + { + "devTeam": "AAMasters", + "bot": "AAOlivia", + "botVersion": { + "major": 1, + "minor": 0 + }, + "product": "Candles", + "dataSetVersion": "dataSet.V1", + "dataSet": "Multi-Period-Market" + } + ] + } + ], + "products": [ + { + "codeName": "Trading-Simulation", + "displayName": "Trading Simulation", + "description": "Trading Simulation", + "storageAccount": "aajason", + "dataSets": [ + { + "codeName": "Multi-Period-Market", + "type": "Market Files", + "validPeriods": [ "24-hs", "12-hs", "08-hs", "06-hs", "04-hs", "03-hs", "02-hs", "01-hs" ], + "filePath": "AAMasters/AAJason.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Trading-Simulation/Multi-Period-Market/@Period", + "fileName": "@AssetA_@AssetB.json" + }, + { + "codeName": "Multi-Period-Daily", + "type": "Daily Files", + "validPeriods": [ "15-min", "10-min", "05-min" ], + "filePath": "AAMasters/AAJason.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Trading-Simulation/Multi-Period-Daily/@Period/@Year/@Month/@Day", + "fileName": "@AssetA_@AssetB.json", + "dataRange": { + "filePath": "AAMasters/AAJason.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Trading-Simulation/Multi-Period-Daily", + "fileName": "Data.Range.@AssetA_@AssetB.json" + } + } + ], + "exchangeList": [ + { + "name": "Poloniex" + }, + { + "name": "Coss" + } + ], + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersTradingSimulation", + "moduleName": "TradingSimulation" + }, + "relatedToTradingEngine": true + }, + { + "codeName": "Simulation-Conditions", + "displayName": "Simulation Conditions", + "description": "Simulation Conditions", + "storageAccount": "aajason", + "dataSets": [ + { + "codeName": "Multi-Period-Market", + "type": "Market Files", + "validPeriods": [ "24-hs", "12-hs", "08-hs", "06-hs", "04-hs", "03-hs", "02-hs", "01-hs" ], + "filePath": "AAMasters/AAJason.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Conditions/Multi-Period-Market/@Period", + "fileName": "@AssetA_@AssetB.json" + }, + { + "codeName": "Multi-Period-Daily", + "type": "Daily Files", + "validPeriods": [ "15-min", "10-min", "05-min" ], + "filePath": "AAMasters/AAJason.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Conditions/Multi-Period-Daily/@Period/@Year/@Month/@Day", + "fileName": "@AssetA_@AssetB.json", + "dataRange": { + "filePath": "AAMasters/AAJason.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Conditions/Multi-Period-Daily", + "fileName": "Data.Range.@AssetA_@AssetB.json" + } + } + ], + "exchangeList": [ + { + "name": "Poloniex" + }, + { + "name": "Coss" + } + ], + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersTradingSimulation", + "moduleName": "Conditions" + }, + "relatedToTradingEngine": true + }, + { + "codeName": "Simulation-Strategies", + "displayName": "Simulation Strategies", + "description": "Simulation Strategies", + "storageAccount": "aajason", + "dataSets": [ + { + "codeName": "Multi-Period-Market", + "type": "Market Files", + "validPeriods": [ "24-hs", "12-hs", "08-hs", "06-hs", "04-hs", "03-hs", "02-hs", "01-hs" ], + "filePath": "AAMasters/AAJason.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Strategies/Multi-Period-Market/@Period", + "fileName": "@AssetA_@AssetB.json" + }, + { + "codeName": "Multi-Period-Daily", + "type": "Daily Files", + "validPeriods": [ "15-min", "10-min", "05-min" ], + "filePath": "AAMasters/AAJason.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Strategies/Multi-Period-Daily/@Period/@Year/@Month/@Day", + "fileName": "@AssetA_@AssetB.json", + "dataRange": { + "filePath": "AAMasters/AAJason.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Strategies/Multi-Period-Daily", + "fileName": "Data.Range.@AssetA_@AssetB.json" + } + } + ], + "exchangeList": [ + { + "name": "Poloniex" + }, + { + "name": "Coss" + } + ], + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersTradingSimulation", + "moduleName": "Strategies" + }, + "relatedToTradingEngine": true + }, + { + "codeName": "Simulation-Trades", + "displayName": "Simulation Trades", + "description": "Simulation Trades", + "storageAccount": "aajason", + "dataSets": [ + { + "codeName": "Multi-Period-Market", + "type": "Market Files", + "validPeriods": [ "24-hs", "12-hs", "08-hs", "06-hs", "04-hs", "03-hs", "02-hs", "01-hs" ], + "filePath": "AAMasters/AAJason.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Trades/Multi-Period-Market/@Period", + "fileName": "@AssetA_@AssetB.json" + }, + { + "codeName": "Multi-Period-Daily", + "type": "Daily Files", + "validPeriods": [ "15-min", "10-min", "05-min" ], + "filePath": "AAMasters/AAJason.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Trades/Multi-Period-Daily/@Period/@Year/@Month/@Day", + "fileName": "@AssetA_@AssetB.json", + "dataRange": { + "filePath": "AAMasters/AAJason.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Trades/Multi-Period-Daily", + "fileName": "Data.Range.@AssetA_@AssetB.json" + } + } + ], + "exchangeList": [ + { + "name": "Poloniex" + }, + { + "name": "Coss" + } + ], + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersTradingSimulation", + "moduleName": "Trades" + }, + "relatedToTradingEngine": true + } + ] +} diff --git a/src/storage/templates/TradingBot/User.Bot.js b/src/storage/templates/TradingBot/User.Bot.js new file mode 100644 index 000000000..963c6d3c9 --- /dev/null +++ b/src/storage/templates/TradingBot/User.Bot.js @@ -0,0 +1,764 @@ +exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { + + // Variable used for logs, which will be passed to the logger instance + const MODULE_NAME = 'User Bot' + const LOG_INFO = true + let assistant, fileStorage + let autopilotControl = false + + const orderMessage = require("@superalgos/mqservice/orderMessage/orderMessage") + + const { + MESSAGE_ENTITY, MESSAGE_TYPE, ORDER_CREATOR, ORDER_TYPE, ORDER_OWNER, + ORDER_DIRECTION, ORDER_STATUS, ORDER_EXIT_OUTCOME, ORDER_MARGIN_ENABLED, + getMessage, createMessageFromObject + } = orderMessage.newOrderMessage() + + const axios = require('axios') + const util = require('util') + + /* + This objects returns two public functions that will be used to integrate with the platform. + */ + return { + initialize: initialize, + start: start, + setAutopilot: setAutopilot + } + + function initialize(pAssistant, pGenes, callBackFunction) { + try { + logInfo('initialize -> Entering function.') + + // Integration with platform logger, when running on cloud + logger.fileName = MODULE_NAME + assistant = pAssistant + + if (bot.timePeriodFileStorage === undefined) { + throw new Error("Execution Parameter Time Period not defined.") + } + + if (bot.dataSet === undefined) { + throw new Error("Execution Parameter Data Set not defined.") + } + + let key = bot.devTeam + '-simulator-' + bot.codeName + '-Trading-Simulation-' + bot.dataSet + '-dataSet.V1' + fileStorage = assistant.dataDependencies.dataSets.get(key) + + if (fileStorage !== undefined){ + callBackFunction(global.DEFAULT_OK_RESPONSE) + }else{ + logError('initialize -> Failed to initialize storage. Key not found:' + key) + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + } + } catch (error) { + logError('initialize -> onDone -> err = ' + error.message) + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + } + } + + function logInfo(message) { + if (LOG_INFO === true) { logger.write(MODULE_NAME, '[INFO] ' + message) } + } + + function logWarn(message) { + if (LOG_INFO === true) { logger.write(MODULE_NAME, '[WARN] ' + message) } + } + + function logError(message) { + logger.write(MODULE_NAME, '[ERROR] ' + message) + } + + function setAutopilot(autopilot) { + autopilotControl = autopilot + } + + /* + The start function is called by the platform for executing the bot every 1 minute + */ + function start(callBackFunction) { + logInfo('start -> Entering function.') + + const callback = util.callbackify(executorLogic) + + callback((err, ret) => { + if (err) { + callBackFunction(err) + } else { + callBackFunction(global.DEFAULT_OK_RESPONSE) + } + }) + } + + async function executorLogic() { + try { + let indicatorFileContent = await getIndicatorFile() + let simulatorEngineMessage = getSimulatorEngineMessageFromFile(indicatorFileContent) + let autopilotResponse = await getAutopilot() + + if (simulatorEngineMessage === undefined) { + logWarn('start -> Indicator message not found.') + throw global.DEFAULT_RETRY_RESPONSE + } + + logInfo('Processing indicator message: ' + JSON.stringify(simulatorEngineMessage)) + + // Checking Stop Loss + let assetABalance = assistant.getAvailableBalance().assetA + let currentRate = assistant.getMarketRate() + + if (simulatorEngineMessage !== undefined && assetABalance > 0 && currentRate >= simulatorEngineMessage.order.stop) { + logInfo('Closing trade with Stop Loss.') + try { + let position = await createBuyPosition(currentRate) + + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.id = position.id + simulatorExecutorMessage.order.id = position.id + simulatorExecutorMessage.order.rate = currentRate + simulatorExecutorMessage.order.stop = simulatorEngineMessage.order.stop + simulatorExecutorMessage.order.takeProfit = simulatorEngineMessage.order.takeProfit + simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Buy + simulatorExecutorMessage.order.size = assetABalance + simulatorExecutorMessage.order.status = ORDER_STATUS.Placed + simulatorExecutorMessage.order.exitOutcome = ORDER_EXIT_OUTCOME.StopLoss + + let message = createMessageFromObject(simulatorExecutorMessage) + assistant.addExtraData(message) + } catch (error) { + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.from = MESSAGE_ENTITY.TradingAssistant + simulatorExecutorMessage.to = MESSAGE_ENTITY.SimulationExecutor + simulatorExecutorMessage.order.rate = position.rate + simulatorExecutorMessage.order.stop = simulatorEngineMessage.order.stop + simulatorExecutorMessage.order.takeProfit = simulatorEngineMessage.order.takeProfit + simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Buy + simulatorExecutorMessage.order.size = assetABalance + simulatorExecutorMessage.order.status = ORDER_STATUS.Rejected + + let message = createMessageFromObject(simulatorExecutorMessage) + assistant.addExtraData(message) + } finally { + return + } + } + + // Checking Take Profits + if (simulatorEngineMessage !== undefined && assetABalance > 0 && currentRate <= simulatorEngineMessage.order.takeProfit) { + logInfo('Closing trade with Take Profits.') + try { + let position = await createBuyPosition(currentRate, assetABalance) + + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.id = position.id + simulatorExecutorMessage.order.id = position.id + simulatorExecutorMessage.order.rate = currentRate + simulatorExecutorMessage.order.stop = simulatorEngineMessage.order.stop + simulatorExecutorMessage.order.takeProfit = simulatorEngineMessage.order.takeProfit + simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Buy + simulatorExecutorMessage.order.size = assetABalance + simulatorExecutorMessage.order.status = ORDER_STATUS.Placed + simulatorExecutorMessage.order.exitOutcome = ORDER_EXIT_OUTCOME.TakeProfit + + let message = createMessageFromObject(simulatorExecutorMessage) + assistant.addExtraData(message) + } catch (error) { + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.from = MESSAGE_ENTITY.TradingAssistant + simulatorExecutorMessage.to = MESSAGE_ENTITY.SimulationExecutor + simulatorExecutorMessage.order.rate = currentRate + simulatorExecutorMessage.order.stop = simulatorEngineMessage.order.stop + simulatorExecutorMessage.order.takeProfit = simulatorEngineMessage.order.takeProfit + simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Buy + simulatorExecutorMessage.order.size = assetABalance + simulatorExecutorMessage.order.status = ORDER_STATUS.Rejected + + let message = createMessageFromObject(simulatorExecutorMessage) + assistant.addExtraData(message) + } finally { + return + } + } + + if (autopilotResponse.autopilot || autopilotControl) { + await manageCloneInAutopilotOn(simulatorEngineMessage) + } else { + await manageCloneInAutopilotOff(simulatorEngineMessage) + } + } catch (error) { + if (error.stack) logError(JSON.stringify(error.stack)) + throw error + } + } + + async function manageCloneInAutopilotOn(simulatorEngineMessage) { + let assetBBalance = assistant.getAvailableBalance().assetB + let currentRate = assistant.getMarketRate() + + if (simulatorEngineMessage.messageType === MESSAGE_TYPE.Order + && simulatorEngineMessage.order.direction === ORDER_DIRECTION.Sell) { + + try { + let position = await createSellPosition(currentRate, assetBBalance) + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.id = position.id + simulatorExecutorMessage.order.id = position.id + simulatorExecutorMessage.order.rate = currentRate + simulatorExecutorMessage.order.stop = simulatorEngineMessage.order.stop + simulatorExecutorMessage.order.takeProfit = simulatorEngineMessage.order.takeProfit + simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Sell + simulatorExecutorMessage.order.size = assetBBalance + + let message = createMessageFromObject(simulatorExecutorMessage) + assistant.addExtraData(message) + } catch (error) { + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.from = MESSAGE_ENTITY.TradingAssistant + simulatorExecutorMessage.to = MESSAGE_ENTITY.SimulationExecutor + simulatorExecutorMessage.messageType = MESSAGE_TYPE.Order + simulatorExecutorMessage.order.rate = currentRate + simulatorExecutorMessage.order.stop = simulatorEngineMessage.order.stop + simulatorExecutorMessage.order.takeProfit = simulatorEngineMessage.order.takeProfit + simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Sell + simulatorExecutorMessage.order.size = assetBBalance + simulatorExecutorMessage.order.status = ORDER_STATUS.Rejected + + let message = createMessageFromObject(simulatorExecutorMessage) + assistant.addExtraData(message) + } + } else { + logInfo("manageCloneInAutopilotOn -> Nothing to do, there isn't a buy or sell opportunity.") + } + } + + async function manageCloneInAutopilotOff(simulatorEngineMessage) { + /** + * If there is a ManualAuthorized order on the cockpit we will proceed to: + * 1) Save an audit containing what we received from the cockpit + * 2) Execute the order in the market as received + * 3) Save the audit of the order placed + * 4) Update the cockpit with the result + */ + let acceptedSignals = await getSignalsByCloneId(ORDER_STATUS.ManualAuthorized) + if (acceptedSignals !== undefined && acceptedSignals.length > 0) { + for (let acceptedSignal of acceptedSignals) { + try { + logInfo('manageCloneInAutopilotOff -> Executing manual authorized signal.') + + // 1) Save audit containing what we received from the cockpit + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.id = 0 + simulatorExecutorMessage.from = MESSAGE_ENTITY.TradingCokpit + simulatorExecutorMessage.to = MESSAGE_ENTITY.SimulationExecutor + simulatorExecutorMessage.order = acceptedSignal.orderData // we save the order as received + simulatorExecutorMessage.order.status = ORDER_STATUS.ManualAuthorized + let message = createMessageFromObject(simulatorExecutorMessage) + assistant.addExtraData(message) + + // 2) Execute the order in the market as received + let position = await createSellPosition(acceptedSignal.orderData.rate, acceptedSignal.orderData.size) + + // 3) Save the audit of the order placed + simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.id = position.id + simulatorExecutorMessage.from = MESSAGE_ENTITY.SimulationExecutor + simulatorExecutorMessage.to = MESSAGE_ENTITY.TradingAssistant + simulatorExecutorMessage.dateTime = position.date + + simulatorExecutorMessage.order = acceptedSignal.orderData + simulatorExecutorMessage.order.id = position.id + simulatorExecutorMessage.order.dateTime = position.date + simulatorExecutorMessage.order.rate = position.rate + simulatorExecutorMessage.order.size = position.amountB + simulatorExecutorMessage.order.status = ORDER_STATUS.Placed + simulatorExecutorMessage.order.sizeFilled = 0 + + // 4) Update the cockpit with the result + await updateSignal(acceptedSignal.id, simulatorExecutorMessage) + + message = createMessageFromObject(simulatorExecutorMessage) + assistant.addExtraData(message) + return + } catch (error) { + // Update the cockpit with the error + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.id = 0 + simulatorExecutorMessage.from = MESSAGE_ENTITY.SimulationExecutor + simulatorExecutorMessage.to = MESSAGE_ENTITY.TradingCokpit + simulatorExecutorMessage.messageType = MESSAGE_TYPE.Order + simulatorExecutorMessage.dateTime = bot.processDatetime.valueOf() + simulatorExecutorMessage.order.status = ORDER_STATUS.Rejected + + simulatorExecutorMessage.order = acceptedSignal.order + + await updateSignal(acceptedSignal.id, simulatorExecutorMessage) + + // Update the audit with the error + let message = createMessageFromObject(simulatorExecutorMessage) + assistant.addExtraData(message) + return + } + } + } + + /** + * If there is a Placed order on the cockpit we will proceed to: + * 1) Check order status on the market + * 2) Save the audit of the status + * 3) Update the cockpit with the results + */ + let inProcessSignals = await getSignalsByCloneId(ORDER_STATUS.Placed) + if (inProcessSignals !== undefined && inProcessSignals.length > 0) { + for (let inProcessSignal of inProcessSignals) { + logInfo('manageCloneInAutopilotOff -> Checking in process signal.') + // 1) Check order status on the market + let position = checkSignalCompletion() + if (position || position === undefined) { + // 2) Save the audit of the status + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.id = inProcessSignal.orderData.id + simulatorExecutorMessage.from = MESSAGE_ENTITY.SimulationExecutor + simulatorExecutorMessage.to = MESSAGE_ENTITY.TradingCokpit + simulatorExecutorMessage.messageType = MESSAGE_TYPE.OrderUpdate + + simulatorExecutorMessage.order = inProcessSignal.orderData + simulatorExecutorMessage.order.status = ORDER_STATUS.Filled + simulatorExecutorMessage.order.sizeFilled = -1 + + // 3) Update the cockpit with the results + await updateSignal(inProcessSignal.id, simulatorExecutorMessage) + + let message = createMessageFromObject(simulatorExecutorMessage) + assistant.addExtraData(message) + } else { + // Otherwise: Save the audit of the order status, wichs is currently saved by the trading process + } + return + } + } + + /** + * If there is any signal not yet authorized: + * 1) Update the signal with the new indicator value on the cockpit + * 3) Save the audit of the message sent + */ + let assetBBalance = assistant.getAvailableBalance().assetB + let currentRate = assistant.getMarketRate() + let signals = await getSignalsByCloneId(ORDER_STATUS.Signaled) + if (signals !== undefined && signals.length > 0) { + for (let inProcessSignal of signals) { + logInfo('manageCloneInAutopilotOff -> Signal waiting for approval found, updating it with latest market data.') + + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.to = MESSAGE_ENTITY.TradingCokpit + simulatorExecutorMessage.messageType = MESSAGE_TYPE.OrderUpdate + simulatorExecutorMessage.order.id = 0 + simulatorExecutorMessage.order.rate = currentRate + simulatorExecutorMessage.order.stop = simulatorEngineMessage.order.stop + simulatorExecutorMessage.order.takeProfit = simulatorEngineMessage.order.takeProfit + simulatorExecutorMessage.order.direction = simulatorEngineMessage.order.direction + simulatorExecutorMessage.order.size = assetBBalance + simulatorExecutorMessage.order.status = ORDER_STATUS.Signaled + + await updateSignal(inProcessSignal.id, simulatorExecutorMessage) + + let message = createMessageFromObject(simulatorExecutorMessage) + assistant.addExtraData(message) + return + } + } + + /** + * Finally: + * 1) Check if it's needed to create a new signal based on the indicator + * 2) Save the audit message for the received info from Trading Simulator + * 3) Create the signal on the cockpit + */ + if (simulatorEngineMessage !== undefined && simulatorEngineMessage.messageType === MESSAGE_TYPE.Order + && simulatorEngineMessage.order.direction === ORDER_DIRECTION.Sell) { + logInfo('Creating new signal.') + + // 2) Save the audit message for the received info from Trading Simulator + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.id = 0 + simulatorExecutorMessage.to = MESSAGE_ENTITY.TradingCokpit + simulatorExecutorMessage.messageType = MESSAGE_TYPE.Order + simulatorExecutorMessage.order.id = 0 + simulatorExecutorMessage.order.status = ORDER_STATUS.Signaled + simulatorExecutorMessage.order.rate = currentRate + simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Sell + simulatorExecutorMessage.order.takeProfit = simulatorEngineMessage.order.takeProfit + simulatorExecutorMessage.order.stop = simulatorEngineMessage.order.stop + simulatorExecutorMessage.order.size = assetBBalance + + let message = createMessageFromObject(simulatorExecutorMessage) + assistant.addExtraData(message) + + // 3) Create the signal on the cockpit + await createSignal(simulatorExecutorMessage) + } else { + logWarn("manageCloneInAutopilotOff -> Nothing to do, there isn't a buy or sell opportunity.") + } + } + + async function createBuyPosition(currentRate, amountToBuy) { + if (!amountToBuy) amountToBuy = assistant.getAvailableBalance().assetA + let amountB = Number((amountToBuy / currentRate).toFixed(8)) + let positions = assistant.getPositions() + let assetABalance = assistant.getAvailableBalance().assetA + + if (positions.length > 0 && positions[0].type === 'buy' && positions[0].status !== 'executed') { + logInfo('createBuyPosition -> Moving an existing BUY position to a new price: $' + Number(currentRate).toLocaleString()) + + assistant.movePosition[util.promisify.custom] = n => new Promise((resolve, reject) => { + assistant.movePosition(positions[0], currentRate, (result, position) => { + if (result !== global.DEFAULT_OK_RESPONSE) { + reject(result) + } else { + resolve(position) + } + }) + }) + + let movePosition = util.promisify(assistant.movePosition) + await movePosition(positions[0], currentRate) + return positions[0] + } else if (assetABalance > 0) { + logInfo('createBuyPosition -> Put a new BUY position at price: $' + Number(currentRate).toLocaleString()) + + assistant.putPosition[util.promisify.custom] = n => new Promise((resolve, reject) => { + assistant.putPosition('buy', currentRate, amountToBuy, amountB, (result, position) => { + if (result !== global.DEFAULT_OK_RESPONSE) { + reject(result) + } else { + resolve(position) + } + }) + }) + + let putPosition = util.promisify(assistant.putPosition) + await putPosition('buy', currentRate, amountToBuy, amountB) + return positions[0] + } else { + logInfo('createBuyPosition -> Not enough available balance to buy.') + throw new Error('There is not enough available balance to buy.') + } + } + + async function createSellPosition(currentRate, amountToSell) { + if (!amountToSell) amountToSell = assistant.getAvailableBalance().assetB + let amountA = amountToSell * currentRate + let positions = assistant.getPositions() + + if (positions.length > 0 && positions[0].type === 'sell' && positions[0].status !== 'executed') { + logInfo('createSellPosition -> Moving an existing SELL position to a new price: $' + Number(currentRate).toLocaleString()) + + assistant.movePosition[util.promisify.custom] = n => new Promise((resolve, reject) => { + assistant.movePosition(positions[0], currentRate, (result, position) => { + if (result !== global.DEFAULT_OK_RESPONSE) { + reject(result) + } else { + resolve(position) + } + }) + }) + + let movePosition = util.promisify(assistant.movePosition) + await movePosition(positions[0], currentRate) + return positions[0] + } else if (assistant.getAvailableBalance().assetB > 0) { + logInfo('createSellPosition -> Put a new SELL position at price: $' + Number(currentRate).toLocaleString()) + assistant.putPosition[util.promisify.custom] = n => new Promise((resolve, reject) => { + assistant.putPosition('sell', currentRate, amountA, amountToSell, (result, position) => { + if (result !== global.DEFAULT_OK_RESPONSE) { + reject(result) + } else { + resolve(position) + } + }) + }) + + let putPosition = util.promisify(assistant.putPosition) + let position = await putPosition('sell', currentRate, amountA, amountToSell) + return position + } else { + logInfo('createSellPosition -> There is not enough available balance to sell.') + throw new Error('There is not enough available balance to sell.') + } + } + + function checkSignalCompletion() { + let positions = assistant.getPositions() + if ((positions.length === 0) || (positions.length > 0 && positions[0].status === 'executed')) { + logInfo('checkSignalCompletion -> The order was executed.') + return positions[0] + } else { + return false + } + } + + // Storage functions + async function getIndicatorFile() { + logInfo('getIndicatorFile -> Entering function.') + + let filePath + if (bot.processes[0].timePeriod > 2700000) { + // Market Files + filePath = 'Trading-Simulation/' + bot.dataSet + '/' + bot.timePeriodFileStorage + } else { + // Daily Files + let dateTime = bot.processDatetime + let datePath = dateTime.getUTCFullYear() + '/' + pad(dateTime.getUTCMonth() + 1, 2) + '/' + pad(dateTime.getUTCDate(), 2) + filePath = 'Trading-Simulation/' + bot.dataSet + '/' + bot.timePeriodFileStorage + '/' + datePath + } + + /* + bot.botCache: allows us to keep a map (key value pairs) between executions, + so we don't need to go to the storage to retrieve this value. + If the value already exist on the cache we will get it from there, + otherwise it will be retrieved from the bot storage. + */ + if (bot.startMode === 'Backtest' && bot.botCache.has(filePath)) { + logInfo('getIndicatorFile -> Getting the file from local cache.') + return bot.botCache.get(filePath) + } else { + let indicatorFileContent = await getFileContent(filePath, 'USDT_BTC.json') + bot.botCache.set(filePath, JSON.parse(indicatorFileContent)) + return JSON.parse(indicatorFileContent) + } + } + + function getSimulatorEngineMessageFromFile(indicatorFileContent) { + try { + let lastIndexIndicatorFile = indicatorFileContent.length - 1 + let lastAvailableDateTime = indicatorFileContent[lastIndexIndicatorFile][0] + + if (bot.processDatetime.valueOf() <= lastAvailableDateTime || !isExecutionToday()) { + for (let i = 0; i < indicatorFileContent.length; i++) { + if (bot.processDatetime.valueOf() >= indicatorFileContent[i][0] && bot.processDatetime.valueOf() < indicatorFileContent[i][1]) { + return getMessage(indicatorFileContent[i][25]) + } + } + + logWarn('getSimulatorEngineMessageFromFile -> The indicator message was not found at time: ' + bot.processDatetime.valueOf()) + } else { + // Running live we will process last available Indicator Message only if it's delayed 25 minutes top + let maxTolerance = 120 * 60 * 1000 + if (bot.processDatetime.valueOf() <= (lastAvailableDateTime + maxTolerance)) { + return getMessage(indicatorFileContent[lastIndexIndicatorFile][25]) + } else { + logWarn('getSimulatorEngineMessageFromFile -> Last available indicator older than 2 hours. Skipping execution.') + } + } + } catch (error) { + logError('getSimulatorEngineMessageFromFile -> error = ' + error.message) + throw global.DEFAULT_RETRY_RESPONSE + } + } + + // Helper Functions + function isExecutionToday() { + let localDate = new Date() + let today = new Date(Date.UTC( + localDate.getUTCFullYear(), + localDate.getUTCMonth(), + localDate.getUTCDate())) + + return (today.getUTCFullYear() === bot.processDatetime.getUTCFullYear() + && today.getUTCMonth() === bot.processDatetime.getUTCMonth() + && today.getUTCDate() === bot.processDatetime.getUTCDate()) + } + + // Modules queries + async function getSignalsByCloneId(orderStatus) { + try { + const cockPit = await axios({ + url: process.env.GATEWAY_ENDPOINT, + method: 'post', + data: { + query: ` + query($cloneId: String!, $orderStatus: String){ + cockpit_SignalsByCloneId(cloneId: $cloneId, orderStatus:$orderStatus){ + id + cloneId + orderId + orderCreator + orderStatus + orderData + } + } + `, + variables: { + cloneId: process.env.CLONE_ID, + orderStatus: orderStatus + }, + }, + headers: { + authorization: 'Bearer ' + global.ACCESS_TOKEN + } + }) + + logInfo("Retrieved signals in status: " + orderStatus) + return cockPit.data.data.cockpit_SignalsByCloneId + + } catch (error) { + logError('getSignalsByCloneId error: ' + error.response.data.errors[0]) + throw global.DEFAULT_RETRY_RESPONSE + } + } + + async function updateSignal(signalId, message) { + try { + const cockPit = await axios({ + url: process.env.GATEWAY_ENDPOINT, + method: 'post', + data: { + query: ` + mutation ($signalId: ID!, $message:cockpit_JSON!){ + cockpit_UpdateSignal( + id: $signalId + message: $message + ) { + id + cloneId + orderStatus + } + } + `, + variables: { + signalId: signalId, + message: message + }, + }, + headers: { + authorization: 'Bearer ' + global.ACCESS_TOKEN + } + }) + + logInfo('updateSignal ok.') + return cockPit.data.data.cockpit_UpdateSignal + } catch (error) { + logError('updateSignal error: ' + error.response.data.errors[0]) + throw global.DEFAULT_RETRY_RESPONSE + } + } + + async function createSignal(message) { + try { + const cockPit = await axios({ + url: process.env.GATEWAY_ENDPOINT, + method: 'post', + data: { + query: ` + mutation ($cloneId: String!, $message: cockpit_JSON!){ + cockpit_CreateSignal( + cloneId: $cloneId + message: $message + ) { + id + cloneId + orderStatus + } + } + `, + variables: { + cloneId: process.env.CLONE_ID, + message: message + }, + }, + headers: { + authorization: 'Bearer ' + global.ACCESS_TOKEN + } + }) + + logInfo('createSignal ok.') + return cockPit.data.data.cockpit_CreateSignal + } catch (error) { + logInfo("createSignal error: " + error.response.data.errors[0]) + throw global.DEFAULT_RETRY_RESPONSE + } + } + + async function getAutopilot() { + try { + const cockPit = await axios({ + url: process.env.GATEWAY_ENDPOINT, + method: 'post', + data: { + query: ` + query($cloneId: String!){ + cockpit_CloneSettingsByCloneId(cloneId: $cloneId){ + id + cloneId + autopilot + } + } + `, + variables: { + cloneId: process.env.CLONE_ID + }, + }, + headers: { + authorization: 'Bearer ' + global.ACCESS_TOKEN + } + }) + + if (cockPit.data.errors) { + throw new Error(cockPit.data.errors[0].message) + } else { + logInfo('getAutopilot -> Ok.') + return cockPit.data.data.cockpit_CloneSettingsByCloneId + } + + } catch (error) { + logInfo('getAutopilot error: ' + error) + throw global.DEFAULT_RETRY_RESPONSE + } + } + + async function getFileContent(containerName, blobName) { + fileStorage.getTextFile[util.promisify.custom] = n => new Promise((resolve, reject) => { + fileStorage.getTextFile(containerName, blobName, (result, fileContent) => { + if (result !== global.DEFAULT_OK_RESPONSE) { + console.log('[WARN] getFileContent -> The indicator dependency is not ready. Will retry later.') + reject(global.DEFAULT_RETRY_RESPONSE) + } else { + resolve(fileContent) + } + }) + }) + + let getTextFile = util.promisify(fileStorage.getTextFile) + let fileContent = await getTextFile(containerName, blobName) + return fileContent + } + + function pad(str, max) { + str = str.toString() + return str.length < max ? pad('0' + str, max) : str + } + + function buildBasicSimulatorExecutorMessage() { + var simulatorExecutorMessage = {} + simulatorExecutorMessage.id = 0 + simulatorExecutorMessage.from = MESSAGE_ENTITY.SimulationExecutor + simulatorExecutorMessage.to = MESSAGE_ENTITY.TradingAssistant + simulatorExecutorMessage.messageType = MESSAGE_TYPE.Order + simulatorExecutorMessage.dateTime = bot.processDatetime.valueOf() + + simulatorExecutorMessage.order = {} + simulatorExecutorMessage.order.dateTime = bot.processDatetime.valueOf() + simulatorExecutorMessage.order.creator = ORDER_CREATOR.SimulationEngine + simulatorExecutorMessage.order.owner = ORDER_OWNER.User + simulatorExecutorMessage.order.exchange = global.EXCHANGE_NAME + simulatorExecutorMessage.order.market = global.MARKET.name + simulatorExecutorMessage.order.marginEnabled = ORDER_MARGIN_ENABLED.False + simulatorExecutorMessage.order.type = ORDER_TYPE.Limit + simulatorExecutorMessage.order.status = ORDER_STATUS.Placed + simulatorExecutorMessage.order.sizeFilled = 0 + simulatorExecutorMessage.order.exitOutcome = '' + + return simulatorExecutorMessage + } + +} diff --git a/src/storage/templates/TradingBot/this.bot.config.json b/src/storage/templates/TradingBot/this.bot.config.json new file mode 100644 index 000000000..c3d22f52e --- /dev/null +++ b/src/storage/templates/TradingBot/this.bot.config.json @@ -0,0 +1,248 @@ +{ + "displayName": "Bumblebee", + "codeName": "bumblebee", + "type": "Trading", + "version": { + "major": 1, + "minor": 0, + "patch": 0 + }, + "devTeam": "aavikings", + "profilePicture": "bumblebee.png", + "dataSetVersion": "dataSet.V1", + "processes": [ + { + "name": "Trading-Process", + "description": "Trading strategy to be used as a template.", + "normalWaitTime": 60000, + "retryWaitTime": 10000, + "sleepWaitTime": 3600000, + "comaWaitTime": 86400000, + "timePeriod": 3600000, + "statusDependencies": [ + { + "devTeam": "aavikings", + "bot": "bumblebee", + "botVersion": { + "major": 1, + "minor": 0 + }, + "process": "Trading-Process", + "dataSetVersion": "dataSet.V1", + "type": "Trading" + }, + { + "devTeam": "AAMasters", + "bot": "AAJason", + "botVersion": { + "major": 1, + "minor": 0 + }, + "process": "Multi-Period-Market", + "dataSetVersion": "dataSet.V1", + "type": "Indicator" + }, + { + "devTeam": "AAMasters", + "bot": "AAJason", + "botVersion": { + "major": 1, + "minor": 0 + }, + "process": "Multi-Period-Daily", + "dataSetVersion": "dataSet.V1", + "type": "Indicator" + } + ], + "dataDependencies": [ + { + "devTeam": "AAMasters", + "bot": "AABruce", + "botVersion": { + "major": 1, + "minor": 0 + }, + "product": "Candles", + "dataSetVersion": "dataSet.V1", + "dataSet": "One-Min" + }, + { + "devTeam": "AAMasters", + "bot": "AAJason", + "botVersion": { + "major": 1, + "minor": 0 + }, + "product": "Trading-Simulation", + "dataSetVersion": "dataSet.V1", + "dataSet": "Multi-Period-Market" + }, + { + "devTeam": "AAMasters", + "bot": "AAJason", + "botVersion": { + "major": 1, + "minor": 0 + }, + "product": "Trading-Simulation", + "dataSetVersion": "dataSet.V1", + "dataSet": "Multi-Period-Daily" + } + ] + } + ], + "products": [ + { + "codeName": "Live Trading History", + "displayName": "Live Trading History", + "shortDisplayName": "Live", + "description": "General information about trading history.", + "dataSets": [ + { + "codeName": "Oficial History", + "type": "File Sequence", + "validPeriods": [ + "24-hs", + "12-hs", + "08-hs", + "06-hs", + "04-hs", + "03-hs", + "02-hs", + "01-hs", + "45-min", + "40-min", + "30-min", + "20-min", + "15-min", + "10-min", + "05-min", + "04-min", + "03-min", + "02-min", + "01-min" + ], + "filePath": "@DevTeam/@Bot.1.0/AACloud.1.1/Poloniex/dataSet.V1/Output/Trading-Process", + "fileName": "Execution.History.Live.@Sequence.json" + } + ], + "exchangeList": [ + { + "name": "Poloniex" + } + ], + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersTradingSimulation", + "moduleName": "SimulationExecution" + } + }, + { + "codeName": "Backtest Trading History", + "displayName": "Backtest Trading History", + "shortDisplayName": "Backtest", + "description": "General information about backtest trading history.", + "dataSets": [ + { + "codeName": "Backtest History", + "type": "File Sequence", + "validPeriods": [ + "24-hs", + "12-hs", + "08-hs", + "06-hs", + "04-hs", + "03-hs", + "02-hs", + "01-hs", + "45-min", + "40-min", + "30-min", + "20-min", + "15-min", + "10-min", + "05-min", + "04-min", + "03-min", + "02-min", + "01-min" + ], + "filePath": "@DevTeam/@Bot.1.0/AACloud.1.1/Poloniex/dataSet.V1/Output/Trading-Process", + "fileName": "Execution.History.Backtest.@Sequence.json" + } + ], + "exchangeList": [ + { + "name": "Poloniex" + } + ], + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersTradingSimulation", + "moduleName": "SimulationExecution" + } + }, + { + "codeName": "Competition Trading History", + "displayName": "Competition Trading History", + "shortDisplayName": "Competition", + "description": "General information about competition trading history.", + "dataSets": [ + { + "codeName": "Competition History", + "type": "File Sequence", + "validPeriods": [ + "24-hs", + "12-hs", + "08-hs", + "06-hs", + "04-hs", + "03-hs", + "02-hs", + "01-hs", + "45-min", + "40-min", + "30-min", + "20-min", + "15-min", + "10-min", + "05-min", + "04-min", + "03-min", + "02-min", + "01-min" + ], + "filePath": "@DevTeam/@Bot.1.0/AACloud.1.1/Poloniex/dataSet.V1/Output/Trading-Process", + "fileName": "Execution.History.Competition.@Sequence.json" + } + ], + "exchangeList": [ + { + "name": "Poloniex" + } + ], + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersTradingSimulation", + "moduleName": "SimulationExecution" + } + } + ], + "marketRateProvider": { + "devTeam": "AAMasters", + "bot": "AABruce", + "host": { + "url": "https://demo-dev.superalgos.org/", + "storage": "localStorage", + "container": "aamasters", + "accessKey": "" + }, + "botVersion": { + "major": 1, + "minor": 0 + }, + "product": "Candles", + "dataSet": "One-Min", + "dataSetVersion": "dataSet.V1" + } +} diff --git a/src/utils/readFile.js b/src/utils/readFile.js new file mode 100644 index 000000000..1be0d4609 --- /dev/null +++ b/src/utils/readFile.js @@ -0,0 +1,14 @@ +import fs from 'fs' + +export default function readFile(fileName) { + return new Promise(function (resolve, reject) { + fs.readFile(fileName, 'utf8', (err, file) => { + if (err) { + reject(err) + } else { + resolve(file) + } + }) + + }) +} diff --git a/test/minioTest.js b/test/minioTest.js new file mode 100644 index 000000000..e7d1bd7b0 --- /dev/null +++ b/test/minioTest.js @@ -0,0 +1,46 @@ +import { assert } from 'chai' +import { + getFileContent, + createContainer, + deleteContainer, + writeFileContent +} from '../src/storage/providers/MinioStorage' + +import 'dotenv/config' + +describe('Minio Storage Tests', function () { + it('Create Container', async function () { + let containerName = 'test-container' + await createContainer(containerName) + assert.isOk('everything', 'everything is ok') + }) + it('Save File', async function () { + let containerName = 'test-container' + let blobName = 'test-folder/test_data_file.json' + let content = '[0,0,0,0]' + await writeFileContent(containerName, blobName, content) + + // for (let index = 0; index < 1000; index++) { + // let blobName = 'test-folder/test_data_file_' + index + '.json' + // await writeFileContent(containerName, blobName, content) + // } + + assert.isOk('everything', 'everything is ok') + }) + it('Retrieve File', async function () { + let containerName = 'test-container' + let blobName = 'test-folder/test_data_file_1.json' + let fileContent = await getFileContent(containerName, blobName) + + let expected = '[0,0,0,0]' + assert.equal(fileContent, expected) + }) + it('Delete Container', async function () { + let containerName = 'test-container' + console.log('start: '+ new Date().toISOString()) + await deleteContainer(containerName) + console.log('end: '+ new Date().toISOString()) + + assert.isOk('everything', 'everything is ok') + }) +})