From 3dcd270bdd8f3ea3c28c32f2a2a6cbf0b171df21 Mon Sep 17 00:00:00 2001 From: benme <34651529+matbenitez@users.noreply.github.com> Date: Wed, 22 May 2019 20:12:37 -0300 Subject: [PATCH 01/34] Initial commit --- README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 000000000..92be9c77b --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# saweb-api \ No newline at end of file From 23d2094234c28edb79d353bb1c9b35cecf0ac70c Mon Sep 17 00:00:00 2001 From: Matias Date: Wed, 22 May 2019 20:22:54 -0300 Subject: [PATCH 02/34] Initial version --- .dockerignore | 14 + .env.local | 6 + .gitignore | 23 + CODE_OF_CONDUCT.md | 46 + CONTRIBUTING.md | 9 + Dockerfile | 19 + Dockerfile.prod | 21 + LICENSE | 201 ++++ docker-compose.prod.yml | 24 + package-lock.json | 1850 +++++++++++++++++++++++++++++++ package.json | 29 + src/app.js | 53 + src/db.js | 10 + src/errors/index.js | 31 + src/errors/notAllowed.json | 8 + src/index.js | 2 + src/models/ecosystem.js | 101 ++ src/models/index.js | 5 + src/schema/index.js | 12 + src/schema/mutations/AddTeam.js | 68 ++ src/schema/mutations/index.js | 14 + src/schema/queries/Teams.js | 21 + src/schema/queries/index.js | 14 + src/schema/types/Bot.js | 14 + src/schema/types/Ecosystem.js | 30 + src/schema/types/EventHost.js | 31 + src/schema/types/Host.js | 15 + src/schema/types/Team.js | 35 + src/schema/types/index.js | 11 + src/schema/types/input/Bot.js | 15 + src/schema/types/input/Host.js | 16 + src/schema/types/input/Team.js | 22 + src/schema/types/input/index.js | 9 + src/storage/AzureStorage.js | 87 ++ src/utils/logger.js | 41 + 35 files changed, 2907 insertions(+) create mode 100644 .dockerignore create mode 100644 .env.local create mode 100644 .gitignore create mode 100644 CODE_OF_CONDUCT.md create mode 100644 CONTRIBUTING.md create mode 100644 Dockerfile create mode 100644 Dockerfile.prod create mode 100644 LICENSE create mode 100644 docker-compose.prod.yml create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/app.js create mode 100644 src/db.js create mode 100644 src/errors/index.js create mode 100644 src/errors/notAllowed.json create mode 100644 src/index.js create mode 100644 src/models/ecosystem.js create mode 100644 src/models/index.js create mode 100644 src/schema/index.js create mode 100644 src/schema/mutations/AddTeam.js create mode 100644 src/schema/mutations/index.js create mode 100644 src/schema/queries/Teams.js create mode 100644 src/schema/queries/index.js create mode 100644 src/schema/types/Bot.js create mode 100644 src/schema/types/Ecosystem.js create mode 100644 src/schema/types/EventHost.js create mode 100644 src/schema/types/Host.js create mode 100644 src/schema/types/Team.js create mode 100644 src/schema/types/index.js create mode 100644 src/schema/types/input/Bot.js create mode 100644 src/schema/types/input/Host.js create mode 100644 src/schema/types/input/Team.js create mode 100644 src/schema/types/input/index.js create mode 100644 src/storage/AzureStorage.js create mode 100644 src/utils/logger.js diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..fe24f1c90 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,14 @@ +# Ignore files to be uploaded to the image +.env +.env.dev +.env.prod +.git +.gitignore +Dockerfile +Dockerfile.prod +README.md +CODE_OF_CONDUCT.md +CONTRIBUTING.md + +/coverage +/.nyc_output diff --git a/.env.local b/.env.local new file mode 100644 index 000000000..ab3193771 --- /dev/null +++ b/.env.local @@ -0,0 +1,6 @@ +NODE_ENV=production +PORT=4011 + +MONGO_URL=mongodb://mongo/saweb + +PRESHARED_GATEWAY_KEY=saweb_key diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..d67bde39e --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +# Logs +logs +*.log +npm-debug.log* + +# Dependency directories +/node_modules + +# Optional npm cache directory +.npm + +# dotenv environment variables file +.env + +# Build folder js files +build/ +dist/ + +# Visual Studio Code +.vscode + +#Mac specific files +.DS_Store diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 000000000..4b37b0e16 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,46 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team using the online form in the contacts page of the superalgos.org website. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 000000000..1d1736260 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,9 @@ +# Contributing + +We are thrilled you are interested in contributing to this repository. Your help may be essential to the project's success and we want you in! + +Superalgos is a collective project, currently at a very early stage. We not only welcome contributions but may even choose to incentivize relevant contributions via the project's native crypto token. + +Please read the [Join the Dev Team]( https://superalgos.org/developers-join-dev-team.shtml) page in our website to learn more about how you may become a relevant member of the Community and even make a living out of contributing code to the project. + +Contributions are deemed _voluntary work_, irrespective of the incentives that may be in place, and are subject to the conditions described in the LICENSE \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..f034cb7a6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM mhart/alpine-node:12 + +# Directory where the service will be installed +ARG API_DIR=/usr/src/saweb-api + +# Prepare required directories +RUN mkdir -p ${API_DIR} + +# Use app directory as current workdir +WORKDIR ${API_DIR} + +# Copy package.json to workdir +COPY . ${API_DIR}/ + +# Install dependencies +RUN npm install --production + +# Expose Port +EXPOSE $PORT diff --git a/Dockerfile.prod b/Dockerfile.prod new file mode 100644 index 000000000..1b6cc3770 --- /dev/null +++ b/Dockerfile.prod @@ -0,0 +1,21 @@ +FROM mhart/alpine-node:8 + +ENV PORT=4004 + +RUN mkdir -p /var/www/app && \ + mkdir -p /var/www/app/node_modules + +# Use app directory as current workdir +WORKDIR /var/www/app +ENV PATH /var/www/app/node_modules/.bin:$PATH +# Copy package.json to workdir +COPY package.json /var/www/app/package.json + +# Install dependencies +RUN npm install + +# Copy application code +COPY . /var/www/app + +# Expose Port +EXPOSE $PORT diff --git a/LICENSE b/LICENSE new file mode 100644 index 000000000..261eeb9e9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml new file mode 100644 index 000000000..9fd6b338c --- /dev/null +++ b/docker-compose.prod.yml @@ -0,0 +1,24 @@ +version: '3' +services: + node: + image: saweb-api:v1 + build: + context: . + dockerfile: ./Dockerfile.prod + env_file: ./.env + container_name: saweb-api + tty: true + stdin_open: true + volumes: + - ./:/var/www/app + - /var/www/app/node_modules + working_dir: /var/www/app + restart: always + command: npm start + expose: + - 4004 + network_mode: webproxy +networks: + default: + external: + name: webproxy diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 000000000..d0d1e0fde --- /dev/null +++ b/package-lock.json @@ -0,0 +1,1850 @@ +{ + "name": "saweb-api", + "version": "0.1.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@apollographql/apollo-tools": { + "version": "0.3.7", + "resolved": "https://registry.npmjs.org/@apollographql/apollo-tools/-/apollo-tools-0.3.7.tgz", + "integrity": "sha512-+ertvzAwzkYmuUtT8zH3Zi6jPdyxZwOgnYaZHY7iLnMVJDhQKWlkyjLMF8wyzlPiEdDImVUMm5lOIBZo7LkGlg==", + "requires": { + "apollo-env": "0.5.1" + } + }, + "@apollographql/apollo-upload-server": { + "version": "5.0.3", + "resolved": "https://registry.npmjs.org/@apollographql/apollo-upload-server/-/apollo-upload-server-5.0.3.tgz", + "integrity": "sha512-tGAp3ULNyoA8b5o9LsU2Lq6SwgVPUOKAqKywu2liEtTvrFSGPrObwanhYwArq3GPeOqp2bi+JknSJCIU3oQN1Q==", + "requires": { + "@babel/runtime-corejs2": "^7.0.0-rc.1", + "busboy": "^0.2.14", + "object-path": "^0.11.4" + } + }, + "@apollographql/graphql-playground-html": { + "version": "1.6.18", + "resolved": "https://registry.npmjs.org/@apollographql/graphql-playground-html/-/graphql-playground-html-1.6.18.tgz", + "integrity": "sha512-7HGC1hQ0Aj93z2kv8h4RBtYClkiccj+F2Juh1m74Pky7D9tJDsQ4eBlaNNn0jMxGAL6EuaGwBN46EA9JtBMRog==" + }, + "@azure/ms-rest-js": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/@azure/ms-rest-js/-/ms-rest-js-1.2.3.tgz", + "integrity": "sha512-eROQ034b+9v0Hd3wETKi/EwF5pqS3VRAk1Lm8iKVPOP8v30f6Zfzsi420MRfBMsbNCx/mE2N0L65Px7tvcGfVg==", + "requires": { + "axios": "^0.18.0", + "form-data": "^2.3.2", + "tough-cookie": "^2.4.3", + "tslib": "^1.9.2", + "uuid": "^3.2.1", + "xml2js": "^0.4.19" + } + }, + "@azure/storage-blob": { + "version": "10.3.0", + "resolved": "https://registry.npmjs.org/@azure/storage-blob/-/storage-blob-10.3.0.tgz", + "integrity": "sha512-KZbJ3q8RpAdeIB5Em1lgXkiq7Mll9bSHHbHavOFMepkkF7HQa3Sez9FdkAVIkVVWK5YoBlshBGZ+mtiSQiS9Fw==", + "requires": { + "@azure/ms-rest-js": "1.2.3", + "events": "3.0.0", + "tslib": "^1.9.3" + } + }, + "@babel/runtime-corejs2": { + "version": "7.4.5", + "resolved": "https://registry.npmjs.org/@babel/runtime-corejs2/-/runtime-corejs2-7.4.5.tgz", + "integrity": "sha512-5yLuwzvIDecKwYMzJtiarky4Fb5643H3Ao5jwX0HrMR5oM5mn2iHH9wSZonxwNK0oAjAFUQAiOd4jT7/9Y2jMQ==", + "requires": { + "core-js": "^2.6.5", + "regenerator-runtime": "^0.13.2" + } + }, + "@protobufjs/aspromise": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@protobufjs/aspromise/-/aspromise-1.1.2.tgz", + "integrity": "sha1-m4sMxmPWaafY9vXQiToU00jzD78=" + }, + "@protobufjs/base64": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@protobufjs/base64/-/base64-1.1.2.tgz", + "integrity": "sha512-AZkcAA5vnN/v4PDqKyMR5lx7hZttPDgClv83E//FMNhR2TMcLUhfRUBHCmSl0oi9zMgDDqRUJkSxO3wm85+XLg==" + }, + "@protobufjs/codegen": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/@protobufjs/codegen/-/codegen-2.0.4.tgz", + "integrity": "sha512-YyFaikqM5sH0ziFZCN3xDC7zeGaB/d0IUb9CATugHWbd1FRFwWwt4ld4OYMPWu5a3Xe01mGAULCdqhMlPl29Jg==" + }, + "@protobufjs/eventemitter": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@protobufjs/eventemitter/-/eventemitter-1.1.0.tgz", + "integrity": "sha1-NVy8mLr61ZePntCV85diHx0Ga3A=" + }, + "@protobufjs/fetch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@protobufjs/fetch/-/fetch-1.1.0.tgz", + "integrity": "sha1-upn7WYYUr2VwDBYZ/wbUVLDYTEU=", + "requires": { + "@protobufjs/aspromise": "^1.1.1", + "@protobufjs/inquire": "^1.1.0" + } + }, + "@protobufjs/float": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@protobufjs/float/-/float-1.0.2.tgz", + "integrity": "sha1-Xp4avctz/Ap8uLKR33jIy9l7h9E=" + }, + "@protobufjs/inquire": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@protobufjs/inquire/-/inquire-1.1.0.tgz", + "integrity": "sha1-/yAOPnzyQp4tyvwRQIKOjMY48Ik=" + }, + "@protobufjs/path": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@protobufjs/path/-/path-1.1.2.tgz", + "integrity": "sha1-bMKyDFya1q0NzP0hynZz2Nf79o0=" + }, + "@protobufjs/pool": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@protobufjs/pool/-/pool-1.1.0.tgz", + "integrity": "sha1-Cf0V8tbTq/qbZbw2ZQbWrXhG/1Q=" + }, + "@protobufjs/utf8": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@protobufjs/utf8/-/utf8-1.1.0.tgz", + "integrity": "sha1-p3c2C1s5oaLlEG+OhY8v0tBgxXA=" + }, + "@types/accepts": { + "version": "1.3.5", + "resolved": "https://registry.npmjs.org/@types/accepts/-/accepts-1.3.5.tgz", + "integrity": "sha512-jOdnI/3qTpHABjM5cx1Hc0sKsPoYCp+DP/GJRGtDlPd7fiV9oXGGIcjW/ZOxLIvjGz8MA+uMZI9metHlgqbgwQ==", + "requires": { + "@types/node": "*" + } + }, + "@types/body-parser": { + "version": "1.17.0", + "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.17.0.tgz", + "integrity": "sha512-a2+YeUjPkztKJu5aIF2yArYFQQp8d51wZ7DavSHjFuY1mqVgidGyzEQ41JIVNy82fXj8yPgy2vJmfIywgESW6w==", + "requires": { + "@types/connect": "*", + "@types/node": "*" + } + }, + "@types/connect": { + "version": "3.4.32", + "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.32.tgz", + "integrity": "sha512-4r8qa0quOvh7lGD0pre62CAb1oni1OO6ecJLGCezTmhQ8Fz50Arx9RUszryR8KlgK6avuSXvviL6yWyViQABOg==", + "requires": { + "@types/node": "*" + } + }, + "@types/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-GmK8AKu8i+s+EChK/uZ5IbrXPcPaQKWaNSGevDT/7o3gFObwSUQwqb1jMqxuo+YPvj0ckGzINI+EO7EHcmJjKg==", + "requires": { + "@types/express": "*" + } + }, + "@types/events": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@types/events/-/events-3.0.0.tgz", + "integrity": "sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g==" + }, + "@types/express": { + "version": "4.16.0", + "resolved": "https://registry.npmjs.org/@types/express/-/express-4.16.0.tgz", + "integrity": "sha512-TtPEYumsmSTtTetAPXlJVf3kEqb6wZK0bZojpJQrnD/djV4q1oB6QQ8aKvKqwNPACoe02GNiy5zDzcYivR5Z2w==", + "requires": { + "@types/body-parser": "*", + "@types/express-serve-static-core": "*", + "@types/serve-static": "*" + } + }, + "@types/express-serve-static-core": { + "version": "4.16.5", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.16.5.tgz", + "integrity": "sha512-T8oej2B9TVALoCK4epWXRNWaFaqDyOaEQ3ntph+tSw5QoKnZfxgCXefIm3+nGp2XCRrkX4X7U5CSTMnwKJcOjQ==", + "requires": { + "@types/node": "*", + "@types/range-parser": "*" + } + }, + "@types/long": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/@types/long/-/long-4.0.0.tgz", + "integrity": "sha512-1w52Nyx4Gq47uuu0EVcsHBxZFJgurQ+rTKS3qMHxR1GY2T8c2AJYd6vZoZ9q1rupaDjU0yT+Jc2XTyXkjeMA+Q==" + }, + "@types/mime": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/@types/mime/-/mime-2.0.1.tgz", + "integrity": "sha512-FwI9gX75FgVBJ7ywgnq/P7tw+/o1GUbtP0KzbtusLigAOgIgNISRK0ZPl4qertvXSIE8YbsVJueQ90cDt9YYyw==" + }, + "@types/node": { + "version": "12.0.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-12.0.2.tgz", + "integrity": "sha512-5tabW/i+9mhrfEOUcLDu2xBPsHJ+X5Orqy9FKpale3SjDA17j5AEpYq5vfy3oAeAHGcvANRCO3NV3d2D6q3NiA==" + }, + "@types/range-parser": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.3.tgz", + "integrity": "sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA==" + }, + "@types/serve-static": { + "version": "1.13.2", + "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.2.tgz", + "integrity": "sha512-/BZ4QRLpH/bNYgZgwhKEh+5AsboDBcUdlBYgzoLX0fpj3Y2gp6EApyOlM3bK53wQS/OE1SrdSYBAbux2D1528Q==", + "requires": { + "@types/express-serve-static-core": "*", + "@types/mime": "*" + } + }, + "@types/ws": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@types/ws/-/ws-6.0.1.tgz", + "integrity": "sha512-EzH8k1gyZ4xih/MaZTXwT2xOkPiIMSrhQ9b8wrlX88L0T02eYsddatQlwVFlEPyEqV0ChpdpNnE51QPH6NVT4Q==", + "requires": { + "@types/events": "*", + "@types/node": "*" + } + }, + "accepts": { + "version": "1.3.7", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", + "integrity": "sha512-Il80Qs2WjYlJIBNzNkK6KYqlVMTbZLXgHx2oT0pU/fjRHyEp+PEfEPY0R3WCwAGVOtauxh1hOxNgIf5bv7dQpA==", + "requires": { + "mime-types": "~2.1.24", + "negotiator": "0.6.2" + } + }, + "apollo-cache-control": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/apollo-cache-control/-/apollo-cache-control-0.6.0.tgz", + "integrity": "sha512-66aCF6MHe0/FdD3knphwTv6CCIdb1ZxrMsiRpxP474qqyYVe2jAwBu6aJBn4emffZHZ7i6gp9dY6cPHThjnbKA==", + "requires": { + "apollo-server-env": "2.3.0", + "graphql-extensions": "0.6.0" + } + }, + "apollo-datasource": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/apollo-datasource/-/apollo-datasource-0.4.0.tgz", + "integrity": "sha512-6QkgnLYwQrW0qv+yXIf617DojJbGmza2XJXUlgnzrGGhxzfAynzEjaLyYkc8rYS1m82vjrl9EOmLHTcnVkvZAQ==", + "requires": { + "apollo-server-caching": "0.4.0", + "apollo-server-env": "2.3.0" + } + }, + "apollo-engine-reporting": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/apollo-engine-reporting/-/apollo-engine-reporting-1.1.0.tgz", + "integrity": "sha512-Dj0BwgcluHL0QVUaquhAoYoLX9Z4DRP/n2REcIwO8d2iy52r+1wN5QqZLx97dEFh7CjhNjTWeysJzc8XMWKa1Q==", + "requires": { + "apollo-engine-reporting-protobuf": "0.3.0", + "apollo-graphql": "^0.2.1-alpha.1", + "apollo-server-core": "2.5.0", + "apollo-server-env": "2.3.0", + "async-retry": "^1.2.1", + "graphql-extensions": "0.6.0" + } + }, + "apollo-engine-reporting-protobuf": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/apollo-engine-reporting-protobuf/-/apollo-engine-reporting-protobuf-0.3.0.tgz", + "integrity": "sha512-PYowpx/E+TJT/8nKpp3JmJuKh3x1SZcxDF6Cquj0soV205TUpFFCZQMi91i5ACiEp2AkYvM/GDBIrw+rfIwzTg==", + "requires": { + "protobufjs": "^6.8.6" + } + }, + "apollo-env": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/apollo-env/-/apollo-env-0.5.1.tgz", + "integrity": "sha512-fndST2xojgSdH02k5hxk1cbqA9Ti8RX4YzzBoAB4oIe1Puhq7+YlhXGXfXB5Y4XN0al8dLg+5nAkyjNAR2qZTw==", + "requires": { + "core-js": "^3.0.1", + "node-fetch": "^2.2.0", + "sha.js": "^2.4.11" + }, + "dependencies": { + "core-js": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/core-js/-/core-js-3.1.2.tgz", + "integrity": "sha512-3poRGjbu56leCtZCZCzCgQ7GcKOflDFnjWIepaPFUsM0IXUBrne10sl3aa2Bkcz3+FjRdIxBe9dAMhIJmEnQNA==" + } + } + }, + "apollo-graphql": { + "version": "0.2.1-register.1", + "resolved": "https://registry.npmjs.org/apollo-graphql/-/apollo-graphql-0.2.1-register.1.tgz", + "integrity": "sha512-Z2LOuvYomC9CN9K+mpFVcVQu6Ml5PIJlV+YOkGzFq73xeqWg1InxHqI3eEdCEhxTJq6H8rlWT8ATrMS+4sIhqw==", + "requires": { + "apollo-env": "0.4.1-register.1", + "lodash.sortby": "^4.7.0" + }, + "dependencies": { + "apollo-env": { + "version": "0.4.1-register.1", + "resolved": "https://registry.npmjs.org/apollo-env/-/apollo-env-0.4.1-register.1.tgz", + "integrity": "sha512-fg1US7YZ6yW1N0tFq8g4HpCR3eJZmI+rIiHDiknYN9D1MTjvwYdmXYhi7VaPvQ21hV5nMRvfBUMqYXjP+6FsGQ==", + "requires": { + "core-js": "3.0.0-beta.13", + "node-fetch": "^2.2.0", + "sha.js": "^2.4.11" + } + }, + "core-js": { + "version": "3.0.0-beta.13", + "resolved": "https://registry.npmjs.org/core-js/-/core-js-3.0.0-beta.13.tgz", + "integrity": "sha512-16Q43c/3LT9NyePUJKL8nRIQgYWjcBhjJSMWg96PVSxoS0PeE0NHitPI3opBrs9MGGHjte1KoEVr9W63YKlTXQ==" + } + } + }, + "apollo-link": { + "version": "1.2.11", + "resolved": "https://registry.npmjs.org/apollo-link/-/apollo-link-1.2.11.tgz", + "integrity": "sha512-PQvRCg13VduLy3X/0L79M6uOpTh5iHdxnxYuo8yL7sJlWybKRJwsv4IcRBJpMFbChOOaHY7Og9wgPo6DLKDKDA==", + "requires": { + "apollo-utilities": "^1.2.1", + "ts-invariant": "^0.3.2", + "tslib": "^1.9.3", + "zen-observable-ts": "^0.8.18" + } + }, + "apollo-server-caching": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/apollo-server-caching/-/apollo-server-caching-0.4.0.tgz", + "integrity": "sha512-GTOZdbLhrSOKYNWMYgaqX5cVNSMT0bGUTZKV8/tYlyYmsB6ey7l6iId3Q7UpHS6F6OR2lstz5XaKZ+T3fDfPzQ==", + "requires": { + "lru-cache": "^5.0.0" + } + }, + "apollo-server-core": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/apollo-server-core/-/apollo-server-core-2.5.0.tgz", + "integrity": "sha512-7hyQ/Rt0hC38bUfxMQmLNHDBIGEBykFWo9EO0W+3o/cno/SqBKd1KKichrABVv+v+SCvZAUutX6gYS5l3G+ULQ==", + "requires": { + "@apollographql/apollo-tools": "^0.3.6-alpha.1", + "@apollographql/graphql-playground-html": "^1.6.6", + "@types/ws": "^6.0.0", + "apollo-cache-control": "0.6.0", + "apollo-datasource": "0.4.0", + "apollo-engine-reporting": "1.1.0", + "apollo-server-caching": "0.4.0", + "apollo-server-env": "2.3.0", + "apollo-server-errors": "2.3.0", + "apollo-server-plugin-base": "0.4.0", + "apollo-tracing": "0.6.0", + "fast-json-stable-stringify": "^2.0.0", + "graphql-extensions": "0.6.0", + "graphql-subscriptions": "^1.0.0", + "graphql-tag": "^2.9.2", + "graphql-tools": "^4.0.0", + "graphql-upload": "^8.0.2", + "sha.js": "^2.4.11", + "subscriptions-transport-ws": "^0.9.11", + "ws": "^6.0.0" + }, + "dependencies": { + "graphql-subscriptions": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/graphql-subscriptions/-/graphql-subscriptions-1.1.0.tgz", + "integrity": "sha512-6WzlBFC0lWmXJbIVE8OgFgXIP4RJi3OQgTPa0DVMsDXdpRDjTsM1K9wfl5HSYX7R87QAGlvcv2Y4BIZa/ItonA==", + "requires": { + "iterall": "^1.2.1" + } + }, + "graphql-tools": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/graphql-tools/-/graphql-tools-4.0.4.tgz", + "integrity": "sha512-chF12etTIGVVGy3fCTJ1ivJX2KB7OSG4c6UOJQuqOHCmBQwTyNgCDuejZKvpYxNZiEx7bwIjrodDgDe9RIkjlw==", + "requires": { + "apollo-link": "^1.2.3", + "apollo-utilities": "^1.0.1", + "deprecated-decorator": "^0.1.6", + "iterall": "^1.1.3", + "uuid": "^3.1.0" + } + } + } + }, + "apollo-server-env": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/apollo-server-env/-/apollo-server-env-2.3.0.tgz", + "integrity": "sha512-WIwlkCM/gir0CkoYWPMTCH8uGCCKB/aM074U1bKayvkFOBVO2VgG5x2kgsfkyF05IMQq2/GOTsKhNY7RnUEhTA==", + "requires": { + "node-fetch": "^2.1.2", + "util.promisify": "^1.0.0" + } + }, + "apollo-server-errors": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/apollo-server-errors/-/apollo-server-errors-2.3.0.tgz", + "integrity": "sha512-rUvzwMo2ZQgzzPh2kcJyfbRSfVKRMhfIlhY7BzUfM4x6ZT0aijlgsf714Ll3Mbf5Fxii32kD0A/DmKsTecpccw==" + }, + "apollo-server-express": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/apollo-server-express/-/apollo-server-express-2.1.0.tgz", + "integrity": "sha512-jLFIz1VLduMA/rme4OAy3IPeoaMEZOPoQXpio8AhfjIqCijRPPfoWJ2QMqz56C/g3vas7rZtgcVOrHpjBKudjw==", + "requires": { + "@apollographql/apollo-upload-server": "^5.0.3", + "@apollographql/graphql-playground-html": "^1.6.0", + "@types/accepts": "^1.3.5", + "@types/body-parser": "1.17.0", + "@types/cors": "^2.8.4", + "@types/express": "4.16.0", + "accepts": "^1.3.5", + "apollo-server-core": "^2.1.0", + "body-parser": "^1.18.3", + "cors": "^2.8.4", + "graphql-subscriptions": "^0.5.8", + "graphql-tools": "^3.0.4", + "type-is": "^1.6.16" + } + }, + "apollo-server-plugin-base": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/apollo-server-plugin-base/-/apollo-server-plugin-base-0.4.0.tgz", + "integrity": "sha512-iD7ARNtwnvHGd1EMPK0CuodM8d8hgDvFwTfIDzJY04QIQ6/KrBFaWhnCXJsy+HMb47GovwBbq67IK6eb2WJgBg==" + }, + "apollo-tracing": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/apollo-tracing/-/apollo-tracing-0.6.0.tgz", + "integrity": "sha512-OpYPHVBgcQ/HT2WLXJQWwhilzR1rrl01tZeMU2N7yinsp/oyKngF5aUSMtuvX1k/T3abilQo+w10oAQlBCGdPA==", + "requires": { + "apollo-server-env": "2.3.0", + "graphql-extensions": "0.6.0" + } + }, + "apollo-utilities": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/apollo-utilities/-/apollo-utilities-1.3.0.tgz", + "integrity": "sha512-wQjV+FdWcTWmWUFlChG5rS0vHKy5OsXC6XlV9STRstQq6VbXANwHy6DHnTEQAfLXWAbNcPgBu+nBUpR3dFhwrA==", + "requires": { + "fast-json-stable-stringify": "^2.0.0", + "ts-invariant": "^0.4.0", + "tslib": "^1.9.3" + }, + "dependencies": { + "ts-invariant": { + "version": "0.4.2", + "resolved": "https://registry.npmjs.org/ts-invariant/-/ts-invariant-0.4.2.tgz", + "integrity": "sha512-PTAAn8lJPEdRBJJEs4ig6MVZWfO12yrFzV7YaPslmyhG7+4MA279y4BXT3f72gXeVl0mC1aAWq2rMX4eKTWU/Q==", + "requires": { + "tslib": "^1.9.3" + } + } + } + }, + "array-flatten": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", + "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" + }, + "async": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/async/-/async-2.6.1.tgz", + "integrity": "sha512-fNEiL2+AZt6AlAw/29Cr0UDe4sRAHCpEHh54WMz+Bb7QfNcFw4h3loofyJpLeQs4Yx7yuqu/2dLgM5hKOs6HlQ==", + "requires": { + "lodash": "^4.17.10" + } + }, + "async-limiter": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz", + "integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==" + }, + "async-retry": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/async-retry/-/async-retry-1.2.3.tgz", + "integrity": "sha512-tfDb02Th6CE6pJUF2gjW5ZVjsgwlucVXOEQMvEX9JgSJMs9gAX+Nz3xRuJBKuUYjTSYORqvDBORdAQ3LU59g7Q==", + "requires": { + "retry": "0.12.0" + } + }, + "asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" + }, + "axios": { + "version": "0.18.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.18.0.tgz", + "integrity": "sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=", + "requires": { + "follow-redirects": "^1.3.0", + "is-buffer": "^1.1.5" + } + }, + "backo2": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz", + "integrity": "sha1-MasayLEpNjRj41s+u2n038+6eUc=" + }, + "bluebird": { + "version": "3.5.1", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz", + "integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA==" + }, + "body-parser": { + "version": "1.19.0", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz", + "integrity": "sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==", + "requires": { + "bytes": "3.1.0", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "~1.1.2", + "http-errors": "1.7.2", + "iconv-lite": "0.4.24", + "on-finished": "~2.3.0", + "qs": "6.7.0", + "raw-body": "2.4.0", + "type-is": "~1.6.17" + } + }, + "bson": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/bson/-/bson-1.0.9.tgz", + "integrity": "sha512-IQX9/h7WdMBIW/q/++tGd+emQr0XMdeZ6icnT/74Xk9fnabWn+gZgpE+9V+gujL3hhJOoNrnDVY7tWdzc7NUTg==" + }, + "busboy": { + "version": "0.2.14", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-0.2.14.tgz", + "integrity": "sha1-bCpiLvz0fFe7vh4qnDetNseSVFM=", + "requires": { + "dicer": "0.2.5", + "readable-stream": "1.1.x" + } + }, + "bytes": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", + "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==" + }, + "color": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/color/-/color-3.0.0.tgz", + "integrity": "sha512-jCpd5+s0s0t7p3pHQKpnJ0TpQKKdleP71LWcA0aqiljpiuAkOSUFN/dyH8ZwF0hRmFlrIuRhufds1QyEP9EB+w==", + "requires": { + "color-convert": "^1.9.1", + "color-string": "^1.5.2" + } + }, + "color-convert": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", + "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", + "requires": { + "color-name": "1.1.3" + } + }, + "color-name": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", + "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" + }, + "color-string": { + "version": "1.5.3", + "resolved": "https://registry.npmjs.org/color-string/-/color-string-1.5.3.tgz", + "integrity": "sha512-dC2C5qeWoYkxki5UAXapdjqO672AM4vZuPGRQfO8b5HKuKGBbKWpITyDYN7TOFKvRW7kOgAn3746clDBMDJyQw==", + "requires": { + "color-name": "^1.0.0", + "simple-swizzle": "^0.2.2" + } + }, + "colornames": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/colornames/-/colornames-1.1.1.tgz", + "integrity": "sha1-+IiQMGhcfE/54qVZ9Qd+t2qBb5Y=" + }, + "colors": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/colors/-/colors-1.3.3.tgz", + "integrity": "sha512-mmGt/1pZqYRjMxB1axhTo16/snVZ5krrKkcmMeVKxzECMMXoCgnvTPp10QgHfcbQZw8Dq2jMNG6je4JlWU0gWg==" + }, + "colorspace": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/colorspace/-/colorspace-1.1.2.tgz", + "integrity": "sha512-vt+OoIP2d76xLhjwbBaucYlNSpPsrJWPlBTtwCpQKIu6/CSMutyzX93O/Do0qzpH3YoHEes8YEFXyZ797rEhzQ==", + "requires": { + "color": "3.0.x", + "text-hex": "1.0.x" + } + }, + "combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "requires": { + "delayed-stream": "~1.0.0" + } + }, + "content-disposition": { + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.2.tgz", + "integrity": "sha1-DPaLud318r55YcOoUXjLhdunjLQ=" + }, + "content-type": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz", + "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==" + }, + "cookie": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.3.1.tgz", + "integrity": "sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s=" + }, + "cookie-signature": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", + "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw=" + }, + "core-js": { + "version": "2.6.8", + "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.6.8.tgz", + "integrity": "sha512-RWlREFU74TEkdXzyl1bka66O3kYp8jeTXrvJZDzVVMH8AiHUSOFpL1yfhQJ+wHocAm1m+4971W1PPzfLuCv1vg==" + }, + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" + }, + "cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "requires": { + "object-assign": "^4", + "vary": "^1" + } + }, + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "requires": { + "ms": "2.0.0" + } + }, + "define-properties": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz", + "integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==", + "requires": { + "object-keys": "^1.0.12" + } + }, + "delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" + }, + "depd": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", + "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=" + }, + "deprecated-decorator": { + "version": "0.1.6", + "resolved": "https://registry.npmjs.org/deprecated-decorator/-/deprecated-decorator-0.1.6.tgz", + "integrity": "sha1-AJZjF7ehL+kvPMgx91g68ym4bDc=" + }, + "destroy": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", + "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" + }, + "diagnostics": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/diagnostics/-/diagnostics-1.1.1.tgz", + "integrity": "sha512-8wn1PmdunLJ9Tqbx+Fx/ZEuHfJf4NKSN2ZBj7SJC/OWRWha843+WsTjqMe1B5E3p28jqBlp+mJ2fPVxPyNgYKQ==", + "requires": { + "colorspace": "1.1.x", + "enabled": "1.0.x", + "kuler": "1.0.x" + } + }, + "dicer": { + "version": "0.2.5", + "resolved": "https://registry.npmjs.org/dicer/-/dicer-0.2.5.tgz", + "integrity": "sha1-WZbAhrszIYyBLAkL3cCc0S+stw8=", + "requires": { + "readable-stream": "1.1.x", + "streamsearch": "0.1.2" + } + }, + "dotenv": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-6.0.0.tgz", + "integrity": "sha512-FlWbnhgjtwD+uNLUGHbMykMOYQaTivdHEmYwAKFjn6GKe/CqY0fNae93ZHTd20snh9ZLr8mTzIL9m0APQ1pjQg==" + }, + "ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" + }, + "enabled": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/enabled/-/enabled-1.0.2.tgz", + "integrity": "sha1-ll9lE9LC0cX0ZStkouM5ZGf8L5M=", + "requires": { + "env-variable": "0.0.x" + } + }, + "encodeurl": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", + "integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=" + }, + "env-variable": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/env-variable/-/env-variable-0.0.5.tgz", + "integrity": "sha512-zoB603vQReOFvTg5xMl9I1P2PnHsHQQKTEowsKKD7nseUfJq6UWzK+4YtlWUO1nhiQUxe6XMkk+JleSZD1NZFA==" + }, + "es-abstract": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.13.0.tgz", + "integrity": "sha512-vDZfg/ykNxQVwup/8E1BZhVzFfBxs9NqMzGcvIJrqg5k2/5Za2bWo40dK2J1pgLngZ7c+Shh8lwYtLGyrwPutg==", + "requires": { + "es-to-primitive": "^1.2.0", + "function-bind": "^1.1.1", + "has": "^1.0.3", + "is-callable": "^1.1.4", + "is-regex": "^1.0.4", + "object-keys": "^1.0.12" + } + }, + "es-to-primitive": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.0.tgz", + "integrity": "sha512-qZryBOJjV//LaxLTV6UC//WewneB3LcXOL9NP++ozKVXsIIIpm/2c13UDiD9Jp2eThsecw9m3jPqDwTyobcdbg==", + "requires": { + "is-callable": "^1.1.4", + "is-date-object": "^1.0.1", + "is-symbol": "^1.0.2" + } + }, + "escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=" + }, + "esm": { + "version": "3.2.25", + "resolved": "https://registry.npmjs.org/esm/-/esm-3.2.25.tgz", + "integrity": "sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==" + }, + "etag": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", + "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=" + }, + "eventemitter3": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-3.1.2.tgz", + "integrity": "sha512-tvtQIeLVHjDkJYnzf2dgVMxfuSGJeM/7UCG17TT4EumTfNtF+0nebF/4zWOIkCreAbtNqhGEboB6BWrwqNaw4Q==" + }, + "events": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/events/-/events-3.0.0.tgz", + "integrity": "sha512-Dc381HFWJzEOhQ+d8pkNon++bk9h6cdAoAj4iE6Q4y6xgTzySWXlKn05/TVNpjnfRqi/X0EpJEJohPjNI3zpVA==" + }, + "express": { + "version": "4.16.3", + "resolved": "https://registry.npmjs.org/express/-/express-4.16.3.tgz", + "integrity": "sha1-avilAjUNsyRuzEvs9rWjTSL37VM=", + "requires": { + "accepts": "~1.3.5", + "array-flatten": "1.1.1", + "body-parser": "1.18.2", + "content-disposition": "0.5.2", + "content-type": "~1.0.4", + "cookie": "0.3.1", + "cookie-signature": "1.0.6", + "debug": "2.6.9", + "depd": "~1.1.2", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "finalhandler": "1.1.1", + "fresh": "0.5.2", + "merge-descriptors": "1.0.1", + "methods": "~1.1.2", + "on-finished": "~2.3.0", + "parseurl": "~1.3.2", + "path-to-regexp": "0.1.7", + "proxy-addr": "~2.0.3", + "qs": "6.5.1", + "range-parser": "~1.2.0", + "safe-buffer": "5.1.1", + "send": "0.16.2", + "serve-static": "1.13.2", + "setprototypeof": "1.1.0", + "statuses": "~1.4.0", + "type-is": "~1.6.16", + "utils-merge": "1.0.1", + "vary": "~1.1.2" + }, + "dependencies": { + "body-parser": { + "version": "1.18.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.18.2.tgz", + "integrity": "sha1-h2eKGdhLR9hZuDGZvVm84iKxBFQ=", + "requires": { + "bytes": "3.0.0", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "~1.1.1", + "http-errors": "~1.6.2", + "iconv-lite": "0.4.19", + "on-finished": "~2.3.0", + "qs": "6.5.1", + "raw-body": "2.3.2", + "type-is": "~1.6.15" + } + }, + "bytes": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", + "integrity": "sha1-0ygVQE1olpn4Wk6k+odV3ROpYEg=" + }, + "http-errors": { + "version": "1.6.3", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.6.3.tgz", + "integrity": "sha1-i1VoC7S+KDoLW/TqLjhYC+HZMg0=", + "requires": { + "depd": "~1.1.2", + "inherits": "2.0.3", + "setprototypeof": "1.1.0", + "statuses": ">= 1.4.0 < 2" + } + }, + "iconv-lite": { + "version": "0.4.19", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.19.tgz", + "integrity": "sha512-oTZqweIP51xaGPI4uPa56/Pri/480R+mo7SeU+YETByQNhDG55ycFyNLIgta9vXhILrxXDmF7ZGhqZIcuN0gJQ==" + }, + "qs": { + "version": "6.5.1", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.5.1.tgz", + "integrity": "sha512-eRzhrN1WSINYCDCbrz796z37LOe3m5tmW7RQf6oBntukAG1nmovJvhnwHHRMAfeoItc1m2Hk02WER2aQ/iqs+A==" + }, + "raw-body": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.3.2.tgz", + "integrity": "sha1-vNYMd9Prk83gBQKVw/N5OJvIj4k=", + "requires": { + "bytes": "3.0.0", + "http-errors": "1.6.2", + "iconv-lite": "0.4.19", + "unpipe": "1.0.0" + }, + "dependencies": { + "depd": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.1.tgz", + "integrity": "sha1-V4O04cRZ8G+lyif5kfPQbnoxA1k=" + }, + "http-errors": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.6.2.tgz", + "integrity": "sha1-CgAsyFcHGSp+eUbO7cERVfYOxzY=", + "requires": { + "depd": "1.1.1", + "inherits": "2.0.3", + "setprototypeof": "1.0.3", + "statuses": ">= 1.3.1 < 2" + } + }, + "setprototypeof": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.0.3.tgz", + "integrity": "sha1-ZlZ+NwQ+608E2RvWWMDL77VbjgQ=" + } + } + }, + "safe-buffer": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz", + "integrity": "sha512-kKvNJn6Mm93gAczWVJg7wH+wGYWNrDHdWvpUmHyEsgCtIwwo3bqPtV4tR5tuPaUhTOo/kvhVwd8XwwOllGYkbg==" + }, + "setprototypeof": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.0.tgz", + "integrity": "sha512-BvE/TwpZX4FXExxOxZyRGQQv651MSwmWKZGqvmPcRIjDqWub67kTKuIMx43cZZrS/cBBzwBcNDWoFxt2XEFIpQ==" + }, + "statuses": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.4.0.tgz", + "integrity": "sha512-zhSCtt8v2NDrRlPQpCNtw/heZLtfUDqxBM1udqikb/Hbk52LK4nQSwr10u77iopCW5LsyHpuXS0GnEc48mLeew==" + } + } + }, + "fast-json-stable-stringify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz", + "integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I=" + }, + "fast-safe-stringify": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.0.6.tgz", + "integrity": "sha512-q8BZ89jjc+mz08rSxROs8VsrBBcn1SIw1kq9NjolL509tkABRk9io01RAjSaEv1Xb2uFLt8VtRiZbGp5H8iDtg==" + }, + "fecha": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fecha/-/fecha-2.3.3.tgz", + "integrity": "sha512-lUGBnIamTAwk4znq5BcqsDaxSmZ9nDVJaij6NvRt/Tg4R69gERA+otPKbS86ROw9nxVMw2/mp1fnaiWqbs6Sdg==" + }, + "finalhandler": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.1.tgz", + "integrity": "sha512-Y1GUDo39ez4aHAw7MysnUD5JzYX+WaIj8I57kO3aEPT1fFRL4sr7mjei97FgnwhAyyzRYmQZaTHb2+9uZ1dPtg==", + "requires": { + "debug": "2.6.9", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "on-finished": "~2.3.0", + "parseurl": "~1.3.2", + "statuses": "~1.4.0", + "unpipe": "~1.0.0" + }, + "dependencies": { + "statuses": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.4.0.tgz", + "integrity": "sha512-zhSCtt8v2NDrRlPQpCNtw/heZLtfUDqxBM1udqikb/Hbk52LK4nQSwr10u77iopCW5LsyHpuXS0GnEc48mLeew==" + } + } + }, + "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==", + "requires": { + "debug": "^3.2.6" + }, + "dependencies": { + "debug": { + "version": "3.2.6", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz", + "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==", + "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==" + } + } + }, + "form-data": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.3.3.tgz", + "integrity": "sha512-1lLKB2Mu3aGP1Q/2eCOx0fNbRMe7XdwktwOruhfqqd0rIJWwN4Dh+E3hrPSlDCXnSR7UtZ1N38rVXm+6+MEhJQ==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.6", + "mime-types": "^2.1.12" + } + }, + "forwarded": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", + "integrity": "sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ=" + }, + "fresh": { + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", + "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" + }, + "fs-capacitor": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/fs-capacitor/-/fs-capacitor-2.0.4.tgz", + "integrity": "sha512-8S4f4WsCryNw2mJJchi46YgB6CR5Ze+4L1h8ewl9tEpL4SJ3ZO+c/bS4BWhB8bK+O3TMqhuZarTitd0S0eh2pA==" + }, + "function-bind": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", + "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" + }, + "graphql": { + "version": "0.13.2", + "resolved": "https://registry.npmjs.org/graphql/-/graphql-0.13.2.tgz", + "integrity": "sha512-QZ5BL8ZO/B20VA8APauGBg3GyEgZ19eduvpLWoq5x7gMmWnHoy8rlQWPLmWgFvo1yNgjSEFMesmS4R6pPr7xog==", + "requires": { + "iterall": "^1.2.1" + } + }, + "graphql-extensions": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/graphql-extensions/-/graphql-extensions-0.6.0.tgz", + "integrity": "sha512-SshzmbD68fHXRv2q3St29olMOxHDLQ5e9TOh+Tz2BYxinrfhjFaPNcEefiK/vF295wW827Y58bdO11Xmhf8J+Q==", + "requires": { + "@apollographql/apollo-tools": "^0.3.6-alpha.1" + } + }, + "graphql-subscriptions": { + "version": "0.5.8", + "resolved": "https://registry.npmjs.org/graphql-subscriptions/-/graphql-subscriptions-0.5.8.tgz", + "integrity": "sha512-0CaZnXKBw2pwnIbvmVckby5Ge5e2ecmjofhYCdyeACbCly2j3WXDP/pl+s+Dqd2GQFC7y99NB+53jrt55CKxYQ==", + "requires": { + "iterall": "^1.2.1" + } + }, + "graphql-tag": { + "version": "2.10.1", + "resolved": "https://registry.npmjs.org/graphql-tag/-/graphql-tag-2.10.1.tgz", + "integrity": "sha512-jApXqWBzNXQ8jYa/HLkZJaVw9jgwNqZkywa2zfFn16Iv1Zb7ELNHkJaXHR7Quvd5SIGsy6Ny7SUKATgnu05uEg==" + }, + "graphql-tools": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/graphql-tools/-/graphql-tools-3.1.1.tgz", + "integrity": "sha512-yHvPkweUB0+Q/GWH5wIG60bpt8CTwBklCSzQdEHmRUgAdEQKxw+9B7zB3dG7wB3Ym7M7lfrS4Ej+jtDZfA2UXg==", + "requires": { + "apollo-link": "^1.2.2", + "apollo-utilities": "^1.0.1", + "deprecated-decorator": "^0.1.6", + "iterall": "^1.1.3", + "uuid": "^3.1.0" + } + }, + "graphql-upload": { + "version": "8.0.6", + "resolved": "https://registry.npmjs.org/graphql-upload/-/graphql-upload-8.0.6.tgz", + "integrity": "sha512-cBRALMOvGBm2AD6M61b1QbSmKfCLXvgV+Z3wXT2JS1EQwGWQ1g5/sby4J/cpwAaGnq5P2eGp+N3HQI9cSn9Jfg==", + "requires": { + "busboy": "^0.3.1", + "fs-capacitor": "^2.0.1", + "http-errors": "^1.7.2", + "object-path": "^0.11.4" + }, + "dependencies": { + "busboy": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-0.3.1.tgz", + "integrity": "sha512-y7tTxhGKXcyBxRKAni+awqx8uqaJKrSFSNFSeRG5CsWNdmy2BIK+6VGWEW7TZnIO/533mtMEA4rOevQV815YJw==", + "requires": { + "dicer": "0.3.0" + } + }, + "dicer": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/dicer/-/dicer-0.3.0.tgz", + "integrity": "sha512-MdceRRWqltEG2dZqO769g27N/3PXfcKl04VhYnBlo2YhH7zPi88VebsjTKclaOyiuMaGU72hTfw3VkUitGcVCA==", + "requires": { + "streamsearch": "0.1.2" + } + } + } + }, + "has": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", + "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", + "requires": { + "function-bind": "^1.1.1" + } + }, + "has-symbols": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.0.tgz", + "integrity": "sha1-uhqPGvKg/DllD1yFA2dwQSIGO0Q=" + }, + "http-errors": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", + "integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==", + "requires": { + "depd": "~1.1.2", + "inherits": "2.0.3", + "setprototypeof": "1.1.1", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.0" + } + }, + "iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" + }, + "ipaddr.js": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.0.tgz", + "integrity": "sha512-M4Sjn6N/+O6/IXSJseKqHoFc+5FdGJ22sXqnjTpdZweHK64MzEPAyQZyEU3R/KRv2GLoa7nNtg/C2Ev6m7z+eA==" + }, + "is-arrayish": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.3.2.tgz", + "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==" + }, + "is-callable": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.4.tgz", + "integrity": "sha512-r5p9sxJjYnArLjObpjA4xu5EKI3CuKHkJXMhT7kwbpUyIFD1n5PMAsoPvWnvtZiNz7LjkYDRZhd7FlI0eMijEA==" + }, + "is-date-object": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.1.tgz", + "integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY=" + }, + "is-regex": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz", + "integrity": "sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=", + "requires": { + "has": "^1.0.1" + } + }, + "is-stream": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz", + "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=" + }, + "is-symbol": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.2.tgz", + "integrity": "sha512-HS8bZ9ox60yCJLH9snBpIwv9pYUAkcuLhSA1oero1UB5y9aiQpRA8y2ex945AOtCZL1lJDeIk3G5LthswI46Lw==", + "requires": { + "has-symbols": "^1.0.0" + } + }, + "isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=" + }, + "iterall": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/iterall/-/iterall-1.2.2.tgz", + "integrity": "sha512-yynBb1g+RFUPY64fTrFv7nsjRrENBQJaX2UL+2Szc9REFrSNm1rpSXHGzhmAy7a9uv3vlvgBlXnf9RqmPH1/DA==" + }, + "kareem": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.0.tgz", + "integrity": "sha512-6hHxsp9e6zQU8nXsP+02HGWXwTkOEw6IROhF2ZA28cYbUk4eJ6QbtZvdqZOdD9YPKghG3apk5eOCvs+tLl3lRg==" + }, + "kuler": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/kuler/-/kuler-1.0.1.tgz", + "integrity": "sha512-J9nVUucG1p/skKul6DU3PUZrhs0LPulNaeUOox0IyXDi8S4CztTHs1gQphhuZmzXG7VOQSf6NJfKuzteQLv9gQ==", + "requires": { + "colornames": "^1.1.1" + } + }, + "lodash": { + "version": "4.17.11", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", + "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" + }, + "lodash.get": { + "version": "4.4.2", + "resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz", + "integrity": "sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=" + }, + "lodash.sortby": { + "version": "4.7.0", + "resolved": "https://registry.npmjs.org/lodash.sortby/-/lodash.sortby-4.7.0.tgz", + "integrity": "sha1-7dFMgk4sycHgsKG0K7UhBRakJDg=" + }, + "logform": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/logform/-/logform-2.1.2.tgz", + "integrity": "sha512-+lZh4OpERDBLqjiwDLpAWNQu6KMjnlXH2ByZwCuSqVPJletw0kTWJf5CgSNAUKn1KUkv3m2cUz/LK8zyEy7wzQ==", + "requires": { + "colors": "^1.2.1", + "fast-safe-stringify": "^2.0.4", + "fecha": "^2.3.3", + "ms": "^2.1.1", + "triple-beam": "^1.3.0" + }, + "dependencies": { + "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==" + } + } + }, + "long": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz", + "integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==" + }, + "lru-cache": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", + "integrity": "sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==", + "requires": { + "yallist": "^3.0.2" + } + }, + "media-typer": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" + }, + "memory-pager": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", + "optional": true + }, + "merge-descriptors": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", + "integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E=" + }, + "methods": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", + "integrity": "sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=" + }, + "mime": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/mime/-/mime-1.4.1.tgz", + "integrity": "sha512-KI1+qOZu5DcW6wayYHSzR/tXKCDC5Om4s1z2QJjDULzLcmf3DvzS7oluY4HCTrc+9FiKmWUgeNLg7W3uIQvxtQ==" + }, + "mime-db": { + "version": "1.40.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.40.0.tgz", + "integrity": "sha512-jYdeOMPy9vnxEqFRRo6ZvTZ8d9oPb+k18PKoYNYUe2stVEBPPwsln/qWzdbmaIvnhZ9v2P+CuecK+fpUfsV2mA==" + }, + "mime-types": { + "version": "2.1.24", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.24.tgz", + "integrity": "sha512-WaFHS3MCl5fapm3oLxU4eYDw77IQM2ACcxQ9RIxfaC3ooc6PFuBMGZZsYpvoXS5D5QTWPieo1jjLdAm3TBP3cQ==", + "requires": { + "mime-db": "1.40.0" + } + }, + "mongodb": { + "version": "3.1.6", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.1.6.tgz", + "integrity": "sha512-E5QJuXQoMlT7KyCYqNNMfAkhfQD79AT4F8Xd+6x37OX+8BL17GyXyWvfm6wuyx4wnzCCPoCSLeMeUN2S7dU9yw==", + "requires": { + "mongodb-core": "3.1.5", + "safe-buffer": "^5.1.2" + } + }, + "mongodb-core": { + "version": "3.1.5", + "resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-3.1.5.tgz", + "integrity": "sha512-emT/tM4ZBinqd6RZok+EzDdtN4LjYJIckv71qQVOEFmvXgT5cperZegVmTgox/1cx4XQu6LJ5ZuIwipP/eKdQg==", + "requires": { + "bson": "^1.1.0", + "require_optional": "^1.0.1", + "safe-buffer": "^5.1.2", + "saslprep": "^1.0.0" + }, + "dependencies": { + "bson": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/bson/-/bson-1.1.1.tgz", + "integrity": "sha512-jCGVYLoYMHDkOsbwJZBCqwMHyH4c+wzgI9hG7Z6SZJRXWr+x58pdIbm2i9a/jFGCkRJqRUr8eoI7lDWa0hTkxg==" + } + } + }, + "mongoose": { + "version": "5.3.4", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.3.4.tgz", + "integrity": "sha512-DIUWOyYgZv2zGi/BoFEaFiaCVuDonnzGhW3cnc3JFjBScYn6z24tS2j3VB0dtMoX8FFjxmmMVnlmHPEIbV4PKA==", + "requires": { + "async": "2.6.1", + "bson": "~1.0.5", + "kareem": "2.3.0", + "lodash.get": "4.4.2", + "mongodb": "3.1.6", + "mongodb-core": "3.1.5", + "mongoose-legacy-pluralize": "1.0.2", + "mpath": "0.5.1", + "mquery": "3.2.0", + "ms": "2.0.0", + "regexp-clone": "0.0.1", + "safe-buffer": "5.1.2", + "sliced": "1.0.1" + } + }, + "mongoose-legacy-pluralize": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz", + "integrity": "sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ==" + }, + "mpath": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.5.1.tgz", + "integrity": "sha512-H8OVQ+QEz82sch4wbODFOz+3YQ61FYz/z3eJ5pIdbMEaUzDqA268Wd+Vt4Paw9TJfvDgVKaayC0gBzMIw2jhsg==" + }, + "mquery": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/mquery/-/mquery-3.2.0.tgz", + "integrity": "sha512-qPJcdK/yqcbQiKoemAt62Y0BAc0fTEKo1IThodBD+O5meQRJT/2HSe5QpBNwaa4CjskoGrYWsEyjkqgiE0qjhg==", + "requires": { + "bluebird": "3.5.1", + "debug": "3.1.0", + "regexp-clone": "0.0.1", + "safe-buffer": "5.1.2", + "sliced": "1.0.1" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + } + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + }, + "negotiator": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", + "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" + }, + "node-fetch": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", + "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" + }, + "object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" + }, + "object-keys": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", + "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==" + }, + "object-path": { + "version": "0.11.4", + "resolved": "https://registry.npmjs.org/object-path/-/object-path-0.11.4.tgz", + "integrity": "sha1-NwrnUvvzfePqcKhhwju6iRVpGUk=" + }, + "object.getownpropertydescriptors": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/object.getownpropertydescriptors/-/object.getownpropertydescriptors-2.0.3.tgz", + "integrity": "sha1-h1jIRvW0B62rDyNuCYbxSwUcqhY=", + "requires": { + "define-properties": "^1.1.2", + "es-abstract": "^1.5.1" + } + }, + "on-finished": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", + "integrity": "sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=", + "requires": { + "ee-first": "1.1.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=" + }, + "parseurl": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", + "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==" + }, + "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=" + }, + "process-nextick-args": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.0.tgz", + "integrity": "sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw==" + }, + "protobufjs": { + "version": "6.8.8", + "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-6.8.8.tgz", + "integrity": "sha512-AAmHtD5pXgZfi7GMpllpO3q1Xw1OYldr+dMUlAnffGTAhqkg72WdmSY71uKBF/JuyiKs8psYbtKrhi0ASCD8qw==", + "requires": { + "@protobufjs/aspromise": "^1.1.2", + "@protobufjs/base64": "^1.1.2", + "@protobufjs/codegen": "^2.0.4", + "@protobufjs/eventemitter": "^1.1.0", + "@protobufjs/fetch": "^1.1.0", + "@protobufjs/float": "^1.0.2", + "@protobufjs/inquire": "^1.1.0", + "@protobufjs/path": "^1.1.2", + "@protobufjs/pool": "^1.1.0", + "@protobufjs/utf8": "^1.1.0", + "@types/long": "^4.0.0", + "@types/node": "^10.1.0", + "long": "^4.0.0" + }, + "dependencies": { + "@types/node": { + "version": "10.14.7", + "resolved": "https://registry.npmjs.org/@types/node/-/node-10.14.7.tgz", + "integrity": "sha512-on4MmIDgHXiuJDELPk1NFaKVUxxCFr37tm8E9yN6rAiF5Pzp/9bBfBHkoexqRiY+hk/Z04EJU9kKEb59YqJ82A==" + } + } + }, + "proxy-addr": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.5.tgz", + "integrity": "sha512-t/7RxHXPH6cJtP0pRG6smSr9QJidhB+3kXu0KgXnbGYMgzEnUxRQ4/LDdfOwZEMyIh3/xHb8PX3t+lfL9z+YVQ==", + "requires": { + "forwarded": "~0.1.2", + "ipaddr.js": "1.9.0" + } + }, + "psl": { + "version": "1.1.31", + "resolved": "https://registry.npmjs.org/psl/-/psl-1.1.31.tgz", + "integrity": "sha512-/6pt4+C+T+wZUieKR620OpzN/LlnNKuWjy1iFLQ/UG35JqHlR/89MP1d96dUfkf6Dne3TuLQzOYEYshJ+Hx8mw==" + }, + "punycode": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", + "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" + }, + "qs": { + "version": "6.7.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", + "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==" + }, + "range-parser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", + "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==" + }, + "raw-body": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.4.0.tgz", + "integrity": "sha512-4Oz8DUIwdvoa5qMJelxipzi/iJIi40O5cGV1wNYp5hvZP8ZN0T+jiNkL0QepXs+EsQ9XJ8ipEDoiH70ySUJP3Q==", + "requires": { + "bytes": "3.1.0", + "http-errors": "1.7.2", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + } + }, + "readable-stream": { + "version": "1.1.14", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", + "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.1", + "isarray": "0.0.1", + "string_decoder": "~0.10.x" + } + }, + "regenerator-runtime": { + "version": "0.13.2", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.2.tgz", + "integrity": "sha512-S/TQAZJO+D3m9xeN1WTI8dLKBBiRgXBlTJvbWjCThHWZj9EvHK70Ff50/tYj2J/fvBY6JtFVwRuazHN2E7M9BA==" + }, + "regexp-clone": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/regexp-clone/-/regexp-clone-0.0.1.tgz", + "integrity": "sha1-p8LgmJH9vzj7sQ03b7cwA+aKxYk=" + }, + "require_optional": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz", + "integrity": "sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==", + "requires": { + "resolve-from": "^2.0.0", + "semver": "^5.1.0" + } + }, + "resolve-from": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-2.0.0.tgz", + "integrity": "sha1-lICrIOlP+h2egKgEx+oUdhGWa1c=" + }, + "retry": { + "version": "0.12.0", + "resolved": "https://registry.npmjs.org/retry/-/retry-0.12.0.tgz", + "integrity": "sha1-G0KmJmoh8HQh0bC1S33BZ7AcATs=" + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "saslprep": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz", + "integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==", + "optional": true, + "requires": { + "sparse-bitfield": "^3.0.3" + } + }, + "sax": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", + "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==" + }, + "semver": { + "version": "5.7.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.0.tgz", + "integrity": "sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA==" + }, + "send": { + "version": "0.16.2", + "resolved": "https://registry.npmjs.org/send/-/send-0.16.2.tgz", + "integrity": "sha512-E64YFPUssFHEFBvpbbjr44NCLtI1AohxQ8ZSiJjQLskAdKuriYEP6VyGEsRDH8ScozGpkaX1BGvhanqCwkcEZw==", + "requires": { + "debug": "2.6.9", + "depd": "~1.1.2", + "destroy": "~1.0.4", + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "fresh": "0.5.2", + "http-errors": "~1.6.2", + "mime": "1.4.1", + "ms": "2.0.0", + "on-finished": "~2.3.0", + "range-parser": "~1.2.0", + "statuses": "~1.4.0" + }, + "dependencies": { + "http-errors": { + "version": "1.6.3", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.6.3.tgz", + "integrity": "sha1-i1VoC7S+KDoLW/TqLjhYC+HZMg0=", + "requires": { + "depd": "~1.1.2", + "inherits": "2.0.3", + "setprototypeof": "1.1.0", + "statuses": ">= 1.4.0 < 2" + } + }, + "setprototypeof": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.0.tgz", + "integrity": "sha512-BvE/TwpZX4FXExxOxZyRGQQv651MSwmWKZGqvmPcRIjDqWub67kTKuIMx43cZZrS/cBBzwBcNDWoFxt2XEFIpQ==" + }, + "statuses": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.4.0.tgz", + "integrity": "sha512-zhSCtt8v2NDrRlPQpCNtw/heZLtfUDqxBM1udqikb/Hbk52LK4nQSwr10u77iopCW5LsyHpuXS0GnEc48mLeew==" + } + } + }, + "serve-static": { + "version": "1.13.2", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.13.2.tgz", + "integrity": "sha512-p/tdJrO4U387R9oMjb1oj7qSMaMfmOyd4j9hOFoxZe2baQszgHcSWjuya/CiT5kgZZKRudHNOA0pYXOl8rQ5nw==", + "requires": { + "encodeurl": "~1.0.2", + "escape-html": "~1.0.3", + "parseurl": "~1.3.2", + "send": "0.16.2" + } + }, + "setprototypeof": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz", + "integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==" + }, + "sha.js": { + "version": "2.4.11", + "resolved": "https://registry.npmjs.org/sha.js/-/sha.js-2.4.11.tgz", + "integrity": "sha512-QMEp5B7cftE7APOjk5Y6xgrbWu+WkLVQwk8JNjZ8nKRciZaByEW6MubieAiToS7+dwvrjGhH8jRXz3MVd0AYqQ==", + "requires": { + "inherits": "^2.0.1", + "safe-buffer": "^5.0.1" + } + }, + "simple-swizzle": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/simple-swizzle/-/simple-swizzle-0.2.2.tgz", + "integrity": "sha1-pNprY1/8zMoz9w0Xy5JZLeleVXo=", + "requires": { + "is-arrayish": "^0.3.1" + } + }, + "sliced": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/sliced/-/sliced-1.0.1.tgz", + "integrity": "sha1-CzpmK10Ewxd7GSa+qCsD+Dei70E=" + }, + "sparse-bitfield": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", + "integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=", + "optional": true, + "requires": { + "memory-pager": "^1.0.2" + } + }, + "stack-trace": { + "version": "0.0.10", + "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz", + "integrity": "sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA=" + }, + "statuses": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", + "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" + }, + "streamsearch": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-0.1.2.tgz", + "integrity": "sha1-gIudDlb8Jz2Am6VzOOkpkZoanxo=" + }, + "string_decoder": { + "version": "0.10.31", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=" + }, + "subscriptions-transport-ws": { + "version": "0.9.16", + "resolved": "https://registry.npmjs.org/subscriptions-transport-ws/-/subscriptions-transport-ws-0.9.16.tgz", + "integrity": "sha512-pQdoU7nC+EpStXnCfh/+ho0zE0Z+ma+i7xvj7bkXKb1dvYHSZxgRPaU6spRP+Bjzow67c/rRDoix5RT0uU9omw==", + "requires": { + "backo2": "^1.0.2", + "eventemitter3": "^3.1.0", + "iterall": "^1.2.1", + "symbol-observable": "^1.0.4", + "ws": "^5.2.0" + }, + "dependencies": { + "ws": { + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/ws/-/ws-5.2.2.tgz", + "integrity": "sha512-jaHFD6PFv6UgoIVda6qZllptQsMlDEJkTQcybzzXDYM1XO9Y8em691FGMPmM46WGyLU4z9KMgQN+qrux/nhlHA==", + "requires": { + "async-limiter": "~1.0.0" + } + } + } + }, + "symbol-observable": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/symbol-observable/-/symbol-observable-1.2.0.tgz", + "integrity": "sha512-e900nM8RRtGhlV36KGEU9k65K3mPb1WV70OdjfxlG2EAuM1noi/E/BaW/uMhL7bPEssK8QV57vN3esixjUvcXQ==" + }, + "text-hex": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/text-hex/-/text-hex-1.0.0.tgz", + "integrity": "sha512-uuVGNWzgJ4yhRaNSiubPY7OjISw4sw4E5Uv0wbjp+OzcbmVU/rsT8ujgcXJhn9ypzsgr5vlzpPqP+MBBKcGvbg==" + }, + "toidentifier": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", + "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==" + }, + "tough-cookie": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.5.0.tgz", + "integrity": "sha512-nlLsUzgm1kfLXSXfRZMc1KLAugd4hqJHDTvc2hDIwS3mZAfMEuMbc03SujMF+GEcpaX/qboeycw6iO8JwVv2+g==", + "requires": { + "psl": "^1.1.28", + "punycode": "^2.1.1" + } + }, + "triple-beam": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/triple-beam/-/triple-beam-1.3.0.tgz", + "integrity": "sha512-XrHUvV5HpdLmIj4uVMxHggLbFSZYIn7HEWsqePZcI50pco+MPqJ50wMGY794X7AOOhxOBAjbkqfAbEe/QMp2Lw==" + }, + "ts-invariant": { + "version": "0.3.3", + "resolved": "https://registry.npmjs.org/ts-invariant/-/ts-invariant-0.3.3.tgz", + "integrity": "sha512-UReOKsrJFGC9tUblgSRWo+BsVNbEd77Cl6WiV/XpMlkifXwNIJbknViCucHvVZkXSC/mcWeRnIGdY7uprcwvdQ==", + "requires": { + "tslib": "^1.9.3" + } + }, + "tslib": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.3.tgz", + "integrity": "sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ==" + }, + "type-is": { + "version": "1.6.18", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", + "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", + "requires": { + "media-typer": "0.3.0", + "mime-types": "~2.1.24" + } + }, + "unpipe": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", + "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" + }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + }, + "util.promisify": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/util.promisify/-/util.promisify-1.0.0.tgz", + "integrity": "sha512-i+6qA2MPhvoKLuxnJNpXAGhg7HphQOSUq2LKMZD0m15EiskXUkMvKdF4Uui0WYeCUGea+o2cw/ZuwehtfsrNkA==", + "requires": { + "define-properties": "^1.1.2", + "object.getownpropertydescriptors": "^2.0.3" + } + }, + "utils-merge": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", + "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=" + }, + "uuid": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", + "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==" + }, + "vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" + }, + "winston": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/winston/-/winston-3.2.1.tgz", + "integrity": "sha512-zU6vgnS9dAWCEKg/QYigd6cgMVVNwyTzKs81XZtTFuRwJOcDdBg7AU0mXVyNbs7O5RH2zdv+BdNZUlx7mXPuOw==", + "requires": { + "async": "^2.6.1", + "diagnostics": "^1.1.1", + "is-stream": "^1.1.0", + "logform": "^2.1.1", + "one-time": "0.0.4", + "readable-stream": "^3.1.1", + "stack-trace": "0.0.x", + "triple-beam": "^1.3.0", + "winston-transport": "^4.3.0" + }, + "dependencies": { + "readable-stream": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.3.0.tgz", + "integrity": "sha512-EsI+s3k3XsW+fU8fQACLN59ky34AZ14LoeVZpYwmZvldCFo0r0gnelwF2TcMjLor/BTL5aDJVBMkss0dthToPw==", + "requires": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + } + }, + "string_decoder": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.2.0.tgz", + "integrity": "sha512-6YqyX6ZWEYguAxgZzHGL7SsCeGx3V2TtOTqZz1xSTSWnqsbWwbptafNyvf/ACquZUXV3DANr5BDIwNYe1mN42w==", + "requires": { + "safe-buffer": "~5.1.0" + } + } + } + }, + "winston-transport": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/winston-transport/-/winston-transport-4.3.0.tgz", + "integrity": "sha512-B2wPuwUi3vhzn/51Uukcao4dIduEiPOcOt9HJ3QeaXgkJ5Z7UwpBzxS4ZGNHtrxrUvTwemsQiSys0ihOf8Mp1A==", + "requires": { + "readable-stream": "^2.3.6", + "triple-beam": "^1.2.0" + }, + "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" + } + } + } + }, + "ws": { + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-6.2.1.tgz", + "integrity": "sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==", + "requires": { + "async-limiter": "~1.0.0" + } + }, + "xml2js": { + "version": "0.4.19", + "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz", + "integrity": "sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q==", + "requires": { + "sax": ">=0.6.0", + "xmlbuilder": "~9.0.1" + } + }, + "xmlbuilder": { + "version": "9.0.7", + "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz", + "integrity": "sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0=" + }, + "yallist": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.0.3.tgz", + "integrity": "sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==" + }, + "zen-observable": { + "version": "0.8.14", + "resolved": "https://registry.npmjs.org/zen-observable/-/zen-observable-0.8.14.tgz", + "integrity": "sha512-kQz39uonEjEESwh+qCi83kcC3rZJGh4mrZW7xjkSQYXkq//JZHTtKo+6yuVloTgMtzsIWOJrjIrKvk/dqm0L5g==" + }, + "zen-observable-ts": { + "version": "0.8.18", + "resolved": "https://registry.npmjs.org/zen-observable-ts/-/zen-observable-ts-0.8.18.tgz", + "integrity": "sha512-q7d05s75Rn1j39U5Oapg3HI2wzriVwERVo4N7uFGpIYuHB9ff02P/E92P9B8T7QVC93jCMHpbXH7X0eVR5LA7A==", + "requires": { + "tslib": "^1.9.3", + "zen-observable": "^0.8.0" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 000000000..1e9b23411 --- /dev/null +++ b/package.json @@ -0,0 +1,29 @@ +{ + "name": "saweb-api", + "version": "0.1.0", + "description": "Superalgos Web backend API", + "author": "Superalgos, ltd.", + "license": "Apache-2.0", + "repository": { + "type": "git", + "url": "git+https://github.com/Superalgos/SAWebServer.git" + }, + "main": "src/index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "devDependencies": { + "express": "^4.16.4" + }, + "dependencies": { + "@azure/storage-blob": "^10.3.0", + "apollo-server-express": "2.1.0", + "axios": "0.18.0", + "dotenv": "6.0.0", + "esm": "^3.2.22", + "express": "4.16.3", + "graphql": "0.13.2", + "mongoose": "5.3.4", + "winston": "3.2.1" + } +} diff --git a/src/app.js b/src/app.js new file mode 100644 index 000000000..2b29d8a1a --- /dev/null +++ b/src/app.js @@ -0,0 +1,53 @@ +import 'dotenv/config'; +import express from 'express'; +import { ApolloServer } from 'apollo-server-express'; +import schema from './schema'; +import wrongPreshared from './errors/notAllowed.json'; +import db from './db'; + +const app = express(); + +app.post('/graphql', (req, res, next) => { + if (req.headers.preshared === process.env.PRESHARED_GATEWAY_KEY) { + next(); + } else { + res.send(wrongPreshared); + } +}); + +// Starting the graphql server +const server = new ApolloServer({ + schema, + playground: { + settings: { + 'editor.theme': 'dark', + 'editor.cursorShape': 'line', + }, + }, + context: ({ req }) => ({ + userId: req.headers.userid, + authorization: req.headers.authorization, + }), + formatError: error => ({ + code: error.extensions.exception.code, + message: error.message, + path: error.path, + }), + introspection: true, +}); + +server.applyMiddleware({ + app, + cors: true, +}); + +db.on('error', (err) => { + console.log(err); +}); +db.once('open', () => { + console.log('Connected to the DB.'); +}); + +app.listen(process.env.PORT, () => { + console.log(`Listening on port ${process.env.PORT}`); +}); diff --git a/src/db.js b/src/db.js new file mode 100644 index 000000000..8bbcd0c63 --- /dev/null +++ b/src/db.js @@ -0,0 +1,10 @@ +import mongoose from 'mongoose'; + +mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true }) +mongoose.set('useCreateIndex', true); +mongoose.set('useFindAndModify', false); +mongoose.Promise = global.Promise; + +const db = mongoose.connection; + +export default db; diff --git a/src/errors/index.js b/src/errors/index.js new file mode 100644 index 000000000..b6c507c56 --- /dev/null +++ b/src/errors/index.js @@ -0,0 +1,31 @@ +class ClientError extends Error { + constructor(args) { + super(args); + } +} + +class ServerError extends Error { + constructor(args) { + super(args); + } +} + +export class AuthenticationError extends ClientError { + // code = 401 + // message = 'Authentication not found, you have to be authenticated to perform this action' +} + +export class DatabaseError extends ClientError { + // code = 404 + // message = `Ressource not found : ${this.message}` +} + +export class WrongArgumentsError extends ClientError { + // code = 400 + // message = `Wrong arguments : ${this.message}` +} + +export class ServiceUnavailableError extends ServerError { + // code = 503 + // message = `At least one service is unresponding ${this.message}` +} diff --git a/src/errors/notAllowed.json b/src/errors/notAllowed.json new file mode 100644 index 000000000..b76604aa2 --- /dev/null +++ b/src/errors/notAllowed.json @@ -0,0 +1,8 @@ +{ + "errors": [ + { + "code": 401, + "message": "Wait, you're not supposed to be here, go through the gate as everyone else" + } + ] +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 000000000..59744b324 --- /dev/null +++ b/src/index.js @@ -0,0 +1,2 @@ +require = require('esm')(module) +module.exports = require('./app.js') diff --git a/src/models/ecosystem.js b/src/models/ecosystem.js new file mode 100644 index 000000000..85384b9e8 --- /dev/null +++ b/src/models/ecosystem.js @@ -0,0 +1,101 @@ +const mongoose = require('mongoose') +const { Schema } = require('mongoose') + +const ecosystemSchema = new Schema({ + authId: { + type: String, + required: true + }, + teams: [{ + codeName: { + type: String, + required: true + }, + displayName: { + type: String, + required: true + }, + host:{ + url: { + type: String, + required: true + }, + container: { + type: String, + required: true + }, + accessKey: { + type: String, + required: true + } + }, + bots: [{ + repo: { + type: String, + required: true + }, + configFile: { + type: String, + required: true + } + }], + plotters: [{ + repo: { + type: String, + required: true + }, + configFile: { + type: String, + required: true + } + }] + }], + hosts: [{ + codeName: { + type: String, + required: true + }, + displayName: { + type: String, + required: true + }, + host:{ + url: { + type: String, + required: true + }, + container: { + type: String, + required: true + }, + accessKey: { + type: String, + required: true + } + }, + competitions: [{ + repo: { + type: String, + required: true + }, + configFile: { + type: String, + required: true + } + }], + plotters: [{ + repo: { + type: String, + required: true + }, + configFile: { + type: String, + required: true + } + }] + }] +}) + +const Ecosystem = mongoose.model('Ecosystem', ecosystemSchema); + +export default Ecosystem; diff --git a/src/models/index.js b/src/models/index.js new file mode 100644 index 000000000..7f988ec48 --- /dev/null +++ b/src/models/index.js @@ -0,0 +1,5 @@ +import Ecosystem from './ecosystem'; + +export { + Ecosystem +}; diff --git a/src/schema/index.js b/src/schema/index.js new file mode 100644 index 000000000..59088712f --- /dev/null +++ b/src/schema/index.js @@ -0,0 +1,12 @@ +import { + GraphQLSchema, +} from 'graphql'; +import RootQuery from './queries'; +import Mutation from './mutations'; + +const Schema = new GraphQLSchema({ + query: RootQuery, + mutation: Mutation, +}); + +export default Schema; diff --git a/src/schema/mutations/AddTeam.js b/src/schema/mutations/AddTeam.js new file mode 100644 index 000000000..69cdd86c2 --- /dev/null +++ b/src/schema/mutations/AddTeam.js @@ -0,0 +1,68 @@ +import { AuthenticationError } from '../../errors'; +import { TeamType } from '../types'; +import { Ecosystem } from '../../models'; +import { TeamInputType } from '../types/input'; +import logger from '../../utils/logger'; +import { createContainer } from '../../storage/AzureStorage'; + +import { + GraphQLString +} from 'graphql'; + +export const args = { + team: { type: TeamInputType }, +}; + +const resolve = async (parent, { team }, context) => { + logger.debug('addTeam -> Entering Fuction.') + + if (!context.userId) { + throw new AuthenticationError() + } + + try { + let userEcosystem = await Ecosystem.findOne({ + authId: context.userId + }) + + if (!userEcosystem) { + logger.debug('addTeam -> Ecosystem not found for the user, creating a new one from default.') + + let defaultEcosystem = require('../../config/ecosystem.json') + userEcosystem = new Ecosystem(defaultEcosystem) + userEcosystem.authId = context.userId + } + + let containerName = team.codeName + await createContainer(containerName) + // let accessKey = await createPrivateAccessKey() + let accessKey = "test" + + if (!team.host) { + let nodeEndpoint = { + url: process.env.SUPERALGOS_NODE_ENDPOINT, + container: containerName, + accessKey: accessKey, + } + team.host = nodeEndpoint + } + + userEcosystem.teams.push(team) + await userEcosystem.save() + + } catch (err) { + logger.error('addTeam -> Error: %s', err.stack) + // throw new ServerError('There has been an error adding the team.') + return 'There has been an error adding the team.' + } +}; + +const mutation = { + addTeam: { + type: GraphQLString, + args, + resolve, + }, +}; + +export default mutation; diff --git a/src/schema/mutations/index.js b/src/schema/mutations/index.js new file mode 100644 index 000000000..fd97d2baa --- /dev/null +++ b/src/schema/mutations/index.js @@ -0,0 +1,14 @@ +import { + GraphQLObjectType, +} from 'graphql'; + +import AddTeamMutation from './AddTeam'; + +const Mutation = new GraphQLObjectType({ + name: 'Mutation', + fields: Object.assign( + AddTeamMutation, + ), +}); + +export default Mutation; diff --git a/src/schema/queries/Teams.js b/src/schema/queries/Teams.js new file mode 100644 index 000000000..2c278e453 --- /dev/null +++ b/src/schema/queries/Teams.js @@ -0,0 +1,21 @@ +import { GraphQLList } from 'graphql'; +import { TeamType } from '../types'; +import { Ecosystem } from '../../models'; +import logger from '../../utils/logger' + +const args = {}; + +const resolve = (parent, args, context) => { + logger.debug('Listing teams.') + +} + +const query = { + teams: { + type: new GraphQLList(TeamType), + args, + resolve, + }, +}; + +export default query; diff --git a/src/schema/queries/index.js b/src/schema/queries/index.js new file mode 100644 index 000000000..5483b5df6 --- /dev/null +++ b/src/schema/queries/index.js @@ -0,0 +1,14 @@ +import { + GraphQLObjectType, +} from 'graphql'; + +import TeamsQuery from './Teams'; + +const RootQuery = new GraphQLObjectType({ + name: 'RootQueryType', + fields: Object.assign( + TeamsQuery + ), +}); + +export default RootQuery; diff --git a/src/schema/types/Bot.js b/src/schema/types/Bot.js new file mode 100644 index 000000000..7e02f2e38 --- /dev/null +++ b/src/schema/types/Bot.js @@ -0,0 +1,14 @@ +import { + GraphQLObjectType, + GraphQLString +} from 'graphql'; + +const BotType = new GraphQLObjectType({ + name: 'Bot', + fields: () => ({ + repo: { type: GraphQLString }, + configFile: { type: GraphQLString } + }), +}); + +export default BotType; diff --git a/src/schema/types/Ecosystem.js b/src/schema/types/Ecosystem.js new file mode 100644 index 000000000..828361682 --- /dev/null +++ b/src/schema/types/Ecosystem.js @@ -0,0 +1,30 @@ +import { + GraphQLObjectType, + GraphQLString, + GraphQLID +} from 'graphql'; + +import { TeamType, EventHostType } from './index'; + +const EcosystemType = new GraphQLObjectType({ + name: 'Ecosystem', + fields: () => ({ + id: { type: GraphQLID }, + authId: { type: GraphQLString }, + userName: { type: GraphQLString }, // TODO this field is temporary, it should be used the user module query + teams: { + type: new GraphQLList(TeamType), + resolve(parent) { + return parent.teams; + } + }, + hosts: { + type: new GraphQLList(EventHostType), + resolve(parent) { + return parent.hosts; + } + } + }), +}); + +export default EcosystemType; diff --git a/src/schema/types/EventHost.js b/src/schema/types/EventHost.js new file mode 100644 index 000000000..91e2c7533 --- /dev/null +++ b/src/schema/types/EventHost.js @@ -0,0 +1,31 @@ +import { + GraphQLObjectType, + GraphQLString +} from 'graphql'; + +import { BotType } from './index'; + +const EventHostType = new GraphQLObjectType({ + name: 'EventHost', + fields: () => ({ + codeName: { type: GraphQLString }, + displayName: { type: GraphQLString }, + host: { type: GraphQLString }, + container: { type: GraphQLString }, + accessKey: { type: GraphQLString }, + competitions: { + type: new GraphQLList(BotType), + resolve(parent) { + return parent.bots; + } + }, + plotters: { + type: new GraphQLList(BotType), + resolve(parent) { + return parent.bots; + } + } + }), +}); + +export default EventHostType; diff --git a/src/schema/types/Host.js b/src/schema/types/Host.js new file mode 100644 index 000000000..4c31de9e7 --- /dev/null +++ b/src/schema/types/Host.js @@ -0,0 +1,15 @@ +import { + GraphQLObjectType, + GraphQLString +} from 'graphql'; + +const HostType = new GraphQLObjectType({ + name: 'Host', + fields: () => ({ + url: { type: GraphQLString }, + container: { type: GraphQLString }, + accessKey: { type: GraphQLString } + }), +}); + +export default HostType; diff --git a/src/schema/types/Team.js b/src/schema/types/Team.js new file mode 100644 index 000000000..e39d0aa5b --- /dev/null +++ b/src/schema/types/Team.js @@ -0,0 +1,35 @@ +import { + GraphQLObjectType, + GraphQLString, + GraphQLList +} from 'graphql'; + +import { BotType, HostType } from './index'; + +const TeamType = new GraphQLObjectType({ + name: 'Team', + fields: () => ({ + codeName: { type: GraphQLString }, + displayName: { type: GraphQLString }, + host: { + type: HostType, + resolve(parent) { + return parent.host; + } + }, + bots: { + type: new GraphQLList(BotType), + resolve(parent) { + return parent.bots; + } + }, + plotters: { + type: new GraphQLList(BotType), + resolve(parent) { + return parent.bots; + } + } + }), +}); + +export default TeamType; diff --git a/src/schema/types/index.js b/src/schema/types/index.js new file mode 100644 index 000000000..8f8f70ce1 --- /dev/null +++ b/src/schema/types/index.js @@ -0,0 +1,11 @@ +import TeamType from './Team'; +import BotType from './Bot'; +import EventHostType from './EventHost'; +import HostType from './Host'; + +export { + TeamType, + BotType, + EventHostType, + HostType +}; diff --git a/src/schema/types/input/Bot.js b/src/schema/types/input/Bot.js new file mode 100644 index 000000000..75f10ebde --- /dev/null +++ b/src/schema/types/input/Bot.js @@ -0,0 +1,15 @@ +import { + GraphQLInputObjectType, + GraphQLString +} from 'graphql'; + +const Type = new GraphQLInputObjectType({ + name: 'BotInput', + description: 'Payload for bot input.', + fields: () => ({ + repo: { type: GraphQLString }, + configFile: { type: GraphQLString } + }), +}); + +export default Type; diff --git a/src/schema/types/input/Host.js b/src/schema/types/input/Host.js new file mode 100644 index 000000000..9574ce095 --- /dev/null +++ b/src/schema/types/input/Host.js @@ -0,0 +1,16 @@ +import { + GraphQLInputObjectType, + GraphQLString +} from 'graphql'; + +const Type = new GraphQLInputObjectType({ + name: 'HostInput', + description: 'Payload for host input.', + fields: () => ({ + url: { type: GraphQLString }, + container: { type: GraphQLString }, + accessKey: { type: GraphQLString } + }), +}); + +export default Type; diff --git a/src/schema/types/input/Team.js b/src/schema/types/input/Team.js new file mode 100644 index 000000000..878dca112 --- /dev/null +++ b/src/schema/types/input/Team.js @@ -0,0 +1,22 @@ +import { + GraphQLString, + GraphQLInputObjectType, + GraphQLNonNull, + GraphQLList +} from 'graphql'; + +import { BotInputType, HostInputType } from './index'; + +const Type = new GraphQLInputObjectType({ + name: 'TeamInput', + description: 'Payload for team input', + fields: () => ({ + codeName: { type: new GraphQLNonNull(GraphQLString) }, + displayName: { type: new GraphQLNonNull(GraphQLString) }, + host: { type: HostInputType }, + bots: { type: new GraphQLNonNull(new GraphQLList(BotInputType)) }, + plotters: { type: new GraphQLList(BotInputType) } + }), +}); + +export default Type; diff --git a/src/schema/types/input/index.js b/src/schema/types/input/index.js new file mode 100644 index 000000000..bee4b6f16 --- /dev/null +++ b/src/schema/types/input/index.js @@ -0,0 +1,9 @@ +import TeamInputType from './Team' +import BotInputType from './Bot' +import HostInputType from './Host' + +export { + TeamInputType, + BotInputType, + HostInputType +}; diff --git a/src/storage/AzureStorage.js b/src/storage/AzureStorage.js new file mode 100644 index 000000000..ea2d49c07 --- /dev/null +++ b/src/storage/AzureStorage.js @@ -0,0 +1,87 @@ +import logger from '../utils/logger' +const { + Aborter, + BlobURL, + BlockBlobURL, + ContainerURL, + ServiceURL, + StorageURL, + SharedKeyCredential +} = require("@azure/storage-blob") + +const AzureServiceURL = () => { + // Enter your storage account name and shared key + const account = process.env.STORAGE_BASE_URL; + const accountKey = process.env.STORAGE_CONNECTION_STRING; + + // Use SharedKeyCredential with storage account and account key + const sharedKeyCredential = new SharedKeyCredential(account, accountKey); + + // Use sharedKeyCredential, tokenCredential or anonymousCredential to create a pipeline + const pipeline = StorageURL.newPipeline(sharedKeyCredential); + + // List containers + const serviceURL = new ServiceURL( + // When using AnonymousCredential, following url should include a valid SAS or support public access + `https://${account}.blob.core.windows.net`, + pipeline + ); + logger.info("Connected to te file Storage.") + return serviceURL +} + +const serviceURL = AzureServiceURL() + +export const getFileContent = async (containerName, blobName) => { + const containerURL = ContainerURL.fromServiceURL(serviceURL, 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 blobURL = BlobURL.fromContainerURL(containerURL, blobName) + const blockBlobURL = BlockBlobURL.fromBlobURL(blobURL) + await blockBlobURL.upload( + Aborter.none, + content, + content.length + ) +} + +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) + } +} + +export const deleteContainer = async (containerName) => { + const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName) + await containerURL.delete(Aborter.none) + logger.debug('deleteContainer -> Successful.') +} + +export const createContainer = async (containerName) => { + const containerURL = ContainerURL.fromServiceURL(serviceURL, 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 = []; + readableStream.on("data", data => { + chunks.push(data.toString()); + }); + readableStream.on("end", () => { + resolve(chunks.join("")); + }); + readableStream.on("error", reject) + }); +} diff --git a/src/utils/logger.js b/src/utils/logger.js new file mode 100644 index 000000000..9ef06e941 --- /dev/null +++ b/src/utils/logger.js @@ -0,0 +1,41 @@ +import { createLogger, format, transports } from 'winston' +import { inspect } from 'util' + +const options = { + file: { + level: 'info', + filename: '../logs/app.log', + handleExceptions: true, + json: true, + maxsize: 5242880, // 5MB + maxFiles: 5, + colorize: false, + timestamp: true + }, + console: { + level: 'debug', + handleExceptions: true, + json: false, + colorize: true, + timestamp: true + } +} + +const logger = createLogger({ + level: 'info', + format: format.combine( + format.colorize(), + format.splat(), + format.timestamp({ format: 'YY/MM/DD HH:mm' }), + format.printf( + info => `${info.timestamp} - ${info.level}: ${JSON.parse(JSON.stringify(inspect(info.message)))}` + ) + ), + transports: [ + new transports.File(options.file), + new transports.Console(options.console) + ], + exitOnError: false // do not exit on handled exceptions +}) + +export default logger From 8ef729b77f5e129d1b9bfa634a9eafa68836f775 Mon Sep 17 00:00:00 2001 From: Matias Date: Fri, 24 May 2019 20:18:11 -0300 Subject: [PATCH 03/34] saweb-api migration --- package-lock.json | 369 ++++++++++++++++++++ package.json | 2 + src/app.js | 7 +- src/models/ecosystem.js | 20 +- src/schema/mutations/AddTeam.js | 50 +-- src/schema/mutations/DeleteTeam.js | 60 ++++ src/schema/mutations/index.js | 2 + src/schema/queries/Ecosystem.js | 42 +++ src/schema/queries/FileContent.js | 33 ++ src/schema/queries/Teams.js | 21 -- src/schema/queries/index.js | 6 +- src/schema/types/Bot.js | 2 + src/schema/types/Ecosystem.js | 3 +- src/schema/types/EventHost.js | 3 +- src/schema/types/File.js | 17 + src/schema/types/Host.js | 1 + src/schema/types/index.js | 6 +- src/schema/types/input/Bot.js | 5 +- src/schema/types/input/File.js | 19 + src/schema/types/input/Host.js | 1 + src/schema/types/input/Team.js | 4 +- src/schema/types/input/index.js | 4 +- src/storage/CopyBot.js | 60 ++++ src/storage/CopySimulator.js | 83 +++++ src/storage/CreateTeam.js | 27 ++ src/storage/DeleteTeam.js | 18 + src/storage/{ => providers}/AzureStorage.js | 86 ++++- src/utils/logger.js | 4 +- 28 files changed, 876 insertions(+), 79 deletions(-) create mode 100644 src/schema/mutations/DeleteTeam.js create mode 100644 src/schema/queries/Ecosystem.js create mode 100644 src/schema/queries/FileContent.js delete mode 100644 src/schema/queries/Teams.js create mode 100644 src/schema/types/File.js create mode 100644 src/schema/types/input/File.js create mode 100644 src/storage/CopyBot.js create mode 100644 src/storage/CopySimulator.js create mode 100644 src/storage/CreateTeam.js create mode 100644 src/storage/DeleteTeam.js rename src/storage/{ => providers}/AzureStorage.js (51%) diff --git a/package-lock.json b/package-lock.json index d0d1e0fde..52d8bf200 100644 --- a/package-lock.json +++ b/package-lock.json @@ -217,6 +217,17 @@ "negotiator": "0.6.2" } }, + "ajv": { + "version": "6.10.0", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.10.0.tgz", + "integrity": "sha512-nffhOpkymDECQyR0mnsUtoCE8RlX38G0rYP+wgLWFyZuUyuuojSSvi/+euOiQBIn63whYwYVIIH1TvE3tu4OEg==", + "requires": { + "fast-deep-equal": "^2.0.1", + "fast-json-stable-stringify": "^2.0.0", + "json-schema-traverse": "^0.4.1", + "uri-js": "^4.2.2" + } + }, "apollo-cache-control": { "version": "0.6.0", "resolved": "https://registry.npmjs.org/apollo-cache-control/-/apollo-cache-control-0.6.0.tgz", @@ -440,6 +451,19 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, + "asn1": { + "version": "0.2.4", + "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.4.tgz", + "integrity": "sha512-jxwzQpLQjSmWXgwaCZE9Nz+glAG01yF1QnWgbhGwHI5A6FRIEY6IVqtHhIepHqI7/kyEyQEagBC5mBEFlIYvdg==", + "requires": { + "safer-buffer": "~2.1.0" + } + }, + "assert-plus": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz", + "integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=" + }, "async": { "version": "2.6.1", "resolved": "https://registry.npmjs.org/async/-/async-2.6.1.tgz", @@ -466,6 +490,16 @@ "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" }, + "aws-sign2": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/aws-sign2/-/aws-sign2-0.7.0.tgz", + "integrity": "sha1-tG6JCTSpWR8tL2+G1+ap8bP+dqg=" + }, + "aws4": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.8.0.tgz", + "integrity": "sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==" + }, "axios": { "version": "0.18.0", "resolved": "https://registry.npmjs.org/axios/-/axios-0.18.0.tgz", @@ -475,11 +509,75 @@ "is-buffer": "^1.1.5" } }, + "azure-storage": { + "version": "2.10.3", + "resolved": "https://registry.npmjs.org/azure-storage/-/azure-storage-2.10.3.tgz", + "integrity": "sha512-IGLs5Xj6kO8Ii90KerQrrwuJKexLgSwYC4oLWmc11mzKe7Jt2E5IVg+ZQ8K53YWZACtVTMBNO3iGuA+4ipjJxQ==", + "requires": { + "browserify-mime": "~1.2.9", + "extend": "^3.0.2", + "json-edm-parser": "0.1.2", + "md5.js": "1.3.4", + "readable-stream": "~2.0.0", + "request": "^2.86.0", + "underscore": "~1.8.3", + "uuid": "^3.0.0", + "validator": "~9.4.1", + "xml2js": "0.2.8", + "xmlbuilder": "^9.0.7" + }, + "dependencies": { + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, + "process-nextick-args": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-1.0.7.tgz", + "integrity": "sha1-FQ4gt1ZZCtP5EJPyWk8q2L/zC6M=" + }, + "readable-stream": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.0.6.tgz", + "integrity": "sha1-j5A0HmilPMySh4jaz80Rs265t44=", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.1", + "isarray": "~1.0.0", + "process-nextick-args": "~1.0.6", + "string_decoder": "~0.10.x", + "util-deprecate": "~1.0.1" + } + }, + "sax": { + "version": "0.5.8", + "resolved": "https://registry.npmjs.org/sax/-/sax-0.5.8.tgz", + "integrity": "sha1-1HLbIo6zMcJQaw6MFVJK25OdEsE=" + }, + "xml2js": { + "version": "0.2.8", + "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.2.8.tgz", + "integrity": "sha1-m4FpCTFjH/CdGVdUn69U9PmAs8I=", + "requires": { + "sax": "0.5.x" + } + } + } + }, "backo2": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz", "integrity": "sha1-MasayLEpNjRj41s+u2n038+6eUc=" }, + "bcrypt-pbkdf": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz", + "integrity": "sha1-pDAdOJtqQ/m2f/PKEaP2Y342Dp4=", + "requires": { + "tweetnacl": "^0.14.3" + } + }, "bluebird": { "version": "3.5.1", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz", @@ -502,6 +600,11 @@ "type-is": "~1.6.17" } }, + "browserify-mime": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/browserify-mime/-/browserify-mime-1.2.9.tgz", + "integrity": "sha1-rrGvKN5sDXpqLOQK22j/GEIq8x8=" + }, "bson": { "version": "1.0.9", "resolved": "https://registry.npmjs.org/bson/-/bson-1.0.9.tgz", @@ -521,6 +624,11 @@ "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==" }, + "caseless": { + "version": "0.12.0", + "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz", + "integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=" + }, "color": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/color/-/color-3.0.0.tgz", @@ -618,6 +726,14 @@ "vary": "^1" } }, + "dashdash": { + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz", + "integrity": "sha1-hTz6D3y+L+1d4gMmuN1YEDX24vA=", + "requires": { + "assert-plus": "^1.0.0" + } + }, "debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -678,6 +794,15 @@ "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-6.0.0.tgz", "integrity": "sha512-FlWbnhgjtwD+uNLUGHbMykMOYQaTivdHEmYwAKFjn6GKe/CqY0fNae93ZHTd20snh9ZLr8mTzIL9m0APQ1pjQg==" }, + "ecc-jsbn": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz", + "integrity": "sha1-OoOpBOVDUyh4dMVkt1SThoSamMk=", + "requires": { + "jsbn": "~0.1.0", + "safer-buffer": "^2.1.0" + } + }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -880,6 +1005,21 @@ } } }, + "extend": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", + "integrity": "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==" + }, + "extsprintf": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/extsprintf/-/extsprintf-1.3.0.tgz", + "integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU=" + }, + "fast-deep-equal": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz", + "integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=" + }, "fast-json-stable-stringify": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz", @@ -939,6 +1079,11 @@ } } }, + "forever-agent": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz", + "integrity": "sha1-+8cfDEGt6zf5bFd60e1C2P2sypE=" + }, "form-data": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.3.3.tgz", @@ -969,6 +1114,14 @@ "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" }, + "getpass": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz", + "integrity": "sha1-Xv+OPmhNVprkyysSgmBOi6YhSfo=", + "requires": { + "assert-plus": "^1.0.0" + } + }, "graphql": { "version": "0.13.2", "resolved": "https://registry.npmjs.org/graphql/-/graphql-0.13.2.tgz", @@ -1039,6 +1192,20 @@ } } }, + "har-schema": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz", + "integrity": "sha1-qUwiJOvKwEeCoNkDVSHyRzW37JI=" + }, + "har-validator": { + "version": "5.1.3", + "resolved": "https://registry.npmjs.org/har-validator/-/har-validator-5.1.3.tgz", + "integrity": "sha512-sNvOCzEQNr/qrvJgc3UG/kD4QtlHycrzwS+6mfTrrSq97BvaYcPZZI1ZSqGSPR73Cxn4LKTD4PttRwfU7jWq5g==", + "requires": { + "ajv": "^6.5.5", + "har-schema": "^2.0.0" + } + }, "has": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", @@ -1052,6 +1219,15 @@ "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.0.tgz", "integrity": "sha1-uhqPGvKg/DllD1yFA2dwQSIGO0Q=" }, + "hash-base": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/hash-base/-/hash-base-3.0.4.tgz", + "integrity": "sha1-X8hoaEfs1zSZQDMZprCj8/auSRg=", + "requires": { + "inherits": "^2.0.1", + "safe-buffer": "^5.0.1" + } + }, "http-errors": { "version": "1.7.2", "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", @@ -1064,6 +1240,16 @@ "toidentifier": "1.0.0" } }, + "http-signature": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz", + "integrity": "sha1-muzZJRFHcvPZW2WmCruPfBj7rOE=", + "requires": { + "assert-plus": "^1.0.0", + "jsprim": "^1.2.2", + "sshpk": "^1.7.0" + } + }, "iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", @@ -1123,16 +1309,75 @@ "has-symbols": "^1.0.0" } }, + "is-typedarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz", + "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=" + }, "isarray": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=" }, + "isstream": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", + "integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=" + }, "iterall": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/iterall/-/iterall-1.2.2.tgz", "integrity": "sha512-yynBb1g+RFUPY64fTrFv7nsjRrENBQJaX2UL+2Szc9REFrSNm1rpSXHGzhmAy7a9uv3vlvgBlXnf9RqmPH1/DA==" }, + "jsbn": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz", + "integrity": "sha1-peZUwuWi3rXyAdls77yoDA7y9RM=" + }, + "json-beautify": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/json-beautify/-/json-beautify-1.1.0.tgz", + "integrity": "sha512-Y8lEcqhyJKRq2IhYBGtRIQO7EgOlyeSCYXnQCzAnGO5DhCBzMBEZ90PTw620NwpfQccJ/sj4+bU+r4GBI0b2Lw==" + }, + "json-edm-parser": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/json-edm-parser/-/json-edm-parser-0.1.2.tgz", + "integrity": "sha1-HmCw/vG8CvZ7wNFG393lSGzWFbQ=", + "requires": { + "jsonparse": "~1.2.0" + } + }, + "json-schema": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.2.3.tgz", + "integrity": "sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM=" + }, + "json-schema-traverse": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", + "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==" + }, + "json-stringify-safe": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz", + "integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=" + }, + "jsonparse": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/jsonparse/-/jsonparse-1.2.0.tgz", + "integrity": "sha1-XAxWhRBxYOcv50ib3eoLRMK8Z70=" + }, + "jsprim": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.1.tgz", + "integrity": "sha1-MT5mvB5cwG5Di8G3SZwuXFastqI=", + "requires": { + "assert-plus": "1.0.0", + "extsprintf": "1.3.0", + "json-schema": "0.2.3", + "verror": "1.10.0" + } + }, "kareem": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.0.tgz", @@ -1193,6 +1438,15 @@ "yallist": "^3.0.2" } }, + "md5.js": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/md5.js/-/md5.js-1.3.4.tgz", + "integrity": "sha1-6b296UogpawYsENA/Fdk1bCdkB0=", + "requires": { + "hash-base": "^3.0.0", + "inherits": "^2.0.1" + } + }, "media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -1326,6 +1580,11 @@ "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" }, + "oauth-sign": { + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz", + "integrity": "sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ==" + }, "object-assign": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", @@ -1373,6 +1632,11 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, + "performance-now": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", + "integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=" + }, "process-nextick-args": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.0.tgz", @@ -1466,6 +1730,54 @@ "resolved": "https://registry.npmjs.org/regexp-clone/-/regexp-clone-0.0.1.tgz", "integrity": "sha1-p8LgmJH9vzj7sQ03b7cwA+aKxYk=" }, + "request": { + "version": "2.88.0", + "resolved": "https://registry.npmjs.org/request/-/request-2.88.0.tgz", + "integrity": "sha512-NAqBSrijGLZdM0WZNsInLJpkJokL72XYjUpnB0iwsRgxh7dB6COrHnTBNwN0E+lHDAJzu7kLAkDeY08z2/A0hg==", + "requires": { + "aws-sign2": "~0.7.0", + "aws4": "^1.8.0", + "caseless": "~0.12.0", + "combined-stream": "~1.0.6", + "extend": "~3.0.2", + "forever-agent": "~0.6.1", + "form-data": "~2.3.2", + "har-validator": "~5.1.0", + "http-signature": "~1.2.0", + "is-typedarray": "~1.0.0", + "isstream": "~0.1.2", + "json-stringify-safe": "~5.0.1", + "mime-types": "~2.1.19", + "oauth-sign": "~0.9.0", + "performance-now": "^2.1.0", + "qs": "~6.5.2", + "safe-buffer": "^5.1.2", + "tough-cookie": "~2.4.3", + "tunnel-agent": "^0.6.0", + "uuid": "^3.3.2" + }, + "dependencies": { + "punycode": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz", + "integrity": "sha1-wNWmOycYgArY4esPpSachN1BhF4=" + }, + "qs": { + "version": "6.5.2", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz", + "integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==" + }, + "tough-cookie": { + "version": "2.4.3", + "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.4.3.tgz", + "integrity": "sha512-Q5srk/4vDM54WJsJio3XNn6K2sCG+CQ8G5Wz6bZhRZoAe/+TxjWB/GlFAnYEbkYVlON9FMk/fE3h2RLpPXo4lQ==", + "requires": { + "psl": "^1.1.24", + "punycode": "^1.4.1" + } + } + } + }, "require_optional": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz", @@ -1604,6 +1916,22 @@ "memory-pager": "^1.0.2" } }, + "sshpk": { + "version": "1.16.1", + "resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.16.1.tgz", + "integrity": "sha512-HXXqVUq7+pcKeLqqZj6mHFUMvXtOJt1uoUx09pFW6011inTMxqI8BA8PM95myrIyyKwdnzjdFjLiE6KBPVtJIg==", + "requires": { + "asn1": "~0.2.3", + "assert-plus": "^1.0.0", + "bcrypt-pbkdf": "^1.0.0", + "dashdash": "^1.12.0", + "ecc-jsbn": "~0.1.1", + "getpass": "^0.1.1", + "jsbn": "~0.1.0", + "safer-buffer": "^2.0.2", + "tweetnacl": "~0.14.0" + } + }, "stack-trace": { "version": "0.0.10", "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz", @@ -1688,6 +2016,19 @@ "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.3.tgz", "integrity": "sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ==" }, + "tunnel-agent": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", + "integrity": "sha1-J6XeoGs2sEoKmWZ3SykIaPD8QP0=", + "requires": { + "safe-buffer": "^5.0.1" + } + }, + "tweetnacl": { + "version": "0.14.5", + "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz", + "integrity": "sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q=" + }, "type-is": { "version": "1.6.18", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", @@ -1697,11 +2038,24 @@ "mime-types": "~2.1.24" } }, + "underscore": { + "version": "1.8.3", + "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz", + "integrity": "sha1-Tz+1OxBuYJf8+ctBCfKl6b36UCI=" + }, "unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" }, + "uri-js": { + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.2.2.tgz", + "integrity": "sha512-KY9Frmirql91X2Qgjry0Wd4Y+YTdrdZheS8TFwvkbLWf/G5KNJDCh6pKL5OZctEW4+0Baa5idK2ZQuELRwPznQ==", + "requires": { + "punycode": "^2.1.0" + } + }, "util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", @@ -1726,11 +2080,26 @@ "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==" }, + "validator": { + "version": "9.4.1", + "resolved": "https://registry.npmjs.org/validator/-/validator-9.4.1.tgz", + "integrity": "sha512-YV5KjzvRmSyJ1ee/Dm5UED0G+1L4GZnLN3w6/T+zZm8scVua4sOhYKWTUrKa0H/tMiJyO9QLHMPN+9mB/aMunA==" + }, "vary": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" }, + "verror": { + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/verror/-/verror-1.10.0.tgz", + "integrity": "sha1-OhBcoXBTr1XW4nDB+CiGguGNpAA=", + "requires": { + "assert-plus": "^1.0.0", + "core-util-is": "1.0.2", + "extsprintf": "^1.2.0" + } + }, "winston": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/winston/-/winston-3.2.1.tgz", diff --git a/package.json b/package.json index 1e9b23411..6dab68f4f 100644 --- a/package.json +++ b/package.json @@ -19,10 +19,12 @@ "@azure/storage-blob": "^10.3.0", "apollo-server-express": "2.1.0", "axios": "0.18.0", + "azure-storage": "^2.10.3", "dotenv": "6.0.0", "esm": "^3.2.22", "express": "4.16.3", "graphql": "0.13.2", + "json-beautify": "^1.1.0", "mongoose": "5.3.4", "winston": "3.2.1" } diff --git a/src/app.js b/src/app.js index 2b29d8a1a..69f1f14ce 100644 --- a/src/app.js +++ b/src/app.js @@ -4,6 +4,7 @@ import { ApolloServer } from 'apollo-server-express'; import schema from './schema'; import wrongPreshared from './errors/notAllowed.json'; import db from './db'; +import logger from './utils/logger' const app = express(); @@ -42,12 +43,12 @@ server.applyMiddleware({ }); db.on('error', (err) => { - console.log(err); + logger.error(err); }); db.once('open', () => { - console.log('Connected to the DB.'); + logger.info('Connected to the DB.'); }); app.listen(process.env.PORT, () => { - console.log(`Listening on port ${process.env.PORT}`); + logger.info(`Listening on port ${process.env.PORT}`); }); diff --git a/src/models/ecosystem.js b/src/models/ecosystem.js index 85384b9e8..5856d8f19 100644 --- a/src/models/ecosystem.js +++ b/src/models/ecosystem.js @@ -20,6 +20,10 @@ const ecosystemSchema = new Schema({ type: String, required: true }, + storage: { + type: String, + required: true + }, container: { type: String, required: true @@ -30,13 +34,21 @@ const ecosystemSchema = new Schema({ } }, bots: [{ - repo: { + codeName: { type: String, required: true }, - configFile: { + displayName: { type: String, required: true + }, + repo: { + type: String, + required: false + }, + configFile: { + type: String, + required: false } }], plotters: [{ @@ -64,6 +76,10 @@ const ecosystemSchema = new Schema({ type: String, required: true }, + storage: { + type: String, + required: true + }, container: { type: String, required: true diff --git a/src/schema/mutations/AddTeam.js b/src/schema/mutations/AddTeam.js index 69cdd86c2..44e6402d0 100644 --- a/src/schema/mutations/AddTeam.js +++ b/src/schema/mutations/AddTeam.js @@ -1,17 +1,11 @@ -import { AuthenticationError } from '../../errors'; -import { TeamType } from '../types'; -import { Ecosystem } from '../../models'; -import { TeamInputType } from '../types/input'; -import logger from '../../utils/logger'; -import { createContainer } from '../../storage/AzureStorage'; +import logger from '../../utils/logger' +import { AuthenticationError, WrongArgumentsError } from '../../errors' +import { TeamType } from '../types' +import { Ecosystem } from '../../models' +import { TeamInputType } from '../types/input' +import { createTeam } from '../../storage/CreateTeam' -import { - GraphQLString -} from 'graphql'; - -export const args = { - team: { type: TeamInputType }, -}; +export const args = { team: { type: TeamInputType } } const resolve = async (parent, { team }, context) => { logger.debug('addTeam -> Entering Fuction.') @@ -33,36 +27,42 @@ const resolve = async (parent, { team }, context) => { userEcosystem.authId = context.userId } - let containerName = team.codeName - await createContainer(containerName) - // let accessKey = await createPrivateAccessKey() - let accessKey = "test" + for (let i = 0; i < userEcosystem.teams.length; i++) { + const auxTeam = userEcosystem.teams[i]; + if (auxTeam.codeName === team.codeName) { + throw new WrongArgumentsError("The team already exist on the user ecosystem.") + } + } + let accessKey = await createTeam(team) if (!team.host) { let nodeEndpoint = { url: process.env.SUPERALGOS_NODE_ENDPOINT, - container: containerName, + container: team.codeName, accessKey: accessKey, } team.host = nodeEndpoint } + let bots= [] + bots.push(team.bot) + team.bots = bots + userEcosystem.teams.push(team) await userEcosystem.save() - + return team } catch (err) { logger.error('addTeam -> Error: %s', err.stack) - // throw new ServerError('There has been an error adding the team.') - return 'There has been an error adding the team.' + throw err } -}; +} const mutation = { addTeam: { - type: GraphQLString, + type: TeamType, args, resolve, - }, -}; + } +} export default mutation; diff --git a/src/schema/mutations/DeleteTeam.js b/src/schema/mutations/DeleteTeam.js new file mode 100644 index 000000000..85f4c403a --- /dev/null +++ b/src/schema/mutations/DeleteTeam.js @@ -0,0 +1,60 @@ +import logger from '../../utils/logger' +import { AuthenticationError, WrongArgumentsError } from '../../errors' +import { TeamType } from '../types' +import { Ecosystem } from '../../models' +import { TeamInputType } from '../types/input' +import { deleteTeam } from '../../storage/DeleteTeam' + +export const args = { team: { type: TeamInputType } } + +const resolve = async (parent, { team }, context) => { + logger.debug('deleteTeam -> Entering Fuction.') + + if (!context.userId) { + throw new AuthenticationError() + } + + try { + let userEcosystem = await Ecosystem.findOne({ + authId: context.userId + }) + + if (!userEcosystem) { + logger.debug('deleteTeam -> Ecosystem not found for the user.') + throw new WrongArgumentsError('The user does not have an ecosystem configured.') + } + + let teamFound = false + for (let i = 0; i < userEcosystem.teams.length; i++) { + const auxTeam = userEcosystem.teams[i] + if (auxTeam.codeName === team.codeName) { + logger.debug('deleteTeam -> Team found, removing.') + teamFound = true + await deleteTeam(team) + userEcosystem.teams.splice(i, 1) + await userEcosystem.save() + break + } + } + + if (!teamFound) { + throw new WrongArgumentsError('Team was not found on the user ecosystem.') + } else { + return team + } + + } catch (err) { + logger.error('deleteTeam -> Error: %s', err.stack) + throw err + } +} + +const mutation = { + deleteTeam: { + type: TeamType, + args, + resolve, + } +} + +export default mutation; diff --git a/src/schema/mutations/index.js b/src/schema/mutations/index.js index fd97d2baa..85ef11366 100644 --- a/src/schema/mutations/index.js +++ b/src/schema/mutations/index.js @@ -3,11 +3,13 @@ import { } from 'graphql'; import AddTeamMutation from './AddTeam'; +import DeleteTeamMutation from './DeleteTeam'; const Mutation = new GraphQLObjectType({ name: 'Mutation', fields: Object.assign( AddTeamMutation, + DeleteTeamMutation ), }); diff --git a/src/schema/queries/Ecosystem.js b/src/schema/queries/Ecosystem.js new file mode 100644 index 000000000..ca55b7fce --- /dev/null +++ b/src/schema/queries/Ecosystem.js @@ -0,0 +1,42 @@ +import logger from '../../utils/logger' +import { Ecosystem } from '../../models' +import { EcosystemType } from '../types' + +const args = {} + +const resolve = async (parent, args, context) => { + logger.debug('getEcosystem -> Entering Fuction.') + + try { + if (!context.userId) { + logger.debug('getEcosystem -> User not logged in, returning default ecosystem.') + let defaultEcosystem = require('../../config/ecosystem.json') + return defaultEcosystem + } else { + let userEcosystem = await Ecosystem.findOne({ + authId: context.userId + }) + + if (!userEcosystem) { + logger.debug('addTeam -> Ecosystem not found for the user, retrieving default.') + let defaultEcosystem = require('../../config/ecosystem.json') + return defaultEcosystem + } + + return userEcosystem + } + } catch (err) { + logger.error('getEcosystem -> Error: %s', err.stack) + throw err + } +} + +const query = { + getEcosystem: { + type: EcosystemType, + args, + resolve, + } +} + +export default query diff --git a/src/schema/queries/FileContent.js b/src/schema/queries/FileContent.js new file mode 100644 index 000000000..910fa8df5 --- /dev/null +++ b/src/schema/queries/FileContent.js @@ -0,0 +1,33 @@ +import logger from '../../utils/logger' +import { FileInputType } from '../types/input' +import { GraphQLString } from 'graphql' + +export const args = { file: { type: FileInputType } } + +const resolve = async (parent, { file }, context) => { + logger.debug('getFileContent -> Entering Fuction: ' + file.filePath) + + try { + + let storage = require('../../storage/providers/AzureStorage') // Default value + if (process.env.STORAGE_PROVIDER === 'Minio') { + storage = require('../storage/providers/MinioStorage') + } + + return storage.getFileContentRemote(file.container, file.filePath, file.storage, file.accessKey) + + } catch (err) { + logger.error('getFileContent -> Error: %s', err.stack) + throw err + } +} + +const query = { + fileContent: { + type: GraphQLString, + args, + resolve, + } +} + +export default query diff --git a/src/schema/queries/Teams.js b/src/schema/queries/Teams.js deleted file mode 100644 index 2c278e453..000000000 --- a/src/schema/queries/Teams.js +++ /dev/null @@ -1,21 +0,0 @@ -import { GraphQLList } from 'graphql'; -import { TeamType } from '../types'; -import { Ecosystem } from '../../models'; -import logger from '../../utils/logger' - -const args = {}; - -const resolve = (parent, args, context) => { - logger.debug('Listing teams.') - -} - -const query = { - teams: { - type: new GraphQLList(TeamType), - args, - resolve, - }, -}; - -export default query; diff --git a/src/schema/queries/index.js b/src/schema/queries/index.js index 5483b5df6..1a9a0fbf6 100644 --- a/src/schema/queries/index.js +++ b/src/schema/queries/index.js @@ -2,12 +2,14 @@ import { GraphQLObjectType, } from 'graphql'; -import TeamsQuery from './Teams'; +import EcosystemQuery from './Ecosystem'; +import FileContentQuery from './FileContent'; const RootQuery = new GraphQLObjectType({ name: 'RootQueryType', fields: Object.assign( - TeamsQuery + EcosystemQuery, + FileContentQuery ), }); diff --git a/src/schema/types/Bot.js b/src/schema/types/Bot.js index 7e02f2e38..6701c467e 100644 --- a/src/schema/types/Bot.js +++ b/src/schema/types/Bot.js @@ -6,6 +6,8 @@ import { const BotType = new GraphQLObjectType({ name: 'Bot', fields: () => ({ + codeName: { type: GraphQLString }, + displayName: { type: GraphQLString }, repo: { type: GraphQLString }, configFile: { type: GraphQLString } }), diff --git a/src/schema/types/Ecosystem.js b/src/schema/types/Ecosystem.js index 828361682..d31cbbde2 100644 --- a/src/schema/types/Ecosystem.js +++ b/src/schema/types/Ecosystem.js @@ -1,7 +1,8 @@ import { GraphQLObjectType, GraphQLString, - GraphQLID + GraphQLID, + GraphQLList } from 'graphql'; import { TeamType, EventHostType } from './index'; diff --git a/src/schema/types/EventHost.js b/src/schema/types/EventHost.js index 91e2c7533..571bab36e 100644 --- a/src/schema/types/EventHost.js +++ b/src/schema/types/EventHost.js @@ -1,6 +1,7 @@ import { GraphQLObjectType, - GraphQLString + GraphQLString, + GraphQLList } from 'graphql'; import { BotType } from './index'; diff --git a/src/schema/types/File.js b/src/schema/types/File.js new file mode 100644 index 000000000..bf3c11136 --- /dev/null +++ b/src/schema/types/File.js @@ -0,0 +1,17 @@ +import { + GraphQLObjectType, + GraphQLString +} from 'graphql'; + +const FileType = new GraphQLObjectType({ + name: 'File', + fields: () => ({ + container: { type: GraphQLString }, + filePath: { type: GraphQLString }, + storage: { type: GraphQLString }, + accessKey: { type: GraphQLString }, + fileContent: { type: GraphQLString } + }), +}); + +export default FileType; diff --git a/src/schema/types/Host.js b/src/schema/types/Host.js index 4c31de9e7..fbc45bcd1 100644 --- a/src/schema/types/Host.js +++ b/src/schema/types/Host.js @@ -7,6 +7,7 @@ const HostType = new GraphQLObjectType({ name: 'Host', fields: () => ({ url: { type: GraphQLString }, + storage: { type: GraphQLString }, container: { type: GraphQLString }, accessKey: { type: GraphQLString } }), diff --git a/src/schema/types/index.js b/src/schema/types/index.js index 8f8f70ce1..17d90c59a 100644 --- a/src/schema/types/index.js +++ b/src/schema/types/index.js @@ -2,10 +2,14 @@ import TeamType from './Team'; import BotType from './Bot'; import EventHostType from './EventHost'; import HostType from './Host'; +import EcosystemType from './Ecosystem'; +import FileType from './File'; export { TeamType, BotType, EventHostType, - HostType + HostType, + EcosystemType, + FileType }; diff --git a/src/schema/types/input/Bot.js b/src/schema/types/input/Bot.js index 75f10ebde..dfbadf5fb 100644 --- a/src/schema/types/input/Bot.js +++ b/src/schema/types/input/Bot.js @@ -1,12 +1,15 @@ import { GraphQLInputObjectType, - GraphQLString + GraphQLString, + GraphQLNonNull } from 'graphql'; const Type = new GraphQLInputObjectType({ name: 'BotInput', description: 'Payload for bot input.', fields: () => ({ + codeName: { type: new GraphQLNonNull(GraphQLString) }, + displayName: { type: new GraphQLNonNull(GraphQLString) }, repo: { type: GraphQLString }, configFile: { type: GraphQLString } }), diff --git a/src/schema/types/input/File.js b/src/schema/types/input/File.js new file mode 100644 index 000000000..d92d4c6f0 --- /dev/null +++ b/src/schema/types/input/File.js @@ -0,0 +1,19 @@ +import { + GraphQLInputObjectType, + GraphQLString, + GraphQLNonNull +} from 'graphql'; + +const Type = new GraphQLInputObjectType({ + name: 'FileInput', + description: 'Payload for file input.', + fields: () => ({ + container: { type: new GraphQLNonNull(GraphQLString) }, + filePath: { type: new GraphQLNonNull(GraphQLString) }, + storage: { type: new GraphQLNonNull(GraphQLString) }, + accessKey: { type: new GraphQLNonNull(GraphQLString) }, + fileContent: { type: GraphQLString } + }), +}); + +export default Type; diff --git a/src/schema/types/input/Host.js b/src/schema/types/input/Host.js index 9574ce095..6f9d4bb5f 100644 --- a/src/schema/types/input/Host.js +++ b/src/schema/types/input/Host.js @@ -8,6 +8,7 @@ const Type = new GraphQLInputObjectType({ description: 'Payload for host input.', fields: () => ({ url: { type: GraphQLString }, + storage: { type: GraphQLString }, container: { type: GraphQLString }, accessKey: { type: GraphQLString } }), diff --git a/src/schema/types/input/Team.js b/src/schema/types/input/Team.js index 878dca112..37f49a33c 100644 --- a/src/schema/types/input/Team.js +++ b/src/schema/types/input/Team.js @@ -14,8 +14,8 @@ const Type = new GraphQLInputObjectType({ codeName: { type: new GraphQLNonNull(GraphQLString) }, displayName: { type: new GraphQLNonNull(GraphQLString) }, host: { type: HostInputType }, - bots: { type: new GraphQLNonNull(new GraphQLList(BotInputType)) }, - plotters: { type: new GraphQLList(BotInputType) } + bot: { type: new GraphQLNonNull(BotInputType) }, + plotter: { type: BotInputType } }), }); diff --git a/src/schema/types/input/index.js b/src/schema/types/input/index.js index bee4b6f16..ec822c308 100644 --- a/src/schema/types/input/index.js +++ b/src/schema/types/input/index.js @@ -1,9 +1,11 @@ import TeamInputType from './Team' import BotInputType from './Bot' import HostInputType from './Host' +import FileInputType from './File' export { TeamInputType, BotInputType, - HostInputType + HostInputType, + FileInputType }; diff --git a/src/storage/CopyBot.js b/src/storage/CopyBot.js new file mode 100644 index 000000000..357f1c493 --- /dev/null +++ b/src/storage/CopyBot.js @@ -0,0 +1,60 @@ +import beautify from 'json-beautify' +import logger from '../utils/logger' + +export const copyBot = async (storage, teamCodeName, botCodeName, botDisplayName) => { + 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') + + // Change config to adapt the new indicator + let parsedConfig = JSON.parse(config) + parsedConfig.displayName = botDisplayName + parsedConfig.codeName = botCodeName + parsedConfig.devTeam = teamCodeName + parsedConfig.profilePicture = botCodeName + '.png' + + parsedConfig.processes[0].statusDependencies[0].devTeam = teamCodeName + parsedConfig.processes[0].statusDependencies[0].bot = botCodeName + parsedConfig.processes[0].statusDependencies[1].devTeam = teamCodeName + parsedConfig.processes[0].statusDependencies[1].bot = 'simulator-' + botCodeName + parsedConfig.processes[0].statusDependencies[2].devTeam = teamCodeName + parsedConfig.processes[0].statusDependencies[2].bot = 'simulator-' + botCodeName + + parsedConfig.processes[0].dataDependencies[1].devTeam = teamCodeName + parsedConfig.processes[0].dataDependencies[1].bot = 'simulator-' + botCodeName + parsedConfig.processes[0].dataDependencies[2].devTeam = teamCodeName + parsedConfig.processes[0].dataDependencies[2].bot = 'simulator-' + botCodeName + + // Write the new files + 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) + + logger.debug('copyBot completed for bot %s', botCodeName) + + } 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/CopySimulator.js b/src/storage/CopySimulator.js new file mode 100644 index 000000000..ca2466891 --- /dev/null +++ b/src/storage/CopySimulator.js @@ -0,0 +1,83 @@ +import beautify from 'json-beautify' +import logger from '../utils/logger' + +export const copySimulator = async (storage, teamCodeName, botCodeName, botDisplayName) => { + 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') + + // Change config to adapt the new indicator + let newBotCodeName = 'simulator-' + botCodeName + let parsedConfig = JSON.parse(config) + parsedConfig.displayName = 'Simulator ' + botDisplayName + parsedConfig.codeName = newBotCodeName + parsedConfig.devTeam = teamCodeName + parsedConfig.profilePicture = newBotCodeName + '.png' + + parsedConfig.processes[0].statusDependencies[0].devTeam = teamCodeName + parsedConfig.processes[0].statusDependencies[0].bot = newBotCodeName + parsedConfig.processes[1].statusDependencies[2].devTeam = teamCodeName + parsedConfig.processes[1].statusDependencies[2].bot = newBotCodeName + + //Changing Indicator Output + parsedConfig.products[0].storageAccount = teamCodeName + parsedConfig.products[0].dataSets[0].filePath = teamCodeName + '/' + parsedConfig.codeName + '.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Trading-Simulation/Multi-Period-Market/@Period' + parsedConfig.products[0].dataSets[1].filePath = teamCodeName + '/' + parsedConfig.codeName + '.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Trading-Simulation/Multi-Period-Daily/@Period/@Year/@Month/@Day' + parsedConfig.products[0].dataSets[1].dataRange.filePath = teamCodeName + '/' + parsedConfig.codeName + '.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Trading-Simulation/Multi-Period-Daily' + + parsedConfig.products[1].storageAccount = teamCodeName + parsedConfig.products[1].dataSets[0].filePath = teamCodeName + '/' + parsedConfig.codeName + '.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Conditions/Multi-Period-Market/@Period' + parsedConfig.products[1].dataSets[1].filePath = teamCodeName + '/' + parsedConfig.codeName + '.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Conditions/Multi-Period-Daily/@Period/@Year/@Month/@Day' + parsedConfig.products[1].dataSets[1].dataRange.filePath = teamCodeName + '/' + parsedConfig.codeName + '.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Conditions/Multi-Period-Daily' + + parsedConfig.products[2].storageAccount = teamCodeName + parsedConfig.products[2].dataSets[0].filePath = teamCodeName + '/' + parsedConfig.codeName + '.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Strategies/Multi-Period-Market/@Period' + parsedConfig.products[2].dataSets[1].filePath = teamCodeName + '/' + parsedConfig.codeName + '.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Strategies/Multi-Period-Daily/@Period/@Year/@Month/@Day' + parsedConfig.products[2].dataSets[1].dataRange.filePath = teamCodeName + '/' + parsedConfig.codeName + '.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Strategies/Multi-Period-Daily' + + parsedConfig.products[3].storageAccount = teamCodeName + parsedConfig.products[3].dataSets[0].filePath = teamCodeName + '/' + parsedConfig.codeName + '.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Trades/Multi-Period-Market/@Period' + parsedConfig.products[3].dataSets[1].filePath = teamCodeName + '/' + parsedConfig.codeName + '.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Simulation-Trades/Multi-Period-Daily/@Period/@Year/@Month/@Day' + 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 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) + + logger.debug('copySimulator completed for bot %s', newBotCodeName) + + } catch (err) { + logger.error('copySimulator error for bot: %s. %s', botCodeName, err) + throw err + } +} + +export const removeSimulator = async (storage, teamCodeName, botCodeName) => { + try { + logger.debug('removeSimulator started for bot %s', botCodeName) + + // Delete simulator code + let newSimulatorName = 'simulator-' + botCodeName + '-Indicator-Bot' + let newSimulatorPath = teamCodeName + '/bots/' + newSimulatorName + await storage.deleteBlob(teamCodeName, newSimulatorPath + '/Commons.js') + await storage.deleteBlob(teamCodeName, newSimulatorPath + '/this.bot.config.json') + await storage.deleteBlob(teamCodeName, newSimulatorPath + '/Multi-Period-Daily/User.Bot.js') + await storage.deleteBlob(teamCodeName, newSimulatorPath + '/Multi-Period-Market/User.Bot.js') + + logger.debug('removeSimulator completed for bot %s', botCodeName) + + } catch (err) { + logger.error('copySimulator error for bot %s: %s', botCodeName, err) + throw err + } +} diff --git a/src/storage/CreateTeam.js b/src/storage/CreateTeam.js new file mode 100644 index 000000000..a2833250d --- /dev/null +++ b/src/storage/CreateTeam.js @@ -0,0 +1,27 @@ +import logger from '../utils/logger' +import { copyBot } from './CopyBot' +import { copySimulator } from './CopySimulator' + +export const createTeam = async (team) => { + try { + logger.debug('createTeam started for team %s', team.codeName) + + let storage = require ('./providers/AzureStorage') // Default value + if (process.env.STORAGE_PROVIDER === 'Minio') { + storage = require ('./providers/MinioStorage') + } + + let containerName = team.codeName + await storage.createContainer(containerName) + let accessKey = storage.createPrivateAccessKey(containerName, "WRITE", 365) + + await copyBot(storage, team.codeName, team.bot.codeName, team.bot.displayName) + await copySimulator(storage, team.codeName, team.bot.codeName, team.bot.displayName) + + return accessKey + + } catch (err) { + logger.error('createTeam error for teamCodeName: %s, error: %s', team.bot.codeName, err.stack) + throw err + } +} diff --git a/src/storage/DeleteTeam.js b/src/storage/DeleteTeam.js new file mode 100644 index 000000000..1f984d634 --- /dev/null +++ b/src/storage/DeleteTeam.js @@ -0,0 +1,18 @@ +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/AzureStorage.js b/src/storage/providers/AzureStorage.js similarity index 51% rename from src/storage/AzureStorage.js rename to src/storage/providers/AzureStorage.js index ea2d49c07..0992f7efb 100644 --- a/src/storage/AzureStorage.js +++ b/src/storage/providers/AzureStorage.js @@ -1,37 +1,49 @@ -import logger from '../utils/logger' -const { +import logger from '../../utils/logger' +import { Aborter, BlobURL, BlockBlobURL, ContainerURL, ServiceURL, StorageURL, - SharedKeyCredential -} = require("@azure/storage-blob") + SharedKeyCredential, + AnonymousCredential +} from '@azure/storage-blob' const AzureServiceURL = () => { - // Enter your storage account name and shared key - const account = process.env.STORAGE_BASE_URL; - const accountKey = process.env.STORAGE_CONNECTION_STRING; - // Use SharedKeyCredential with storage account and account key - const sharedKeyCredential = new SharedKeyCredential(account, accountKey); + const sharedKeyCredential = new SharedKeyCredential(process.env.STORAGE_BASE_URL, process.env.STORAGE_CONNECTION_STRING); // Use sharedKeyCredential, tokenCredential or anonymousCredential to create a pipeline - const pipeline = StorageURL.newPipeline(sharedKeyCredential); + const pipeline = StorageURL.newPipeline(sharedKeyCredential, { + retryOptions: { maxTries: 10 } + }); // List containers const serviceURL = new ServiceURL( // When using AnonymousCredential, following url should include a valid SAS or support public access - `https://${account}.blob.core.windows.net`, + `https://${process.env.STORAGE_BASE_URL}.blob.core.windows.net`, pipeline - ); - logger.info("Connected to te file Storage.") + ) 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 + ); + const containerURL = ContainerURL.fromServiceURL(serviceURLWithAccessKey, containerName) + const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName) + let downloadResponse = await blockBlobURL.download(Aborter.none, 0) + return await streamToString(downloadResponse.readableStreamBody) +} + export const getFileContent = async (containerName, blobName) => { const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName) const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName) @@ -76,12 +88,52 @@ export const createContainer = async (containerName) => { async function streamToString(readableStream) { return new Promise((resolve, reject) => { const chunks = []; - readableStream.on("data", data => { + readableStream.on('data', data => { chunks.push(data.toString()); }); - readableStream.on("end", () => { - resolve(chunks.join("")); + readableStream.on('end', () => { + resolve(chunks.join('')); }); - readableStream.on("error", reject) + 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/utils/logger.js b/src/utils/logger.js index 9ef06e941..3932769cc 100644 --- a/src/utils/logger.js +++ b/src/utils/logger.js @@ -13,7 +13,7 @@ const options = { timestamp: true }, console: { - level: 'debug', + level: process.env.LOG_LEVEL || 'debug', handleExceptions: true, json: false, colorize: true, @@ -32,7 +32,7 @@ const logger = createLogger({ ) ), transports: [ - new transports.File(options.file), + // new transports.File(options.file), new transports.Console(options.console) ], exitOnError: false // do not exit on handled exceptions From 7f5b093de0bec372bca00206528aa2a68a848000 Mon Sep 17 00:00:00 2001 From: Matias Date: Mon, 27 May 2019 13:48:51 -0300 Subject: [PATCH 04/34] Progress pre master app --- src/models/ecosystem.js | 204 ++++++++++++++++++++++++++- src/schema/mutations/AddTeam.js | 25 +++- src/schema/mutations/DeleteTeam.js | 6 +- src/schema/types/Bot.js | 14 +- src/schema/types/Competition.js | 32 +++++ src/schema/types/DataRange.js | 14 ++ src/schema/types/DataSet.js | 21 +++ src/schema/types/Ecosystem.js | 4 +- src/schema/types/EventHost.js | 17 ++- src/schema/types/Exchange.js | 13 ++ src/schema/types/Module.js | 25 ++++ src/schema/types/Panel.js | 15 ++ src/schema/types/Plotter.js | 26 ++++ src/schema/types/PlotterReference.js | 17 +++ src/schema/types/Product.js | 31 ++++ src/schema/types/Team.js | 6 +- src/schema/types/index.js | 20 ++- 17 files changed, 464 insertions(+), 26 deletions(-) create mode 100644 src/schema/types/Competition.js create mode 100644 src/schema/types/DataRange.js create mode 100644 src/schema/types/DataSet.js create mode 100644 src/schema/types/Exchange.js create mode 100644 src/schema/types/Module.js create mode 100644 src/schema/types/Panel.js create mode 100644 src/schema/types/Plotter.js create mode 100644 src/schema/types/PlotterReference.js create mode 100644 src/schema/types/Product.js diff --git a/src/models/ecosystem.js b/src/models/ecosystem.js index 5856d8f19..36ce90642 100644 --- a/src/models/ecosystem.js +++ b/src/models/ecosystem.js @@ -6,7 +6,7 @@ const ecosystemSchema = new Schema({ type: String, required: true }, - teams: [{ + devTeams: [{ codeName: { type: String, required: true @@ -15,7 +15,7 @@ const ecosystemSchema = new Schema({ type: String, required: true }, - host:{ + host: { url: { type: String, required: true @@ -42,6 +42,10 @@ const ecosystemSchema = new Schema({ type: String, required: true }, + type: { + type: String, + required: false + }, repo: { type: String, required: false @@ -49,9 +53,115 @@ const ecosystemSchema = new Schema({ configFile: { type: String, required: false - } + }, + products: [{ + codeName: { + type: String, + required: true + }, + displayName: { + type: String, + required: true + }, + description: { + type: String, + required: true + }, + dataSets: [{ + codeName: { + type: String, + required: true + }, + type: { + type: String, + required: true + }, + validPeriods: [{ + type: String, + required: true + }], + filePath: { + type: String, + required: true + }, + fileName: { + type: String, + required: true + }, + dataRange: { + filePath: { + type: String, + required: false + }, + fileName: { + type: String, + required: false + }, + }, + }], + exchangeList: [{ + name: { + type: String, + required: true + } + }], + plotter: { + codeName: { + type: String, + required: false + }, + devTeam: { + type: String, + required: false + }, + moduleName: { + type: String, + required: true + }, + } + }] }], plotters: [{ + codeName: { + type: String, + required: true + }, + displayName: { + type: String, + required: true + }, + modules: [{ + codeName: { + type: String, + required: true + }, + moduleName: { + type: String, + required: true + }, + description: { + type: String, + required: false + }, + profilePicture: { + type: String, + required: false + }, + panels: [{ + codeName: { + type: String, + required: true + }, + moduleName: { + type: String, + required: true + }, + event: { + type: String, + required: true + } + }], + }], repo: { type: String, required: true @@ -71,7 +181,7 @@ const ecosystemSchema = new Schema({ type: String, required: true }, - host:{ + host: { url: { type: String, required: true @@ -90,6 +200,52 @@ const ecosystemSchema = new Schema({ } }, competitions: [{ + codeName: { + type: String, + required: true + }, + displayName: { + type: String, + required: true + }, + description: { + type: String, + required: false + }, + startDatetime: { + type: String, + required: false + }, + finishDatetime: { + type: String, + required: false + }, + formula: { + type: String, + required: false + }, + plotter: { + codeName: { + type: String, + required: false + }, + host: { + type: String, + required: false + }, + repo: { + type: String, + required: true + }, + moduleName: { + type: String, + required: true + }, + }, + participants: [{ + type: String, + required: false + }], repo: { type: String, required: true @@ -100,6 +256,46 @@ const ecosystemSchema = new Schema({ } }], plotters: [{ + codeName: { + type: String, + required: true + }, + displayName: { + type: String, + required: true + }, + modules: [{ + codeName: { + type: String, + required: true + }, + moduleName: { + type: String, + required: true + }, + description: { + type: String, + required: false + }, + profilePicture: { + type: String, + required: false + }, + panels: [{ + codeName: { + type: String, + required: true + }, + moduleName: { + type: String, + required: true + }, + event: { + type: String, + required: true + } + }], + }], repo: { type: String, required: true diff --git a/src/schema/mutations/AddTeam.js b/src/schema/mutations/AddTeam.js index 44e6402d0..87e339e94 100644 --- a/src/schema/mutations/AddTeam.js +++ b/src/schema/mutations/AddTeam.js @@ -27,8 +27,8 @@ const resolve = async (parent, { team }, context) => { userEcosystem.authId = context.userId } - for (let i = 0; i < userEcosystem.teams.length; i++) { - const auxTeam = userEcosystem.teams[i]; + for (let i = 0; i < userEcosystem.devTeams.length; i++) { + const auxTeam = userEcosystem.devTeams[i]; if (auxTeam.codeName === team.codeName) { throw new WrongArgumentsError("The team already exist on the user ecosystem.") } @@ -44,11 +44,26 @@ const resolve = async (parent, { team }, context) => { team.host = nodeEndpoint } - let bots= [] - bots.push(team.bot) + let bots = [] + let tradingBot = { + codeName: team.bot.codeName, + displayName: team.bot.displayName, + repo: team.bot.codeName + '-Trading-Bot', + configFile: 'this.bot.config.json' + } + bots.push(tradingBot) + + let simulatorBot = { + codeName: 'simulator-' + team.bot.codeName, + displayName: 'Simulator ' + team.bot.displayName, + repo: 'simulator-' + team.bot.codeName + '-Indicator-Bot', + configFile: 'this.bot.config.json' + } + bots.push(simulatorBot) + team.bots = bots - userEcosystem.teams.push(team) + userEcosystem.devTeams.push(team) await userEcosystem.save() return team } catch (err) { diff --git a/src/schema/mutations/DeleteTeam.js b/src/schema/mutations/DeleteTeam.js index 85f4c403a..dd4061919 100644 --- a/src/schema/mutations/DeleteTeam.js +++ b/src/schema/mutations/DeleteTeam.js @@ -25,13 +25,13 @@ const resolve = async (parent, { team }, context) => { } let teamFound = false - for (let i = 0; i < userEcosystem.teams.length; i++) { - const auxTeam = userEcosystem.teams[i] + for (let i = 0; i < userEcosystem.devTeams.length; i++) { + const auxTeam = userEcosystem.devTeams[i] if (auxTeam.codeName === team.codeName) { logger.debug('deleteTeam -> Team found, removing.') teamFound = true await deleteTeam(team) - userEcosystem.teams.splice(i, 1) + userEcosystem.devTeams.splice(i, 1) await userEcosystem.save() break } diff --git a/src/schema/types/Bot.js b/src/schema/types/Bot.js index 6701c467e..f983f1129 100644 --- a/src/schema/types/Bot.js +++ b/src/schema/types/Bot.js @@ -1,15 +1,25 @@ import { GraphQLObjectType, - GraphQLString + GraphQLString, + GraphQLList } from 'graphql'; +import { ProductType } from './index' + const BotType = new GraphQLObjectType({ name: 'Bot', fields: () => ({ codeName: { type: GraphQLString }, displayName: { type: GraphQLString }, + type: { type: GraphQLString }, repo: { type: GraphQLString }, - configFile: { type: GraphQLString } + configFile: { type: GraphQLString }, + products: { + type: new GraphQLList(ProductType), + resolve(parent) { + return parent.products; + } + }, }), }); diff --git a/src/schema/types/Competition.js b/src/schema/types/Competition.js new file mode 100644 index 000000000..16f42cd4a --- /dev/null +++ b/src/schema/types/Competition.js @@ -0,0 +1,32 @@ +import { + GraphQLObjectType, + GraphQLString, + GraphQLList +} from 'graphql'; + +import { PlotterReferenceType } from './index'; + +const CompetitionType = new GraphQLObjectType({ + name: 'Competition', + fields: () => ({ + codeName: { type: GraphQLString }, + displayName: { type: GraphQLString }, + description: { type: GraphQLString }, + startDatetime: { type: GraphQLString }, + finishDatetime: { type: GraphQLString }, + formula: { type: GraphQLString }, + plotter: { + type: PlotterReferenceType, + resolve(parent) { + return parent.plotter; + } + }, + rules:{ type: new GraphQLList(GraphQLString) }, + prizes:{ type: new GraphQLList(GraphQLString) }, + participants:{ type: new GraphQLList(GraphQLString) }, + repo: { type: GraphQLString }, + configFile: { type: GraphQLString } + }), +}); + +export default CompetitionType; diff --git a/src/schema/types/DataRange.js b/src/schema/types/DataRange.js new file mode 100644 index 000000000..aebeb3cd7 --- /dev/null +++ b/src/schema/types/DataRange.js @@ -0,0 +1,14 @@ +import { + GraphQLObjectType, + GraphQLString +} from 'graphql'; + +const DataRangeType = new GraphQLObjectType({ + name: 'DataRange', + fields: () => ({ + filePath: { type: GraphQLString }, + fileName: { type: GraphQLString } + }), +}); + +export default DataRangeType; diff --git a/src/schema/types/DataSet.js b/src/schema/types/DataSet.js new file mode 100644 index 000000000..a96ff8ff5 --- /dev/null +++ b/src/schema/types/DataSet.js @@ -0,0 +1,21 @@ +import { + GraphQLObjectType, + GraphQLString, + GraphQLList +} from 'graphql'; + +import { DataRangeType } from './index' + +const DataSetType = new GraphQLObjectType({ + name: 'DataSet', + fields: () => ({ + codeName: { type: GraphQLString }, + type: { type: GraphQLString }, + validPeriods: { type: new GraphQLList(GraphQLString) }, + filePath: { type: GraphQLString }, + fileName: { type: GraphQLString }, + dataRange: { type: DataRangeType } + }), +}); + +export default DataSetType; diff --git a/src/schema/types/Ecosystem.js b/src/schema/types/Ecosystem.js index d31cbbde2..8fabc5847 100644 --- a/src/schema/types/Ecosystem.js +++ b/src/schema/types/Ecosystem.js @@ -13,10 +13,10 @@ const EcosystemType = new GraphQLObjectType({ id: { type: GraphQLID }, authId: { type: GraphQLString }, userName: { type: GraphQLString }, // TODO this field is temporary, it should be used the user module query - teams: { + devTeams: { type: new GraphQLList(TeamType), resolve(parent) { - return parent.teams; + return parent.devTeams; } }, hosts: { diff --git a/src/schema/types/EventHost.js b/src/schema/types/EventHost.js index 571bab36e..c951dabba 100644 --- a/src/schema/types/EventHost.js +++ b/src/schema/types/EventHost.js @@ -4,26 +4,31 @@ import { GraphQLList } from 'graphql'; -import { BotType } from './index'; +import { CompetitionType, PlotterType, HostType } from './index'; const EventHostType = new GraphQLObjectType({ name: 'EventHost', fields: () => ({ codeName: { type: GraphQLString }, displayName: { type: GraphQLString }, - host: { type: GraphQLString }, + host: { + type: HostType, + resolve(parent) { + return parent.host; + } + }, container: { type: GraphQLString }, accessKey: { type: GraphQLString }, competitions: { - type: new GraphQLList(BotType), + type: new GraphQLList(CompetitionType), resolve(parent) { - return parent.bots; + return parent.competitions; } }, plotters: { - type: new GraphQLList(BotType), + type: new GraphQLList(PlotterType), resolve(parent) { - return parent.bots; + return parent.plotters; } } }), diff --git a/src/schema/types/Exchange.js b/src/schema/types/Exchange.js new file mode 100644 index 000000000..b80b7ec03 --- /dev/null +++ b/src/schema/types/Exchange.js @@ -0,0 +1,13 @@ +import { + GraphQLObjectType, + GraphQLString +} from 'graphql'; + +const ExchangeType = new GraphQLObjectType({ + name: 'Exchange', + fields: () => ({ + name: { type: GraphQLString } + }), +}); + +export default ExchangeType; diff --git a/src/schema/types/Module.js b/src/schema/types/Module.js new file mode 100644 index 000000000..eb1addabb --- /dev/null +++ b/src/schema/types/Module.js @@ -0,0 +1,25 @@ +import { + GraphQLObjectType, + GraphQLString, + GraphQLList +} from 'graphql' + +import { PanelType } from './index' + +const ModuleType = new GraphQLObjectType({ + name: 'Module', + fields: () => ({ + codeName: { type: GraphQLString }, + moduleName: { type: GraphQLString }, + description: { type: GraphQLString }, + profilePicture: { type: GraphQLString }, + panels: { + type: new GraphQLList(PanelType), + resolve(parent) { + return parent.panels; + } + } + }), +}); + +export default ModuleType; diff --git a/src/schema/types/Panel.js b/src/schema/types/Panel.js new file mode 100644 index 000000000..a3e19b4aa --- /dev/null +++ b/src/schema/types/Panel.js @@ -0,0 +1,15 @@ +import { + GraphQLObjectType, + GraphQLString +} from 'graphql'; + +const PanelType = new GraphQLObjectType({ + name: 'Panel', + fields: () => ({ + codeName: { type: GraphQLString }, + moduleName: { type: GraphQLString }, + event: { type: GraphQLString } + }), +}); + +export default PanelType; diff --git a/src/schema/types/Plotter.js b/src/schema/types/Plotter.js new file mode 100644 index 000000000..b0b672625 --- /dev/null +++ b/src/schema/types/Plotter.js @@ -0,0 +1,26 @@ +import { + GraphQLObjectType, + GraphQLString, + GraphQLList +} from 'graphql'; + +import { ModuleType } from './index' + +const PlotterType = new GraphQLObjectType({ + name: 'Plotter', + fields: () => ({ + codeName: { type: GraphQLString }, + displayName: { type: GraphQLString }, + host: { type: GraphQLString }, + modules: { + type: new GraphQLList(ModuleType), + resolve(parent) { + return parent.modules; + } + }, + repo: { type: GraphQLString }, + configFile: { type: GraphQLString } + }), +}); + +export default PlotterType; diff --git a/src/schema/types/PlotterReference.js b/src/schema/types/PlotterReference.js new file mode 100644 index 000000000..1c6007032 --- /dev/null +++ b/src/schema/types/PlotterReference.js @@ -0,0 +1,17 @@ +import { + GraphQLObjectType, + GraphQLString +} from 'graphql'; + +const PlotterReferenceType = new GraphQLObjectType({ + name: 'PlotterReference', + fields: () => ({ + codeName: { type: GraphQLString }, + host: { type: GraphQLString }, + devTeam: { type: GraphQLString }, + moduleName: { type: GraphQLString }, + repo: { type: GraphQLString } + }), +}); + +export default PlotterReferenceType; diff --git a/src/schema/types/Product.js b/src/schema/types/Product.js new file mode 100644 index 000000000..1494872c3 --- /dev/null +++ b/src/schema/types/Product.js @@ -0,0 +1,31 @@ +import { + GraphQLObjectType, + GraphQLString, + GraphQLList +} from 'graphql'; + +import { ExchangeType, DataSetType, PlotterReferenceType } from './index' + +const ProductType = new GraphQLObjectType({ + name: 'Product', + fields: () => ({ + codeName: { type: GraphQLString }, + displayName: { type: GraphQLString }, + description: { type: GraphQLString }, + dataSets: { + type: new GraphQLList(DataSetType), + resolve(parent) { + return parent.dataSets; + } + }, + exchangeList: { + type: new GraphQLList(ExchangeType), + resolve(parent) { + return parent.exchangeList; + } + }, + plotter: { type: PlotterReferenceType } + }), +}); + +export default ProductType; diff --git a/src/schema/types/Team.js b/src/schema/types/Team.js index e39d0aa5b..00607f192 100644 --- a/src/schema/types/Team.js +++ b/src/schema/types/Team.js @@ -4,7 +4,7 @@ import { GraphQLList } from 'graphql'; -import { BotType, HostType } from './index'; +import { BotType, PlotterType, HostType } from './index'; const TeamType = new GraphQLObjectType({ name: 'Team', @@ -24,9 +24,9 @@ const TeamType = new GraphQLObjectType({ } }, plotters: { - type: new GraphQLList(BotType), + type: new GraphQLList(PlotterType), resolve(parent) { - return parent.bots; + return parent.plotters; } } }), diff --git a/src/schema/types/index.js b/src/schema/types/index.js index 17d90c59a..31708e9b2 100644 --- a/src/schema/types/index.js +++ b/src/schema/types/index.js @@ -4,6 +4,15 @@ import EventHostType from './EventHost'; import HostType from './Host'; import EcosystemType from './Ecosystem'; import FileType from './File'; +import PanelType from './Panel'; +import ModuleType from './Module'; +import PlotterType from './Plotter'; +import CompetitionType from './Competition'; +import PlotterReferenceType from './PlotterReference'; +import ProductType from './Product'; +import DataSetType from './DataSet'; +import DataRangeType from './DataRange'; +import ExchangeType from './Exchange'; export { TeamType, @@ -11,5 +20,14 @@ export { EventHostType, HostType, EcosystemType, - FileType + FileType, + PanelType, + ModuleType, + PlotterType, + CompetitionType, + PlotterReferenceType, + ProductType, + DataSetType, + DataRangeType, + ExchangeType }; From 2c588cba717cf5e1afbf71afea01a29fb0068d91 Mon Sep 17 00:00:00 2001 From: Matias Date: Wed, 29 May 2019 11:29:37 -0300 Subject: [PATCH 05/34] Adding stable version --- src/config/ecosystem.json | 886 +++++++++++++++++++++++++++ src/models/ecosystem.js | 30 +- src/schema/mutations/AddTeam.js | 36 +- src/schema/mutations/DeleteTeam.js | 6 +- src/schema/queries/Ecosystem.js | 2 +- src/schema/queries/FileContent.js | 20 +- src/schema/types/Bot.js | 1 + src/schema/types/PlotterReference.js | 11 +- src/schema/types/input/DeleteTeam.js | 18 + src/schema/types/input/Team.js | 3 +- src/schema/types/input/index.js | 6 +- src/storage/CopyBot.js | 2 +- src/storage/CopyBots.js | 22 + src/storage/CopySimulator.js | 2 +- src/storage/CreateTeam.js | 27 - 15 files changed, 1005 insertions(+), 67 deletions(-) create mode 100644 src/config/ecosystem.json create mode 100644 src/schema/types/input/DeleteTeam.js create mode 100644 src/storage/CopyBots.js delete mode 100644 src/storage/CreateTeam.js diff --git a/src/config/ecosystem.json b/src/config/ecosystem.json new file mode 100644 index 000000000..1c76887be --- /dev/null +++ b/src/config/ecosystem.json @@ -0,0 +1,886 @@ +{ + "authId": "", + "devTeams": [ + { + "codeName": "AAMasters", + "displayName": "Masters", + "host": { + "url": "http://localhost:4100/", + "storage": "https://aadevelop.blob.core.windows.net", + "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" + }, + "bots": [ + { + "codeName": "AAOlivia", + "displayName": "Olivia", + "type": "Indicator", + "profilePicture": "Olivia.png", + "repo": "AAOlivia-Indicator-Bot", + "configFile": "this.bot.config.json", + "products": [ + { + "codeName": "Candles", + "displayName": "Candles", + "description": "Two sets of files per period that contains candles.", + "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/AAOlivia.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Candles/Multi-Period-Market/@Period", + "fileName": "@AssetA_@AssetB.json" + }, + { + "codeName": "Multi-Period-Daily", + "type": "Daily Files", + "validPeriods": [ + "45-min", + "40-min", + "30-min", + "20-min", + "15-min", + "10-min", + "05-min", + "04-min", + "03-min", + "02-min", + "01-min" + ], + "filePath": "AAMasters/AAOlivia.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Candles/Multi-Period-Daily/@Period/@Year/@Month/@Day", + "fileName": "@AssetA_@AssetB.json", + "dataRange": { + "filePath": "AAMasters/AAOlivia.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Candles/Multi-Period-Daily", + "fileName": "Data.Range.@AssetA_@AssetB.json" + } + } + ], + "exchangeList": [ + { + "name": "Poloniex" + }, + { + "name": "Coss" + } + ], + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersCandlesVolumes", + "moduleName": "Candles" + } + }, + { + "codeName": "Volumes", + "displayName": "Buy and Sell Volumes", + "description": "Two sets of files per period that contains buy and sell volumes.", + "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/AAOlivia.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Volumes/Multi-Period-Market/@Period", + "fileName": "@AssetA_@AssetB.json" + }, + { + "codeName": "Multi-Period-Daily", + "type": "Daily Files", + "validPeriods": [ + "45-min", + "40-min", + "30-min", + "20-min", + "15-min", + "10-min", + "05-min", + "04-min", + "03-min", + "02-min", + "01-min" + ], + "filePath": "AAMasters/AAOlivia.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Volumes/Multi-Period-Daily/@Period/@Year/@Month/@Day", + "fileName": "@AssetA_@AssetB.json", + "dataRange": { + "filePath": "AAMasters/AAOlivia.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Volumes/Multi-Period-Daily", + "fileName": "Data.Range.@AssetA_@AssetB.json" + } + } + ], + "exchangeList": [ + { + "name": "Poloniex" + }, + { + "name": "Coss" + } + ], + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersCandlesVolumes", + "moduleName": "Volumes" + } + } + ] + }, + { + "codeName": "AATom", + "displayName": "Tom", + "type": "Indicator", + "profilePicture": "Tom.png", + "repo": "AATom-Indicator-Bot", + "configFile": "this.bot.config.json", + "products": [ + { + "codeName": "Candle-Stairs", + "displayName": "Candle-Stairs Patters", + "description": "Two sets of files per period that contains candle-stairs patterns.", + "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/AATom.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Candle-Stairs/Multi-Period-Market/@Period", + "fileName": "@AssetA_@AssetB.json" + }, + { + "codeName": "Multi-Period-Daily", + "type": "Daily Files", + "validPeriods": [ + "45-min", + "40-min", + "30-min", + "20-min", + "15-min", + "10-min", + "05-min", + "04-min", + "03-min", + "02-min", + "01-min" + ], + "filePath": "AAMasters/AATom.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Candle-Stairs/Multi-Period-Daily/@Period/@Year/@Month/@Day", + "fileName": "@AssetA_@AssetB.json", + "dataRange": { + "filePath": "AAMasters/AATom.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Candle-Stairs/Multi-Period-Daily", + "fileName": "Data.Range.@AssetA_@AssetB.json" + } + } + ], + "exchangeList": [ + { + "name": "Poloniex" + }, + { + "name": "Coss" + } + ], + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersStairsPatterns", + "moduleName": "CandleStairs" + } + }, + { + "codeName": "Volume-Stairs", + "displayName": "Volume-Stairs Patters", + "description": "Two sets of files per period that contains volume-stairs patterns.", + "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/AATom.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Volume-Stairs/Multi-Period-Market/@Period", + "fileName": "@AssetA_@AssetB.json" + }, + { + "codeName": "Multi-Period-Daily", + "type": "Daily Files", + "validPeriods": [ + "45-min", + "40-min", + "30-min", + "20-min", + "15-min", + "10-min", + "05-min", + "04-min", + "03-min", + "02-min", + "01-min" + ], + "filePath": "AAMasters/AATom.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Volume-Stairs/Multi-Period-Daily/@Period/@Year/@Month/@Day", + "fileName": "@AssetA_@AssetB.json", + "dataRange": { + "filePath": "AAMasters/AATom.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Volume-Stairs/Multi-Period-Daily", + "fileName": "Data.Range.@AssetA_@AssetB.json" + } + } + ], + "exchangeList": [ + { + "name": "Poloniex" + }, + { + "name": "Coss" + } + ], + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersStairsPatterns", + "moduleName": "VolumeStairs" + } + } + ] + }, + { + "codeName": "AAChris", + "displayName": "Chris", + "type": "Indicator", + "profilePicture": "Chris.png", + "repo": "AAChris-Indicator-Bot", + "configFile": "this.bot.config.json", + "products": [ + { + "codeName": "Bollinger-Bands", + "displayName": "Bollinger Bands", + "description": "Bollinger Bands", + "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/AAChris.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Bollinger-Bands/Multi-Period-Market/@Period", + "fileName": "@AssetA_@AssetB.json" + }, + { + "codeName": "Multi-Period-Daily", + "type": "Daily Files", + "validPeriods": [ + "45-min", + "40-min", + "30-min", + "20-min", + "15-min", + "10-min", + "05-min", + "04-min", + "03-min", + "02-min", + "01-min" + ], + "filePath": "AAMasters/AAChris.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Bollinger-Bands/Multi-Period-Daily/@Period/@Year/@Month/@Day", + "fileName": "@AssetA_@AssetB.json", + "dataRange": { + "filePath": "AAMasters/AAChris.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Bollinger-Bands/Multi-Period-Daily", + "fileName": "Data.Range.@AssetA_@AssetB.json" + } + } + ], + "exchangeList": [ + { + "name": "Poloniex" + }, + { + "name": "Coss" + } + ], + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersBollingerBands", + "moduleName": "BollingerBands" + } + }, + { + "codeName": "Percentage-Bandwidth", + "displayName": "Percentage Bandwidth", + "description": "Percentage Bandwidth", + "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/AAChris.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Percentage-Bandwidth/Multi-Period-Market/@Period", + "fileName": "@AssetA_@AssetB.json" + }, + { + "codeName": "Multi-Period-Daily", + "type": "Daily Files", + "validPeriods": [ + "45-min", + "40-min", + "30-min", + "20-min", + "15-min", + "10-min", + "05-min", + "04-min", + "03-min", + "02-min", + "01-min" + ], + "filePath": "AAMasters/AAChris.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Percentage-Bandwidth/Multi-Period-Daily/@Period/@Year/@Month/@Day", + "fileName": "@AssetA_@AssetB.json", + "dataRange": { + "filePath": "AAMasters/AAChris.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Percentage-Bandwidth/Multi-Period-Daily", + "fileName": "Data.Range.@AssetA_@AssetB.json" + } + } + ], + "exchangeList": [ + { + "name": "Poloniex" + }, + { + "name": "Coss" + } + ], + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersBollingerBands", + "moduleName": "PercentageBandwidth" + } + } + ] + }, + { + "codeName": "AAPaula", + "displayName": "Paula", + "type": "Indicator", + "profilePicture": "Paula.png", + "repo": "AAPaula-Indicator-Bot", + "configFile": "this.bot.config.json", + "products": [ + { + "codeName": "Bollinger-Channels", + "displayName": "Bollinger-Channels", + "description": "Two sets of files per period that contains band-channel patterns.", + "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/AAPaula.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Bollinger-Channels/Multi-Period-Market/@Period", + "fileName": "@AssetA_@AssetB.json" + }, + { + "codeName": "Multi-Period-Daily", + "type": "Daily Files", + "validPeriods": [ + "45-min", + "40-min", + "30-min", + "20-min", + "15-min", + "10-min", + "05-min", + "04-min", + "03-min", + "02-min", + "01-min" + ], + "filePath": "AAMasters/AAPaula.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Bollinger-Channels/Multi-Period-Daily/@Period/@Year/@Month/@Day", + "fileName": "@AssetA_@AssetB.json", + "dataRange": { + "filePath": "AAMasters/AAPaula.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Bollinger-Channels/Multi-Period-Daily", + "fileName": "Data.Range.@AssetA_@AssetB.json" + } + } + ], + "exchangeList": [ + { + "name": "Poloniex" + }, + { + "name": "Coss" + } + ], + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersBollingerChannels", + "moduleName": "BollingerChannels" + } + }, + { + "codeName": "Bollinger-Sub-Channels", + "displayName": "Bollinger-Sub-Channels", + "description": "Bollinger Sub Channels with slope.", + "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/AAPaula.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Bollinger-Sub-Channels/Multi-Period-Market/@Period", + "fileName": "@AssetA_@AssetB.json" + }, + { + "codeName": "Multi-Period-Daily", + "type": "Daily Files", + "validPeriods": [ + "45-min", + "40-min", + "30-min", + "20-min", + "15-min", + "10-min", + "05-min", + "04-min", + "03-min", + "02-min", + "01-min" + ], + "filePath": "AAMasters/AAPaula.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Bollinger-Sub-Channels/Multi-Period-Daily/@Period/@Year/@Month/@Day", + "fileName": "@AssetA_@AssetB.json", + "dataRange": { + "filePath": "AAMasters/AAPaula.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/Bollinger-Sub-Channels/Multi-Period-Daily", + "fileName": "Data.Range.@AssetA_@AssetB.json" + } + } + ], + "exchangeList": [ + { + "name": "Poloniex" + }, + { + "name": "Coss" + } + ], + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersBollingerChannels", + "moduleName": "BollingerSubChannels" + } + } + ] + } + ], + "plotters": [ + { + "displayName": "Plotters Candles & Volumes", + "codeName": "PlottersCandlesVolumes", + "devTeam": "AAMasters", + "modules": [ + { + "codeName": "Candles", + "moduleName": "Candles", + "description": "Plots traditional red and green candles.", + "profilePicture": "Candles.png", + "panels": [ + { + "codeName": "CandlePanel", + "moduleName": "CandlePanel", + "event": "Current Candle Changed" + } + ] + }, + { + "codeName": "Volumes", + "description": "Plots volumes separated in buy and sell.", + "moduleName": "Volumes", + "profilePicture": "Volumes.png", + "panels": [ + { + "codeName": "VolumePanel", + "moduleName": "VolumePanel", + "event": "Current Volume Changed" + } + ] + } + ], + "repo": "Plotters-Candles-Volumes", + "configFile": "this.plotter.config.json" + }, + { + "displayName": "Plotters Stairs Patterns", + "codeName": "PlottersStairsPatterns", + "devTeam": "AAMasters", + "modules": [ + { + "codeName": "CandleStairs", + "moduleName": "CandleStairs", + "description": "Plots Candle Stairs patterns.", + "profilePicture": "CandleStairs.png", + "panels": [] + }, + { + "codeName": "VolumeStairs", + "description": "Plots Volume Stairs patterns.", + "moduleName": "VolumeStairs", + "profilePicture": "VolumeStairs.png", + "panels": [] + } + ], + "repo": "Plotters-Stairs-Patterns", + "configFile": "this.plotter.config.json" + }, + { + "displayName": "Plotters Trading", + "codeName": "PlottersTrading", + "devTeam": "AAMasters", + "modules": [ + { + "codeName": "Details", + "moduleName": "Details", + "description": "Plots trading decitions in an easy to understand way.", + "panels": [] + }, + { + "codeName": "History", + "moduleName": "History", + "description": "Plots trading history.", + "profilePicture": "History.png", + "panels": [] + } + ], + "repo": "Plotters-Trading", + "configFile": "this.plotter.config.json" + }, + { + "displayName": "Plotters Bollinger Bands", + "codeName": "PlottersBollingerBands", + "devTeam": "AAMasters", + "modules": [ + { + "codeName": "BollingerBands", + "moduleName": "BollingerBands", + "description": "Plots Bollinger Bands.", + "profilePicture": "BollingerBands.png", + "panels": [ + { + "codeName": "BollingerBandsPanel", + "moduleName": "BollingerBandsPanel", + "event": "Current Band Changed" + } + ] + }, + { + "codeName": "PercentageBandwidth", + "moduleName": "PercentageBandwidth", + "description": "Plots Bollinger Bands.", + "profilePicture": "PercentageBandwidth.png", + "panels": [] + } + ], + "repo": "Plotters-Bollinger-Bands", + "configFile": "this.plotter.config.json" + }, + { + "displayName": "Plotters Trading Simulation", + "codeName": "PlottersTradingSimulation", + "devTeam": "AAMasters", + "modules": [ + { + "codeName": "TradingSimulation", + "moduleName": "TradingSimulation", + "description": "Plots Bollinger Records.", + "profilePicture": "TradingSimulation.png", + "panels": [ + { + "codeName": "TradingSimulationPanel", + "moduleName": "TradingSimulationPanel", + "event": "Current Record Changed" + } + ] + }, + { + "codeName": "Conditions", + "moduleName": "Conditions", + "description": "Plots Simulation Conditions.", + "profilePicture": "Conditions.png", + "panels": [] + }, + { + "codeName": "Strategies", + "moduleName": "Strategies", + "description": "Plots Strategies.", + "profilePicture": "Strategies.png", + "panels": [] + }, + { + "codeName": "Trades", + "moduleName": "Trades", + "description": "Plots Trades.", + "profilePicture": "Trades.png", + "panels": [] + }, + { + "codeName": "SimulationExecution", + "moduleName": "SimulationExecution", + "description": "Plots Simulation Execution.", + "profilePicture": "SimulationExecution.png", + "panels": [] + } + ], + "repo": "Plotters-Trading-Simulation", + "configFile": "this.plotter.config.json" + }, + { + "displayName": "Plotters Bollinger Channels", + "codeName": "PlottersBollingerChannels", + "devTeam": "AAMasters", + "modules": [ + { + "codeName": "BollingerChannels", + "moduleName": "BollingerChannels", + "description": "Plots Bollinger Channels.", + "profilePicture": "BollingerChannels.png", + "panels": [ + { + "codeName": "ChannelPanel", + "moduleName": "ChannelPanel", + "event": "Current Channel Changed" + } + ] + }, + { + "codeName": "BollingerSubChannels", + "moduleName": "BollingerSubChannels", + "description": "Plots Bollinger Sub-Channels.", + "profilePicture": "BollingerSubChannels.png", + "panels": [ + { + "codeName": "SubChannelPanel", + "moduleName": "SubChannelPanel", + "event": "Current Sub-Channel Changed" + } + ] + } + ], + "repo": "Plotters-Bollinger-Channels", + "configFile": "this.plotter.config.json" + } + ] + }, + { + "codeName": "AAVikings", + "displayName": "Vikings", + "host": { + "url": "http://localhost:4100/", + "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" + }, + "bots": [ + { + "codeName": "AAGauss", + "displayName": "Gauss", + "type": "Indicator", + "profilePicture": "Gauss.png", + "repo": "AAGauss-Indicator-Bot", + "configFile": "this.bot.config.json", + "products": [ + { + "codeName": "LRCPoints", + "displayName": "LRCPoints", + "description": "Two sets of files per period that contains LRC points.", + "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": "AAVikings/AAGauss.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/LRC-Points/Multi-Period-Market/@Period", + "fileName": "@AssetA_@AssetB.json" + }, + { + "codeName": "Multi-Period-Daily", + "type": "Daily Files", + "validPeriods": [ + "45-min", + "40-min", + "30-min", + "20-min", + "15-min", + "10-min", + "05-min", + "04-min", + "03-min", + "02-min", + "01-min" + ], + "filePath": "AAVikings/AAGauss.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/LRC-Points/Multi-Period-Daily/@Period/@Year/@Month/@Day", + "fileName": "@AssetA_@AssetB.json", + "dataRange": { + "filePath": "AAVikings/AAGauss.1.0/AACloud.1.1/@Exchange/dataSet.V1/Output/LRC-Points/Multi-Period-Daily", + "fileName": "Data.Range.@AssetA_@AssetB.json" + } + } + ], + "exchangeList": [ + { + "name": "Poloniex" + }, + { + "name": "Coss" + } + ], + "plotter": { + "devTeam": "AAVikings", + "codeName": "PlotterLinearRegressionCurve", + "moduleName": "LRC" + } + } + ] + } + ], + "plotters": [ + { + "displayName": "Plotter LRC", + "codeName": "PlotterLinearRegressionCurve", + "devTeam": "AAVikings", + "modules": [ + { + "codeName": "LRC", + "moduleName": "LRC", + "description": "Plots the linear regression curve.", + "profilePicture": "LRC.png", + "panels": [ + { + "codeName": "ChannelPanel", + "moduleName": "ChannelPanel", + "event": "Current LRC Changed" + } + ] + } + ], + "repo": "Plotters-Linear-Regression-Curve", + "configFile": "this.plotter.config.json" + } + ] + } + ], + "hosts": [ + { + "codeName": "AAMasters", + "displayName": "AA Arena", + "host": { + "url": "http://localhost:4100/", + "storage": "https://aadevelop.blob.core.windows.net", + "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" + }, + "competitions": [ + { + "codeName": "Weekend-Deathmatch", + "description": "First AA Internal Competition. Bots will be trading during 3 days at the USDT/BTC market of Poloniex to see who can make the gratest return over investment (ROI).", + "displayName": "AA Internal Challenge", + "startDatetime": "2018-01-01T16:00:00.000Z", + "finishDatetime": "2018-01-02T16:00:00.000Z", + "formula": "ROI", + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersROI", + "host": { + "url": "http://localhost:4100/", + "storage": "https://aadevelop.blob.core.windows.net", + "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" + }, + "moduleName": "CombinedProfits", + "repo": "Plotters-ROI" + }, + "rules": [], + "prizes": [], + "participants": [], + "repo": "Weekend-Deathmatch", + "configFile": "this.competition.config.json" + } + ], + "plotters": [ + { + "displayName": "Plotters ROI", + "codeName": "PlottersROI", + "devTeam": "AAMasters", + "modules": [ + { + "codeName": "CombinedProfits", + "moduleName": "CombinedProfits", + "description": "Plots participants of a competition by the ROI each one had over time.", + "panels": [] + } + ], + "repo": "Plotters-ROI", + "configFile": "this.plotter.config.json" + } + ] + } + ] +} diff --git a/src/models/ecosystem.js b/src/models/ecosystem.js index 36ce90642..827a3ae42 100644 --- a/src/models/ecosystem.js +++ b/src/models/ecosystem.js @@ -44,7 +44,11 @@ const ecosystemSchema = new Schema({ }, type: { type: String, - required: false + required: true + }, + profilePicture: { + type: String, + required: true }, repo: { type: String, @@ -225,13 +229,31 @@ const ecosystemSchema = new Schema({ required: false }, plotter: { + devTeam: { + type: String, + required: true + }, codeName: { type: String, - required: false + required: true }, host: { - type: String, - required: false + url: { + type: String, + required: true + }, + storage: { + type: String, + required: true + }, + container: { + type: String, + required: true + }, + accessKey: { + type: String, + required: true + } }, repo: { type: String, diff --git a/src/schema/mutations/AddTeam.js b/src/schema/mutations/AddTeam.js index 87e339e94..81030d41f 100644 --- a/src/schema/mutations/AddTeam.js +++ b/src/schema/mutations/AddTeam.js @@ -3,12 +3,12 @@ import { AuthenticationError, WrongArgumentsError } from '../../errors' import { TeamType } from '../types' import { Ecosystem } from '../../models' import { TeamInputType } from '../types/input' -import { createTeam } from '../../storage/CreateTeam' +import { copyBots } from '../../storage/CopyBots' export const args = { team: { type: TeamInputType } } const resolve = async (parent, { team }, context) => { - logger.debug('addTeam -> Entering Fuction.') + logger.debug('addTeam -> Entering Function.') if (!context.userId) { throw new AuthenticationError() @@ -34,35 +34,27 @@ const resolve = async (parent, { team }, context) => { } } - let accessKey = await createTeam(team) + let storage = require ('../../storage/providers/AzureStorage') // Default value + if (process.env.STORAGE_PROVIDER === 'Minio') { + storage = require ('../../storage/providers/MinioStorage') + } + + 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, container: team.codeName, accessKey: accessKey, } team.host = nodeEndpoint } - let bots = [] - let tradingBot = { - codeName: team.bot.codeName, - displayName: team.bot.displayName, - repo: team.bot.codeName + '-Trading-Bot', - configFile: 'this.bot.config.json' - } - bots.push(tradingBot) - - let simulatorBot = { - codeName: 'simulator-' + team.bot.codeName, - displayName: 'Simulator ' + team.bot.displayName, - repo: 'simulator-' + team.bot.codeName + '-Indicator-Bot', - configFile: 'this.bot.config.json' - } - bots.push(simulatorBot) - - team.bots = bots - userEcosystem.devTeams.push(team) await userEcosystem.save() return team diff --git a/src/schema/mutations/DeleteTeam.js b/src/schema/mutations/DeleteTeam.js index dd4061919..9e8ee3626 100644 --- a/src/schema/mutations/DeleteTeam.js +++ b/src/schema/mutations/DeleteTeam.js @@ -2,13 +2,13 @@ import logger from '../../utils/logger' import { AuthenticationError, WrongArgumentsError } from '../../errors' import { TeamType } from '../types' import { Ecosystem } from '../../models' -import { TeamInputType } from '../types/input' +import { DeleteTeamInputType } from '../types/input' import { deleteTeam } from '../../storage/DeleteTeam' -export const args = { team: { type: TeamInputType } } +export const args = { team: { type: DeleteTeamInputType } } const resolve = async (parent, { team }, context) => { - logger.debug('deleteTeam -> Entering Fuction.') + logger.debug('deleteTeam -> Entering Function.') if (!context.userId) { throw new AuthenticationError() diff --git a/src/schema/queries/Ecosystem.js b/src/schema/queries/Ecosystem.js index ca55b7fce..d6eeafb6f 100644 --- a/src/schema/queries/Ecosystem.js +++ b/src/schema/queries/Ecosystem.js @@ -5,7 +5,7 @@ import { EcosystemType } from '../types' const args = {} const resolve = async (parent, args, context) => { - logger.debug('getEcosystem -> Entering Fuction.') + logger.debug('getEcosystem -> Entering Function.') try { if (!context.userId) { diff --git a/src/schema/queries/FileContent.js b/src/schema/queries/FileContent.js index 910fa8df5..0c5ce7515 100644 --- a/src/schema/queries/FileContent.js +++ b/src/schema/queries/FileContent.js @@ -5,7 +5,7 @@ import { GraphQLString } from 'graphql' export const args = { file: { type: FileInputType } } const resolve = async (parent, { file }, context) => { - logger.debug('getFileContent -> Entering Fuction: ' + file.filePath) + logger.debug('getFileContent -> Entering Function: ' + file.filePath) try { @@ -14,7 +14,23 @@ const resolve = async (parent, { file }, context) => { storage = require('../storage/providers/MinioStorage') } - return storage.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) diff --git a/src/schema/types/Bot.js b/src/schema/types/Bot.js index f983f1129..9ce0405ab 100644 --- a/src/schema/types/Bot.js +++ b/src/schema/types/Bot.js @@ -12,6 +12,7 @@ const BotType = new GraphQLObjectType({ codeName: { type: GraphQLString }, displayName: { type: GraphQLString }, type: { type: GraphQLString }, + profilePicture: { type: GraphQLString }, repo: { type: GraphQLString }, configFile: { type: GraphQLString }, products: { diff --git a/src/schema/types/PlotterReference.js b/src/schema/types/PlotterReference.js index 1c6007032..06baa44f0 100644 --- a/src/schema/types/PlotterReference.js +++ b/src/schema/types/PlotterReference.js @@ -3,12 +3,19 @@ import { GraphQLString } from 'graphql'; +import { HostType } from './index'; + const PlotterReferenceType = new GraphQLObjectType({ name: 'PlotterReference', fields: () => ({ - codeName: { type: GraphQLString }, - host: { type: GraphQLString }, devTeam: { type: GraphQLString }, + codeName: { type: GraphQLString }, + host: { + type: HostType, + resolve(parent) { + return parent.host; + } + }, moduleName: { type: GraphQLString }, repo: { type: GraphQLString } }), diff --git a/src/schema/types/input/DeleteTeam.js b/src/schema/types/input/DeleteTeam.js new file mode 100644 index 000000000..bd7ed63d2 --- /dev/null +++ b/src/schema/types/input/DeleteTeam.js @@ -0,0 +1,18 @@ +import { + GraphQLString, + GraphQLInputObjectType, + GraphQLNonNull +} from 'graphql'; + +import { HostInputType } from './index'; + +const Type = new GraphQLInputObjectType({ + name: 'DeleteTeamInput', + description: 'Payload for team input', + fields: () => ({ + codeName: { type: new GraphQLNonNull(GraphQLString) }, + host: { type: HostInputType } + }), +}); + +export default Type; diff --git a/src/schema/types/input/Team.js b/src/schema/types/input/Team.js index 37f49a33c..c4423a55a 100644 --- a/src/schema/types/input/Team.js +++ b/src/schema/types/input/Team.js @@ -1,8 +1,7 @@ import { GraphQLString, GraphQLInputObjectType, - GraphQLNonNull, - GraphQLList + GraphQLNonNull } from 'graphql'; import { BotInputType, HostInputType } from './index'; diff --git a/src/schema/types/input/index.js b/src/schema/types/input/index.js index ec822c308..2e3d3dd55 100644 --- a/src/schema/types/input/index.js +++ b/src/schema/types/input/index.js @@ -2,10 +2,12 @@ import TeamInputType from './Team' import BotInputType from './Bot' import HostInputType from './Host' import FileInputType from './File' +import DeleteTeamInputType from './DeleteTeam' export { TeamInputType, BotInputType, HostInputType, - FileInputType -}; + FileInputType, + DeleteTeamInputType +} diff --git a/src/storage/CopyBot.js b/src/storage/CopyBot.js index 357f1c493..309bb8808 100644 --- a/src/storage/CopyBot.js +++ b/src/storage/CopyBot.js @@ -35,7 +35,7 @@ export const copyBot = async (storage, teamCodeName, botCodeName, botDisplayName await storage.writeFileContent(teamCodeName, newBotPath + '/Trading-Bot/User.Bot.js', code) logger.debug('copyBot completed for bot %s', botCodeName) - + return parsedConfig } 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 new file mode 100644 index 000000000..ede1b352b --- /dev/null +++ b/src/storage/CopyBots.js @@ -0,0 +1,22 @@ +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 ca2466891..1931ee9df 100644 --- a/src/storage/CopySimulator.js +++ b/src/storage/CopySimulator.js @@ -55,7 +55,7 @@ export const copySimulator = async (storage, teamCodeName, botCodeName, botDispl await storage.writeFileContent(teamCodeName, newSimulatorPath + '/Multi-Period-Market/User.Bot.js', codeMarket) logger.debug('copySimulator completed for bot %s', newBotCodeName) - + return parsedConfig } catch (err) { logger.error('copySimulator error for bot: %s. %s', botCodeName, err) throw err diff --git a/src/storage/CreateTeam.js b/src/storage/CreateTeam.js deleted file mode 100644 index a2833250d..000000000 --- a/src/storage/CreateTeam.js +++ /dev/null @@ -1,27 +0,0 @@ -import logger from '../utils/logger' -import { copyBot } from './CopyBot' -import { copySimulator } from './CopySimulator' - -export const createTeam = async (team) => { - try { - logger.debug('createTeam started for team %s', team.codeName) - - let storage = require ('./providers/AzureStorage') // Default value - if (process.env.STORAGE_PROVIDER === 'Minio') { - storage = require ('./providers/MinioStorage') - } - - let containerName = team.codeName - await storage.createContainer(containerName) - let accessKey = storage.createPrivateAccessKey(containerName, "WRITE", 365) - - await copyBot(storage, team.codeName, team.bot.codeName, team.bot.displayName) - await copySimulator(storage, team.codeName, team.bot.codeName, team.bot.displayName) - - return accessKey - - } catch (err) { - logger.error('createTeam error for teamCodeName: %s, error: %s', team.bot.codeName, err.stack) - throw err - } -} From 9f227257741e3bbe30141669b512fce1ca841c2b Mon Sep 17 00:00:00 2001 From: Matias Date: Wed, 29 May 2019 12:50:41 -0300 Subject: [PATCH 06/34] Adding default provider to dev server --- docker-compose.prod.yml | 2 +- src/config/ecosystem.json | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 9fd6b338c..7ec7ba765 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -14,7 +14,7 @@ services: - /var/www/app/node_modules working_dir: /var/www/app restart: always - command: npm start + command: node . expose: - 4004 network_mode: webproxy diff --git a/src/config/ecosystem.json b/src/config/ecosystem.json index 1c76887be..138bb6cc5 100644 --- a/src/config/ecosystem.json +++ b/src/config/ecosystem.json @@ -5,7 +5,7 @@ "codeName": "AAMasters", "displayName": "Masters", "host": { - "url": "http://localhost:4100/", + "url": "https://platform-api-dev.superalgos.org/g", "storage": "https://aadevelop.blob.core.windows.net", "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" @@ -725,7 +725,7 @@ "codeName": "AAVikings", "displayName": "Vikings", "host": { - "url": "http://localhost:4100/", + "url": "https://platform-api-dev.superalgos.org/g", "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" @@ -832,7 +832,7 @@ "codeName": "AAMasters", "displayName": "AA Arena", "host": { - "url": "http://localhost:4100/", + "url": "https://platform-api-dev.superalgos.org/g", "storage": "https://aadevelop.blob.core.windows.net", "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" @@ -849,7 +849,7 @@ "devTeam": "AAMasters", "codeName": "PlottersROI", "host": { - "url": "http://localhost:4100/", + "url": "https://platform-api-dev.superalgos.org/g", "storage": "https://aadevelop.blob.core.windows.net", "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" From 1e83ca9f86c5886e4c17504aff86ab26fe702138 Mon Sep 17 00:00:00 2001 From: Matias Date: Wed, 29 May 2019 12:54:31 -0300 Subject: [PATCH 07/34] Fixing typo on the url --- src/config/ecosystem.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/config/ecosystem.json b/src/config/ecosystem.json index 138bb6cc5..656159ce7 100644 --- a/src/config/ecosystem.json +++ b/src/config/ecosystem.json @@ -5,7 +5,7 @@ "codeName": "AAMasters", "displayName": "Masters", "host": { - "url": "https://platform-api-dev.superalgos.org/g", + "url": "https://platform-api-dev.superalgos.org/", "storage": "https://aadevelop.blob.core.windows.net", "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" @@ -725,7 +725,7 @@ "codeName": "AAVikings", "displayName": "Vikings", "host": { - "url": "https://platform-api-dev.superalgos.org/g", + "url": "https://platform-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" @@ -832,7 +832,7 @@ "codeName": "AAMasters", "displayName": "AA Arena", "host": { - "url": "https://platform-api-dev.superalgos.org/g", + "url": "https://platform-api-dev.superalgos.org/", "storage": "https://aadevelop.blob.core.windows.net", "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" @@ -849,7 +849,7 @@ "devTeam": "AAMasters", "codeName": "PlottersROI", "host": { - "url": "https://platform-api-dev.superalgos.org/g", + "url": "https://platform-api-dev.superalgos.org/", "storage": "https://aadevelop.blob.core.windows.net", "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" From 8afa9f36f523533ad76dd67902f53adadd9a30ab Mon Sep 17 00:00:00 2001 From: Matias Date: Tue, 4 Jun 2019 20:17:16 -0300 Subject: [PATCH 08/34] ALGO node progress --- .env.local | 17 +- package-lock.json | 1126 ++++++++++++- package.json | 10 +- src/models/ecosystem.js | 6 +- src/schema/mutations/AddTeam.js | 27 +- src/schema/mutations/CreateFile.js | 28 + src/schema/mutations/DeleteTeam.js | 4 +- src/schema/mutations/index.js | 16 +- src/schema/queries/FileContent.js | 29 +- src/schema/queries/PlotterCode.js | 46 + src/schema/queries/index.js | 4 +- src/storage/CopyBot.js | 35 +- src/storage/CopyBots.js | 22 - src/storage/CopySimulator.js | 24 +- src/storage/DeleteTeam.js | 18 - src/storage/providers/AzureStorage.js | 80 +- src/storage/providers/MinioStorage.js | 99 ++ src/storage/templates/Simulator/Commons.js | 1456 +++++++++++++++++ .../templates/Simulator/User.Bot.Daily.js | 468 ++++++ .../templates/Simulator/User.Bot.Market.js | 441 +++++ .../templates/Simulator/this.bot.config.json | 394 +++++ src/storage/templates/TradingBot/User.Bot.js | 764 +++++++++ .../templates/TradingBot/this.bot.config.json | 248 +++ src/utils/readFile.js | 14 + test/minioTest.js | 46 + 25 files changed, 5204 insertions(+), 218 deletions(-) create mode 100644 src/schema/mutations/CreateFile.js create mode 100644 src/schema/queries/PlotterCode.js delete mode 100644 src/storage/CopyBots.js delete mode 100644 src/storage/DeleteTeam.js create mode 100644 src/storage/providers/MinioStorage.js create mode 100644 src/storage/templates/Simulator/Commons.js create mode 100644 src/storage/templates/Simulator/User.Bot.Daily.js create mode 100644 src/storage/templates/Simulator/User.Bot.Market.js create mode 100644 src/storage/templates/Simulator/this.bot.config.json create mode 100644 src/storage/templates/TradingBot/User.Bot.js create mode 100644 src/storage/templates/TradingBot/this.bot.config.json create mode 100644 src/utils/readFile.js create mode 100644 test/minioTest.js diff --git a/.env.local b/.env.local index ab3193771..0c3309d98 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://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/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..f0c81fb5f --- /dev/null +++ b/src/schema/queries/PlotterCode.js @@ -0,0 +1,46 @@ +import logger from '../../utils/logger' +import { FileInputType } from '../types/input' +import { GraphQLString } from 'graphql' + +export const args = { file: { type: FileInputType } } + +const resolve = async (parent, { file }, context) => { + logger.debug('getPlotterCode -> Entering Function: ' + file.filePath) + + try { + + // 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." + } + + let storage = require('../../storage/providers/AzureStorage') + let fileContent = await storage.getFileContentRemote(file.container, file.filePath, file.storage, file.accessKey) + + return fileContent + + } catch (err) { + logger.error('getPlotterCode -> Error: %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/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..3b4ac69a5 --- /dev/null +++ b/src/storage/templates/Simulator/Commons.js @@ -0,0 +1,1456 @@ +const axios = require('axios') +const auth = require('./utils/auth') + +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, + simulationLogic, + timePeriod, + currentDay, + startDate, + endDate, + interExecutionMemory, + callback) { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> Entering function."); } + + /* Initial Values */ + + let initialDate = startDate; + let initialBalanceA = 1; + let minimunBalanceA = 0.5; + + /* Strategy and Phases */ + + let strategyNumber = 0; + let strategyPhase = 0; // So far we will consider 5 possible phases: 0 = Initial state, 1 = Signal to buy, 2 = Buy, 3 = After Buy, 4 = Sell. + + /* 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. + let stopLossPercentage = 50; + let previousStopLoss = 0; + let stopLoss = 0; + let stopLossDecay = 0; + let stopLossDecayIncrement = 0.06; + let stopLossPhase = 0; + + /* Buy Order Management */ + + const MIN_BUY_ORDER_VALUE = 1 // We can not let the buy order be zero to avoid division by 0 error or infinity numbers as a result. + let buyOrderPercentage = 1; + let previousBuyOrder = 0; + let buyOrder = 0; + let buyOrderDecay = 0; + let buyOrderPhase = 0; + + /* Building records */ + + let record; + let sellRate = 0; + let sellAmount = 0; + let sellInstant; + + let previousBalanceAssetA = 0; + let hitRatio = 0; + let ROI = 0; + let days = 0; + let anualizedRateOfReturn = 0; + let type = '""'; + let rate = 0; + let newStopLoss; + + /* 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 = 0; + + 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; + + } + + simulationLogic.strategies = await getStrategy(); + + /* 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 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. + + /* We define and evaluate all conditions to be used later during the simulation loop. */ + + conditionsArrayRecord.push(candle.begin); + conditionsArrayRecord.push(candle.end); + + for (let j = 0; j < simulationLogic.strategies.length; j++) { + + let strategy = simulationLogic.strategies[j]; + + for (let k = 0; k < strategy.entryPoint.situations.length; k++) { + + let situation = strategy.entryPoint.situations[k]; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = strategy.name + '-' + situation.name + '-' + condition.name; + + newCondition(key, condition.code); + } + } + + for (let k = 0; k < strategy.exitPoint.situations.length; k++) { + + let situation = strategy.exitPoint.situations[k]; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = strategy.name + '-' + situation.name + '-' + condition.name; + + newCondition(key, condition.code); + } + } + + for (let k = 0; k < strategy.sellPoint.situations.length; k++) { + + let situation = strategy.sellPoint.situations[k]; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = strategy.name + '-' + situation.name + '-' + condition.name; + + newCondition(key, condition.code); + } + } + + for (let p = 0; p < strategy.stopLoss.phases.length; p++) { + + let phase = strategy.stopLoss.phases[p]; + + for (let k = 0; k < phase.situations.length; k++) { + + let situation = phase.situations[k]; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = strategy.name + '-' + phase.name + '-' + situation.name + '-' + condition.name; + + newCondition(key, condition.code); + } + } + } + + for (let p = 0; p < strategy.buyOrder.phases.length; p++) { + + let phase = strategy.buyOrder.phases[p]; + + for (let k = 0; k < phase.situations.length; k++) { + + let situation = phase.situations[k]; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = strategy.name + '-' + phase.name + '-' + situation.name + '-' + condition.name; + + newCondition(key, condition.code); + } + } + } + } + + function newCondition(key, code) { + + let condition; + let value = false; + + try { + value = eval(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 + } + + condition = { + key: key, + value: value + }; + + conditions.set(condition.key, condition); + + if (condition.value) { + conditionsArrayValues.push(1); + } else { + conditionsArrayValues.push(0); + } + } + + + /* Strategy Enter Condition */ + + if (strategyNumber === 0 && + balanceAssetA > minimunBalanceA) { + + /* + 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 an entering condition. Once we enter + into one strategy, we will ignore market conditions for others. However there is also + a strategy exit condition which can be hit before entering into a trade. If hit, we would + be outside a strategy again and looking for the condition to enter all over again. + + */ + + checkEntryPoints(); + + function checkEntryPoints() { + + for (let j = 0; j < simulationLogic.strategies.length; j++) { + + let strategy = simulationLogic.strategies[j]; + + for (let k = 0; k < strategy.entryPoint.situations.length; k++) { + + let situation = strategy.entryPoint.situations[k]; + let passed = true; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = strategy.name + '-' + situation.name + '-' + condition.name + + let value = conditions.get(key).value; + + if (value === false) { passed = false; } + } + + if (passed) { + + strategyPhase = 1; + strategyNumber = j + 1; + currentStrategy.begin = candle.begin; + currentStrategy.beginRate = candle.min; + currentStrategy.endRate = candle.min; // In case the strategy does not get exited + return; + } + } + } + } + } + + /* Strategy Exit Condition */ + + if (strategyPhase === 1) { + + checkExitPoints(); + + function checkExitPoints() { + + let strategy = simulationLogic.strategies[strategyNumber - 1]; + + for (let k = 0; k < strategy.exitPoint.situations.length; k++) { + + let situation = strategy.exitPoint.situations[k]; + let passed = true; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = strategy.name + '-' + situation.name + '-' + condition.name + + let value = conditions.get(key).value; + + if (value === false) { passed = false; } + } + + if (passed) { + + currentStrategy.number = strategyNumber - 1 + currentStrategy.end = candle.end; + currentStrategy.endRate = candle.min; + currentStrategy.status = 1; + strategyPhase = 0; + strategyNumber = 0; + + return; + } + } + } + } + + /* Checking if Stop or Take Profit were hit */ + + if (strategyPhase === 3) { + + /* Checking what happened since the last execution. We need to know if the Stop Loss + or our Buy Order were hit. */ + + /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ + + if (candle.max >= stopLoss) { + + balanceAssetA = balanceAssetB / stopLoss; + balanceAssetB = 0; + + if (currentDay !== undefined) { + if (sellInstant < currentDay.valueOf()) { + yesterday.balanceAssetA = balanceAssetA; + yesterday.balanceAssetB = balanceAssetB; + } + } + + rate = stopLoss; + type = '"Buy@StopLoss"'; + strategyPhase = 4; + currentTrade.end = candle.end; + currentTrade.status = 1; + currentTrade.exitType = 1; + currentTrade.endRate = stopLoss; + + currentStrategy.number = strategyNumber - 1 + currentStrategy.end = candle.end; + currentStrategy.endRate = candle.min; + currentStrategy.status = 1; + } + + /* Buy Order condition: Here we verify if the Buy Order was filled or not. */ + + if (candle.min <= buyOrder) { + + balanceAssetA = balanceAssetB / buyOrder; + balanceAssetB = 0; + + if (currentDay !== undefined) { + if (sellInstant < currentDay.valueOf()) { + yesterday.balanceAssetA = balanceAssetA; + yesterday.balanceAssetB = balanceAssetB; + + } + } + + rate = buyOrder; + type = '"Buy@BuyOrder"'; + strategyPhase = 4; + currentTrade.end = candle.end; + currentTrade.status = 1; + currentTrade.exitType = 2; + currentTrade.endRate = buyOrder; + + currentStrategy.number = strategyNumber - 1 + currentStrategy.end = candle.end; + currentStrategy.endRate = candle.min; + currentStrategy.status = 1; + } + } + + /* Trade Enter Condition */ + + if (strategyPhase === 1) { + + checkSellPoints(); + + function checkSellPoints() { + + let strategy = simulationLogic.strategies[strategyNumber - 1]; + + for (let k = 0; k < strategy.sellPoint.situations.length; k++) { + + let situation = strategy.sellPoint.situations[k]; + let passed = true; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = strategy.name + '-' + situation.name + '-' + condition.name + + let value = conditions.get(key).value; + + if (value === false) { passed = false; } + } + + if (passed) { + + type = '"Sell"'; + + strategyPhase = 2; + stopLossPhase = 1; + buyOrderPhase = 1; + currentTrade.begin = candle.begin; + currentTrade.beginRate = candle.close; + return; + } + } + } + } + + /* Stop Loss Management */ + + if (strategyPhase === 3) { + + checkStopPhases() + checkStopLoss(); + + } + + function checkStopPhases() { + + let strategy = simulationLogic.strategies[strategyNumber - 1]; + let phase = strategy.stopLoss.phases[stopLossPhase - 1]; + + for (let k = 0; k < phase.situations.length; k++) { + + let situation = phase.situations[k]; + let passed = true; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = strategy.name + '-' + phase.name + '-' + situation.name + '-' + condition.name + + let value = conditions.get(key).value; + + if (value === false) { passed = false; } + } + + if (passed) { + + stopLossPhase++; + + return; + } + } + } + + function checkStopLoss() { + + let strategy = simulationLogic.strategies[strategyNumber - 1]; + let phase = strategy.stopLoss.phases[stopLossPhase - 1]; + + try { + eval(phase.code); // Here is where we apply the formula given for the stop loss for this phase. + } catch (err) { + /* + If the code produces an exception, we are covered. + */ + } + + if (newStopLoss < previousStopLoss) { + stopLoss = newStopLoss; + } else { + stopLoss = previousStopLoss; + } + + if (stopLoss < MIN_STOP_LOSS_VALUE) { + stopLoss = MIN_STOP_LOSS_VALUE + } + + } + + /* Take Profit Management */ + + if (strategyPhase === 3) { + + checkBuyOrderPhases(); + checkBuyOrder(); + + } + + function checkBuyOrderPhases() { + + let strategy = simulationLogic.strategies[strategyNumber - 1]; + let phase = strategy.buyOrder.phases[buyOrderPhase - 1]; + + for (let k = 0; k < phase.situations.length; k++) { + + let situation = phase.situations[k]; + let passed = true; + + for (let m = 0; m < situation.conditions.length; m++) { + + let condition = situation.conditions[m]; + let key = strategy.name + '-' + phase.name + '-' + situation.name + '-' + condition.name + + let value = conditions.get(key).value; + + if (value === false) { passed = false; } + } + + if (passed) { + + buyOrderPhase++; + + return; + } + } + } + + function checkBuyOrder() { + + let strategy = simulationLogic.strategies[strategyNumber - 1]; + let phase = strategy.buyOrder.phases[buyOrderPhase - 1]; + + try { + eval(phase.code); // Here is where we apply the formula given for the buy order at this phase. + } catch (err) { + /* + If the code produces an exception, we are covered. + */ + } + + if (buyOrder < MIN_BUY_ORDER_VALUE) { + buyOrder = MIN_BUY_ORDER_VALUE + } + } + + /* Entering into a Trade */ + + if (strategyPhase === 2) { + + rate = candle.close; + sellRate = rate; + sellAmount = balanceAssetA; + + stopLoss = sellRate + sellRate * stopLossPercentage / 100; + previousStopLoss = stopLoss; + + stopLossDecay = 0; + + checkStopLoss(); + checkBuyOrder(); + + previousBalanceAssetA = balanceAssetA; + lastProfit = 0; + lastProfitPercent = 0; + + balanceAssetB = balanceAssetA * rate; + balanceAssetA = 0; + + sellInstant = candle.end; + + if (currentDay !== undefined) { + if (sellInstant < currentDay.valueOf()) { + yesterday.balanceAssetA = balanceAssetA; + yesterday.balanceAssetB = balanceAssetB; + + yesterday.lastProfit = lastProfit; + yesterday.lastProfitPercent = lastProfitPercent; + } + } + + addRecord(); + + strategyPhase = 3; + continue; + } + + /* Exiting a Trade */ + + if (strategyPhase === 4) { + + roundtrips++; + + if (currentDay !== undefined) { + if (sellInstant < currentDay.valueOf()) { + yesterday.Roundtrips++; + } + } + + lastProfit = balanceAssetA - previousBalanceAssetA; + lastProfitPercent = lastProfit / previousBalanceAssetA * 100; + if (isNaN(lastProfitPercent)) { lastProfitPercent = 0; } + profit = balanceAssetA - initialBalanceA; + + if (currentDay !== undefined) { + if (sellInstant < currentDay.valueOf()) { + yesterday.lastProfit = lastProfit; + yesterday.profit = profit; + yesterday.lastProfitPercent = lastProfitPercent; + } + } + + currentTrade.lastProfitPercent = lastProfitPercent; + currentTrade.stopRate = stopLoss; + + //if (isNaN(ROI)) { ROI = 0; } + + if (lastProfit > 0) { + hits++; + + if (currentDay !== undefined) { + if (sellInstant < currentDay.valueOf()) { + yesterday.hits++; + } + } + + } else { + fails++; + + if (currentDay !== undefined) { + if (sellInstant < currentDay.valueOf()) { + yesterday.fails++; + } + } + } + + ROI = (initialBalanceA + profit) / initialBalanceA - 1; + hitRatio = hits / roundtrips; + anualizedRateOfReturn = ROI / days * 365; + + if (currentDay !== undefined) { + if (sellInstant < currentDay.valueOf()) { + yesterday.ROI = ROI; + yesterday.hitRatio = hitRatio; + yesterday.anualizedRateOfReturn = anualizedRateOfReturn; + } + } + + + addRecord(); + + strategyNumber = 0; + stopLoss = 0; + sellRate = 0; + sellAmount = 0; + sellInstant = undefined; + buyOrder = 0; + strategyPhase = 0; + stopLossPhase = 0; + buyOrderPhase = 0; + continue; + + } + + /* Not a buy or sell condition */ + + rate = 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 record; + let orderRecord; + + messageId++; + + if (strategyPhase === 2 || strategyPhase === 3) { + + if (strategyPhase === 2) { + messageType = MESSAGE_TYPE.Order; + orderId++; + } + if (strategyPhase === 3) { + 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, + rate, + stopLoss, + buyOrder, + 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, + "") + } + + record = { + begin: candle.begin, + end: candle.end, + type: type, + rate: rate, + 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, + sellRate: sellRate, + lastProfitPercent: lastProfitPercent, + strategy: strategyNumber, + strategyPhase: strategyPhase, + buyOrder: buyOrder, + stopLossPhase: stopLossPhase, + buyOrderPhase: buyOrderPhase, + orderRecord: orderRecord, + sellAmount: sellAmount + } + + recordsArray.push(record); + + previousStopLoss = stopLoss; + + type = '""'; + + /* Prepare the information for the Conditions File */ + + conditionsArrayRecord.push(strategyNumber); + conditionsArrayRecord.push(strategyPhase); + conditionsArrayRecord.push(stopLossPhase); + conditionsArrayRecord.push(buyOrderPhase); + conditionsArrayRecord.push(conditionsArrayValues); + + 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(); + + 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); + } + } + + async function getStrategy() { + + try { + + const accessToken = await auth.authenticate() + + let fbSlug = bot.codeName + if (process.env.TEST_FB !== undefined) { + fbSlug = process.env.TEST_FB + } + + const strategizerResponse = await axios({ + url: process.env.GATEWAY_ENDPOINT, + method: 'post', + data: { + query: ` + query($fbSlug: String!){ + + strategizer_StrategyByFb(fbSlug: $fbSlug){ + subStrategies(activeOnly: true){ + name + entryPoint{ + situations{ + name + conditions{ + name + code + } + } + } + exitPoint{ + situations{ + name + conditions{ + name + code + } + } + } + sellPoint{ + situations{ + name + conditions{ + name + code + } + } + } + buyPoint{ + situations{ + name + conditions{ + name + code + } + } + } + stopLoss{ + phases{ + name + code + situations{ + name + conditions{ + name + code + } + } + } + } + buyOrder{ + phases{ + name + code + situations{ + name + conditions{ + name + code + } + } + } + } + sellOrder{ + phases{ + name + code + situations{ + name + conditions{ + name + code + } + } + } + } + } + } + } + + `, + variables: { + fbSlug: fbSlug + }, + }, + headers: { + authorization: 'Bearer ' + accessToken + } + }) + + if (strategizerResponse.data.errors) + throw new Error(strategizerResponse.data.errors[0].message) + + return strategizerResponse.data.data.strategizer_StrategyByFb.subStrategies; + + } catch (error) { + throw new Error('There has been an error getting the strategy to run on the simulator. Error: ' + error) + } + } +}; + + + 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..eb96ce694 --- /dev/null +++ b/src/storage/templates/Simulator/User.Bot.Daily.js @@ -0,0 +1,468 @@ +exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_STORAGE, FILE_STORAGE) { + + 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 thisBotStorage = BLOB_STORAGE.newBlobStorage(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; + + thisBotStorage.initialize(bot.devTeam, onStorageInizialized); + + function onStorageInizialized(err) { + + if (err.result === global.DEFAULT_OK_RESPONSE.result) { + + callBackFunction(global.DEFAULT_OK_RESPONSE); + + } else { + logger.write(MODULE_NAME, "[ERROR] initializeStorage -> onStorageInizialized -> err = " + err.stack); + callBackFunction(err); + } + } + } 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 simulationLogic = {}; + 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, + simulationLogic, + timePeriod, + currentDay, + startDate, + endDate, + interExecutionMemory, + 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.rate + "," + + 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.sellRate + "," + + record.lastProfitPercent + "," + + record.strategy + "," + + record.strategyPhase + "," + + record.buyOrder + "," + + record.stopLossPhase + "," + + record.buyOrderPhase + "," + + JSON.stringify(record.orderRecord) + "," + + record.sellAmount + "]"; + + 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.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + let filePath = filePathRoot + "/Output/" + SIMULATED_RECORDS_FOLDER_NAME + "/" + "Multi-Period-Daily" + "/" + outputPeriodLabel + "/" + dateForPath; + + thisBotStorage.createTextFile(filePath, fileName, 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 - 1; j++) { + conditions = conditions + conditionsSeparator + record[j]; + if (conditionsSeparator === "") { conditionsSeparator = ","; } + } + + conditions = conditions + conditionsSeparator + '[' + record[record.length - 1] + ']'; // The last item contains an Array of condition values. + + fileContent = fileContent + separator + '[' + conditions + ']'; + + if (separator === "") { separator = ","; } + + fileRecordCounter++; + + } + + fileContent = "[" + fileContent + "]"; + fileContent = "[" + JSON.stringify(simulationLogic) + "," + 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.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + let filePath = filePathRoot + "/Output/" + CONDITIONS_FOLDER_NAME + "/" + "Multi-Period-Daily" + "/" + outputPeriodLabel + "/" + dateForPath; + + thisBotStorage.createTextFile(filePath, fileName, 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.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + let filePath = filePathRoot + "/Output/" + STRATEGIES_FOLDER_NAME + "/" + "Multi-Period-Daily" + "/" + outputPeriodLabel + "/" + dateForPath; + + thisBotStorage.createTextFile(filePath, fileName, 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.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + let filePath = filePathRoot + "/Output/" + TRADES_FOLDER_NAME + "/" + "Multi-Period-Daily" + "/" + outputPeriodLabel + "/" + dateForPath; + + thisBotStorage.createTextFile(filePath, fileName, 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..d0aaf744c --- /dev/null +++ b/src/storage/templates/Simulator/User.Bot.Market.js @@ -0,0 +1,441 @@ +exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_STORAGE, FILE_STORAGE) { + + 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 thisBotStorage = BLOB_STORAGE.newBlobStorage(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; + + thisBotStorage.initialize(bot.devTeam, onStorageInizialized); + + function onStorageInizialized(err) { + + if (err.result === global.DEFAULT_OK_RESPONSE.result) { + + callBackFunction(global.DEFAULT_OK_RESPONSE); + + } else { + logger.write(MODULE_NAME, "[ERROR] initializeStorage -> onStorageInizialized -> err = " + err.stack); + callBackFunction(err); + } + } + } 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 simulationLogic = {}; + 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, + simulationLogic, + timePeriod, + currentDay, + startDate, + endDate, + interExecutionMemory, + 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.rate + "," + + 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.sellRate + "," + + record.lastProfitPercent + "," + + record.strategy + "," + + record.strategyPhase + "," + + record.buyOrder + "," + + record.stopLossPhase + "," + + record.buyOrderPhase + "," + + + JSON.stringify(record.orderRecord) + "," + + record.sellAmount + "]"; + + 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.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + let filePath = filePathRoot + "/Output/" + SIMULATED_RECORDS_FOLDER_NAME + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel; + + thisBotStorage.createTextFile(filePath, fileName, 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 - 1; j++) { + conditions = conditions + conditionsSeparator + record[j]; + if (conditionsSeparator === "") { conditionsSeparator = ","; } + } + + conditions = conditions + conditionsSeparator + '[' + record[record.length - 1] + ']'; // The last item contains an Array of condition values. + + fileContent = fileContent + separator + '[' + conditions + ']'; + + if (separator === "") { separator = ","; } + + fileRecordCounter++; + + } + + fileContent = "[" + fileContent + "]"; + fileContent = "[" + JSON.stringify(simulationLogic) + "," + JSON.stringify(lastObjectsArray) + "," + fileContent + "]"; + let fileName = '' + market.assetA + '_' + market.assetB + '.json'; + + let filePathRoot = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + global.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + let filePath = filePathRoot + "/Output/" + CONDITIONS_FOLDER_NAME + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel; + + thisBotStorage.createTextFile(filePath, fileName, 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.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + let filePath = filePathRoot + "/Output/" + STRATEGIES_FOLDER_NAME + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel; + + thisBotStorage.createTextFile(filePath, fileName, 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.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + let filePath = filePathRoot + "/Output/" + TRADES_FOLDER_NAME + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel; + + thisBotStorage.createTextFile(filePath, fileName, 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..2f17c31ad --- /dev/null +++ b/src/storage/templates/Simulator/this.bot.config.json @@ -0,0 +1,394 @@ +{ + "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..9074de4d5 --- /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": "http://localhost:4100/", + "storage": "https://aadevelop.blob.core.windows.net", + "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" + }, + "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') + }) +}) From 8cbe7631cec289c2d8e21ebd86a5b96938aea1f6 Mon Sep 17 00:00:00 2001 From: Matias Date: Mon, 17 Jun 2019 21:08:06 -0300 Subject: [PATCH 09/34] Adding new templates --- .env.local | 2 +- src/storage/templates/Simulator/Commons.js | 1316 +++++++++++------ .../templates/Simulator/User.Bot.Daily.js | 75 +- .../templates/Simulator/User.Bot.Market.js | 76 +- .../templates/Simulator/this.bot.config.json | 3 - 5 files changed, 933 insertions(+), 539 deletions(-) diff --git a/.env.local b/.env.local index 0c3309d98..fb69d8efa 100644 --- a/.env.local +++ b/.env.local @@ -9,7 +9,7 @@ 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://localhost:4100/ +NODE_ENDPOINT=http://docker.for.win.localhost:4100/ # Storage Configuration STORAGE_PROVIDER=Minio diff --git a/src/storage/templates/Simulator/Commons.js b/src/storage/templates/Simulator/Commons.js index 3b4ac69a5..d0f98ae4b 100644 --- a/src/storage/templates/Simulator/Commons.js +++ b/src/storage/templates/Simulator/Commons.js @@ -1,5 +1,4 @@ -const axios = require('axios') -const auth = require('./utils/auth') +const strategy = require('./Integrations/Strategy') exports.newCommons = function newCommons(bot, logger, UTILITIES) { @@ -59,7 +58,6 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { strategiesArray, tradesArray, lastObjectsArray, - simulationLogic, timePeriod, currentDay, startDate, @@ -71,51 +69,130 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> Entering function."); } - /* Initial Values */ + let tradingSystem = await strategy.getStrategy(); - let initialDate = startDate; - let initialBalanceA = 1; - let minimunBalanceA = 0.5; + /* 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 strategyNumber = 0; - let strategyPhase = 0; // So far we will consider 5 possible phases: 0 = Initial state, 1 = Signal to buy, 2 = Buy, 3 = After Buy, 4 = Sell. + 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. - let stopLossPercentage = 50; - let previousStopLoss = 0; + const MAX_STOP_LOSS_VALUE = Number.MAX_SAFE_INTEGER let stopLoss = 0; - let stopLossDecay = 0; - let stopLossDecayIncrement = 0.06; let stopLossPhase = 0; + let stopLossStage = 'No Stage'; - /* Buy Order Management */ + /* Take Profit Management */ - const MIN_BUY_ORDER_VALUE = 1 // We can not let the buy order be zero to avoid division by 0 error or infinity numbers as a result. - let buyOrderPercentage = 1; - let previousBuyOrder = 0; - let buyOrder = 0; - let buyOrderDecay = 0; - let buyOrderPhase = 0; + 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'; - /* Building records */ + /* Simulation Records */ - let record; - let sellRate = 0; - let sellAmount = 0; - let sellInstant; + 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 rate = 0; - let newStopLoss; + 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. */ @@ -160,7 +237,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { */ let balanceAssetA = initialBalanceA; - let balanceAssetB = 0; + let balanceAssetB = initialBalanceB; let lastProfit = 0; let profit = 0; @@ -255,8 +332,6 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { yesterday.anualizedRateOfReturn = anualizedRateOfReturn; } - - simulationLogic.strategies = await getStrategy(); /* Main Simulation Loop: We go thourgh all the candles at this time period. */ @@ -317,157 +392,442 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } 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); - for (let j = 0; j < simulationLogic.strategies.length; j++) { + evaluateConditionsAndFormulas(tradingSystem, conditions); - let strategy = simulationLogic.strategies[j]; + function evaluateConditionsAndFormulas(tradingSystem, conditions) { - for (let k = 0; k < strategy.entryPoint.situations.length; k++) { + for (let j = 0; j < tradingSystem.strategies.length; j++) { - let situation = strategy.entryPoint.situations[k]; + let strategy = tradingSystem.strategies[j]; - for (let m = 0; m < situation.conditions.length; m++) { + let triggerStage = strategy.triggerStage - let condition = situation.conditions[m]; - let key = strategy.name + '-' + situation.name + '-' + condition.name; + if (triggerStage !== undefined) { - newCondition(key, condition.code); + 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); + } + } + } + } } - } - for (let k = 0; k < strategy.exitPoint.situations.length; k++) { + let openStage = strategy.openStage - let situation = strategy.exitPoint.situations[k]; + if (openStage !== undefined) { - for (let m = 0; m < situation.conditions.length; m++) { + let initialDefinition = openStage.initialDefinition - let condition = situation.conditions[m]; - let key = strategy.name + '-' + situation.name + '-' + condition.name; + if (initialDefinition !== undefined) { - newCondition(key, condition.code); + 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); + } + } + } + } + } + } + } } - } - for (let k = 0; k < strategy.sellPoint.situations.length; k++) { + let manageStage = strategy.manageStage - let situation = strategy.sellPoint.situations[k]; + if (manageStage !== undefined) { - for (let m = 0; m < situation.conditions.length; m++) { + if (manageStage.stopLoss !== undefined) { - let condition = situation.conditions[m]; - let key = strategy.name + '-' + situation.name + '-' + condition.name; + for (let p = 0; p < manageStage.stopLoss.phases.length; p++) { - newCondition(key, condition.code); - } - } + let phase = manageStage.stopLoss.phases[p]; - for (let p = 0; p < strategy.stopLoss.phases.length; p++) { + /* Evaluate Formula */ + let formulaValue + let formulaError = '' - let phase = strategy.stopLoss.phases[p]; + 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 + } + } - for (let k = 0; k < phase.situations.length; k++) { + formulasErrors.push('"' + formulaError + '"') + formulasValues.push(formulaValue) + let key = j + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + p; + formulas.set(key, formulaValue) - let situation = phase.situations[k]; + /* next phase event */ + let nextPhaseEvent = phase.nextPhaseEvent; + if (nextPhaseEvent !== undefined) { - for (let m = 0; m < situation.conditions.length; m++) { + for (let k = 0; k < nextPhaseEvent.situations.length; k++) { - let condition = situation.conditions[m]; - let key = strategy.name + '-' + phase.name + '-' + situation.name + '-' + condition.name; + let situation = nextPhaseEvent.situations[k]; - newCondition(key, condition.code); + 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); + } + } + } + } + } } } } - for (let p = 0; p < strategy.buyOrder.phases.length; p++) { + function newCondition(key, node) { - let phase = strategy.buyOrder.phases[p]; + let condition; + let error = '' + let value - for (let k = 0; k < phase.situations.length; k++) { + 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 + } - let situation = phase.situations[k]; + condition = { + key: key, + value: value + }; - for (let m = 0; m < situation.conditions.length; m++) { + conditions.set(condition.key, condition); - let condition = situation.conditions[m]; - let key = strategy.name + '-' + phase.name + '-' + situation.name + '-' + condition.name; - - newCondition(key, condition.code); - } + if (condition.value) { + conditionsArrayValues.push(1); + } else { + conditionsArrayValues.push(0); } } + } - function newCondition(key, code) { - let condition; - let value = false; + if ( + strategyStage === 'No Stage' && + currentStrategyIndex === -1 + ) { + let minimunBalance + let maximunBalance + let balance - try { - value = eval(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 - } - - condition = { - key: key, - value: value - }; - - conditions.set(condition.key, condition); - - if (condition.value) { - conditionsArrayValues.push(1); + if (baseAsset === 'BTC') { + balance = balanceAssetA + minimunBalance = minimunBalanceA + maximunBalance = maximunBalanceA } else { - conditionsArrayValues.push(0); + 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 */ - /* Strategy Enter Condition */ + if (strategyStage === 'Trigger Stage') { - if (strategyNumber === 0 && - balanceAssetA > minimunBalanceA) { + let strategy = tradingSystem.strategies[currentStrategyIndex]; - /* - 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 an entering condition. Once we enter - into one strategy, we will ignore market conditions for others. However there is also - a strategy exit condition which can be hit before entering into a trade. If hit, we would - be outside a strategy again and looking for the condition to enter all over again. + let triggerStage = strategy.triggerStage - */ + if (triggerStage !== undefined) { - checkEntryPoints(); + if (triggerStage.triggerOff !== undefined) { - function checkEntryPoints() { + for (let k = 0; k < triggerStage.triggerOff.situations.length; k++) { - for (let j = 0; j < simulationLogic.strategies.length; j++) { - - let strategy = simulationLogic.strategies[j]; - - for (let k = 0; k < strategy.entryPoint.situations.length; k++) { - - let situation = strategy.entryPoint.situations[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 = strategy.name + '-' + situation.name + '-' + condition.name + let key = currentStrategyIndex + '-' + 'triggerStage' + '-' + 'triggerOff' + '-' + k + '-' + m; let value = conditions.get(key).value; @@ -476,142 +836,204 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (passed) { - strategyPhase = 1; - strategyNumber = j + 1; - currentStrategy.begin = candle.begin; - currentStrategy.beginRate = candle.min; - currentStrategy.endRate = candle.min; // In case the strategy does not get exited - return; + currentStrategy.number = currentStrategyIndex + currentStrategy.end = candle.end; + currentStrategy.endRate = candle.min; + currentStrategy.status = 1; + strategyStage = 'No Stage'; + currentStrategyIndex = -1; + break; } } } } } - /* Strategy Exit Condition */ - - if (strategyPhase === 1) { - - checkExitPoints(); - - function checkExitPoints() { - - let strategy = simulationLogic.strategies[strategyNumber - 1]; - - for (let k = 0; k < strategy.exitPoint.situations.length; k++) { - - let situation = strategy.exitPoint.situations[k]; - let passed = true; - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m]; - let key = strategy.name + '-' + situation.name + '-' + condition.name - - let value = conditions.get(key).value; - - if (value === false) { passed = false; } - } - - if (passed) { - - currentStrategy.number = strategyNumber - 1 - currentStrategy.end = candle.end; - currentStrategy.endRate = candle.min; - currentStrategy.status = 1; - strategyPhase = 0; - strategyNumber = 0; - - return; - } - } - } - } - /* Checking if Stop or Take Profit were hit */ - if (strategyPhase === 3) { + if ( + strategyStage === 'Open Stage' || + strategyStage === 'Manage Stage' + ) { /* Checking what happened since the last execution. We need to know if the Stop Loss - or our Buy Order were hit. */ + or our Take Profit were hit. */ /* Stop Loss condition: Here we verify if the Stop Loss was hitted or not. */ - if (candle.max >= stopLoss) { + if ((baseAsset === 'BTC' && candle.max >= stopLoss) || (baseAsset !== 'BTC' && candle.min <= stopLoss)) { - balanceAssetA = balanceAssetB / stopLoss; - balanceAssetB = 0; + if (baseAsset === 'BTC') { + balanceAssetA = balanceAssetA + balanceAssetB / stopLoss; + balanceAssetB = 0; + } else { + balanceAssetB = balanceAssetB + balanceAssetA * stopLoss; + balanceAssetA = 0; + } if (currentDay !== undefined) { - if (sellInstant < currentDay.valueOf()) { + if (positionInstant < currentDay.valueOf()) { yesterday.balanceAssetA = balanceAssetA; yesterday.balanceAssetB = balanceAssetB; } } - rate = stopLoss; + marketRate = stopLoss; type = '"Buy@StopLoss"'; - strategyPhase = 4; + strategyStage = 'Close Stage'; + stopLossStage = 'No Stage'; + takeProfitStage = 'No Stage'; currentTrade.end = candle.end; currentTrade.status = 1; currentTrade.exitType = 1; currentTrade.endRate = stopLoss; - currentStrategy.number = strategyNumber - 1 + currentStrategy.number = currentStrategyIndex currentStrategy.end = candle.end; currentStrategy.endRate = candle.min; currentStrategy.status = 1; } - /* Buy Order condition: Here we verify if the Buy Order was filled or not. */ + /* Take Profit condition: Here we verify if the Take Profit was filled or not. */ - if (candle.min <= buyOrder) { + if ((baseAsset === 'BTC' && candle.min <= takeProfit) || (baseAsset !== 'BTC' && candle.max >= takeProfit)) { - balanceAssetA = balanceAssetB / buyOrder; - balanceAssetB = 0; + if (baseAsset === 'BTC') { + balanceAssetA = balanceAssetA + balanceAssetB / takeProfit; + balanceAssetB = 0; + } else { + balanceAssetB = balanceAssetB + balanceAssetA * takeProfit; + balanceAssetA = 0; + } if (currentDay !== undefined) { - if (sellInstant < currentDay.valueOf()) { + if (positionInstant < currentDay.valueOf()) { yesterday.balanceAssetA = balanceAssetA; yesterday.balanceAssetB = balanceAssetB; } } - rate = buyOrder; - type = '"Buy@BuyOrder"'; - strategyPhase = 4; + 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 = buyOrder; + currentTrade.endRate = takeProfit; - currentStrategy.number = strategyNumber - 1 + currentStrategy.number = currentStrategyIndex currentStrategy.end = candle.end; currentStrategy.endRate = candle.min; currentStrategy.status = 1; } } - /* Trade Enter Condition */ + /* Take Position Condition */ - if (strategyPhase === 1) { + if (strategyStage === 'Trigger Stage') { - checkSellPoints(); + let strategy = tradingSystem.strategies[currentStrategyIndex]; - function checkSellPoints() { + let triggerStage = strategy.triggerStage - let strategy = simulationLogic.strategies[strategyNumber - 1]; + if (triggerStage !== undefined) { - for (let k = 0; k < strategy.sellPoint.situations.length; k++) { + if (triggerStage.takePosition !== undefined) { - let situation = strategy.sellPoint.situations[k]; + 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 = strategy.name + '-' + situation.name + '-' + condition.name + let key = j + '-' + stageKey + initialDefinitionKey + '-' + 'stopLoss' + '-' + p + '-' + k + '-' + m; let value = conditions.get(key).value; @@ -620,165 +1042,208 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (passed) { - type = '"Sell"'; - - strategyPhase = 2; - stopLossPhase = 1; - buyOrderPhase = 1; - currentTrade.begin = candle.begin; - currentTrade.beginRate = candle.close; + stopLossPhase++; + stopLossStage = 'Manage Stage' + if (takeProfitPhase > 1) { strategyStage = 'Manage Stage' } return; } } } } - /* Stop Loss Management */ + function calculateStopLoss() { - if (strategyPhase === 3) { + let strategy = tradingSystem.strategies[currentStrategyIndex]; + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let phase + let key - checkStopPhases() - checkStopLoss(); - - } - - function checkStopPhases() { - - let strategy = simulationLogic.strategies[strategyNumber - 1]; - let phase = strategy.stopLoss.phases[stopLossPhase - 1]; - - for (let k = 0; k < phase.situations.length; k++) { - - let situation = phase.situations[k]; - let passed = true; - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m]; - let key = strategy.name + '-' + phase.name + '-' + situation.name + '-' + condition.name - - let value = conditions.get(key).value; - - if (value === false) { passed = false; } - } - - if (passed) { - - stopLossPhase++; - - return; + 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); + } } } - } - function checkStopLoss() { - - let strategy = simulationLogic.strategies[strategyNumber - 1]; - let phase = strategy.stopLoss.phases[stopLossPhase - 1]; - - try { - eval(phase.code); // Here is where we apply the formula given for the stop loss for this phase. - } catch (err) { - /* - If the code produces an exception, we are covered. - */ + if (stopLossStage === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.stopLoss !== undefined) { + phase = manageStage.stopLoss.phases[stopLossPhase - 2]; + key = currentStrategyIndex + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + (stopLossPhase - 2); + } } - if (newStopLoss < previousStopLoss) { - stopLoss = newStopLoss; - } else { - stopLoss = previousStopLoss; + if (phase.formula !== undefined) { + stopLoss = formulas.get(key) } - - if (stopLoss < MIN_STOP_LOSS_VALUE) { - stopLoss = MIN_STOP_LOSS_VALUE - } - } /* Take Profit Management */ - if (strategyPhase === 3) { + if ( + strategyStage === 'Open Stage' || + strategyStage === 'Manage Stage' + ) { - checkBuyOrderPhases(); - checkBuyOrder(); + checkTakeProfitPhases(); + calculateTakeProfit(); } - function checkBuyOrderPhases() { + function checkTakeProfitPhases() { - let strategy = simulationLogic.strategies[strategyNumber - 1]; - let phase = strategy.buyOrder.phases[buyOrderPhase - 1]; + let strategy = tradingSystem.strategies[currentStrategyIndex]; - for (let k = 0; k < phase.situations.length; k++) { + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let parentNode + let j = currentStrategyIndex + let stageKey + let initialDefinitionKey = '' + let p - let situation = phase.situations[k]; - let passed = true; - - for (let m = 0; m < situation.conditions.length; m++) { - - let condition = situation.conditions[m]; - let key = strategy.name + '-' + phase.name + '-' + situation.name + '-' + condition.name - - let value = conditions.get(key).value; - - if (value === false) { passed = false; } + 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 (passed) { + if (takeProfitStage === 'Manage Stage' && manageStage !== undefined) { + if (manageStage.takeProfit !== undefined) { + parentNode = manageStage + stageKey = 'manageStage' + p = takeProfitPhase - 2 + } + } - buyOrderPhase++; + let phase = parentNode.takeProfit.phases[p]; - return; + 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 checkBuyOrder() { + function calculateTakeProfit() { - let strategy = simulationLogic.strategies[strategyNumber - 1]; - let phase = strategy.buyOrder.phases[buyOrderPhase - 1]; + let strategy = tradingSystem.strategies[currentStrategyIndex]; + let openStage = strategy.openStage + let manageStage = strategy.manageStage + let phase + let key - try { - eval(phase.code); // Here is where we apply the formula given for the buy order at this phase. - } catch (err) { - /* - If the code produces an exception, we are covered. - */ + 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 (buyOrder < MIN_BUY_ORDER_VALUE) { - buyOrder = MIN_BUY_ORDER_VALUE + 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) } } - /* Entering into a Trade */ + /* Taking a Position */ - if (strategyPhase === 2) { + if ( + takePositionNow === true + ) { + takePositionNow = false - rate = candle.close; - sellRate = rate; - sellAmount = balanceAssetA; + /* 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 - stopLoss = sellRate + sellRate * stopLossPercentage / 100; - previousStopLoss = stopLoss; + 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; + } + } + } + } + } - stopLossDecay = 0; + marketRate = candle.close; + positionRate = marketRate; - checkStopLoss(); - checkBuyOrder(); + calculateStopLoss(); + calculateTakeProfit(); previousBalanceAssetA = balanceAssetA; + previousBalanceAssetB = balanceAssetB; + lastProfit = 0; lastProfitPercent = 0; - balanceAssetB = balanceAssetA * rate; - balanceAssetA = 0; + if (baseAsset === 'BTC') { + balanceAssetB = balanceAssetB + positionSize * marketRate; + balanceAssetA = balanceAssetA - positionSize; + } else { + balanceAssetA = balanceAssetA + positionSize / marketRate; + balanceAssetB = balanceAssetB - positionSize; + } - sellInstant = candle.end; + positionInstant = candle.end; if (currentDay !== undefined) { - if (sellInstant < currentDay.valueOf()) { + if (positionInstant < currentDay.valueOf()) { yesterday.balanceAssetA = balanceAssetA; yesterday.balanceAssetB = balanceAssetB; @@ -788,30 +1253,35 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } addRecord(); - - strategyPhase = 3; continue; } - /* Exiting a Trade */ + /* Closing a Position */ - if (strategyPhase === 4) { + if (strategyStage === 'Close Stage') { roundtrips++; if (currentDay !== undefined) { - if (sellInstant < currentDay.valueOf()) { + if (positionInstant < currentDay.valueOf()) { yesterday.Roundtrips++; } } - - lastProfit = balanceAssetA - previousBalanceAssetA; - lastProfitPercent = lastProfit / previousBalanceAssetA * 100; - if (isNaN(lastProfitPercent)) { lastProfitPercent = 0; } - profit = balanceAssetA - initialBalanceA; + + 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 (sellInstant < currentDay.valueOf()) { + if (positionInstant < currentDay.valueOf()) { yesterday.lastProfit = lastProfit; yesterday.profit = profit; yesterday.lastProfitPercent = lastProfitPercent; @@ -820,14 +1290,12 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { currentTrade.lastProfitPercent = lastProfitPercent; currentTrade.stopRate = stopLoss; - - //if (isNaN(ROI)) { ROI = 0; } - + if (lastProfit > 0) { hits++; if (currentDay !== undefined) { - if (sellInstant < currentDay.valueOf()) { + if (positionInstant < currentDay.valueOf()) { yesterday.hits++; } } @@ -836,18 +1304,24 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { fails++; if (currentDay !== undefined) { - if (sellInstant < currentDay.valueOf()) { + if (positionInstant < currentDay.valueOf()) { yesterday.fails++; } } } - ROI = (initialBalanceA + profit) / initialBalanceA - 1; - hitRatio = hits / roundtrips; - anualizedRateOfReturn = ROI / days * 365; + 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 (sellInstant < currentDay.valueOf()) { + if (positionInstant < currentDay.valueOf()) { yesterday.ROI = ROI; yesterday.hitRatio = hitRatio; yesterday.anualizedRateOfReturn = anualizedRateOfReturn; @@ -857,22 +1331,24 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { addRecord(); - strategyNumber = 0; + currentStrategyIndex = -1; stopLoss = 0; - sellRate = 0; - sellAmount = 0; - sellInstant = undefined; - buyOrder = 0; - strategyPhase = 0; + positionRate = 0; + positionSize = 0; + positionInstant = undefined; + takeProfit = 0; + strategyStage = 'No Stage'; + stopLossStage = 'No Stage'; + takeProfitStage = 'No Stage'; stopLossPhase = 0; - buyOrderPhase = 0; + takeProfitPhase = 0; continue; } /* Not a buy or sell condition */ - rate = candle.close; + marketRate = candle.close; addRecord(); function addRecord() { @@ -880,18 +1356,18 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { // 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 record; + let simulationRecord; let orderRecord; messageId++; - if (strategyPhase === 2 || strategyPhase === 3) { + if (strategyStage === 'Open Stage' || strategyStage === 'Manage Stage') { - if (strategyPhase === 2) { + if (strategyStage === 'Open Stage') { messageType = MESSAGE_TYPE.Order; orderId++; } - if (strategyPhase === 3) { + if (strategyStage === 'Manage Stage') { messageType = MESSAGE_TYPE.OrderUpdate; } @@ -909,9 +1385,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { "BTC_USDT", 0, ORDER_TYPE.Limit, - rate, + marketRate, stopLoss, - buyOrder, + takeProfit, ORDER_DIRECTION.Sell, -1, ORDER_STATUS.Signaled, @@ -945,11 +1421,35 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { "") } - record = { + 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, - rate: rate, + marketRate: marketRate, amount: 1, balanceA: balanceAssetA, balanceB: balanceAssetB, @@ -964,30 +1464,36 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { periods: periods, days: days, anualizedRateOfReturn: anualizedRateOfReturn, - sellRate: sellRate, + positionRate: positionRate, lastProfitPercent: lastProfitPercent, - strategy: strategyNumber, - strategyPhase: strategyPhase, - buyOrder: buyOrder, + strategy: currentStrategyIndex, + strategyStageNumber: strategyStageNumber, + takeProfit: takeProfit, stopLossPhase: stopLossPhase, - buyOrderPhase: buyOrderPhase, + takeProfitPhase: takeProfitPhase, orderRecord: orderRecord, - sellAmount: sellAmount + positionSize: positionSize, + initialBalanceA: initialBalanceA, + minimunBalanceA: minimunBalanceA, + maximunBalanceA: maximunBalanceA, + initialBalanceB: initialBalanceB, + minimunBalanceB: minimunBalanceB, + maximunBalanceB: maximunBalanceB } - recordsArray.push(record); - - previousStopLoss = stopLoss; + recordsArray.push(simulationRecord); type = '""'; /* Prepare the information for the Conditions File */ - conditionsArrayRecord.push(strategyNumber); - conditionsArrayRecord.push(strategyPhase); + conditionsArrayRecord.push(currentStrategyIndex); + conditionsArrayRecord.push(strategyStageNumber); conditionsArrayRecord.push(stopLossPhase); - conditionsArrayRecord.push(buyOrderPhase); + conditionsArrayRecord.push(takeProfitPhase); conditionsArrayRecord.push(conditionsArrayValues); + conditionsArrayRecord.push(formulasErrors); + conditionsArrayRecord.push(formulasValues); conditionsArray.push(conditionsArrayRecord); @@ -1063,7 +1569,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } - callback(); + callback(tradingSystem); function getElement(pArray, begin, end) { @@ -1330,126 +1836,6 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } - - async function getStrategy() { - - try { - - const accessToken = await auth.authenticate() - - let fbSlug = bot.codeName - if (process.env.TEST_FB !== undefined) { - fbSlug = process.env.TEST_FB - } - - const strategizerResponse = await axios({ - url: process.env.GATEWAY_ENDPOINT, - method: 'post', - data: { - query: ` - query($fbSlug: String!){ - - strategizer_StrategyByFb(fbSlug: $fbSlug){ - subStrategies(activeOnly: true){ - name - entryPoint{ - situations{ - name - conditions{ - name - code - } - } - } - exitPoint{ - situations{ - name - conditions{ - name - code - } - } - } - sellPoint{ - situations{ - name - conditions{ - name - code - } - } - } - buyPoint{ - situations{ - name - conditions{ - name - code - } - } - } - stopLoss{ - phases{ - name - code - situations{ - name - conditions{ - name - code - } - } - } - } - buyOrder{ - phases{ - name - code - situations{ - name - conditions{ - name - code - } - } - } - } - sellOrder{ - phases{ - name - code - situations{ - name - conditions{ - name - code - } - } - } - } - } - } - } - - `, - variables: { - fbSlug: fbSlug - }, - }, - headers: { - authorization: 'Bearer ' + accessToken - } - }) - - if (strategizerResponse.data.errors) - throw new Error(strategizerResponse.data.errors[0].message) - - return strategizerResponse.data.data.strategizer_StrategyByFb.subStrategies; - - } catch (error) { - throw new Error('There has been an error getting the strategy to run on the simulator. Error: ' + error) - } - } }; diff --git a/src/storage/templates/Simulator/User.Bot.Daily.js b/src/storage/templates/Simulator/User.Bot.Daily.js index eb96ce694..1b03337f1 100644 --- a/src/storage/templates/Simulator/User.Bot.Daily.js +++ b/src/storage/templates/Simulator/User.Bot.Daily.js @@ -1,4 +1,4 @@ -exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_STORAGE, FILE_STORAGE) { +exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES) { const FULL_LOG = true; const LOG_FILE_CONTENT = false; @@ -18,7 +18,9 @@ }; let utilities = UTILITIES.newCloudUtilities(bot, logger); - let thisBotStorage = BLOB_STORAGE.newBlobStorage(bot, logger); + + const FILE_STORAGE = require('./Integrations/FileStorage.js'); + let botStorage = FILE_STORAGE.newFileStorage(); let dataDependencies; @@ -35,19 +37,8 @@ dataDependencies = pDataDependencies; - thisBotStorage.initialize(bot.devTeam, onStorageInizialized); + callBackFunction(global.DEFAULT_OK_RESPONSE); - function onStorageInizialized(err) { - - if (err.result === global.DEFAULT_OK_RESPONSE.result) { - - callBackFunction(global.DEFAULT_OK_RESPONSE); - - } else { - logger.write(MODULE_NAME, "[ERROR] initializeStorage -> onStorageInizialized -> err = " + err.stack); - callBackFunction(err); - } - } } catch (err) { logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); @@ -68,7 +59,7 @@ let strategiesArray = []; let tradesArray = []; - let simulationLogic = {}; + let tradingSystem = {}; let lastObjectsArray = []; commons.initializeData(); @@ -113,13 +104,17 @@ strategiesArray, tradesArray, lastObjectsArray, - simulationLogic, timePeriod, currentDay, startDate, endDate, interExecutionMemory, - writeRecordsFile) + writeFiles) + + function writeFiles(pTradingSystem) { + tradingSystem = pTradingSystem + writeRecordsFile() + } function writeRecordsFile() { @@ -144,7 +139,7 @@ record.begin + "," + record.end + "," + record.type + "," + - record.rate + "," + + record.marketRate + "," + record.amount + "," + record.balanceA + "," + record.balanceB + "," + @@ -159,15 +154,21 @@ record.periods + "," + record.days + "," + record.anualizedRateOfReturn + "," + - record.sellRate + "," + + record.positionRate + "," + record.lastProfitPercent + "," + record.strategy + "," + - record.strategyPhase + "," + - record.buyOrder + "," + + record.strategyStageNumber + "," + + record.takeProfit + "," + record.stopLossPhase + "," + - record.buyOrderPhase + "," + + record.takeProfitPhase + "," + JSON.stringify(record.orderRecord) + "," + - record.sellAmount + "]"; + record.positionSize + "," + + record.initialBalanceA + "," + + record.minimunBalanceA + "," + + record.maximunBalanceA + "," + + record.initialBalanceB + "," + + record.minimunBalanceB + "," + + record.maximunBalanceB + "]"; if (separator === "") { separator = ","; } @@ -180,10 +181,11 @@ 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.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + 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 - thisBotStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated); + botStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); function onFileCreated(err) { @@ -244,12 +246,14 @@ if (record.begin < currentDay.valueOf()) { continue; } - for (let j = 0; j < record.length - 1; j++) { + for (let j = 0; j < record.length - 3; j++) { conditions = conditions + conditionsSeparator + record[j]; if (conditionsSeparator === "") { conditionsSeparator = ","; } } - conditions = conditions + conditionsSeparator + '[' + record[record.length - 1] + ']'; // The last item contains an Array of condition values. + 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 + ']'; @@ -260,15 +264,16 @@ } fileContent = "[" + fileContent + "]"; - fileContent = "[" + JSON.stringify(simulationLogic) + "," + JSON.stringify(lastObjectsArray) + "," + 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.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + 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 - thisBotStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated); + botStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); function onFileCreated(err) { @@ -341,10 +346,11 @@ 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.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + 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 - thisBotStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated); + botStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); function onFileCreated(err) { @@ -420,10 +426,11 @@ 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.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + 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 - thisBotStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated); + botStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); function onFileCreated(err) { diff --git a/src/storage/templates/Simulator/User.Bot.Market.js b/src/storage/templates/Simulator/User.Bot.Market.js index d0aaf744c..699cfc583 100644 --- a/src/storage/templates/Simulator/User.Bot.Market.js +++ b/src/storage/templates/Simulator/User.Bot.Market.js @@ -1,4 +1,4 @@ -exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, BLOB_STORAGE, FILE_STORAGE) { +exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES) { const FULL_LOG = true; const LOG_FILE_CONTENT = false; @@ -17,8 +17,8 @@ start: start }; - let utilities = UTILITIES.newCloudUtilities(bot, logger); - let thisBotStorage = BLOB_STORAGE.newBlobStorage(bot, logger); + const FILE_STORAGE = require('./Integrations/FileStorage.js'); + let botStorage = FILE_STORAGE.newFileStorage(); let dataDependencies; @@ -35,19 +35,8 @@ dataDependencies = pDataDependencies; - thisBotStorage.initialize(bot.devTeam, onStorageInizialized); + callBackFunction(global.DEFAULT_OK_RESPONSE); - function onStorageInizialized(err) { - - if (err.result === global.DEFAULT_OK_RESPONSE.result) { - - callBackFunction(global.DEFAULT_OK_RESPONSE); - - } else { - logger.write(MODULE_NAME, "[ERROR] initializeStorage -> onStorageInizialized -> err = " + err.stack); - callBackFunction(err); - } - } } catch (err) { logger.write(MODULE_NAME, "[ERROR] initialize -> err = " + err.stack); callBackFunction(global.DEFAULT_FAIL_RESPONSE); @@ -69,7 +58,7 @@ let tradesArray = []; let lastObjectsArray = []; - let simulationLogic = {}; + let tradingSystem = {}; let interExecutionMemory = {}; let currentDay = bot.processDatetime; @@ -115,13 +104,17 @@ strategiesArray, tradesArray, lastObjectsArray, - simulationLogic, timePeriod, currentDay, startDate, endDate, interExecutionMemory, - writeRecordsFile) + writeFiles) + + function writeFiles(pTradingSystem) { + tradingSystem = pTradingSystem + writeRecordsFile() + } function writeRecordsFile() { @@ -142,7 +135,7 @@ record.begin + "," + record.end + "," + record.type + "," + - record.rate + "," + + record.marketRate + "," + record.amount + "," + record.balanceA + "," + record.balanceB + "," + @@ -157,16 +150,21 @@ record.periods + "," + record.days + "," + record.anualizedRateOfReturn + "," + - record.sellRate + "," + + record.positionRate + "," + record.lastProfitPercent + "," + record.strategy + "," + - record.strategyPhase + "," + - record.buyOrder + "," + + record.strategyStageNumber + "," + + record.takeProfit + "," + record.stopLossPhase + "," + - record.buyOrderPhase + "," + - + record.takeProfitPhase + "," + JSON.stringify(record.orderRecord) + "," + - record.sellAmount + "]"; + record.positionSize + "," + + record.initialBalanceA + "," + + record.minimunBalanceA + "," + + record.maximunBalanceA + "," + + record.initialBalanceB + "," + + record.minimunBalanceB + "," + + record.maximunBalanceB + "]"; if (separator === "") { separator = ","; } @@ -178,10 +176,11 @@ let fileName = '' + market.assetA + '_' + market.assetB + '.json'; - let filePathRoot = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + global.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + 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 - thisBotStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated); + botStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); function onFileCreated(err) { @@ -234,12 +233,14 @@ let conditions = ""; let conditionsSeparator = ""; - for (let j = 0; j < record.length - 1; j++) { + for (let j = 0; j < record.length - 3; j++) { conditions = conditions + conditionsSeparator + record[j]; if (conditionsSeparator === "") { conditionsSeparator = ","; } } - conditions = conditions + conditionsSeparator + '[' + record[record.length - 1] + ']'; // The last item contains an Array of condition values. + 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 + ']'; @@ -250,13 +251,14 @@ } fileContent = "[" + fileContent + "]"; - fileContent = "[" + JSON.stringify(simulationLogic) + "," + JSON.stringify(lastObjectsArray) + "," + 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.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + 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 - thisBotStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated); + botStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); function onFileCreated(err) { @@ -322,10 +324,11 @@ fileContent = "[" + fileContent + "]"; let fileName = '' + market.assetA + '_' + market.assetB + '.json'; - let filePathRoot = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + global.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + 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 - thisBotStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated); + botStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); function onFileCreated(err) { @@ -394,10 +397,11 @@ fileContent = "[" + fileContent + "]"; let fileName = '' + market.assetA + '_' + market.assetB + '.json'; - let filePathRoot = bot.devTeam + "/" + bot.codeName + "." + bot.version.major + "." + bot.version.minor + "/" + global.PLATFORM_CONFIG.codeName + "." + global.PLATFORM_CONFIG.version.major + "." + global.PLATFORM_CONFIG.version.minor + "/" + global.EXCHANGE_NAME + "/" + bot.dataSetVersion; + 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 - thisBotStorage.createTextFile(filePath, fileName, fileContent + '\n', onFileCreated); + botStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); function onFileCreated(err) { diff --git a/src/storage/templates/Simulator/this.bot.config.json b/src/storage/templates/Simulator/this.bot.config.json index 2f17c31ad..b6e0d1b8f 100644 --- a/src/storage/templates/Simulator/this.bot.config.json +++ b/src/storage/templates/Simulator/this.bot.config.json @@ -389,6 +389,3 @@ } ] } - - - From 5193cf891b76a6575a0a3f6a8ad08bc039b0c15d Mon Sep 17 00:00:00 2001 From: Matias Date: Sat, 22 Jun 2019 19:23:46 -0300 Subject: [PATCH 10/34] Superalgos node v1 --- src/app.js | 4 ++ src/config/ecosystem.json | 24 ++++++------ src/schema/queries/PlotterCode.js | 37 +++++++++++-------- src/schema/types/input/File.js | 2 +- .../templates/Simulator/User.Bot.Daily.js | 17 ++++----- .../templates/Simulator/User.Bot.Market.js | 17 ++++----- .../templates/TradingBot/this.bot.config.json | 6 +-- 7 files changed, 56 insertions(+), 51 deletions(-) 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/schema/queries/PlotterCode.js b/src/schema/queries/PlotterCode.js index f0c81fb5f..b16afd5c6 100644 --- a/src/schema/queries/PlotterCode.js +++ b/src/schema/queries/PlotterCode.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,28 +11,33 @@ const resolve = async (parent, { file }, context) => { try { - // 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 + 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) } - if (file.storage === '' || file.accessKey === '') { - throw "Wrong parameters." - } - - let storage = require('../../storage/providers/AzureStorage') - let fileContent = await storage.getFileContentRemote(file.container, file.filePath, file.storage, file.accessKey) return fileContent } catch (err) { - logger.error('getPlotterCode -> Error: %s', err.stack) + logger.error('getPlotterCode -> Error: %s', err) + logger.error('getPlotterCode -> Stack: %s', err.stack) throw err } } 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/templates/Simulator/User.Bot.Daily.js b/src/storage/templates/Simulator/User.Bot.Daily.js index 1b03337f1..7f2baca56 100644 --- a/src/storage/templates/Simulator/User.Bot.Daily.js +++ b/src/storage/templates/Simulator/User.Bot.Daily.js @@ -1,4 +1,4 @@ -exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES) { +exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, fileStorage) { const FULL_LOG = true; const LOG_FILE_CONTENT = false; @@ -19,9 +19,6 @@ let utilities = UTILITIES.newCloudUtilities(bot, logger); - const FILE_STORAGE = require('./Integrations/FileStorage.js'); - let botStorage = FILE_STORAGE.newFileStorage(); - let dataDependencies; return thisObject; @@ -113,7 +110,7 @@ function writeFiles(pTradingSystem) { tradingSystem = pTradingSystem - writeRecordsFile() + writeRecordsFile() } function writeRecordsFile() { @@ -168,7 +165,7 @@ record.maximunBalanceA + "," + record.initialBalanceB + "," + record.minimunBalanceB + "," + - record.maximunBalanceB + "]"; + record.maximunBalanceB + "]"; if (separator === "") { separator = ","; } @@ -185,7 +182,7 @@ let filePath = filePathRoot + "/Output/" + SIMULATED_RECORDS_FOLDER_NAME + "/" + "Multi-Period-Daily" + "/" + outputPeriodLabel + "/" + dateForPath; filePath += '/' + fileName - botStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); + fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); function onFileCreated(err) { @@ -273,7 +270,7 @@ let filePath = filePathRoot + "/Output/" + CONDITIONS_FOLDER_NAME + "/" + "Multi-Period-Daily" + "/" + outputPeriodLabel + "/" + dateForPath; filePath += '/' + fileName - botStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); + fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); function onFileCreated(err) { @@ -350,7 +347,7 @@ let filePath = filePathRoot + "/Output/" + STRATEGIES_FOLDER_NAME + "/" + "Multi-Period-Daily" + "/" + outputPeriodLabel + "/" + dateForPath; filePath += '/' + fileName - botStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); + fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); function onFileCreated(err) { @@ -430,7 +427,7 @@ let filePath = filePathRoot + "/Output/" + TRADES_FOLDER_NAME + "/" + "Multi-Period-Daily" + "/" + outputPeriodLabel + "/" + dateForPath; filePath += '/' + fileName - botStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); + fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); function onFileCreated(err) { diff --git a/src/storage/templates/Simulator/User.Bot.Market.js b/src/storage/templates/Simulator/User.Bot.Market.js index 699cfc583..52658e30e 100644 --- a/src/storage/templates/Simulator/User.Bot.Market.js +++ b/src/storage/templates/Simulator/User.Bot.Market.js @@ -1,4 +1,4 @@ -exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES) { +exports.newUserBot = function newUserBot(bot, logger, COMMONS, UTILITIES, fileStorage) { const FULL_LOG = true; const LOG_FILE_CONTENT = false; @@ -17,9 +17,6 @@ start: start }; - const FILE_STORAGE = require('./Integrations/FileStorage.js'); - let botStorage = FILE_STORAGE.newFileStorage(); - let dataDependencies; return thisObject; @@ -164,8 +161,8 @@ record.maximunBalanceA + "," + record.initialBalanceB + "," + record.minimunBalanceB + "," + - record.maximunBalanceB + "]"; - + record.maximunBalanceB + "]"; + if (separator === "") { separator = ","; } fileRecordCounter++; @@ -180,7 +177,7 @@ let filePath = filePathRoot + "/Output/" + SIMULATED_RECORDS_FOLDER_NAME + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel; filePath += '/' + fileName - botStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); + fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); function onFileCreated(err) { @@ -258,7 +255,7 @@ let filePath = filePathRoot + "/Output/" + CONDITIONS_FOLDER_NAME + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel; filePath += '/' + fileName - botStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); + fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); function onFileCreated(err) { @@ -328,7 +325,7 @@ let filePath = filePathRoot + "/Output/" + STRATEGIES_FOLDER_NAME + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel; filePath += '/' + fileName - botStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); + fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); function onFileCreated(err) { @@ -401,7 +398,7 @@ let filePath = filePathRoot + "/Output/" + TRADES_FOLDER_NAME + "/" + "Multi-Period-Market" + "/" + outputPeriodLabel; filePath += '/' + fileName - botStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); + fileStorage.createTextFile(bot.devTeam, filePath, fileContent + '\n', onFileCreated); function onFileCreated(err) { diff --git a/src/storage/templates/TradingBot/this.bot.config.json b/src/storage/templates/TradingBot/this.bot.config.json index 9074de4d5..c3d22f52e 100644 --- a/src/storage/templates/TradingBot/this.bot.config.json +++ b/src/storage/templates/TradingBot/this.bot.config.json @@ -232,10 +232,10 @@ "devTeam": "AAMasters", "bot": "AABruce", "host": { - "url": "http://localhost:4100/", - "storage": "https://aadevelop.blob.core.windows.net", + "url": "https://demo-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": "" }, "botVersion": { "major": 1, From 1b58955a177ffe6d6ca10c896bdcd703e855b518 Mon Sep 17 00:00:00 2001 From: Matias Date: Mon, 24 Jun 2019 20:52:38 -0300 Subject: [PATCH 11/34] Adding template fixes --- src/schema/queries/FileContent.js | 3 + src/storage/templates/Simulator/Commons.js | 111 +++++++++--------- .../templates/Simulator/User.Bot.Daily.js | 1 + .../templates/Simulator/User.Bot.Market.js | 1 + .../templates/Simulator/this.bot.config.json | 12 +- 5 files changed, 63 insertions(+), 65 deletions(-) diff --git a/src/schema/queries/FileContent.js b/src/schema/queries/FileContent.js index 7c6e5e532..348714f25 100644 --- a/src/schema/queries/FileContent.js +++ b/src/schema/queries/FileContent.js @@ -22,6 +22,9 @@ const resolve = async (parent, { file }, context) => { return fileContent } catch (err) { + if(err.message === 'The specified key does not exist.') + logger.warn('getFileContent -> Error: %s', err.message) + else logger.error('getFileContent -> Error: %s', err.message) throw err } diff --git a/src/storage/templates/Simulator/Commons.js b/src/storage/templates/Simulator/Commons.js index d0f98ae4b..fd0fc4237 100644 --- a/src/storage/templates/Simulator/Commons.js +++ b/src/storage/templates/Simulator/Commons.js @@ -73,7 +73,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { /* Initial Default Values */ - let initialDate = startDate; + let initialDate = startDate; const DEFAULT_BASE_ASSET_BALANCE = 1 const DEFAULT_BASE_ASSET_MINIMUN_BALANCE = 0.5 @@ -92,7 +92,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (tradingSystem.parameters !== undefined) { if (tradingSystem.parameters.baseAsset !== undefined) { if (tradingSystem.parameters.baseAsset.formula !== undefined) { - let receivedParameters + let receivedParameters try { receivedParameters = JSON.parse(tradingSystem.parameters.baseAsset.formula.code); @@ -159,7 +159,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { /* Strategy and Phases */ let currentStrategyIndex = -1; - let strategyStage = 'No Stage'; + let strategyStage = 'No Stage'; /* Stop Loss Management */ @@ -167,7 +167,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { const MAX_STOP_LOSS_VALUE = Number.MAX_SAFE_INTEGER let stopLoss = 0; let stopLossPhase = 0; - let stopLossStage = 'No Stage'; + let stopLossStage = 'No Stage'; /* Take Profit Management */ @@ -176,7 +176,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let previousTakeProfit = 0; let takeProfit = 0; let takeProfitPhase = 0; - let takeProfitStage = 'No Stage'; + let takeProfitStage = 'No Stage'; /* Simulation Records */ @@ -309,8 +309,8 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { yesterday.balanceAssetA = balanceAssetA; yesterday.balanceAssetB = balanceAssetB; - } - + } + lastProfit = interExecutionMemory.lastProfit; profit = interExecutionMemory.profit; lastProfitPercent = interExecutionMemory.lastProfitPercent; @@ -330,9 +330,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { 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++) { @@ -490,7 +490,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (phase.formula !== undefined) { try { - formulaValue = eval(phase.formula.code); + formulaValue = eval(phase.formula.code); if (formulaValue === Infinity) { formulaError= "Formula evaluates to Infinity." formulaValue = MAX_STOP_LOSS_VALUE @@ -502,12 +502,12 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { 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) + formulasErrors.push('"' + formulaError + '"') + formulasValues.push(formulaValue) + let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'stopLoss' + '-' + p; + formulas.set(key, formulaValue) + } /* next phase event */ let nextPhaseEvent = phase.nextPhaseEvent; @@ -555,12 +555,12 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { 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) + formulasErrors.push('"' + formulaError + '"') + formulasValues.push(formulaValue) + let key = j + '-' + 'openStage' + '-' + 'initialDefinition' + '-' + 'takeProfit' + '-' + p; + formulas.set(key, formulaValue) + } /* next phase event */ let nextPhaseEvent = phase.nextPhaseEvent; @@ -614,12 +614,12 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { 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) + formulasErrors.push('"' + formulaError + '"') + formulasValues.push(formulaValue) + let key = j + '-' + 'manageStage' + '-' + 'stopLoss' + '-' + p; + formulas.set(key, formulaValue) + } /* next phase event */ let nextPhaseEvent = phase.nextPhaseEvent; @@ -667,12 +667,12 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { 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) + formulasErrors.push('"' + formulaError + '"') + formulasValues.push(formulaValue) + let key = j + '-' + 'manageStage' + '-' + 'takeProfit' + '-' + p; + formulas.set(key, formulaValue) + } /* next phase event */ let nextPhaseEvent = phase.nextPhaseEvent; @@ -702,7 +702,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let condition; let error = '' - let value + let value try { value = eval(node.code); @@ -734,11 +734,11 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if ( strategyStage === 'No Stage' && - currentStrategyIndex === -1 + currentStrategyIndex === -1 ) { let minimunBalance let maximunBalance - let balance + let balance if (baseAsset === 'BTC') { balance = balanceAssetA @@ -756,12 +756,12 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { /* 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++) { @@ -869,7 +869,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } else { balanceAssetB = balanceAssetB + balanceAssetA * stopLoss; balanceAssetA = 0; - } + } if (currentDay !== undefined) { if (positionInstant < currentDay.valueOf()) { @@ -904,7 +904,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } else { balanceAssetB = balanceAssetB + balanceAssetA * takeProfit; balanceAssetA = 0; - } + } if (currentDay !== undefined) { if (positionInstant < currentDay.valueOf()) { @@ -920,7 +920,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { stopLossStage = 'No Stage'; takeProfitStage = 'No Stage'; currentTrade.end = candle.end; - currentTrade.status = 1; + currentTrade.status = 1; currentTrade.exitType = 2; currentTrade.endRate = takeProfit; @@ -995,7 +995,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let openStage = strategy.openStage let manageStage = strategy.manageStage - let parentNode + let parentNode let j = currentStrategyIndex let stageKey let initialDefinitionKey = '' @@ -1008,7 +1008,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { initialDefinitionKey = '-' + 'initialDefinition' stageKey = 'openStage' p = stopLossPhase - 1 - } + } } } @@ -1017,7 +1017,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { parentNode = manageStage stageKey = 'manageStage' p = stopLossPhase - 2 - } + } } let phase = parentNode.stopLoss.phases[p]; @@ -1056,7 +1056,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let strategy = tradingSystem.strategies[currentStrategyIndex]; let openStage = strategy.openStage let manageStage = strategy.manageStage - let phase + let phase let key if (stopLossStage === 'Open Stage' && openStage !== undefined) { @@ -1192,11 +1192,11 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { /* positionSize default is the whole balance if no size was defined by the user */ if (baseAsset === 'BTC') { - positionSize = balanceAssetA; + positionSize = balanceAssetA; } else { - positionSize = balanceAssetB; - } - + positionSize = balanceAssetB; + } + /* Check if the user defined a position size */ let strategy = tradingSystem.strategies[currentStrategyIndex]; let triggerStage = strategy.triggerStage @@ -1205,7 +1205,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (triggerStage.positionSize !== undefined) { if (triggerStage.positionSize.formula !== undefined) { try { - positionSize = eval(triggerStage.positionSize.formula.code); + positionSize = eval(triggerStage.positionSize.formula.code); } catch (err) { triggerStage.positionSize.formula.error = err.message } @@ -1214,7 +1214,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { positionSize = balanceAssetA; } else { positionSize = balanceAssetB; - } + } } } } @@ -1238,7 +1238,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } else { balanceAssetA = balanceAssetA + positionSize / marketRate; balanceAssetB = balanceAssetB - positionSize; - } + } positionInstant = candle.end; @@ -1265,7 +1265,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (currentDay !== undefined) { if (positionInstant < currentDay.valueOf()) { yesterday.Roundtrips++; - } + } } if (baseAsset === 'BTC') { @@ -1278,7 +1278,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { lastProfitPercent = lastProfit / previousBalanceAssetB * 100; if (isNaN(lastProfitPercent)) { lastProfitPercent = 0; } profit = balanceAssetB - initialBalanceB; - } + } if (currentDay !== undefined) { if (positionInstant < currentDay.valueOf()) { @@ -1290,7 +1290,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { currentTrade.lastProfitPercent = lastProfitPercent; currentTrade.stopRate = stopLoss; - + if (lastProfit > 0) { hits++; @@ -1318,7 +1318,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { ROI = (initialBalanceB + profit) / initialBalanceB - 1; hitRatio = hits / roundtrips; anualizedRateOfReturn = ROI / days * 365; - } + } if (currentDay !== undefined) { if (positionInstant < currentDay.valueOf()) { @@ -1421,7 +1421,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { "") } - let strategyStageNumber + let strategyStageNumber switch (strategyStage) { case 'No Stage': { strategyStageNumber = 0 @@ -1837,6 +1837,3 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } }; - - - diff --git a/src/storage/templates/Simulator/User.Bot.Daily.js b/src/storage/templates/Simulator/User.Bot.Daily.js index 7f2baca56..9bd5892a0 100644 --- a/src/storage/templates/Simulator/User.Bot.Daily.js +++ b/src/storage/templates/Simulator/User.Bot.Daily.js @@ -401,6 +401,7 @@ at the current date. */ if (record.end < currentDay.valueOf()) { continue; } + if (record.stopRate === undefined) { record.stopRate = 0} fileContent = fileContent + separator + '[' + record.begin + "," + diff --git a/src/storage/templates/Simulator/User.Bot.Market.js b/src/storage/templates/Simulator/User.Bot.Market.js index 52658e30e..8c976caed 100644 --- a/src/storage/templates/Simulator/User.Bot.Market.js +++ b/src/storage/templates/Simulator/User.Bot.Market.js @@ -374,6 +374,7 @@ for (let i = 0; i < tradesArray.length; i++) { let record = tradesArray[i]; + if (record.stopRate === undefined) { record.stopRate = 0 } fileContent = fileContent + separator + '[' + record.begin + "," + diff --git a/src/storage/templates/Simulator/this.bot.config.json b/src/storage/templates/Simulator/this.bot.config.json index b6e0d1b8f..d0d8572de 100644 --- a/src/storage/templates/Simulator/this.bot.config.json +++ b/src/storage/templates/Simulator/this.bot.config.json @@ -264,8 +264,7 @@ "devTeam": "AAMasters", "codeName": "PlottersTradingSimulation", "moduleName": "TradingSimulation" - }, - "relatedToTradingEngine": true + } }, { "codeName": "Simulation-Conditions", @@ -304,8 +303,7 @@ "devTeam": "AAMasters", "codeName": "PlottersTradingSimulation", "moduleName": "Conditions" - }, - "relatedToTradingEngine": true + } }, { "codeName": "Simulation-Strategies", @@ -344,8 +342,7 @@ "devTeam": "AAMasters", "codeName": "PlottersTradingSimulation", "moduleName": "Strategies" - }, - "relatedToTradingEngine": true + } }, { "codeName": "Simulation-Trades", @@ -384,8 +381,7 @@ "devTeam": "AAMasters", "codeName": "PlottersTradingSimulation", "moduleName": "Trades" - }, - "relatedToTradingEngine": true + } } ] } From dbf137315b538079c69fed2bc55dee58c043752d Mon Sep 17 00:00:00 2001 From: Matias Date: Sat, 29 Jun 2019 15:01:17 -0300 Subject: [PATCH 12/34] Adding container to log --- src/schema/queries/FileContent.js | 2 +- src/schema/queries/PlotterCode.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/schema/queries/FileContent.js b/src/schema/queries/FileContent.js index 348714f25..ec30b555f 100644 --- a/src/schema/queries/FileContent.js +++ b/src/schema/queries/FileContent.js @@ -7,7 +7,7 @@ import { getFileContentRemote } from '../../storage/providers/AzureStorage' export const args = { file: { type: FileInputType } } const resolve = async (parent, { file }, context) => { - logger.debug('getFileContent -> Entering Function: ' + file.filePath) + logger.debug('getFileContent -> Entering Function: ' + file.container + '/' + file.filePath) try { diff --git a/src/schema/queries/PlotterCode.js b/src/schema/queries/PlotterCode.js index b16afd5c6..6008bd919 100644 --- a/src/schema/queries/PlotterCode.js +++ b/src/schema/queries/PlotterCode.js @@ -7,7 +7,7 @@ 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) + logger.debug('getPlotterCode -> Entering Function: ' + file.container + '/' + file.filePath) try { From a1acacbe1c8aaa37020a5209d98608bf2db06dd1 Mon Sep 17 00:00:00 2001 From: Matias Date: Sat, 29 Jun 2019 15:01:28 -0300 Subject: [PATCH 13/34] bot updates --- src/storage/templates/Simulator/Commons.js | 290 +++++++++++---------- 1 file changed, 157 insertions(+), 133 deletions(-) diff --git a/src/storage/templates/Simulator/Commons.js b/src/storage/templates/Simulator/Commons.js index fd0fc4237..f408aac7f 100644 --- a/src/storage/templates/Simulator/Commons.js +++ b/src/storage/templates/Simulator/Commons.js @@ -398,6 +398,14 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { 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 set some variables that would be nice if they have a value before the formulas are calculated. */ + + if (strategyStage === 'Trigger Stage' || strategyStage === 'No Stage') { + + positionRate = candle.close; + + } + /* We define and evaluate all conditions to be used later during the simulation loop. */ conditionsArrayRecord.push(candle.begin); @@ -405,7 +413,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { evaluateConditionsAndFormulas(tradingSystem, conditions); - function evaluateConditionsAndFormulas(tradingSystem, conditions) { + function evaluateConditionsAndFormulas(tradingSystem, conditions) { for (let j = 0; j < tradingSystem.strategies.length; j++) { @@ -731,7 +739,6 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } - if ( strategyStage === 'No Stage' && currentStrategyIndex === -1 @@ -849,88 +856,6 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } - /* 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') { @@ -1075,8 +1000,10 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } - if (phase.formula !== undefined) { - stopLoss = formulas.get(key) + if (phase !== undefined) { + if (phase.formula !== undefined) { + stopLoss = formulas.get(key) + } } } @@ -1178,8 +1105,92 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } - if (phase.formula !== undefined) { - takeProfit = formulas.get(key) + if (phase !== undefined) { + if (phase.formula !== undefined) { + takeProfit = formulas.get(key) + } + } + } + + /* 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; } } @@ -1215,13 +1226,18 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } else { positionSize = balanceAssetB; } + } else { + if (baseAsset === 'BTC') { + if (positionSize > balanceAssetA) { positionSize = balanceAssetA} + } else { + if (positionSize > balanceAssetB) { positionSize = balanceAssetB} + } } } } } marketRate = candle.close; - positionRate = marketRate; calculateStopLoss(); calculateTakeProfit(); @@ -1233,10 +1249,10 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { lastProfitPercent = 0; if (baseAsset === 'BTC') { - balanceAssetB = balanceAssetB + positionSize * marketRate; + balanceAssetB = balanceAssetB + positionSize * positionRate; balanceAssetA = balanceAssetA - positionSize; } else { - balanceAssetA = balanceAssetA + positionSize / marketRate; + balanceAssetA = balanceAssetA + positionSize / positionRate; balanceAssetB = balanceAssetB - positionSize; } @@ -1372,53 +1388,53 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } 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, - "") + 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, - "") + messageId, + MESSAGE_ENTITY.SimulationEngine, + MESSAGE_ENTITY.SimulationExecutor, + MESSAGE_TYPE.HeartBeat, + (new Date()).valueOf(), + "", + "", + 0, + "", + "", + "", + 0, + "", + 0, + 0, + 0, + "", + 0, + "", + 0, + "") } let strategyStageNumber @@ -1445,6 +1461,14 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } + if (balanceAssetA === Infinity) { + balanceAssetA = Number.MAX_SAFE_INTEGER + } + + if (balanceAssetB === Infinity) { + balanceAssetB = Number.MAX_SAFE_INTEGER + } + simulationRecord = { begin: candle.begin, end: candle.end, From 8de0ab20a857c159f3dd76ba78bd74d93c683e41 Mon Sep 17 00:00:00 2001 From: Matias Date: Sat, 29 Jun 2019 15:04:19 -0300 Subject: [PATCH 14/34] Simulator template update --- src/storage/templates/Simulator/Commons.js | 211 +++++++++--------- .../templates/Simulator/User.Bot.Daily.js | 41 ++-- .../templates/Simulator/User.Bot.Market.js | 42 ++-- 3 files changed, 155 insertions(+), 139 deletions(-) diff --git a/src/storage/templates/Simulator/Commons.js b/src/storage/templates/Simulator/Commons.js index f408aac7f..570286d26 100644 --- a/src/storage/templates/Simulator/Commons.js +++ b/src/storage/templates/Simulator/Commons.js @@ -53,38 +53,41 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } async function runSimulation( - recordsArray, - conditionsArray, - strategiesArray, - tradesArray, - lastObjectsArray, timePeriod, currentDay, startDate, endDate, interExecutionMemory, - callback) { + callback, + callBackFunction) { try { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> Entering function."); } + let recordsArray = []; + let conditionsArray = []; + let strategiesArray = []; + let tradesArray = []; + let lastObjectsArray = []; + let tradingSystem = await strategy.getStrategy(); /* Initial Default Values */ let initialDate = startDate; + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> initialDate = " + initialDate); } 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 minimumBalanceA = DEFAULT_BASE_ASSET_MINIMUN_BALANCE + let maximumBalanceA = DEFAULT_BASE_ASSET_MAXIMUN_BALANCE let initialBalanceB = 0 - let minimunBalanceB = 0 - let maximunBalanceB = 0 + let minimumBalanceB = 0 + let maximumBalanceB = 0 let baseAsset = 'BTC' /* Parameters Processing */ @@ -112,19 +115,19 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { initialBalanceA = DEFAULT_BASE_ASSET_BALANCE; initialBalanceB = 0 } - if (receivedParameters.minimunBalance !== undefined) { - minimunBalanceA = receivedParameters.minimunBalance; - minimunBalanceB = 0 + if (receivedParameters.minimumBalance !== undefined) { + minimumBalanceA = receivedParameters.minimumBalance; + minimumBalanceB = 0 } else { - minimunBalanceA = DEFAULT_BASE_ASSET_MINIMUN_BALANCE; - minimunBalanceB = 0 + minimumBalanceA = DEFAULT_BASE_ASSET_MINIMUN_BALANCE; + minimumBalanceB = 0 } - if (receivedParameters.maximunBalance !== undefined) { - maximunBalanceA = receivedParameters.maximunBalance; - maximunBalanceB = 0 + if (receivedParameters.maximumBalance !== undefined) { + maximumBalanceA = receivedParameters.maximumBalance; + maximumBalanceB = 0 } else { - maximunBalanceA = DEFAULT_BASE_ASSET_MAXIMUN_BALANCE; - maximunBalanceB = 0 + maximumBalanceA = DEFAULT_BASE_ASSET_MAXIMUN_BALANCE; + maximumBalanceB = 0 } } else { if (receivedParameters.initialBalance !== undefined) { @@ -134,19 +137,19 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { initialBalanceB = DEFAULT_BASE_ASSET_BALANCE; initialBalanceA = 0 } - if (receivedParameters.minimunBalance !== undefined) { - minimunBalanceB = receivedParameters.minimunBalance; - minimunBalanceA = 0 + if (receivedParameters.minimumBalance !== undefined) { + minimumBalanceB = receivedParameters.minimumBalance; + minimumBalanceA = 0 } else { - minimunBalanceB = DEFAULT_BASE_ASSET_MINIMUN_BALANCE; - minimunBalanceA = 0 + minimumBalanceB = DEFAULT_BASE_ASSET_MINIMUN_BALANCE; + minimumBalanceA = 0 } - if (receivedParameters.maximunBalance !== undefined) { - maximunBalanceB = receivedParameters.maximunBalance; - maximunBalanceA = 0 + if (receivedParameters.maximumBalance !== undefined) { + maximumBalanceB = receivedParameters.maximumBalance; + maximumBalanceA = 0 } else { - maximunBalanceB = DEFAULT_BASE_ASSET_MAXIMUN_BALANCE; - maximunBalanceA = 0 + maximumBalanceB = DEFAULT_BASE_ASSET_MAXIMUN_BALANCE; + maximumBalanceA = 0 } } } catch (err) { @@ -239,9 +242,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let balanceAssetA = initialBalanceA; let balanceAssetB = initialBalanceB; - let lastProfit = 0; + let lastTradeProfitLoss = 0; let profit = 0; - let lastProfitPercent = 0; + let lastTradeROI = 0; let roundtrips = 0; let fails = 0; @@ -258,9 +261,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { yesterday.balanceAssetA = balanceAssetA; yesterday.balanceAssetB = balanceAssetB; - yesterday.lastProfit = 0; + yesterday.lastTradeProfitLoss = 0; yesterday.profit = 0; - yesterday.lastProfitPercent = 0; + yesterday.lastTradeROI = 0; yesterday.Roundtrips = 0; yesterday.fails = 0; @@ -281,9 +284,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { interExecutionMemory.balanceAssetA = balanceAssetA; interExecutionMemory.balanceAssetB = balanceAssetB; - interExecutionMemory.lastProfit = lastProfit; + interExecutionMemory.lastTradeProfitLoss = lastTradeProfitLoss; interExecutionMemory.profit = profit; - interExecutionMemory.lastProfitPercent = lastProfitPercent; + interExecutionMemory.lastTradeROI = lastTradeROI; interExecutionMemory.roundtrips = 0; interExecutionMemory.fails = 0; @@ -311,9 +314,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } - lastProfit = interExecutionMemory.lastProfit; + lastTradeProfitLoss = interExecutionMemory.lastTradeProfitLoss; profit = interExecutionMemory.profit; - lastProfitPercent = interExecutionMemory.lastProfitPercent; + lastTradeROI = interExecutionMemory.lastTradeROI; roundtrips = interExecutionMemory.roundtrips; fails = interExecutionMemory.fails; @@ -334,6 +337,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } /* Main Simulation Loop: We go thourgh all the candles at this time period. */ + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> Main Simulation Loop -> candles.length = " + candles.length); } for (let i = 0; i < candles.length; i++) { @@ -398,14 +402,6 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { 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 set some variables that would be nice if they have a value before the formulas are calculated. */ - - if (strategyStage === 'Trigger Stage' || strategyStage === 'No Stage') { - - positionRate = candle.close; - - } - /* We define and evaluate all conditions to be used later during the simulation loop. */ conditionsArrayRecord.push(candle.begin); @@ -739,26 +735,26 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } + /* Trigger On Conditions */ if ( strategyStage === 'No Stage' && currentStrategyIndex === -1 ) { - let minimunBalance - let maximunBalance + let minimumBalance + let maximumBalance let balance if (baseAsset === 'BTC') { balance = balanceAssetA - minimunBalance = minimunBalanceA - maximunBalance = maximunBalanceA + minimumBalance = minimumBalanceA + maximumBalance = maximumBalanceA } else { balance = balanceAssetB - minimunBalance = minimunBalanceB - maximunBalance = maximunBalanceB + minimumBalance = minimumBalanceB + maximumBalance = maximumBalanceB } - if (balance > minimunBalance && balance < maximunBalance) { - /* Trigger On Conditions */ + if (balance > minimumBalance && balance < maximumBalance) { /* Here we need to pick a strategy, or if there is not suitable strategy for the current @@ -791,7 +787,10 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let condition = situation.conditions[m]; let key = j + '-' + 'triggerStage' + '-' + 'triggerOn' + '-' + k + '-' + m; - let value = conditions.get(key).value; + let value = false + if (conditions.get(key) !== undefined) { + value = conditions.get(key).value; + } if (value === false) { passed = false; } } @@ -810,12 +809,16 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } } else { - tradingSystem.error = "Balance below the minimun. No more strategies will be executed." + if (balance < minimumBalance) { + tradingSystem.error = "Balance below the minimum." + } + if (balance > maximumBalance) { + tradingSystem.error = "Balance above the maximum." + } } } /* Trigger Off Condition */ - if (strategyStage === 'Trigger Stage') { let strategy = tradingSystem.strategies[currentStrategyIndex]; @@ -836,7 +839,10 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let condition = situation.conditions[m]; let key = currentStrategyIndex + '-' + 'triggerStage' + '-' + 'triggerOff' + '-' + k + '-' + m; - let value = conditions.get(key).value; + let value = false + if (conditions.get(key) !== undefined) { + value = conditions.get(key).value; + } if (value === false) { passed = false; } } @@ -857,7 +863,6 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } /* Take Position Condition */ - if (strategyStage === 'Trigger Stage') { let strategy = tradingSystem.strategies[currentStrategyIndex]; @@ -878,7 +883,10 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let condition = situation.conditions[m]; let key = currentStrategyIndex + '-' + 'triggerStage' + '-' + 'takePosition' + '-' + k + '-' + m; - let value = conditions.get(key).value; + let value = false + if (conditions.get(key) !== undefined) { + value = conditions.get(key).value; + } if (value === false) { passed = false; } } @@ -903,10 +911,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } /* Stop Loss Management */ - if ( - strategyStage === 'Open Stage' || - strategyStage === 'Manage Stage' + (strategyStage === 'Open Stage' || strategyStage === 'Manage Stage') && + takePositionNow !== true ) { checkStopPhases() @@ -960,7 +967,10 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let condition = situation.conditions[m]; let key = j + '-' + stageKey + initialDefinitionKey + '-' + 'stopLoss' + '-' + p + '-' + k + '-' + m; - let value = conditions.get(key).value; + let value = false + if (conditions.get(key) !== undefined) { + value = conditions.get(key).value; + } if (value === false) { passed = false; } } @@ -1008,10 +1018,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } /* Take Profit Management */ - if ( - strategyStage === 'Open Stage' || - strategyStage === 'Manage Stage' + (strategyStage === 'Open Stage' || strategyStage === 'Manage Stage') && + takePositionNow !== true ) { checkTakeProfitPhases(); @@ -1065,7 +1074,10 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let condition = situation.conditions[m]; let key = j + '-' + stageKey + initialDefinitionKey + '-' + 'takeProfit' + '-' + p + '-' + k + '-' + m; - let value = conditions.get(key).value; + let value = false + if (conditions.get(key) !== undefined) { + value = conditions.get(key).value; + } if (value === false) { passed = false; } } @@ -1113,10 +1125,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } /* Checking if Stop or Take Profit were hit */ - if ( - strategyStage === 'Open Stage' || - strategyStage === 'Manage Stage' + (strategyStage === 'Open Stage' || strategyStage === 'Manage Stage') && + takePositionNow !== true ) { /* Checking what happened since the last execution. We need to know if the Stop Loss @@ -1195,7 +1206,6 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } /* Taking a Position */ - if ( takePositionNow === true ) { @@ -1238,15 +1248,13 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } marketRate = candle.close; - - calculateStopLoss(); - calculateTakeProfit(); + positionRate = candle.close; previousBalanceAssetA = balanceAssetA; previousBalanceAssetB = balanceAssetB; - lastProfit = 0; - lastProfitPercent = 0; + lastTradeProfitLoss = 0; + lastTradeROI = 0; if (baseAsset === 'BTC') { balanceAssetB = balanceAssetB + positionSize * positionRate; @@ -1263,8 +1271,8 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { yesterday.balanceAssetA = balanceAssetA; yesterday.balanceAssetB = balanceAssetB; - yesterday.lastProfit = lastProfit; - yesterday.lastProfitPercent = lastProfitPercent; + yesterday.lastTradeProfitLoss = lastTradeProfitLoss; + yesterday.lastTradeROI = lastTradeROI; } } @@ -1273,7 +1281,6 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } /* Closing a Position */ - if (strategyStage === 'Close Stage') { roundtrips++; @@ -1285,29 +1292,29 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } if (baseAsset === 'BTC') { - lastProfit = balanceAssetA - previousBalanceAssetA; - lastProfitPercent = lastProfit / previousBalanceAssetA * 100; - if (isNaN(lastProfitPercent)) { lastProfitPercent = 0; } + lastTradeProfitLoss = balanceAssetA - previousBalanceAssetA; + lastTradeROI = lastTradeProfitLoss * 100 / positionSize; + if (isNaN(lastTradeROI)) { lastTradeROI = 0; } profit = balanceAssetA - initialBalanceA; } else { - lastProfit = balanceAssetB - previousBalanceAssetB; - lastProfitPercent = lastProfit / previousBalanceAssetB * 100; - if (isNaN(lastProfitPercent)) { lastProfitPercent = 0; } + lastTradeProfitLoss = balanceAssetB - previousBalanceAssetB; + lastTradeROI = lastTradeProfitLoss * 100 / positionSize; + if (isNaN(lastTradeROI)) { lastTradeROI = 0; } profit = balanceAssetB - initialBalanceB; } if (currentDay !== undefined) { if (positionInstant < currentDay.valueOf()) { - yesterday.lastProfit = lastProfit; + yesterday.lastTradeProfitLoss = lastTradeProfitLoss; yesterday.profit = profit; - yesterday.lastProfitPercent = lastProfitPercent; + yesterday.lastTradeROI = lastTradeROI; } } - currentTrade.lastProfitPercent = lastProfitPercent; + currentTrade.lastTradeROI = lastTradeROI; currentTrade.stopRate = stopLoss; - if (lastProfit > 0) { + if (lastTradeProfitLoss > 0) { hits++; if (currentDay !== undefined) { @@ -1478,7 +1485,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { balanceA: balanceAssetA, balanceB: balanceAssetB, profit: profit, - lastProfit: lastProfit, + lastTradeProfitLoss: lastTradeProfitLoss, stopLoss: stopLoss, roundtrips: roundtrips, hits: hits, @@ -1489,7 +1496,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { days: days, anualizedRateOfReturn: anualizedRateOfReturn, positionRate: positionRate, - lastProfitPercent: lastProfitPercent, + lastTradeROI: lastTradeROI, strategy: currentStrategyIndex, strategyStageNumber: strategyStageNumber, takeProfit: takeProfit, @@ -1498,11 +1505,11 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { orderRecord: orderRecord, positionSize: positionSize, initialBalanceA: initialBalanceA, - minimunBalanceA: minimunBalanceA, - maximunBalanceA: maximunBalanceA, + minimumBalanceA: minimumBalanceA, + maximumBalanceA: maximumBalanceA, initialBalanceB: initialBalanceB, - minimunBalanceB: minimunBalanceB, - maximunBalanceB: maximunBalanceB + minimumBalanceB: minimumBalanceB, + maximumBalanceB: maximumBalanceB } recordsArray.push(simulationRecord); @@ -1547,7 +1554,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { (currentTrade.begin !== 0 && i === candles.length - 1 && lastCandle.end !== lastInstantOfTheDay) ) { - currentTrade.profit = lastProfit; + currentTrade.profit = lastTradeProfitLoss; tradesArray.push(currentTrade); @@ -1555,7 +1562,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { begin: 0, end: 0, status: 0, - lastProfitPercent: 0, + lastTradeROI: 0, exitType: 0, beginRate: 0, endRate: 0 @@ -1575,9 +1582,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { interExecutionMemory.balanceAssetA = yesterday.balanceAssetA; interExecutionMemory.balanceAssetB = yesterday.balanceAssetB; - interExecutionMemory.lastProfit = yesterday.lastProfit; + interExecutionMemory.lastTradeProfitLoss = yesterday.lastTradeProfitLoss; interExecutionMemory.profit = yesterday.profit; - interExecutionMemory.lastProfitPercent = yesterday.lastProfitPercent; + interExecutionMemory.lastTradeROI = yesterday.lastTradeROI; interExecutionMemory.roundtrips = interExecutionMemory.roundtrips + yesterday.Roundtrips; interExecutionMemory.fails = interExecutionMemory.fails + yesterday.fails; @@ -1593,7 +1600,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } - callback(tradingSystem); + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> callback -> recordsArray.length = " + recordsArray.length); } + + callback(tradingSystem, recordsArray, conditionsArray, strategiesArray, tradesArray, lastObjectsArray); function getElement(pArray, begin, end) { diff --git a/src/storage/templates/Simulator/User.Bot.Daily.js b/src/storage/templates/Simulator/User.Bot.Daily.js index 9bd5892a0..7cc323c75 100644 --- a/src/storage/templates/Simulator/User.Bot.Daily.js +++ b/src/storage/templates/Simulator/User.Bot.Daily.js @@ -51,13 +51,13 @@ let market = global.MARKET; let dataFile; - let recordsArray = []; - let conditionsArray = []; - let strategiesArray = []; - let tradesArray = []; + let recordsArray + let conditionsArray + let strategiesArray + let tradesArray + let lastObjectsArray let tradingSystem = {}; - let lastObjectsArray = []; commons.initializeData(); @@ -96,20 +96,23 @@ } commons.runSimulation( - recordsArray, - conditionsArray, - strategiesArray, - tradesArray, - lastObjectsArray, timePeriod, currentDay, startDate, endDate, interExecutionMemory, - writeFiles) + writeFiles, + callBackFunction) + + function writeFiles(pTradingSystem, pRecordsArray, pConditionsArray, pStrategiesArray, pTradesArray, pLastObjectsArray) { - function writeFiles(pTradingSystem) { tradingSystem = pTradingSystem + recordsArray = pRecordsArray + conditionsArray = pConditionsArray + strategiesArray = pStrategiesArray + tradesArray = pTradesArray + lastObjectsArray = pLastObjectsArray + writeRecordsFile() } @@ -141,7 +144,7 @@ record.balanceA + "," + record.balanceB + "," + record.profit + "," + - record.lastProfit + "," + + record.lastTradeProfitLoss + "," + record.stopLoss + "," + record.roundtrips + "," + record.hits + "," + @@ -152,7 +155,7 @@ record.days + "," + record.anualizedRateOfReturn + "," + record.positionRate + "," + - record.lastProfitPercent + "," + + record.lastTradeROI + "," + record.strategy + "," + record.strategyStageNumber + "," + record.takeProfit + "," + @@ -161,11 +164,11 @@ JSON.stringify(record.orderRecord) + "," + record.positionSize + "," + record.initialBalanceA + "," + - record.minimunBalanceA + "," + - record.maximunBalanceA + "," + + record.minimumBalanceA + "," + + record.maximumBalanceA + "," + record.initialBalanceB + "," + - record.minimunBalanceB + "," + - record.maximunBalanceB + "]"; + record.minimumBalanceB + "," + + record.maximumBalanceB + "]"; if (separator === "") { separator = ","; } @@ -407,7 +410,7 @@ record.begin + "," + record.end + "," + record.status + "," + - record.lastProfitPercent + "," + + record.lastTradeROI + "," + record.beginRate + "," + record.endRate + "," + record.exitType + "," + diff --git a/src/storage/templates/Simulator/User.Bot.Market.js b/src/storage/templates/Simulator/User.Bot.Market.js index 8c976caed..dcdcc835b 100644 --- a/src/storage/templates/Simulator/User.Bot.Market.js +++ b/src/storage/templates/Simulator/User.Bot.Market.js @@ -49,11 +49,11 @@ let market = global.MARKET; let dataFile; - let recordsArray = []; - let conditionsArray = []; - let strategiesArray = []; - let tradesArray = []; - let lastObjectsArray = []; + let recordsArray + let conditionsArray + let strategiesArray + let tradesArray + let lastObjectsArray let tradingSystem = {}; let interExecutionMemory = {}; @@ -96,20 +96,23 @@ } commons.runSimulation( - recordsArray, - conditionsArray, - strategiesArray, - tradesArray, - lastObjectsArray, timePeriod, currentDay, startDate, endDate, interExecutionMemory, - writeFiles) + writeFiles, + callBackFunction) + + function writeFiles(pTradingSystem, pRecordsArray, pConditionsArray, pStrategiesArray, pTradesArray, pLastObjectsArray) { - function writeFiles(pTradingSystem) { tradingSystem = pTradingSystem + recordsArray = pRecordsArray + conditionsArray = pConditionsArray + strategiesArray = pStrategiesArray + tradesArray = pTradesArray + lastObjectsArray = pLastObjectsArray + writeRecordsFile() } @@ -137,7 +140,7 @@ record.balanceA + "," + record.balanceB + "," + record.profit + "," + - record.lastProfit + "," + + record.lastTradeProfitLoss + "," + record.stopLoss + "," + record.roundtrips + "," + record.hits + "," + @@ -148,7 +151,7 @@ record.days + "," + record.anualizedRateOfReturn + "," + record.positionRate + "," + - record.lastProfitPercent + "," + + record.lastTradeROI + "," + record.strategy + "," + record.strategyStageNumber + "," + record.takeProfit + "," + @@ -157,11 +160,11 @@ JSON.stringify(record.orderRecord) + "," + record.positionSize + "," + record.initialBalanceA + "," + - record.minimunBalanceA + "," + - record.maximunBalanceA + "," + + record.minimumBalanceA + "," + + record.maximumBalanceA + "," + record.initialBalanceB + "," + - record.minimunBalanceB + "," + - record.maximunBalanceB + "]"; + record.minimumBalanceB + "," + + record.maximumBalanceB + "]"; if (separator === "") { separator = ","; } @@ -380,7 +383,7 @@ record.begin + "," + record.end + "," + record.status + "," + - record.lastProfitPercent + "," + + record.lastTradeROI + "," + record.beginRate + "," + record.endRate + "," + record.exitType + "," + @@ -441,3 +444,4 @@ } } }; + From 23fde238880fc80de61bc2f40ee9107242272ddd Mon Sep 17 00:00:00 2001 From: Matias Date: Mon, 1 Jul 2019 16:34:11 -0300 Subject: [PATCH 15/34] Adding Clone Id --- src/models/ecosystem.js | 4 ++++ src/schema/types/Bot.js | 1 + src/schema/types/input/Bot.js | 3 ++- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/models/ecosystem.js b/src/models/ecosystem.js index 1e6a2b9d8..bcaf36a46 100644 --- a/src/models/ecosystem.js +++ b/src/models/ecosystem.js @@ -58,6 +58,10 @@ const ecosystemSchema = new Schema({ type: String, required: false }, + cloneId: { + type: String, + required: false + }, products: [{ codeName: { type: String, diff --git a/src/schema/types/Bot.js b/src/schema/types/Bot.js index 9ce0405ab..347587f02 100644 --- a/src/schema/types/Bot.js +++ b/src/schema/types/Bot.js @@ -15,6 +15,7 @@ const BotType = new GraphQLObjectType({ profilePicture: { type: GraphQLString }, repo: { type: GraphQLString }, configFile: { type: GraphQLString }, + cloneId: { type: GraphQLString }, products: { type: new GraphQLList(ProductType), resolve(parent) { diff --git a/src/schema/types/input/Bot.js b/src/schema/types/input/Bot.js index dfbadf5fb..5b1ed1096 100644 --- a/src/schema/types/input/Bot.js +++ b/src/schema/types/input/Bot.js @@ -11,7 +11,8 @@ const Type = new GraphQLInputObjectType({ codeName: { type: new GraphQLNonNull(GraphQLString) }, displayName: { type: new GraphQLNonNull(GraphQLString) }, repo: { type: GraphQLString }, - configFile: { type: GraphQLString } + configFile: { type: GraphQLString }, + cloneId: { type: GraphQLString } }), }); From 5a6f38fa7b558b87f72e740739fcd7be91b5563c Mon Sep 17 00:00:00 2001 From: Matias Date: Mon, 1 Jul 2019 16:34:24 -0300 Subject: [PATCH 16/34] Fixing env variable names --- src/storage/templates/TradingBot/User.Bot.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/storage/templates/TradingBot/User.Bot.js b/src/storage/templates/TradingBot/User.Bot.js index 963c6d3c9..7cb73e57d 100644 --- a/src/storage/templates/TradingBot/User.Bot.js +++ b/src/storage/templates/TradingBot/User.Bot.js @@ -573,7 +573,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { async function getSignalsByCloneId(orderStatus) { try { const cockPit = await axios({ - url: process.env.GATEWAY_ENDPOINT, + url: process.env.GATEWAY_ENDPOINT_K8S, method: 'post', data: { query: ` @@ -594,7 +594,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { }, }, headers: { - authorization: 'Bearer ' + global.ACCESS_TOKEN + authorization: process.env.AUTHORIZATION } }) @@ -610,7 +610,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { async function updateSignal(signalId, message) { try { const cockPit = await axios({ - url: process.env.GATEWAY_ENDPOINT, + url: process.env.GATEWAY_ENDPOINT_K8S, method: 'post', data: { query: ` @@ -631,7 +631,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { }, }, headers: { - authorization: 'Bearer ' + global.ACCESS_TOKEN + authorization: process.env.AUTHORIZATION } }) @@ -646,7 +646,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { async function createSignal(message) { try { const cockPit = await axios({ - url: process.env.GATEWAY_ENDPOINT, + url: process.env.GATEWAY_ENDPOINT_K8S, method: 'post', data: { query: ` @@ -667,7 +667,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { }, }, headers: { - authorization: 'Bearer ' + global.ACCESS_TOKEN + authorization: process.env.AUTHORIZATION } }) @@ -682,7 +682,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { async function getAutopilot() { try { const cockPit = await axios({ - url: process.env.GATEWAY_ENDPOINT, + url: process.env.GATEWAY_ENDPOINT_K8S, method: 'post', data: { query: ` @@ -699,7 +699,7 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { }, }, headers: { - authorization: 'Bearer ' + global.ACCESS_TOKEN + authorization: process.env.AUTHORIZATION } }) From 09eb8a53e2504de49ea8fdc4a3650a02c26e7b76 Mon Sep 17 00:00:00 2001 From: Matias Date: Wed, 3 Jul 2019 12:42:41 -0300 Subject: [PATCH 17/34] Adding new template --- src/storage/templates/Simulator/Commons.js | 5 ++++- src/storage/templates/Simulator/User.Bot.Daily.js | 3 ++- src/storage/templates/Simulator/User.Bot.Market.js | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/storage/templates/Simulator/Commons.js b/src/storage/templates/Simulator/Commons.js index 570286d26..f96787ff9 100644 --- a/src/storage/templates/Simulator/Commons.js +++ b/src/storage/templates/Simulator/Commons.js @@ -1476,6 +1476,8 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { balanceAssetB = Number.MAX_SAFE_INTEGER } + let quotedBaseAsset = '"' + baseAsset + '"' + simulationRecord = { begin: candle.begin, end: candle.end, @@ -1509,7 +1511,8 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { maximumBalanceA: maximumBalanceA, initialBalanceB: initialBalanceB, minimumBalanceB: minimumBalanceB, - maximumBalanceB: maximumBalanceB + maximumBalanceB: maximumBalanceB, + baseAsset: quotedBaseAsset } recordsArray.push(simulationRecord); diff --git a/src/storage/templates/Simulator/User.Bot.Daily.js b/src/storage/templates/Simulator/User.Bot.Daily.js index 7cc323c75..8cc5e30d0 100644 --- a/src/storage/templates/Simulator/User.Bot.Daily.js +++ b/src/storage/templates/Simulator/User.Bot.Daily.js @@ -168,7 +168,8 @@ record.maximumBalanceA + "," + record.initialBalanceB + "," + record.minimumBalanceB + "," + - record.maximumBalanceB + "]"; + record.maximumBalanceB + "," + + record.baseAsset + "]"; if (separator === "") { separator = ","; } diff --git a/src/storage/templates/Simulator/User.Bot.Market.js b/src/storage/templates/Simulator/User.Bot.Market.js index dcdcc835b..80afe2836 100644 --- a/src/storage/templates/Simulator/User.Bot.Market.js +++ b/src/storage/templates/Simulator/User.Bot.Market.js @@ -164,7 +164,8 @@ record.maximumBalanceA + "," + record.initialBalanceB + "," + record.minimumBalanceB + "," + - record.maximumBalanceB + "]"; + record.maximumBalanceB + "," + + record.baseAsset + "]"; if (separator === "") { separator = ","; } @@ -444,4 +445,3 @@ } } }; - From 8a9268d7dd3245de31438c1980817f86a796b271 Mon Sep 17 00:00:00 2001 From: Matias Date: Wed, 3 Jul 2019 12:43:12 -0300 Subject: [PATCH 18/34] Adding logs --- src/schema/mutations/CreateFile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/schema/mutations/CreateFile.js b/src/schema/mutations/CreateFile.js index ab2992f8b..a1dcdcb6b 100644 --- a/src/schema/mutations/CreateFile.js +++ b/src/schema/mutations/CreateFile.js @@ -6,7 +6,7 @@ import { writeFileContent } from '../../storage/providers/MinioStorage' export const args = { file: { type: FileInputType } } const resolve = async (parent, { file }, context) => { - logger.debug('createFile -> Entering Function.') + logger.debug('createFile -> Entering Function: ' + file.container + '/' + file.filePath) try { //TODO Add permissions await writeFileContent(file.container, file.filePath, file.fileContent) From 6d28784d91a06bad858d1a6f808ae5a87977e206 Mon Sep 17 00:00:00 2001 From: Matias Date: Wed, 3 Jul 2019 12:43:26 -0300 Subject: [PATCH 19/34] fixing braces issue --- src/schema/queries/FileContent.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/schema/queries/FileContent.js b/src/schema/queries/FileContent.js index ec30b555f..2b328b154 100644 --- a/src/schema/queries/FileContent.js +++ b/src/schema/queries/FileContent.js @@ -22,11 +22,12 @@ const resolve = async (parent, { file }, context) => { return fileContent } catch (err) { - if(err.message === 'The specified key does not exist.') + if (err.message === 'The specified key does not exist.') logger.warn('getFileContent -> Error: %s', err.message) - else - logger.error('getFileContent -> Error: %s', err.message) - throw err + else { + logger.error('getFileContent -> Error: %s', err.message) + throw err + } } } From 9a6b8a1572ae2e4c754bdbde528ee0da8a8d1ad9 Mon Sep 17 00:00:00 2001 From: Matias Date: Wed, 3 Jul 2019 20:10:19 -0300 Subject: [PATCH 20/34] Add and remove bot to ecosystem --- src/schema/mutations/AddBot.js | 70 ++++++++++++++++++++++++++++++ src/schema/mutations/RemoveBot.js | 63 +++++++++++++++++++++++++++ src/schema/mutations/index.js | 6 ++- src/schema/types/input/Bot.js | 7 ++- src/schema/types/input/DataSet.js | 19 ++++++++ src/schema/types/input/Exchange.js | 15 +++++++ src/schema/types/input/Plotter.js | 17 ++++++++ src/schema/types/input/Product.js | 24 ++++++++++ src/schema/types/input/index.js | 10 ++++- 9 files changed, 227 insertions(+), 4 deletions(-) create mode 100644 src/schema/mutations/AddBot.js create mode 100644 src/schema/mutations/RemoveBot.js create mode 100644 src/schema/types/input/DataSet.js create mode 100644 src/schema/types/input/Exchange.js create mode 100644 src/schema/types/input/Plotter.js create mode 100644 src/schema/types/input/Product.js diff --git a/src/schema/mutations/AddBot.js b/src/schema/mutations/AddBot.js new file mode 100644 index 000000000..d269a1d5f --- /dev/null +++ b/src/schema/mutations/AddBot.js @@ -0,0 +1,70 @@ +import logger from '../../utils/logger' +import { BotInputType, } from '../types/input' +import { BotType, } from '../types' +import { Ecosystem } from '../../models' + +export const args = { bot: { type: BotInputType } } + +const resolve = async (parent, { bot }, context) => { + logger.debug('addBot -> Dev team: %s. Bot: %s', bot.devTeam, bot.codeName) + try { + + let userEcosystem = await Ecosystem.findOne({ + authId: context.userId + }) + + if (!userEcosystem) { + throw 'Ecosystem not found for the user.' + } + + let newBotClone + for (let i = 0; i < userEcosystem.devTeams.length; i++) { + const auxTeam = userEcosystem.devTeams[i] + if (auxTeam.codeName === bot.devTeam) { + for (let j = 0; j < auxTeam.bots.length; j++) { + const auxBot = auxTeam.bots[j] + if (auxBot.codeName === bot.codeName) { + for (let k = 0; k < auxBot.products.length; k++) { + const auxProduct = auxBot.products[k] + if (auxProduct.codeName === bot.productCodeName) { + newBotClone = { + displayName: auxBot.displayName, + codeName: auxBot.codeName + '-' + bot.cloneId, + type: auxBot.type, + profilePicture: auxBot.profilePicture, + cloneId: bot.cloneId, + products: [auxProduct] + } + auxTeam.bots.push(newBotClone) + break + } + } + break + } + } + break + } + } + + if (newBotClone) { + await userEcosystem.save() + return newBotClone + } else { + throw 'Algobot not found.' + } + + } catch (err) { + logger.error('addBot -> Error: %s', err.message) + throw err + } +} + +const mutation = { + addBot: { + type: BotType, + args, + resolve, + } +} + +export default mutation; diff --git a/src/schema/mutations/RemoveBot.js b/src/schema/mutations/RemoveBot.js new file mode 100644 index 000000000..a09ce89f1 --- /dev/null +++ b/src/schema/mutations/RemoveBot.js @@ -0,0 +1,63 @@ +import logger from '../../utils/logger' +import { BotInputType, } from '../types/input' +import { BotType, } from '../types' +import { Ecosystem } from '../../models' + +export const args = { bot: { type: BotInputType } } + +const resolve = async (parent, { bot }, context) => { + logger.debug('removeBot -> Dev team: %s. Bot: %s', bot.devTeam, bot.codeName) + try { + + let userEcosystem = await Ecosystem.findOne({ + authId: context.userId + }) + + if (!userEcosystem) { + throw 'Ecosystem not found for the user.' + } + + let removedBotClone + for (let i = 0; i < userEcosystem.devTeams.length; i++) { + const auxTeam = userEcosystem.devTeams[i] + if (auxTeam.codeName === bot.devTeam) { + for (let j = 0; j < auxTeam.bots.length; j++) { + const auxBot = auxTeam.bots[j] + if (auxBot.codeName + '-' + bot.cloneId === bot.codeName) { + for (let k = 0; k < auxBot.products.length; k++) { + const auxProduct = auxBot.products[k] + if (auxProduct.codeName === bot.productCodeName) { + removedBotClone = auxBot + auxTeam.bots.splice(j, 1) + break + } + } + break + } + } + break + } + } + + if (removedBotClone) { + await userEcosystem.save() + return removedBotClone + } else { + throw 'Algobot not found.' + } + + } catch (err) { + logger.error('removeBot -> Error: %s', err.message) + throw err + } +} + +const mutation = { + removeBot: { + type: BotType, + args, + resolve, + } +} + +export default mutation; diff --git a/src/schema/mutations/index.js b/src/schema/mutations/index.js index 7e0b35711..be7aad941 100644 --- a/src/schema/mutations/index.js +++ b/src/schema/mutations/index.js @@ -5,13 +5,17 @@ import { import AddTeamMutation from './AddTeam' import DeleteTeamMutation from './DeleteTeam' import CreateFileMutation from './CreateFile' +import AddBotMutation from './AddBot' +import RemoveBotMutation from './RemoveBot' const Mutation = new GraphQLObjectType({ name: 'Mutation', fields: Object.assign( AddTeamMutation, DeleteTeamMutation, - CreateFileMutation + CreateFileMutation, + AddBotMutation, + RemoveBotMutation ) }) diff --git a/src/schema/types/input/Bot.js b/src/schema/types/input/Bot.js index 5b1ed1096..b71172621 100644 --- a/src/schema/types/input/Bot.js +++ b/src/schema/types/input/Bot.js @@ -8,11 +8,14 @@ const Type = new GraphQLInputObjectType({ name: 'BotInput', description: 'Payload for bot input.', fields: () => ({ + devTeam: { type: new GraphQLNonNull(GraphQLString) }, codeName: { type: new GraphQLNonNull(GraphQLString) }, - displayName: { type: new GraphQLNonNull(GraphQLString) }, + productCodeName: { type: new GraphQLNonNull(GraphQLString) }, + displayName: { type: GraphQLString }, repo: { type: GraphQLString }, configFile: { type: GraphQLString }, - cloneId: { type: GraphQLString } + cloneId: { type: GraphQLString }, + validPeriod: { type: GraphQLString }, }), }); diff --git a/src/schema/types/input/DataSet.js b/src/schema/types/input/DataSet.js new file mode 100644 index 000000000..71efbf934 --- /dev/null +++ b/src/schema/types/input/DataSet.js @@ -0,0 +1,19 @@ +import { + GraphQLInputObjectType, + GraphQLString, + GraphQLList +} from 'graphql'; + +const Type = new GraphQLInputObjectType({ + name: 'DataSetInput', + description: 'Payload for data set input.', + fields: () => ({ + codeName: { type: new GraphQLNonNull(GraphQLString) }, + type: { type: new GraphQLNonNull(GraphQLString) }, + filePath: { type: new GraphQLNonNull(GraphQLString) }, + fileName: { type: new GraphQLNonNull(GraphQLString) }, + validPeriods: { type: new GraphQLList(GraphQLString) } + }), +}); + +export default Type; diff --git a/src/schema/types/input/Exchange.js b/src/schema/types/input/Exchange.js new file mode 100644 index 000000000..738aca4ac --- /dev/null +++ b/src/schema/types/input/Exchange.js @@ -0,0 +1,15 @@ +import { + GraphQLInputObjectType, + GraphQLString, + GraphQLNonNull +} from 'graphql'; + +const Type = new GraphQLInputObjectType({ + name: 'ExchangeInput', + description: 'Payload for exchange input.', + fields: () => ({ + name: { type: new GraphQLNonNull(GraphQLString) } + }), +}); + +export default Type; diff --git a/src/schema/types/input/Plotter.js b/src/schema/types/input/Plotter.js new file mode 100644 index 000000000..1faa1a322 --- /dev/null +++ b/src/schema/types/input/Plotter.js @@ -0,0 +1,17 @@ +import { + GraphQLInputObjectType, + GraphQLString, + GraphQLNonNull +} from 'graphql'; + +const Type = new GraphQLInputObjectType({ + name: 'PlotterInput', + description: 'Payload for plotter input.', + fields: () => ({ + devTeam: { type: new GraphQLNonNull(GraphQLString) }, + codeName: { type: new GraphQLNonNull(GraphQLString) }, + moduleName: { type: new GraphQLNonNull(GraphQLString) } + }), +}); + +export default Type; diff --git a/src/schema/types/input/Product.js b/src/schema/types/input/Product.js new file mode 100644 index 000000000..9fb26fab2 --- /dev/null +++ b/src/schema/types/input/Product.js @@ -0,0 +1,24 @@ +import { + GraphQLInputObjectType, + GraphQLString, + GraphQLNonNull +} from 'graphql'; + +import { DataSetInputType } from './index'; +import { ExchangeInputType } from './index'; +import { PlotterInputType } from './index'; + +const Type = new GraphQLInputObjectType({ + name: 'ProductInput', + description: 'Payload for product input.', + fields: () => ({ + codeName: { type: new GraphQLNonNull(GraphQLString) }, + displayName: { type: new GraphQLNonNull(GraphQLString) }, + description: { type: GraphQLString }, + dataSets: { type: new GraphQLList(DataSetInputType) }, + exchangeList: { type: new GraphQLList(ExchangeInputType) }, + plotter: { type: PlotterInputType } + }), +}); + +export default Type; diff --git a/src/schema/types/input/index.js b/src/schema/types/input/index.js index 2e3d3dd55..6344400c3 100644 --- a/src/schema/types/input/index.js +++ b/src/schema/types/input/index.js @@ -3,11 +3,19 @@ import BotInputType from './Bot' import HostInputType from './Host' import FileInputType from './File' import DeleteTeamInputType from './DeleteTeam' +import DataSetInputType from './DataSet' +import ExchangeInputType from './Exchange' +import PlotterInputType from './Plotter' +import ProductInputType from './Product' export { TeamInputType, BotInputType, HostInputType, FileInputType, - DeleteTeamInputType + DeleteTeamInputType, + DataSetInputType, + ExchangeInputType, + PlotterInputType, + ProductInputType } From 248123763166949c309bd8bfb61595c70e4138db Mon Sep 17 00:00:00 2001 From: Matias Date: Thu, 4 Jul 2019 19:11:58 -0300 Subject: [PATCH 21/34] Removing unused error definitions --- src/errors/index.js | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/errors/index.js b/src/errors/index.js index b6c507c56..e9999e0a1 100644 --- a/src/errors/index.js +++ b/src/errors/index.js @@ -1,31 +1,23 @@ -class ClientError extends Error { - constructor(args) { - super(args); +export class AuthenticationError extends Error { + constructor(message) { + super(message) + this.code = 401 + this.message = 'Autentification not found, you have to be authentificated to perform this action.' } } -class ServerError extends Error { - constructor(args) { - super(args); +export class WrongArgumentsError extends Error { + constructor(message) { + super(message) + this.code = 400 + this.message = `Wrong arguments : ${this.message}` } } -export class AuthenticationError extends ClientError { - // code = 401 - // message = 'Authentication not found, you have to be authenticated to perform this action' -} - -export class DatabaseError extends ClientError { - // code = 404 - // message = `Ressource not found : ${this.message}` -} - -export class WrongArgumentsError extends ClientError { - // code = 400 - // message = `Wrong arguments : ${this.message}` -} - -export class ServiceUnavailableError extends ServerError { - // code = 503 - // message = `At least one service is unresponding ${this.message}` +export class ServiceUnavailableError extends Error { + constructor(message) { + super(message) + this.code = 503 + this.message = `At least one service is unresponding ${this.message}` + } } From 2b7f4eaed4a1cc7ae2453a3a02df8d7bcbc60d5e Mon Sep 17 00:00:00 2001 From: Matias Date: Thu, 4 Jul 2019 19:12:24 -0300 Subject: [PATCH 22/34] Removing not null to fin on create issue --- src/schema/types/input/Bot.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/schema/types/input/Bot.js b/src/schema/types/input/Bot.js index b71172621..1fdcd09ad 100644 --- a/src/schema/types/input/Bot.js +++ b/src/schema/types/input/Bot.js @@ -8,9 +8,9 @@ const Type = new GraphQLInputObjectType({ name: 'BotInput', description: 'Payload for bot input.', fields: () => ({ - devTeam: { type: new GraphQLNonNull(GraphQLString) }, codeName: { type: new GraphQLNonNull(GraphQLString) }, - productCodeName: { type: new GraphQLNonNull(GraphQLString) }, + productCodeName: { type: GraphQLString }, + devTeam: { type: GraphQLString}, displayName: { type: GraphQLString }, repo: { type: GraphQLString }, configFile: { type: GraphQLString }, From afb5a352699e4be89d0462184261f653adc640b7 Mon Sep 17 00:00:00 2001 From: Matias Date: Fri, 5 Jul 2019 17:56:55 -0300 Subject: [PATCH 23/34] Fixing clone id name --- src/schema/mutations/AddBot.js | 2 +- src/schema/mutations/RemoveBot.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/schema/mutations/AddBot.js b/src/schema/mutations/AddBot.js index d269a1d5f..a3fba7964 100644 --- a/src/schema/mutations/AddBot.js +++ b/src/schema/mutations/AddBot.js @@ -29,7 +29,7 @@ const resolve = async (parent, { bot }, context) => { if (auxProduct.codeName === bot.productCodeName) { newBotClone = { displayName: auxBot.displayName, - codeName: auxBot.codeName + '-' + bot.cloneId, + codeName: auxBot.codeName, type: auxBot.type, profilePicture: auxBot.profilePicture, cloneId: bot.cloneId, diff --git a/src/schema/mutations/RemoveBot.js b/src/schema/mutations/RemoveBot.js index a09ce89f1..68e093e27 100644 --- a/src/schema/mutations/RemoveBot.js +++ b/src/schema/mutations/RemoveBot.js @@ -23,7 +23,7 @@ const resolve = async (parent, { bot }, context) => { if (auxTeam.codeName === bot.devTeam) { for (let j = 0; j < auxTeam.bots.length; j++) { const auxBot = auxTeam.bots[j] - if (auxBot.codeName + '-' + bot.cloneId === bot.codeName) { + if (auxBot.codeName === bot.codeName && auxBot.cloneId === bot.cloneId) { for (let k = 0; k < auxBot.products.length; k++) { const auxProduct = auxBot.products[k] if (auxProduct.codeName === bot.productCodeName) { From 4b519f25b62e57ebca65a7dc181ba819d8b293bd Mon Sep 17 00:00:00 2001 From: Matias Date: Sun, 7 Jul 2019 21:13:43 -0300 Subject: [PATCH 24/34] Adding read and write permissions to teams --- src/config/ecosystem.json | 8 +++---- src/models/ecosystem.js | 4 ++++ src/schema/mutations/AddTeam.js | 4 +++- src/schema/mutations/CreateFile.js | 13 +++++++++++- src/schema/queries/FileContent.js | 12 ++++++++++- src/schema/types/Host.js | 3 ++- src/utils/getDevTeamHost.js | 34 ++++++++++++++++++++++++++++++ 7 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 src/utils/getDevTeamHost.js diff --git a/src/config/ecosystem.json b/src/config/ecosystem.json index 3641c3fe7..20982e0fb 100644 --- a/src/config/ecosystem.json +++ b/src/config/ecosystem.json @@ -8,7 +8,7 @@ "url": "https://demo-api-dev.superalgos.org/", "storage": "localStorage", "container": "aamasters", - "accessKey": "" + "accessKey": "09e890b76767ede3e10f0526b5036600a6b8d3faac540336e12b493fdb2aa9ed46f86094c34652f99fb6322e5f09806412f17725807f85e4dc4169bd22dd59f0b23aba5251f5544478a1aabf5a2dea5260e6eb0c29af3a788208af85025a02f94950437d501b91fd55b9d97b3d6ac3af4e1161321f78b73ca0fda524dc3a5d03" }, "bots": [ { @@ -728,7 +728,7 @@ "url": "https://demo-api-dev.superalgos.org/", "container": "aavikings", "storage": "localStorage", - "accessKey": "" + "accessKey": "0f38fea438be5adfc5873228f503c55353fb334406128b16a958851b8e47c118a08f0105dd8985d5479758fc4cc3f56c2e2364c2d7c3d0fdffba8d7e9356056a34629e3b926b1ebb940122d973db30b04fc18fab542cbc1afdc032dccf64b0fe308ee6bbc74f82bd350aaebf723428a50d8a699fa2b4b96d58716986ed24069f" }, "bots": [ { @@ -834,8 +834,8 @@ "host": { "url": "https://demo-api-dev.superalgos.org/", "storage": "localStorage", - "container": "aaplatform", - "accessKey": "" + "container": "aamasters", + "accessKey": "09e890b76767ede3e10f0526b5036600a6b8d3faac540336e12b493fdb2aa9ed46f86094c34652f99fb6322e5f09806412f17725807f85e4dc4169bd22dd59f0b23aba5251f5544478a1aabf5a2dea5260e6eb0c29af3a788208af85025a02f94950437d501b91fd55b9d97b3d6ac3af4e1161321f78b73ca0fda524dc3a5d03" }, "competitions": [ { diff --git a/src/models/ecosystem.js b/src/models/ecosystem.js index bcaf36a46..e50759767 100644 --- a/src/models/ecosystem.js +++ b/src/models/ecosystem.js @@ -31,6 +31,10 @@ const ecosystemSchema = new Schema({ accessKey: { type: String, required: false + }, + ownerKey: { + type: String, + required: false } }, bots: [{ diff --git a/src/schema/mutations/AddTeam.js b/src/schema/mutations/AddTeam.js index 94f557eb6..995f87c6e 100644 --- a/src/schema/mutations/AddTeam.js +++ b/src/schema/mutations/AddTeam.js @@ -6,6 +6,7 @@ import { TeamInputType } from '../types/input' import { copyBot } from '../../storage/CopyBot' import { copySimulator } from '../../storage/CopySimulator' import { createContainer, writeFileContent } from '../../storage/providers/MinioStorage' +import crypto from 'crypto' export const args = { team: { type: TeamInputType } } @@ -51,7 +52,8 @@ const resolve = async (parent, { team }, context) => { url: process.env.NODE_ENDPOINT, storage: 'localStorage', container: team.codeName, - accessKey: '', + accessKey: crypto.randomBytes(128).toString('hex'), + ownerKey: crypto.randomBytes(128).toString('hex') } team.host = nodeEndpoint } diff --git a/src/schema/mutations/CreateFile.js b/src/schema/mutations/CreateFile.js index a1dcdcb6b..0911543cb 100644 --- a/src/schema/mutations/CreateFile.js +++ b/src/schema/mutations/CreateFile.js @@ -2,13 +2,24 @@ import logger from '../../utils/logger' import { FileInputType } from '../types/input' import { GraphQLString } from 'graphql' import { writeFileContent } from '../../storage/providers/MinioStorage' +import { AuthenticationError, WrongArgumentsError } from '../../errors' +import getDevTeamHost from '../../utils/getDevTeamHost' export const args = { file: { type: FileInputType } } const resolve = async (parent, { file }, context) => { logger.debug('createFile -> Entering Function: ' + file.container + '/' + file.filePath) try { - //TODO Add permissions + if (file.accessKey === undefined || file.accessKey === null) { + throw new WrongArgumentsError() + } + + let host = await getDevTeamHost(file.container, undefined, file.accessKey) + + if (host === undefined || host === null) { + throw new AuthenticationError() + } + await writeFileContent(file.container, file.filePath, file.fileContent) return 'File created.' } catch (err) { diff --git a/src/schema/queries/FileContent.js b/src/schema/queries/FileContent.js index 2b328b154..6856b6f93 100644 --- a/src/schema/queries/FileContent.js +++ b/src/schema/queries/FileContent.js @@ -3,6 +3,8 @@ import { FileInputType } from '../types/input' import { GraphQLString } from 'graphql' import { getFileContent } from '../../storage/providers/MinioStorage' import { getFileContentRemote } from '../../storage/providers/AzureStorage' +import { AuthenticationError, WrongArgumentsError } from '../../errors' +import getDevTeamHost from '../../utils/getDevTeamHost' export const args = { file: { type: FileInputType } } @@ -10,10 +12,18 @@ const resolve = async (parent, { file }, context) => { logger.debug('getFileContent -> Entering Function: ' + file.container + '/' + file.filePath) try { + if (file.accessKey === undefined || file.accessKey === null) { + throw new WrongArgumentsError() + } + + let host = await getDevTeamHost(file.container, file.accessKey) + + if (host === undefined || host === null) { + throw new AuthenticationError() + } 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) diff --git a/src/schema/types/Host.js b/src/schema/types/Host.js index fbc45bcd1..0b36b8e00 100644 --- a/src/schema/types/Host.js +++ b/src/schema/types/Host.js @@ -9,7 +9,8 @@ const HostType = new GraphQLObjectType({ url: { type: GraphQLString }, storage: { type: GraphQLString }, container: { type: GraphQLString }, - accessKey: { type: GraphQLString } + accessKey: { type: GraphQLString }, + ownerKey: { type: GraphQLString } }), }); diff --git a/src/utils/getDevTeamHost.js b/src/utils/getDevTeamHost.js new file mode 100644 index 000000000..c244cf8e1 --- /dev/null +++ b/src/utils/getDevTeamHost.js @@ -0,0 +1,34 @@ +import { Ecosystem } from '../models' + +let teamCache = new Map() +export default async function getDevTeamHost(devTeamName, accessKey, ownerKey) { + let host + if (accessKey) { + host = teamCache.get(devTeamName + '.accessKey') + } else { + host = teamCache.get(devTeamName + '.ownerKey') + } + if (host) { + if (host.accessKey === accessKey || host.ownerKey === ownerKey) { + return host + } + } else { + let ecosystem = await Ecosystem.find() + + for (var i = 0; i < ecosystem.length; i++) { + for (var j = 0; j < ecosystem[i].devTeams.length; j++) { + if (ecosystem[i].devTeams[j].codeName.toLowerCase() === devTeamName.toLowerCase()) { + if (ecosystem[i].devTeams[j].host.accessKey === accessKey) { + teamCache.set(devTeamName + '.accessKey', ecosystem[i].devTeams[j].host) + return ecosystem[i].devTeams[j].host + } + if (ecosystem[i].devTeams[j].host.ownerKey === ownerKey) { + teamCache.set(devTeamName + '.ownerKey', ecosystem[i].devTeams[j].host) + return ecosystem[i].devTeams[j].host + } + } + } + } + + } +} From 9ac27a7fc58bbb7b044d38f1d7bd8b08e922bd14 Mon Sep 17 00:00:00 2001 From: Matias Date: Tue, 9 Jul 2019 22:49:37 -0300 Subject: [PATCH 25/34] New bot template --- src/storage/templates/TradingBot/User.Bot.js | 81 +++++++++++++------- 1 file changed, 54 insertions(+), 27 deletions(-) diff --git a/src/storage/templates/TradingBot/User.Bot.js b/src/storage/templates/TradingBot/User.Bot.js index 7cb73e57d..c8fc78f3a 100644 --- a/src/storage/templates/TradingBot/User.Bot.js +++ b/src/storage/templates/TradingBot/User.Bot.js @@ -1,4 +1,4 @@ -exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { +exports.newUserBot = function newUserBot(bot, logger) { // Variable used for logs, which will be passed to the logger instance const MODULE_NAME = 'User Bot' @@ -45,9 +45,12 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { let key = bot.devTeam + '-simulator-' + bot.codeName + '-Trading-Simulation-' + bot.dataSet + '-dataSet.V1' fileStorage = assistant.dataDependencies.dataSets.get(key) - if (fileStorage !== undefined){ + if (fileStorage !== undefined) { + assistant.rememberThis('lastSimulatorEngineMessageId', 0) + assistant.rememberThis('lastStopLoss', 0) + assistant.rememberThis('lastTakeProfit', 0) callBackFunction(global.DEFAULT_OK_RESPONSE) - }else{ + } else { logError('initialize -> Failed to initialize storage. Key not found:' + key) callBackFunction(global.DEFAULT_FAIL_RESPONSE) } @@ -96,18 +99,22 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { let simulatorEngineMessage = getSimulatorEngineMessageFromFile(indicatorFileContent) let autopilotResponse = await getAutopilot() + let stopLoss, takeProfit if (simulatorEngineMessage === undefined) { - logWarn('start -> Indicator message not found.') - throw global.DEFAULT_RETRY_RESPONSE + logWarn('start -> Simulator message not available. Will proceed to check SL and TP from previous execution.') + stopLoss = assistant.remindMeOf('lastStopLoss') + takeProfit = assistant.remindMeOf('lastTakeProfit') + } else { + logInfo('Processing simulator record: ' + JSON.stringify(simulatorEngineMessage)) + stopLoss = simulatorEngineMessage.order.stop + takeProfit = simulatorEngineMessage.order.takeProfit } - 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) { + if (stopLoss > 0 && assetABalance > 0 && currentRate >= stopLoss) { logInfo('Closing trade with Stop Loss.') try { let position = await createBuyPosition(currentRate) @@ -116,8 +123,8 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { 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.stop = stopLoss + simulatorExecutorMessage.order.takeProfit = takeProfit simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Buy simulatorExecutorMessage.order.size = assetABalance simulatorExecutorMessage.order.status = ORDER_STATUS.Placed @@ -125,13 +132,16 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { let message = createMessageFromObject(simulatorExecutorMessage) assistant.addExtraData(message) + assistant.rememberThis('lastSimulatorEngineMessageId', 0) + assistant.rememberThis('lastStopLoss', 0) + assistant.rememberThis('lastTakeProfit', 0) } 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.stop = stopLoss + simulatorExecutorMessage.order.takeProfit = takeProfit simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Buy simulatorExecutorMessage.order.size = assetABalance simulatorExecutorMessage.order.status = ORDER_STATUS.Rejected @@ -144,8 +154,8 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { } // Checking Take Profits - if (simulatorEngineMessage !== undefined && assetABalance > 0 && currentRate <= simulatorEngineMessage.order.takeProfit) { - logInfo('Closing trade with Take Profits.') + if (takeProfit > 0 && assetABalance > 0 && currentRate <= takeProfit) { + logInfo('Closing trade with Take Profit.') try { let position = await createBuyPosition(currentRate, assetABalance) @@ -153,8 +163,8 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { 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.stop = stopLoss + simulatorExecutorMessage.order.takeProfit = takeProfit simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Buy simulatorExecutorMessage.order.size = assetABalance simulatorExecutorMessage.order.status = ORDER_STATUS.Placed @@ -162,13 +172,16 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { let message = createMessageFromObject(simulatorExecutorMessage) assistant.addExtraData(message) + assistant.rememberThis('lastSimulatorEngineMessageId', 0) + assistant.rememberThis('lastStopLoss', 0) + assistant.rememberThis('lastTakeProfit', 0) } 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.stop = stopLoss + simulatorExecutorMessage.order.takeProfit = takeProfit simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Buy simulatorExecutorMessage.order.size = assetABalance simulatorExecutorMessage.order.status = ORDER_STATUS.Rejected @@ -180,13 +193,16 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { } } - if (autopilotResponse.autopilot || autopilotControl) { - await manageCloneInAutopilotOn(simulatorEngineMessage) - } else { - await manageCloneInAutopilotOff(simulatorEngineMessage) + if (simulatorEngineMessage !== undefined) { + if (autopilotResponse.autopilot || autopilotControl) { + await manageCloneInAutopilotOn(simulatorEngineMessage) + } else { + await manageCloneInAutopilotOff(simulatorEngineMessage) + } } } catch (error) { - if (error.stack) logError(JSON.stringify(error.stack)) + logError('Error on executorLogic: ' + error) + logError('Stack: ' + JSON.stringify(error.stack)) throw error } } @@ -198,6 +214,12 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { if (simulatorEngineMessage.messageType === MESSAGE_TYPE.Order && simulatorEngineMessage.order.direction === ORDER_DIRECTION.Sell) { + let lastSimulatorEngineMessageId = assistant.remindMeOf('lastSimulatorEngineMessageId') + if (lastSimulatorEngineMessageId >= simulatorEngineMessage.id) { + logInfo("manageCloneInAutopilotOn -> Order message was previously processed.") + return + } + try { let position = await createSellPosition(currentRate, assetBBalance) let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() @@ -211,6 +233,9 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { let message = createMessageFromObject(simulatorExecutorMessage) assistant.addExtraData(message) + assistant.rememberThis('lastSimulatorEngineMessageId', parseInt(simulatorEngineMessage.id)) + assistant.rememberThis('lastStopLoss', simulatorEngineMessage.order.stop) + assistant.rememberThis('lastTakeProfit', simulatorEngineMessage.order.takeProfit) } catch (error) { let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() simulatorExecutorMessage.from = MESSAGE_ENTITY.TradingAssistant @@ -542,12 +567,12 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { 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 + // Running live we will process last available Indicator Message only if it's delayed 10 minutes top + let maxTolerance = 10 * 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.') + logWarn('getSimulatorEngineMessageFromFile -> Last available indicator older than 10 minutes.') } } } catch (error) { @@ -720,7 +745,9 @@ exports.newUserBot = function newUserBot(bot, logger, COMMONS_MODULE) { 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.') + let errorMessage = '[WARN] getFileContent -> The indicator dependency is not ready. Will retry later.' + console.log(errorMessage) + logWarn(errorMessage) reject(global.DEFAULT_RETRY_RESPONSE) } else { resolve(fileContent) From 0bd6299d176390c4ef0d29923b82e3642403b854 Mon Sep 17 00:00:00 2001 From: Matias Date: Tue, 9 Jul 2019 22:50:42 -0300 Subject: [PATCH 26/34] Adding log info for performance check --- src/schema/mutations/CreateFile.js | 5 +++-- src/schema/queries/FileContent.js | 16 +++++----------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/schema/mutations/CreateFile.js b/src/schema/mutations/CreateFile.js index 0911543cb..b1e501e80 100644 --- a/src/schema/mutations/CreateFile.js +++ b/src/schema/mutations/CreateFile.js @@ -8,7 +8,6 @@ import getDevTeamHost from '../../utils/getDevTeamHost' export const args = { file: { type: FileInputType } } const resolve = async (parent, { file }, context) => { - logger.debug('createFile -> Entering Function: ' + file.container + '/' + file.filePath) try { if (file.accessKey === undefined || file.accessKey === null) { throw new WrongArgumentsError() @@ -19,8 +18,10 @@ const resolve = async (parent, { file }, context) => { if (host === undefined || host === null) { throw new AuthenticationError() } - + let date = new Date() await writeFileContent(file.container, file.filePath, file.fileContent) + let dateAfter = new Date() + logger.info('createFile: ' + file.container + '/...' + file.filePath.substring(file.filePath.length -110 , file.filePath.length) + ', ' + (dateAfter - date)) return 'File created.' } catch (err) { logger.error('createFile -> Error: %s', err.message) diff --git a/src/schema/queries/FileContent.js b/src/schema/queries/FileContent.js index 6856b6f93..63a9164a8 100644 --- a/src/schema/queries/FileContent.js +++ b/src/schema/queries/FileContent.js @@ -2,15 +2,12 @@ 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' import { AuthenticationError, WrongArgumentsError } from '../../errors' import getDevTeamHost from '../../utils/getDevTeamHost' export const args = { file: { type: FileInputType } } const resolve = async (parent, { file }, context) => { - logger.debug('getFileContent -> Entering Function: ' + file.container + '/' + file.filePath) - try { if (file.accessKey === undefined || file.accessKey === null) { throw new WrongArgumentsError() @@ -22,18 +19,15 @@ const resolve = async (parent, { file }, context) => { throw new AuthenticationError() } - let fileContent - if (file.storage === 'localStorage') { - fileContent = await getFileContent(file.container, file.filePath) - } else { - fileContent = await getFileContentRemote(file.container, file.filePath, file.storage, file.accessKey) - } - + let date = new Date() + let fileContent = await getFileContent(file.container, file.filePath) + let dateAfter = new Date() + logger.info('getFileContent: ' + file.container + '/...' + file.filePath.substring(file.filePath.length -110 , file.filePath.length) + ', ' + (dateAfter - date)) return fileContent } catch (err) { if (err.message === 'The specified key does not exist.') - logger.warn('getFileContent -> Error: %s', err.message) + logger.debug('getFileContent -> Error: %s', err.message) else { logger.error('getFileContent -> Error: %s', err.message) throw err From a4f4fef51f2f7598c01ed3c071193c63df45dc66 Mon Sep 17 00:00:00 2001 From: Matias Date: Thu, 11 Jul 2019 16:55:38 -0300 Subject: [PATCH 27/34] Updating templates --- src/schema/queries/FileContent.js | 1 + src/storage/templates/Simulator/Commons.js | 363 ++++++++++++------ .../templates/Simulator/User.Bot.Daily.js | 82 +++- .../templates/Simulator/User.Bot.Market.js | 77 +++- .../templates/Simulator/this.bot.config.json | 20 +- src/storage/templates/TradingBot/User.Bot.js | 120 ++++-- .../templates/TradingBot/this.bot.config.json | 102 +---- 7 files changed, 517 insertions(+), 248 deletions(-) diff --git a/src/schema/queries/FileContent.js b/src/schema/queries/FileContent.js index 63a9164a8..43bf10c3d 100644 --- a/src/schema/queries/FileContent.js +++ b/src/schema/queries/FileContent.js @@ -16,6 +16,7 @@ const resolve = async (parent, { file }, context) => { let host = await getDevTeamHost(file.container, file.accessKey) if (host === undefined || host === null) { + logger.error('Error getting file:' + JSON.stringify(file)) throw new AuthenticationError() } diff --git a/src/storage/templates/Simulator/Commons.js b/src/storage/templates/Simulator/Commons.js index f96787ff9..cb6a0f0e8 100644 --- a/src/storage/templates/Simulator/Commons.js +++ b/src/storage/templates/Simulator/Commons.js @@ -65,6 +65,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> Entering function."); } + let executionArray = []; let recordsArray = []; let conditionsArray = []; let strategiesArray = []; @@ -183,8 +184,8 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { /* Simulation Records */ - let positionRate = 0; - let positionSize = 0; + let tradePositionRate = 0; + let tradePositionSize = 0; let positionInstant; let previousBalanceAssetA = 0; @@ -341,36 +342,56 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { 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 bollingerChannel = getElement(bollingerChannelsArray, candle.begin, candle.end); + let bollingerSubChannel = getElement(bollingerSubChannelsArray, candle.begin, candle.end); + //let LRC = LRCMap.get(candle.begin); - //if (LRC === undefined) { continue; } - if (percentageBandwidth === undefined) { continue; } // percentageBandwidth might start after the first few candles. + /* If any of the needed indicators is missing, then that period is not calculated */ + if (candle.begin < initialDate.valueOf()) { continue; } + if (bollingerBand === undefined) { continue; } + if (percentageBandwidth === undefined) { continue; } // percentageBandwidth might start after the first few candles. + if (bollingerChannel === undefined) { continue; } + if (bollingerSubChannel === undefined) { continue; } + + //if (LRC === undefined) { continue; } periods++; days = periods * timePeriod / ONE_DAY_IN_MILISECONDS; - if (currentDay !== undefined) { + if (currentDay !== undefined) { // This means that we are processing Daily Files if (candle.end < currentDay.valueOf()) { yesterday.Periods++; } - } - let channel = getElement(bollingerChannelsArray, candle.begin, candle.end); - let subChannel = getElement(bollingerSubChannelsArray, candle.begin, candle.end); + /* We skip the candle at the head of the market because i has not closed yet. */ + let candlesPerDay = ONE_DAY_IN_MILISECONDS / timePeriod + if (i === candles.length - 1) { + if ((candles.length < candlesPerDay) || (candles.length > candlesPerDay && candles.length < candlesPerDay * 2) ) { + /*We are at the head of the market, thus we skip the last candle because it has not close yet. */ + continue; + /* Note here that in the last candle of the first day or the second day it will use an incomplete candle and partially calculated indicators. + if we skip these two periods, then there will be a hole in the file since the last period will be missing. */ + } + } + } else { // We are processing Market Files + if (i === candles.length - 1) { + continue; + } + } let lastObjects = { candle: clone(candle), //LRC: clone(LRC), bollingerBand: clone(bollingerBand), percentageBandwidth: clone(percentageBandwidth), - channel: clone(channel), - subChannel: clone(subChannel) + bollingerChannel: clone(bollingerChannel), + bollingerSubChannel: clone(bollingerSubChannel) } function clone(obj) { @@ -386,8 +407,8 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { //lastObjects.LRC.previous = undefined; lastObjects.bollingerBand.previous = undefined; lastObjects.percentageBandwidth.previous = undefined; - lastObjects.channel.previous = undefined; - lastObjects.subChannel.previous = undefined; + lastObjects.bollingerChannel.previous = undefined; + lastObjects.bollingerSubChannel.previous = undefined; lastObjectsArray.push(lastObjects); @@ -415,6 +436,11 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let strategy = tradingSystem.strategies[j]; + let positionSize = 0 + let positionRate = 0 + + /* Continue with the rest of the formulas and conditions */ + let triggerStage = strategy.triggerStage if (triggerStage !== undefined) { @@ -478,10 +504,67 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (openStage !== undefined) { + /* Default Values*/ + if (baseAsset === 'BTC') { + positionSize = balanceAssetA; + positionRate = candle.close; + } else { + positionSize = balanceAssetB; + positionRate = candle.close; + } + let initialDefinition = openStage.initialDefinition if (initialDefinition !== undefined) { + if (tradePositionSize !== 0) { + positionSize = tradePositionSize + } else { + if (initialDefinition.positionSize !== undefined) { + if (initialDefinition.positionSize.formula !== undefined) { + try { + positionSize = eval(initialDefinition.positionSize.formula.code); + } catch (err) { + initialDefinition.positionSize.formula.error = err.message + } + if (isNaN(positionSize)) { + if (baseAsset === 'BTC') { + positionSize = balanceAssetA; + } else { + positionSize = balanceAssetB; + } + } else { + if (baseAsset === 'BTC') { + if (positionSize > balanceAssetA) { positionSize = balanceAssetA } + } else { + if (positionSize > balanceAssetB) { positionSize = balanceAssetB } + } + } + } + } + } + + if (tradePositionRate !== 0) { + positionRate = tradePositionRate + } else { + if (initialDefinition.positionRate !== undefined) { + if (initialDefinition.positionRate.formula !== undefined) { + try { + positionRate = eval(initialDefinition.positionRate.formula.code); + } catch (err) { + initialDefinition.positionRate.formula.error = err.message + } + if (isNaN(positionRate)) { + if (baseAsset === 'BTC') { + positionRate = candle.close; + } else { + positionRate = candle.close; + } + } + } + } + } + if (initialDefinition.stopLoss !== undefined) { for (let p = 0; p < initialDefinition.stopLoss.phases.length; p++) { @@ -500,7 +583,13 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { formulaValue = MAX_STOP_LOSS_VALUE } } catch (err) { - formulaError = err.message + if (phase.formula.code.indexOf('previous') > 0 && err.message.indexOf('of undefined') > 0) { + /* + We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. + */ + } else { + formulaError = err.message + } } if (isNaN(formulaValue)) { formulaValue = 0; } if (formulaValue < MIN_STOP_LOSS_VALUE) { @@ -553,7 +642,13 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { formulaValue = MAX_TAKE_PROFIT_VALUE } } catch (err) { - formulaError = err.message + if (phase.formula.code.indexOf('previous') > 0 && err.message.indexOf('of undefined') > 0) { + /* + We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. + */ + } else { + formulaError = err.message + } } if (isNaN(formulaValue)) { formulaValue = 0; } if (formulaValue < MIN_TAKE_PROFIT_VALUE) { @@ -588,6 +683,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } } + + strategy.positionSize = positionSize + strategy.positionRate = positionRate } let manageStage = strategy.manageStage @@ -612,7 +710,13 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { formulaValue = MAX_STOP_LOSS_VALUE } } catch (err) { - formulaError = err.message + if (phase.formula.code.indexOf('previous') > 0 && err.message.indexOf('of undefined') > 0) { + /* + We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. + */ + } else { + formulaError = err.message + } } if (isNaN(formulaValue)) { formulaValue = 0; } if (formulaValue < MIN_STOP_LOSS_VALUE) { @@ -665,7 +769,13 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { formulaValue = MAX_TAKE_PROFIT_VALUE } } catch (err) { - formulaError = err.message + if (phase.formula.code.indexOf('previous') > 0 && err.message.indexOf('of undefined') > 0) { + /* + We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. + */ + } else { + formulaError = err.message + } } if (isNaN(formulaValue)) { formulaValue = 0; } if (formulaValue < MIN_TAKE_PROFIT_VALUE) { @@ -700,39 +810,45 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } } - } - function newCondition(key, node) { + function newCondition(key, node) { - let condition; - let error = '' - let value + 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 - } + 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 - condition = { - key: key, - value: value - }; + if (node.code.indexOf('previous') > 0 && err.message.indexOf('of undefined') > 0) { + /* + We are not going to set an error for the casess we are using previous and the error is that the indicator is undefined. + */ + } else { + node.error = err.message + } + } - conditions.set(condition.key, condition); + condition = { + key: key, + value: value + }; - if (condition.value) { - conditionsArrayValues.push(1); - } else { - conditionsArrayValues.push(0); + conditions.set(condition.key, condition); + + if (condition.value) { + conditionsArrayValues.push(1); + } else { + conditionsArrayValues.push(0); + } } } - } /* Trigger On Conditions */ @@ -809,11 +925,12 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } } else { + let stopRunningDate = new Date(candle.begin) if (balance < minimumBalance) { - tradingSystem.error = "Balance below the minimum." + tradingSystem.error = "Min Balance @ " + stopRunningDate.toUTCString() } if (balance > maximumBalance) { - tradingSystem.error = "Balance above the maximum." + tradingSystem.error = "Max Balance @ " + stopRunningDate.toUTCString() } } } @@ -893,7 +1010,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (passed) { - type = '"Sell"'; + type = '"Take Position"'; strategyStage = 'Open Stage'; stopLossStage = 'Open Stage'; @@ -1153,7 +1270,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } marketRate = stopLoss; - type = '"Buy@StopLoss"'; + type = '"Close@StopLoss"'; strategyStage = 'Close Stage'; stopLossStage = 'No Stage'; takeProfitStage = 'No Stage'; @@ -1189,7 +1306,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } marketRate = takeProfit; - type = '"Buy@TakeProfit"'; + type = '"Close@TakeProfit"'; strategyStage = 'Close Stage'; stopLossStage = 'No Stage'; takeProfitStage = 'No Stage'; @@ -1211,44 +1328,24 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { ) { takePositionNow = false - /* positionSize default is the whole balance if no size was defined by the user */ + /* tradePositionSize default is the whole balance if no size was defined by the user */ if (baseAsset === 'BTC') { - positionSize = balanceAssetA; + tradePositionSize = balanceAssetA; } else { - positionSize = balanceAssetB; + tradePositionSize = balanceAssetB; } + tradePositionRate = candle.close - /* Check if the user defined a position size */ + /* Check if the user defined a position size or rate */ 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; - } - } else { - if (baseAsset === 'BTC') { - if (positionSize > balanceAssetA) { positionSize = balanceAssetA} - } else { - if (positionSize > balanceAssetB) { positionSize = balanceAssetB} - } - } - } - } - } + tradePositionSize = strategy.positionSize; + tradePositionRate = strategy.positionRate; + + calculateTakeProfit(); + calculateStopLoss(); marketRate = candle.close; - positionRate = candle.close; previousBalanceAssetA = balanceAssetA; previousBalanceAssetB = balanceAssetB; @@ -1257,11 +1354,11 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { lastTradeROI = 0; if (baseAsset === 'BTC') { - balanceAssetB = balanceAssetB + positionSize * positionRate; - balanceAssetA = balanceAssetA - positionSize; + balanceAssetB = balanceAssetB + tradePositionSize * tradePositionRate; + balanceAssetA = balanceAssetA - tradePositionSize; } else { - balanceAssetA = balanceAssetA + positionSize / positionRate; - balanceAssetB = balanceAssetB - positionSize; + balanceAssetA = balanceAssetA + tradePositionSize / tradePositionRate; + balanceAssetB = balanceAssetB - tradePositionSize; } positionInstant = candle.end; @@ -1293,12 +1390,12 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (baseAsset === 'BTC') { lastTradeProfitLoss = balanceAssetA - previousBalanceAssetA; - lastTradeROI = lastTradeProfitLoss * 100 / positionSize; + lastTradeROI = lastTradeProfitLoss * 100 / tradePositionSize; if (isNaN(lastTradeROI)) { lastTradeROI = 0; } profit = balanceAssetA - initialBalanceA; } else { lastTradeProfitLoss = balanceAssetB - previousBalanceAssetB; - lastTradeROI = lastTradeProfitLoss * 100 / positionSize; + lastTradeROI = lastTradeProfitLoss * 100 / tradePositionSize; if (isNaN(lastTradeROI)) { lastTradeROI = 0; } profit = balanceAssetB - initialBalanceB; } @@ -1356,8 +1453,8 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { currentStrategyIndex = -1; stopLoss = 0; - positionRate = 0; - positionSize = 0; + tradePositionRate = 0; + tradePositionSize = 0; positionInstant = undefined; takeProfit = 0; strategyStage = 'No Stage'; @@ -1380,21 +1477,33 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { let messageType; let message; let simulationRecord; - let orderRecord; + let executionRecord; + let executionMessage; messageId++; - if (strategyStage === 'Open Stage' || strategyStage === 'Manage Stage') { + if (strategyStage === 'Open Stage' || strategyStage === 'Manage Stage' || type === '"Close@TakeProfit"' || type === '"Close@StopLoss"') { - if (strategyStage === 'Open Stage') { + if (type === '"Take Position"') { messageType = MESSAGE_TYPE.Order; orderId++; - } - if (strategyStage === 'Manage Stage') { - messageType = MESSAGE_TYPE.OrderUpdate; + } else { + if (type === '"Close@TakeProfit"' || type === '"Close@StopLoss"') { + messageType = MESSAGE_TYPE.OrderClose; + } else { + messageType = MESSAGE_TYPE.OrderUpdate; + } } - orderRecord = createMessage( + let exitOutcome = "" + if (messageType === MESSAGE_TYPE.OrderClose && type === '"Close@TakeProfit"') { + exitOutcome = "TP" + } + if (messageType === MESSAGE_TYPE.OrderClose && type === '"Close@StopLoss"') { + exitOutcome = "SL" + } + + executionMessage = createMessage( messageId, MESSAGE_ENTITY.SimulationEngine, MESSAGE_ENTITY.SimulationExecutor, @@ -1408,19 +1517,19 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { "BTC_USDT", 0, ORDER_TYPE.Limit, - marketRate, + tradePositionRate, stopLoss, takeProfit, ORDER_DIRECTION.Sell, -1, ORDER_STATUS.Signaled, 0, - "") + exitOutcome) } else { - orderRecord = createMessage( + executionMessage = createMessage( messageId, MESSAGE_ENTITY.SimulationEngine, MESSAGE_ENTITY.SimulationExecutor, @@ -1444,6 +1553,14 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { "") } + executionRecord = { + begin: candle.begin, + end: candle.end, + executionRecord: executionMessage + } + + executionArray.push(executionRecord) + let strategyStageNumber switch (strategyStage) { case 'No Stage': { @@ -1497,15 +1614,15 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { periods: periods, days: days, anualizedRateOfReturn: anualizedRateOfReturn, - positionRate: positionRate, + positionRate: tradePositionRate, lastTradeROI: lastTradeROI, strategy: currentStrategyIndex, strategyStageNumber: strategyStageNumber, takeProfit: takeProfit, stopLossPhase: stopLossPhase, takeProfitPhase: takeProfitPhase, - orderRecord: orderRecord, - positionSize: positionSize, + executionRecord: executionMessage, + positionSize: tradePositionSize, initialBalanceA: initialBalanceA, minimumBalanceA: minimumBalanceA, maximumBalanceA: maximumBalanceA, @@ -1605,7 +1722,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> callback -> recordsArray.length = " + recordsArray.length); } - callback(tradingSystem, recordsArray, conditionsArray, strategiesArray, tradesArray, lastObjectsArray); + callback(tradingSystem, executionArray, recordsArray, conditionsArray, strategiesArray, tradesArray, lastObjectsArray); function getElement(pArray, begin, end) { @@ -1620,11 +1737,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { } } - element = { - direction: 'unknown', - slope: 'unknown' - }; - return element; + return } } catch (err) { @@ -1653,17 +1766,17 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { 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._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._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'; } + if (previous._60 > LRC._60) { LRC.direction60 = 'Down'; } + if (previous._60 < LRC._60) { LRC.direction60 = 'Up'; } + if (previous._60 === LRC._60) { LRC.direction60 = 'Side'; } } @@ -1700,9 +1813,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { 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'; } + if (previous.movingAverage > percentageBandwidth.movingAverage) { percentageBandwidth.direction = 'Down'; } + if (previous.movingAverage < percentageBandwidth.movingAverage) { percentageBandwidth.direction = 'Up'; } + if (previous.movingAverage === percentageBandwidth.movingAverage) { percentageBandwidth.direction = 'Side'; } } @@ -1739,9 +1852,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { 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'; } + if (previous.movingAverage > bollingerBand.movingAverage) { bollingerBand.direction = 'Down'; } + if (previous.movingAverage < bollingerBand.movingAverage) { bollingerBand.direction = 'Up'; } + if (previous.movingAverage === bollingerBand.movingAverage) { bollingerBand.direction = 'Side'; } } @@ -1772,11 +1885,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { 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] + period: dataFile[i][3] }; bollingerChannel.previous = previous; @@ -1856,9 +1965,9 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { 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'; } + 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; diff --git a/src/storage/templates/Simulator/User.Bot.Daily.js b/src/storage/templates/Simulator/User.Bot.Daily.js index 8cc5e30d0..ef105c9a6 100644 --- a/src/storage/templates/Simulator/User.Bot.Daily.js +++ b/src/storage/templates/Simulator/User.Bot.Daily.js @@ -5,6 +5,7 @@ const MODULE_NAME = "User Bot"; + const EXECUTION_FOLDER_NAME = "Trading-Execution"; const SIMULATED_RECORDS_FOLDER_NAME = "Trading-Simulation"; const CONDITIONS_FOLDER_NAME = "Simulation-Conditions"; const STRATEGIES_FOLDER_NAME = "Simulation-Strategies"; @@ -104,16 +105,91 @@ writeFiles, callBackFunction) - function writeFiles(pTradingSystem, pRecordsArray, pConditionsArray, pStrategiesArray, pTradesArray, pLastObjectsArray) { + function writeFiles(pTradingSystem, pExecutionArray, pRecordsArray, pConditionsArray, pStrategiesArray, pTradesArray, pLastObjectsArray) { tradingSystem = pTradingSystem + executionArray = pExecutionArray recordsArray = pRecordsArray conditionsArray = pConditionsArray strategiesArray = pStrategiesArray tradesArray = pTradesArray lastObjectsArray = pLastObjectsArray - writeRecordsFile() + writeExecutionFile() + } + + function writeExecutionFile() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeExecutionFile -> Entering function."); } + + let separator = ""; + let fileRecordCounter = 0; + + let fileContent = ""; + + for (let i = 0; i < executionArray.length; i++) { + + let record = executionArray[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 + "," + + JSON.stringify(record.executionRecord) + "]"; + + 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/" + EXECUTION_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 -> writeExecutionFile -> onFileCreated -> Entering function."); } + if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> writeExecutionFile -> onFileCreated -> fileContent = " + fileContent); } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + logger.write(MODULE_NAME, "[ERROR] start -> writeExecutionFile -> onFileCreated -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> writeExecutionFile -> onFileCreated -> filePath = " + filePath); + logger.write(MODULE_NAME, "[ERROR] start -> writeExecutionFile -> onFileCreated -> market = " + market.assetA + "_" + market.assetB); + + callBackFunction(err); + return; + + } + + writeRecordsFile(); + + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeExecutionFile -> onFileCreated -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeExecutionFile -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } } function writeRecordsFile() { @@ -161,7 +237,7 @@ record.takeProfit + "," + record.stopLossPhase + "," + record.takeProfitPhase + "," + - JSON.stringify(record.orderRecord) + "," + + JSON.stringify(record.executionRecord) + "," + record.positionSize + "," + record.initialBalanceA + "," + record.minimumBalanceA + "," + diff --git a/src/storage/templates/Simulator/User.Bot.Market.js b/src/storage/templates/Simulator/User.Bot.Market.js index 80afe2836..a4dc38283 100644 --- a/src/storage/templates/Simulator/User.Bot.Market.js +++ b/src/storage/templates/Simulator/User.Bot.Market.js @@ -5,6 +5,7 @@ const MODULE_NAME = "User Bot"; + const EXECUTION_FOLDER_NAME = "Trading-Execution"; const SIMULATED_RECORDS_FOLDER_NAME = "Trading-Simulation"; const CONDITIONS_FOLDER_NAME = "Simulation-Conditions"; const STRATEGIES_FOLDER_NAME = "Simulation-Strategies"; @@ -104,16 +105,86 @@ writeFiles, callBackFunction) - function writeFiles(pTradingSystem, pRecordsArray, pConditionsArray, pStrategiesArray, pTradesArray, pLastObjectsArray) { + function writeFiles(pTradingSystem, pExecutionArray, pRecordsArray, pConditionsArray, pStrategiesArray, pTradesArray, pLastObjectsArray) { tradingSystem = pTradingSystem + executionArray = pExecutionArray recordsArray = pRecordsArray conditionsArray = pConditionsArray strategiesArray = pStrategiesArray tradesArray = pTradesArray lastObjectsArray = pLastObjectsArray - writeRecordsFile() + writeExecutionFile() + } + + function writeExecutionFile() { + + try { + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] start -> writeExecutionFile -> Entering function."); } + + let separator = ""; + let fileRecordCounter = 0; + + let fileContent = ""; + + for (let i = 0; i < executionArray.length; i++) { + + let record = executionArray[i]; + + fileContent = fileContent + separator + '[' + + record.begin + "," + + record.end + "," + + JSON.stringify(record.executionRecord) + "]"; + + 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/" + EXECUTION_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 -> writeExecutionFile -> onFileCreated -> Entering function."); } + if (LOG_FILE_CONTENT === true) { logger.write(MODULE_NAME, "[INFO] start -> writeExecutionFile -> onFileCreated -> fileContent = " + fileContent); } + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + logger.write(MODULE_NAME, "[ERROR] start -> writeExecutionFile -> onFileCreated -> err = " + err.stack); + logger.write(MODULE_NAME, "[ERROR] start -> writeExecutionFile -> onFileCreated -> filePath = " + filePath); + logger.write(MODULE_NAME, "[ERROR] start -> writeExecutionFile -> onFileCreated -> market = " + market.assetA + "_" + market.assetB); + + callBackFunction(err); + return; + + } + + writeRecordsFile(); + + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeExecutionFile -> onFileCreated -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + } + catch (err) { + logger.write(MODULE_NAME, "[ERROR] start -> writeExecutionFile -> err = " + err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } } function writeRecordsFile() { @@ -157,7 +228,7 @@ record.takeProfit + "," + record.stopLossPhase + "," + record.takeProfitPhase + "," + - JSON.stringify(record.orderRecord) + "," + + JSON.stringify(record.executionRecord) + "," + record.positionSize + "," + record.initialBalanceA + "," + record.minimumBalanceA + "," + diff --git a/src/storage/templates/Simulator/this.bot.config.json b/src/storage/templates/Simulator/this.bot.config.json index d0d8572de..15b52e5e1 100644 --- a/src/storage/templates/Simulator/this.bot.config.json +++ b/src/storage/templates/Simulator/this.bot.config.json @@ -79,7 +79,7 @@ "major": 1, "minor": 0 }, - "product": "Bollinger-Channels", + "product": "Bollinger-Standard-Channels", "dataSetVersion": "dataSet.V1", "dataSet": "Multi-Period-Market" }, @@ -90,7 +90,7 @@ "major": 1, "minor": 0 }, - "product": "Bollinger-Sub-Channels", + "product": "Bollinger-Standard-Sub-Channels", "dataSetVersion": "dataSet.V1", "dataSet": "Multi-Period-Market" }, @@ -166,7 +166,7 @@ }, "product": "LRC-Points", "dataSetVersion": "dataSet.V1", - "dataSet": "Multi-Period-Market" + "dataSet": "Multi-Period-Daily" }, { "devTeam": "AAMasters", @@ -177,7 +177,7 @@ }, "product": "Percentage-Bandwidth", "dataSetVersion": "dataSet.V1", - "dataSet": "Multi-Period-Market" + "dataSet": "Multi-Period-Daily" }, { "devTeam": "AAMasters", @@ -188,7 +188,7 @@ }, "product": "Bollinger-Bands", "dataSetVersion": "dataSet.V1", - "dataSet": "Multi-Period-Market" + "dataSet": "Multi-Period-Daily" }, { "devTeam": "AAMasters", @@ -197,9 +197,9 @@ "major": 1, "minor": 0 }, - "product": "Bollinger-Channels", + "product": "Bollinger-Standard-Channels", "dataSetVersion": "dataSet.V1", - "dataSet": "Multi-Period-Market" + "dataSet": "Multi-Period-Daily" }, { "devTeam": "AAMasters", @@ -208,9 +208,9 @@ "major": 1, "minor": 0 }, - "product": "Bollinger-Sub-Channels", + "product": "Bollinger-Standard-Sub-Channels", "dataSetVersion": "dataSet.V1", - "dataSet": "Multi-Period-Market" + "dataSet": "Multi-Period-Daily" }, { "devTeam": "AAMasters", @@ -221,7 +221,7 @@ }, "product": "Candles", "dataSetVersion": "dataSet.V1", - "dataSet": "Multi-Period-Market" + "dataSet": "Multi-Period-Daily" } ] } diff --git a/src/storage/templates/TradingBot/User.Bot.js b/src/storage/templates/TradingBot/User.Bot.js index c8fc78f3a..d1a4d4820 100644 --- a/src/storage/templates/TradingBot/User.Bot.js +++ b/src/storage/templates/TradingBot/User.Bot.js @@ -42,13 +42,10 @@ exports.newUserBot = function newUserBot(bot, logger) { throw new Error("Execution Parameter Data Set not defined.") } - let key = bot.devTeam + '-simulator-' + bot.codeName + '-Trading-Simulation-' + bot.dataSet + '-dataSet.V1' + let key = bot.devTeam + '-simulator-' + bot.codeName + '-Trading-Execution-' + bot.dataSet + '-dataSet.V1' fileStorage = assistant.dataDependencies.dataSets.get(key) if (fileStorage !== undefined) { - assistant.rememberThis('lastSimulatorEngineMessageId', 0) - assistant.rememberThis('lastStopLoss', 0) - assistant.rememberThis('lastTakeProfit', 0) callBackFunction(global.DEFAULT_OK_RESPONSE) } else { logError('initialize -> Failed to initialize storage. Key not found:' + key) @@ -93,21 +90,72 @@ exports.newUserBot = function newUserBot(bot, logger) { }) } + function getValueFromPreviousExecution(key) { + let value = assistant.remindMeOf(key) + if (value === undefined) + value = 0 + return value + } + async function executorLogic() { try { let indicatorFileContent = await getIndicatorFile() let simulatorEngineMessage = getSimulatorEngineMessageFromFile(indicatorFileContent) let autopilotResponse = await getAutopilot() - let stopLoss, takeProfit + let stopLoss, takeProfit, receivedMessageId if (simulatorEngineMessage === undefined) { logWarn('start -> Simulator message not available. Will proceed to check SL and TP from previous execution.') - stopLoss = assistant.remindMeOf('lastStopLoss') - takeProfit = assistant.remindMeOf('lastTakeProfit') + stopLoss = getValueFromPreviousExecution('lastStopLoss') + takeProfit = getValueFromPreviousExecution('lastTakeProfit') + receivedMessageId = 0 } else { logInfo('Processing simulator record: ' + JSON.stringify(simulatorEngineMessage)) - stopLoss = simulatorEngineMessage.order.stop - takeProfit = simulatorEngineMessage.order.takeProfit + receivedMessageId = simulatorEngineMessage.id + + // Verify if there is an inconsistent state between the simulator and executor + let isATradeOpen = (getValueFromPreviousExecution('lastSimulatorEngineMessageId') > 0) + let isMessageAHeartBeat = (simulatorEngineMessage.messageType === MESSAGE_TYPE.HeartBeat) + if (isATradeOpen && isMessageAHeartBeat) { + logWarn('An inconsisten state was detected: there is an open trade and a close trade was expected.') + let skippedMessageOrderClose = getSkippedMessageOrderClose(indicatorFileContent) + if (skippedMessageOrderClose === undefined) { + logWarn('There was not a Skipped Message Order Close. Closing the position the best possible.') + stopLoss = assistant.getMarketRate() + takeProfit = assistant.getMarketRate() + } else { + let lastSimulatorEngineMessageId = getValueFromPreviousExecution('lastSimulatorEngineMessageId') + if (skippedMessageOrderClose.id > lastSimulatorEngineMessageId) { + logWarn('The Skipped Message Order Close was found and using it to set the SL and TP.') + isMessageAnOrderClose = (simulatorEngineMessage.messageType === MESSAGE_TYPE.OrderClose) + isStopLoss = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) + isTakeProfit = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) + } else { + logWarn('One Message Order Close was found but is older than the Open Order. Closing the position the best possible.') + stopLoss = assistant.getMarketRate() + takeProfit = assistant.getMarketRate() + } + } + } + + // Validations to check the sync state between the simulator and executor + let isMessageAnOrderUpdate = (simulatorEngineMessage.messageType === MESSAGE_TYPE.OrderUpdate) + let isMessageAnOrderClose = (simulatorEngineMessage.messageType === MESSAGE_TYPE.OrderClose) + let isStopLoss = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) + let isTakeProfit = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) + + if (isMessageAnOrderUpdate) { + stopLoss = simulatorEngineMessage.order.stop + takeProfit = simulatorEngineMessage.order.takeProfit + } else if (isMessageAnOrderClose && isStopLoss) { + stopLoss = assistant.getMarketRate() + takeProfit = 0 + } else if (isMessageAnOrderClose && isTakeProfit) { + stopLoss = 0 + takeProfit = assistant.getMarketRate() + } + assistant.rememberThis('lastStopLoss', stopLoss) + assistant.rememberThis('lastTakeProfit', takeProfit) } // Checking Stop Loss @@ -120,7 +168,7 @@ exports.newUserBot = function newUserBot(bot, logger) { let position = await createBuyPosition(currentRate) let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = position.id + simulatorExecutorMessage.id = receivedMessageId simulatorExecutorMessage.order.id = position.id simulatorExecutorMessage.order.rate = currentRate simulatorExecutorMessage.order.stop = stopLoss @@ -137,6 +185,7 @@ exports.newUserBot = function newUserBot(bot, logger) { assistant.rememberThis('lastTakeProfit', 0) } catch (error) { let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.id = receivedMessageId simulatorExecutorMessage.from = MESSAGE_ENTITY.TradingAssistant simulatorExecutorMessage.to = MESSAGE_ENTITY.SimulationExecutor simulatorExecutorMessage.order.rate = position.rate @@ -160,7 +209,7 @@ exports.newUserBot = function newUserBot(bot, logger) { let position = await createBuyPosition(currentRate, assetABalance) let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = position.id + simulatorExecutorMessage.id = receivedMessageId simulatorExecutorMessage.order.id = position.id simulatorExecutorMessage.order.rate = currentRate simulatorExecutorMessage.order.stop = stopLoss @@ -177,6 +226,7 @@ exports.newUserBot = function newUserBot(bot, logger) { assistant.rememberThis('lastTakeProfit', 0) } catch (error) { let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.id = receivedMessageId simulatorExecutorMessage.from = MESSAGE_ENTITY.TradingAssistant simulatorExecutorMessage.to = MESSAGE_ENTITY.SimulationExecutor simulatorExecutorMessage.order.rate = currentRate @@ -185,6 +235,7 @@ exports.newUserBot = function newUserBot(bot, logger) { simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Buy simulatorExecutorMessage.order.size = assetABalance simulatorExecutorMessage.order.status = ORDER_STATUS.Rejected + simulatorExecutorMessage.order.statusDetails = error.message let message = createMessageFromObject(simulatorExecutorMessage) assistant.addExtraData(message) @@ -214,7 +265,7 @@ exports.newUserBot = function newUserBot(bot, logger) { if (simulatorEngineMessage.messageType === MESSAGE_TYPE.Order && simulatorEngineMessage.order.direction === ORDER_DIRECTION.Sell) { - let lastSimulatorEngineMessageId = assistant.remindMeOf('lastSimulatorEngineMessageId') + let lastSimulatorEngineMessageId = getValueFromPreviousExecution('lastSimulatorEngineMessageId') if (lastSimulatorEngineMessageId >= simulatorEngineMessage.id) { logInfo("manageCloneInAutopilotOn -> Order message was previously processed.") return @@ -223,7 +274,7 @@ exports.newUserBot = function newUserBot(bot, logger) { try { let position = await createSellPosition(currentRate, assetBBalance) let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = position.id + simulatorExecutorMessage.id = simulatorEngineMessage.id simulatorExecutorMessage.order.id = position.id simulatorExecutorMessage.order.rate = currentRate simulatorExecutorMessage.order.stop = simulatorEngineMessage.order.stop @@ -233,11 +284,12 @@ exports.newUserBot = function newUserBot(bot, logger) { let message = createMessageFromObject(simulatorExecutorMessage) assistant.addExtraData(message) - assistant.rememberThis('lastSimulatorEngineMessageId', parseInt(simulatorEngineMessage.id)) + assistant.rememberThis('lastSimulatorEngineMessageId', simulatorEngineMessage.id) assistant.rememberThis('lastStopLoss', simulatorEngineMessage.order.stop) assistant.rememberThis('lastTakeProfit', simulatorEngineMessage.order.takeProfit) } catch (error) { let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.id = receivedMessageId simulatorExecutorMessage.from = MESSAGE_ENTITY.TradingAssistant simulatorExecutorMessage.to = MESSAGE_ENTITY.SimulationExecutor simulatorExecutorMessage.messageType = MESSAGE_TYPE.Order @@ -247,6 +299,7 @@ exports.newUserBot = function newUserBot(bot, logger) { simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Sell simulatorExecutorMessage.order.size = assetBBalance simulatorExecutorMessage.order.status = ORDER_STATUS.Rejected + simulatorExecutorMessage.order.statusDetails = error.message let message = createMessageFromObject(simulatorExecutorMessage) assistant.addExtraData(message) @@ -272,7 +325,7 @@ exports.newUserBot = function newUserBot(bot, logger) { // 1) Save audit containing what we received from the cockpit let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = 0 + simulatorExecutorMessage.id = simulatorEngineMessage.id simulatorExecutorMessage.from = MESSAGE_ENTITY.TradingCokpit simulatorExecutorMessage.to = MESSAGE_ENTITY.SimulationExecutor simulatorExecutorMessage.order = acceptedSignal.orderData // we save the order as received @@ -285,7 +338,7 @@ exports.newUserBot = function newUserBot(bot, logger) { // 3) Save the audit of the order placed simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = position.id + simulatorExecutorMessage.id = simulatorEngineMessage.id simulatorExecutorMessage.from = MESSAGE_ENTITY.SimulationExecutor simulatorExecutorMessage.to = MESSAGE_ENTITY.TradingAssistant simulatorExecutorMessage.dateTime = position.date @@ -307,12 +360,13 @@ exports.newUserBot = function newUserBot(bot, logger) { } catch (error) { // Update the cockpit with the error let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = 0 + simulatorExecutorMessage.id = simulatorEngineMessage.id 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.statusDetails = error.message simulatorExecutorMessage.order = acceptedSignal.order @@ -405,7 +459,7 @@ exports.newUserBot = function newUserBot(bot, logger) { // 2) Save the audit message for the received info from Trading Simulator let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = 0 + simulatorExecutorMessage.id = simulatorEngineMessage.id simulatorExecutorMessage.to = MESSAGE_ENTITY.TradingCokpit simulatorExecutorMessage.messageType = MESSAGE_TYPE.Order simulatorExecutorMessage.order.id = 0 @@ -524,17 +578,17 @@ exports.newUserBot = function newUserBot(bot, logger) { // Storage functions async function getIndicatorFile() { - logInfo('getIndicatorFile -> Entering function.') + logInfo('getIndicatorFile -> Entering function. ' + bot.processDatetime.toISOString()) let filePath if (bot.processes[0].timePeriod > 2700000) { // Market Files - filePath = 'Trading-Simulation/' + bot.dataSet + '/' + bot.timePeriodFileStorage + filePath = 'Trading-Execution/' + 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 + filePath = 'Trading-Execution/' + bot.dataSet + '/' + bot.timePeriodFileStorage + '/' + datePath } /* @@ -561,7 +615,7 @@ exports.newUserBot = function newUserBot(bot, logger) { 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]) + return getMessage(indicatorFileContent[i][2]) } } @@ -570,7 +624,7 @@ exports.newUserBot = function newUserBot(bot, logger) { // Running live we will process last available Indicator Message only if it's delayed 10 minutes top let maxTolerance = 10 * 60 * 1000 if (bot.processDatetime.valueOf() <= (lastAvailableDateTime + maxTolerance)) { - return getMessage(indicatorFileContent[lastIndexIndicatorFile][25]) + return getMessage(indicatorFileContent[lastIndexIndicatorFile][2]) } else { logWarn('getSimulatorEngineMessageFromFile -> Last available indicator older than 10 minutes.') } @@ -581,6 +635,22 @@ exports.newUserBot = function newUserBot(bot, logger) { } } + function getSkippedMessageOrderClose(indicatorFileContent) { + try { + let lastIndexIndicatorFile = indicatorFileContent.length - 1 + + for (let i = lastIndexIndicatorFile; i >= 0; i--) { + let simulatorMessage = getMessage(indicatorFileContent[i][2]) + if (simulatorMessage.messageType === MESSAGE_TYPE.OrderClose) { + return simulatorMessage + } + } + } catch (error) { + logError('getSkippedMessageOrderClose -> error = ' + error.message) + throw global.DEFAULT_RETRY_RESPONSE + } + } + // Helper Functions function isExecutionToday() { let localDate = new Date() @@ -774,16 +844,18 @@ exports.newUserBot = function newUserBot(bot, logger) { simulatorExecutorMessage.dateTime = bot.processDatetime.valueOf() simulatorExecutorMessage.order = {} + simulatorExecutorMessage.order.id = 0 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.market = global.MARKET.assetA + '_' + global.MARKET.assetB 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 = '' + simulatorExecutorMessage.order.statusMessage = '' return simulatorExecutorMessage } diff --git a/src/storage/templates/TradingBot/this.bot.config.json b/src/storage/templates/TradingBot/this.bot.config.json index c3d22f52e..d59dba4bc 100644 --- a/src/storage/templates/TradingBot/this.bot.config.json +++ b/src/storage/templates/TradingBot/this.bot.config.json @@ -7,7 +7,7 @@ "minor": 0, "patch": 0 }, - "devTeam": "aavikings", + "devTeam": "vikings", "profilePicture": "bumblebee.png", "dataSetVersion": "dataSet.V1", "processes": [ @@ -73,7 +73,7 @@ "major": 1, "minor": 0 }, - "product": "Trading-Simulation", + "product": "Trading-Execution", "dataSetVersion": "dataSet.V1", "dataSet": "Multi-Period-Market" }, @@ -84,7 +84,7 @@ "major": 1, "minor": 0 }, - "product": "Trading-Simulation", + "product": "Trading-Execution", "dataSetVersion": "dataSet.V1", "dataSet": "Multi-Period-Daily" } @@ -101,27 +101,7 @@ { "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" - ], + "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" } @@ -131,11 +111,11 @@ "name": "Poloniex" } ], - "plotter": { - "devTeam": "AAMasters", - "codeName": "PlottersTradingSimulation", - "moduleName": "SimulationExecution" - } + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersTradingSimulation", + "moduleName": "SimulationExecution" + } }, { "codeName": "Backtest Trading History", @@ -146,27 +126,7 @@ { "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" - ], + "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" } @@ -176,11 +136,11 @@ "name": "Poloniex" } ], - "plotter": { - "devTeam": "AAMasters", - "codeName": "PlottersTradingSimulation", - "moduleName": "SimulationExecution" - } + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersTradingSimulation", + "moduleName": "SimulationExecution" + } }, { "codeName": "Competition Trading History", @@ -191,27 +151,7 @@ { "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" - ], + "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" } @@ -221,11 +161,11 @@ "name": "Poloniex" } ], - "plotter": { - "devTeam": "AAMasters", - "codeName": "PlottersTradingSimulation", - "moduleName": "SimulationExecution" - } + "plotter": { + "devTeam": "AAMasters", + "codeName": "PlottersTradingSimulation", + "moduleName": "SimulationExecution" + } } ], "marketRateProvider": { From 0ad5aad7edcf3f707f9f5d29bc04185d56709fdc Mon Sep 17 00:00:00 2001 From: Matias Date: Thu, 11 Jul 2019 17:20:27 -0300 Subject: [PATCH 28/34] Fixing variable definition issue --- src/storage/templates/TradingBot/User.Bot.js | 21 +++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/storage/templates/TradingBot/User.Bot.js b/src/storage/templates/TradingBot/User.Bot.js index d1a4d4820..4eab68256 100644 --- a/src/storage/templates/TradingBot/User.Bot.js +++ b/src/storage/templates/TradingBot/User.Bot.js @@ -113,23 +113,26 @@ exports.newUserBot = function newUserBot(bot, logger) { logInfo('Processing simulator record: ' + JSON.stringify(simulatorEngineMessage)) receivedMessageId = simulatorEngineMessage.id + // Verify if there is an inconsistent state between the simulator and executor + let isMessageAnOrderUpdate = (simulatorEngineMessage.messageType === MESSAGE_TYPE.OrderUpdate) + let isMessageAnOrderClose = (simulatorEngineMessage.messageType === MESSAGE_TYPE.OrderClose) + let isStopLoss = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) + let isTakeProfit = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) + let isATradeOpen = (getValueFromPreviousExecution('lastSimulatorEngineMessageId') > 0) let isMessageAHeartBeat = (simulatorEngineMessage.messageType === MESSAGE_TYPE.HeartBeat) if (isATradeOpen && isMessageAHeartBeat) { logWarn('An inconsisten state was detected: there is an open trade and a close trade was expected.') - let skippedMessageOrderClose = getSkippedMessageOrderClose(indicatorFileContent) - if (skippedMessageOrderClose === undefined) { + simulatorEngineMessage = getSkippedMessageOrderClose(indicatorFileContent) + if (simulatorEngineMessage === undefined) { logWarn('There was not a Skipped Message Order Close. Closing the position the best possible.') stopLoss = assistant.getMarketRate() takeProfit = assistant.getMarketRate() } else { let lastSimulatorEngineMessageId = getValueFromPreviousExecution('lastSimulatorEngineMessageId') - if (skippedMessageOrderClose.id > lastSimulatorEngineMessageId) { + if (simulatorEngineMessage.id > lastSimulatorEngineMessageId) { logWarn('The Skipped Message Order Close was found and using it to set the SL and TP.') - isMessageAnOrderClose = (simulatorEngineMessage.messageType === MESSAGE_TYPE.OrderClose) - isStopLoss = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) - isTakeProfit = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) } else { logWarn('One Message Order Close was found but is older than the Open Order. Closing the position the best possible.') stopLoss = assistant.getMarketRate() @@ -138,12 +141,6 @@ exports.newUserBot = function newUserBot(bot, logger) { } } - // Validations to check the sync state between the simulator and executor - let isMessageAnOrderUpdate = (simulatorEngineMessage.messageType === MESSAGE_TYPE.OrderUpdate) - let isMessageAnOrderClose = (simulatorEngineMessage.messageType === MESSAGE_TYPE.OrderClose) - let isStopLoss = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) - let isTakeProfit = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) - if (isMessageAnOrderUpdate) { stopLoss = simulatorEngineMessage.order.stop takeProfit = simulatorEngineMessage.order.takeProfit From 5150ff0db865a6800208510cdab2c280849e871a Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 12 Jul 2019 20:34:56 +0200 Subject: [PATCH 29/34] Update ecosystem.json --- src/config/ecosystem.json | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/config/ecosystem.json b/src/config/ecosystem.json index 20982e0fb..dfde878c0 100644 --- a/src/config/ecosystem.json +++ b/src/config/ecosystem.json @@ -678,7 +678,13 @@ "moduleName": "SimulationExecution", "description": "Plots Simulation Execution.", "profilePicture": "SimulationExecution.png", - "panels": [] + "panels": [ + { + "codeName": "SimulationExecutionPanel", + "moduleName": "SimulationExecutionPanel", + "event": "Current Record Changed" + } + ] } ], "repo": "Plotters-Trading-Simulation", From 84e3981f0964623643f7b9ca8fddcc6fb57c1325 Mon Sep 17 00:00:00 2001 From: Matias Date: Sat, 13 Jul 2019 23:23:11 -0300 Subject: [PATCH 30/34] Fixing minor issues --- src/errors/index.js | 2 +- src/storage/templates/TradingBot/User.Bot.js | 393 +++++++++++-------- src/utils/getDevTeamHost.js | 6 +- 3 files changed, 228 insertions(+), 173 deletions(-) diff --git a/src/errors/index.js b/src/errors/index.js index e9999e0a1..4680e2202 100644 --- a/src/errors/index.js +++ b/src/errors/index.js @@ -2,7 +2,7 @@ export class AuthenticationError extends Error { constructor(message) { super(message) this.code = 401 - this.message = 'Autentification not found, you have to be authentificated to perform this action.' + this.message = 'Authentication not found, you have to be authenticated to perform this action.' } } diff --git a/src/storage/templates/TradingBot/User.Bot.js b/src/storage/templates/TradingBot/User.Bot.js index 4eab68256..fb8ba4b08 100644 --- a/src/storage/templates/TradingBot/User.Bot.js +++ b/src/storage/templates/TradingBot/User.Bot.js @@ -23,7 +23,8 @@ exports.newUserBot = function newUserBot(bot, logger) { return { initialize: initialize, start: start, - setAutopilot: setAutopilot + setAutopilot: setAutopilot, + executorLogic: executorLogic } function initialize(pAssistant, pGenes, callBackFunction) { @@ -101,212 +102,257 @@ exports.newUserBot = function newUserBot(bot, logger) { try { let indicatorFileContent = await getIndicatorFile() let simulatorEngineMessage = getSimulatorEngineMessageFromFile(indicatorFileContent) - let autopilotResponse = await getAutopilot() - let stopLoss, takeProfit, receivedMessageId + let stopLoss = getValueFromPreviousExecution('lastStopLoss') + let takeProfit = getValueFromPreviousExecution('lastTakeProfit') + let openTradeSimulatorMessageId = getValueFromPreviousExecution('openTradeSimulatorMessageId') + if (simulatorEngineMessage === undefined) { - logWarn('start -> Simulator message not available. Will proceed to check SL and TP from previous execution.') - stopLoss = getValueFromPreviousExecution('lastStopLoss') - takeProfit = getValueFromPreviousExecution('lastTakeProfit') - receivedMessageId = 0 + if (openTradeSimulatorMessageId > 0) { + logWarn('executorLogic -> Simulator message not available. Will proceed to check if the trade needs to be closed.') + let simulatorMessajeNotFound = -1 + await closeTrade(stopLoss, takeProfit, simulatorMessajeNotFound) + } } else { - logInfo('Processing simulator record: ' + JSON.stringify(simulatorEngineMessage)) - receivedMessageId = simulatorEngineMessage.id - + logInfo('executorLogic -> Processing simulator record: ' + JSON.stringify(simulatorEngineMessage)) // Verify if there is an inconsistent state between the simulator and executor + let isMessageAnOrder = (simulatorEngineMessage.messageType === MESSAGE_TYPE.Order) let isMessageAnOrderUpdate = (simulatorEngineMessage.messageType === MESSAGE_TYPE.OrderUpdate) let isMessageAnOrderClose = (simulatorEngineMessage.messageType === MESSAGE_TYPE.OrderClose) - let isStopLoss = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) - let isTakeProfit = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) - - let isATradeOpen = (getValueFromPreviousExecution('lastSimulatorEngineMessageId') > 0) let isMessageAHeartBeat = (simulatorEngineMessage.messageType === MESSAGE_TYPE.HeartBeat) - if (isATradeOpen && isMessageAHeartBeat) { - logWarn('An inconsisten state was detected: there is an open trade and a close trade was expected.') - simulatorEngineMessage = getSkippedMessageOrderClose(indicatorFileContent) - if (simulatorEngineMessage === undefined) { - logWarn('There was not a Skipped Message Order Close. Closing the position the best possible.') - stopLoss = assistant.getMarketRate() - takeProfit = assistant.getMarketRate() - } else { - let lastSimulatorEngineMessageId = getValueFromPreviousExecution('lastSimulatorEngineMessageId') - if (simulatorEngineMessage.id > lastSimulatorEngineMessageId) { - logWarn('The Skipped Message Order Close was found and using it to set the SL and TP.') - } else { - logWarn('One Message Order Close was found but is older than the Open Order. Closing the position the best possible.') + + let existsAnOpenTrade = (openTradeSimulatorMessageId > 0) + if (existsAnOpenTrade) { + if (isMessageAnOrder && simulatorEngineMessage.id === openTradeSimulatorMessageId) { + logInfo('We just put an order with the latest simualtor record available, checking SL and TP.') + } else if (isMessageAnOrderUpdate) { + await openOrMoveTrade(simulatorEngineMessage) + } else if (isMessageAnOrderClose) { + assistant.rememberThis('closeTradeSimulatorMessageId', simulatorEngineMessage.id) + let isStopLoss = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) + let isTakeProfit = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) + if (isStopLoss) { stopLoss = assistant.getMarketRate() + takeProfit = 0 + } else if (isTakeProfit) { + stopLoss = 0 takeProfit = assistant.getMarketRate() } + } else if (isMessageAHeartBeat) { + logWarn('An inconsisten state was detected: there is an open trade and a close trade was expected.') + let skippedSimulatorEngineMessage = getSkippedMessageOrderClose(indicatorFileContent) + if (skippedSimulatorEngineMessage === undefined) { + logWarn('There was not a Skipped Message Order Close. Closing the position the best possible.') + stopLoss = assistant.getMarketRate() + takeProfit = 0 + } else { + if (skippedSimulatorEngineMessage.id > openTradeSimulatorMessageId) { + logWarn('The Skipped Message Order Close was found and using it to set the SL and TP.') + stopLoss = skippedSimulatorEngineMessage.order.stop + takeProfit = skippedSimulatorEngineMessage.order.takeProfit + } else { + logWarn('One Message Order Close was found but is older than the Open Order. Closing the position the best possible.') + stopLoss = assistant.getMarketRate() + takeProfit = 0 + } + } + } else { + logError('Not managed condition.') } - } - - if (isMessageAnOrderUpdate) { - stopLoss = simulatorEngineMessage.order.stop - takeProfit = simulatorEngineMessage.order.takeProfit - } else if (isMessageAnOrderClose && isStopLoss) { - stopLoss = assistant.getMarketRate() - takeProfit = 0 - } else if (isMessageAnOrderClose && isTakeProfit) { - stopLoss = 0 - takeProfit = assistant.getMarketRate() - } - assistant.rememberThis('lastStopLoss', stopLoss) - assistant.rememberThis('lastTakeProfit', takeProfit) - } - - // Checking Stop Loss - let assetABalance = assistant.getAvailableBalance().assetA - let currentRate = assistant.getMarketRate() - - if (stopLoss > 0 && assetABalance > 0 && currentRate >= stopLoss) { - logInfo('Closing trade with Stop Loss.') - try { - let position = await createBuyPosition(currentRate) - - let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = receivedMessageId - simulatorExecutorMessage.order.id = position.id - simulatorExecutorMessage.order.rate = currentRate - simulatorExecutorMessage.order.stop = stopLoss - simulatorExecutorMessage.order.takeProfit = 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) - assistant.rememberThis('lastSimulatorEngineMessageId', 0) - assistant.rememberThis('lastStopLoss', 0) - assistant.rememberThis('lastTakeProfit', 0) - } catch (error) { - let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = receivedMessageId - simulatorExecutorMessage.from = MESSAGE_ENTITY.TradingAssistant - simulatorExecutorMessage.to = MESSAGE_ENTITY.SimulationExecutor - simulatorExecutorMessage.order.rate = position.rate - simulatorExecutorMessage.order.stop = stopLoss - simulatorExecutorMessage.order.takeProfit = 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 (takeProfit > 0 && assetABalance > 0 && currentRate <= takeProfit) { - logInfo('Closing trade with Take Profit.') - try { - let position = await createBuyPosition(currentRate, assetABalance) - - let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = receivedMessageId - simulatorExecutorMessage.order.id = position.id - simulatorExecutorMessage.order.rate = currentRate - simulatorExecutorMessage.order.stop = stopLoss - simulatorExecutorMessage.order.takeProfit = 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) - assistant.rememberThis('lastSimulatorEngineMessageId', 0) - assistant.rememberThis('lastStopLoss', 0) - assistant.rememberThis('lastTakeProfit', 0) - } catch (error) { - let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = receivedMessageId - simulatorExecutorMessage.from = MESSAGE_ENTITY.TradingAssistant - simulatorExecutorMessage.to = MESSAGE_ENTITY.SimulationExecutor - simulatorExecutorMessage.order.rate = currentRate - simulatorExecutorMessage.order.stop = stopLoss - simulatorExecutorMessage.order.takeProfit = takeProfit - simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Buy - simulatorExecutorMessage.order.size = assetABalance - simulatorExecutorMessage.order.status = ORDER_STATUS.Rejected - simulatorExecutorMessage.order.statusDetails = error.message - - let message = createMessageFromObject(simulatorExecutorMessage) - assistant.addExtraData(message) - } finally { - return - } - } - - if (simulatorEngineMessage !== undefined) { - if (autopilotResponse.autopilot || autopilotControl) { - await manageCloneInAutopilotOn(simulatorEngineMessage) + assistant.rememberThis('lastStopLoss', stopLoss) + assistant.rememberThis('lastTakeProfit', takeProfit) + await closeTrade(stopLoss, takeProfit, openTradeSimulatorMessageId) } else { - await manageCloneInAutopilotOff(simulatorEngineMessage) + if (isMessageAnOrder) { + await openOrMoveTrade(simulatorEngineMessage) + } else { + logInfo('Nothing to do, there is not an open order.') + } } } } catch (error) { - logError('Error on executorLogic: ' + error) - logError('Stack: ' + JSON.stringify(error.stack)) + logError('Error on executorLogic: ' + JSON.stringify(error)) throw error } } - async function manageCloneInAutopilotOn(simulatorEngineMessage) { - let assetBBalance = assistant.getAvailableBalance().assetB + async function closeTrade(stopLoss, takeProfit, openTradeSimulatorMessageId) { + let assetABalance = assistant.getAvailableBalance().assetA let currentRate = assistant.getMarketRate() - if (simulatorEngineMessage.messageType === MESSAGE_TYPE.Order - && simulatorEngineMessage.order.direction === ORDER_DIRECTION.Sell) { - - let lastSimulatorEngineMessageId = getValueFromPreviousExecution('lastSimulatorEngineMessageId') - if (lastSimulatorEngineMessageId >= simulatorEngineMessage.id) { - logInfo("manageCloneInAutopilotOn -> Order message was previously processed.") - return + // Check completion of current positions at the exchange + let closeTradeSimulatorMessageId = getValueFromPreviousExecution('closeTradeSimulatorMessageId') + let positions = assistant.getPositions() + let isPositionExecuted = false + for (let i = 0; i < positions.length; i++) { + const position = positions[i]; + if (position.id === closeTradeSimulatorMessageId && position.status === 'executed') { + isPositionExecuted = true + break } + } + let wasExecuted = closeTradeSimulatorMessageId > 0 && assistant.getAvailableBalance().assetA === 0 && assistant.getAvailableBalance().assetB > 0 + if (isPositionExecuted || wasExecuted) { + assistant.rememberThis('openTradeSimulatorMessageId', 0) + assistant.rememberThis('closeTradeSimulatorMessageId', 0) + assistant.rememberThis('openTradePositionId', 0) + assistant.rememberThis('lastStopLoss', 0) + assistant.rememberThis('lastTakeProfit', 0) + + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.id = openTradeSimulatorMessageId + simulatorExecutorMessage.order.id = positions.length > 0 ? positions[0].id : 0 + simulatorExecutorMessage.order.rate = currentRate + simulatorExecutorMessage.order.stop = stopLoss + simulatorExecutorMessage.order.takeProfit = 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) + } + + // Checking Stop Loss + if (stopLoss > 0 && closeTradeSimulatorMessageId > 0) { + logInfo('Closing trade with Stop Loss.') try { - let position = await createSellPosition(currentRate, assetBBalance) + let position = await createBuyPosition(currentRate) + assistant.rememberThis('closeTradePositionId', position.id) + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = simulatorEngineMessage.id + simulatorExecutorMessage.id = openTradeSimulatorMessageId 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 + simulatorExecutorMessage.order.stop = stopLoss + simulatorExecutorMessage.order.takeProfit = 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) - assistant.rememberThis('lastSimulatorEngineMessageId', simulatorEngineMessage.id) - assistant.rememberThis('lastStopLoss', simulatorEngineMessage.order.stop) - assistant.rememberThis('lastTakeProfit', simulatorEngineMessage.order.takeProfit) + } catch (error) { let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = receivedMessageId + simulatorExecutorMessage.id = openTradeSimulatorMessageId 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.stop = stopLoss + simulatorExecutorMessage.order.takeProfit = 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 (takeProfit > 0 && closeTradeSimulatorMessageId > 0) { + logInfo('Closing trade with Take Profit.') + try { + let position = await createBuyPosition(currentRate, assetABalance) + assistant.rememberThis('closeTradePositionId', position.id) + + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.id = openTradeSimulatorMessageId + simulatorExecutorMessage.order.id = position.id + simulatorExecutorMessage.order.rate = currentRate + simulatorExecutorMessage.order.stop = stopLoss + simulatorExecutorMessage.order.takeProfit = 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.id = openTradeSimulatorMessageId + simulatorExecutorMessage.from = MESSAGE_ENTITY.TradingAssistant + simulatorExecutorMessage.to = MESSAGE_ENTITY.SimulationExecutor + simulatorExecutorMessage.order.rate = currentRate + simulatorExecutorMessage.order.stop = stopLoss + simulatorExecutorMessage.order.takeProfit = takeProfit + simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Buy + simulatorExecutorMessage.order.size = assetABalance simulatorExecutorMessage.order.status = ORDER_STATUS.Rejected simulatorExecutorMessage.order.statusDetails = error.message let message = createMessageFromObject(simulatorExecutorMessage) assistant.addExtraData(message) + } finally { + return } + } + + } + + async function openOrMoveTrade(simulatorEngineMessage) { + autopilotControl = true // Force autopilot on for now + if (!autopilotControl) { + autopilotControl = await getAutopilot() + } + if (autopilotControl.autopilot || autopilotControl) { + await openTradeAutopilotOn(simulatorEngineMessage) } else { - logInfo("manageCloneInAutopilotOn -> Nothing to do, there isn't a buy or sell opportunity.") + await openTradeAutopilotOff(simulatorEngineMessage) } } - async function manageCloneInAutopilotOff(simulatorEngineMessage) { + async function openTradeAutopilotOn(simulatorEngineMessage) { + let assetBBalance = assistant.getAvailableBalance().assetB + let currentRate = assistant.getMarketRate() + + try { + // Create a new position or move existing position to complete the order execution + let position = await createSellPosition(currentRate, assetBBalance) + assistant.rememberThis('openTradePositionId', position.id) + assistant.rememberThis('openTradeSimulatorMessageId', simulatorEngineMessage.id) + assistant.rememberThis('lastStopLoss', simulatorEngineMessage.order.stop) + assistant.rememberThis('lastTakeProfit', simulatorEngineMessage.order.takeProfit) + + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.id = simulatorEngineMessage.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.id = simulatorEngineMessage.id + 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 + simulatorExecutorMessage.order.statusDetails = error.message + + let message = createMessageFromObject(simulatorExecutorMessage) + assistant.addExtraData(message) + } + } + + async function openTradeAutopilotOff(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 @@ -318,7 +364,7 @@ exports.newUserBot = function newUserBot(bot, logger) { if (acceptedSignals !== undefined && acceptedSignals.length > 0) { for (let acceptedSignal of acceptedSignals) { try { - logInfo('manageCloneInAutopilotOff -> Executing manual authorized signal.') + logInfo('openTradeAutopilotOff -> Executing manual authorized signal.') // 1) Save audit containing what we received from the cockpit let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() @@ -332,6 +378,10 @@ exports.newUserBot = function newUserBot(bot, logger) { // 2) Execute the order in the market as received let position = await createSellPosition(acceptedSignal.orderData.rate, acceptedSignal.orderData.size) + assistant.rememberThis('openTradePositionId', position.id) + assistant.rememberThis('openTradeSimulatorMessageId', simulatorEngineMessage.id) + assistant.rememberThis('lastStopLoss', simulatorEngineMessage.order.stop) + assistant.rememberThis('lastTakeProfit', simulatorEngineMessage.order.takeProfit) // 3) Save the audit of the order placed simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() @@ -386,7 +436,7 @@ exports.newUserBot = function newUserBot(bot, logger) { let inProcessSignals = await getSignalsByCloneId(ORDER_STATUS.Placed) if (inProcessSignals !== undefined && inProcessSignals.length > 0) { for (let inProcessSignal of inProcessSignals) { - logInfo('manageCloneInAutopilotOff -> Checking in process signal.') + logInfo('openTradeAutopilotOff -> Checking in process signal.') // 1) Check order status on the market let position = checkSignalCompletion() if (position || position === undefined) { @@ -423,7 +473,7 @@ exports.newUserBot = function newUserBot(bot, logger) { 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.') + logInfo('openTradeAutopilotOff -> Signal waiting for approval found, updating it with latest market data.') let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() simulatorExecutorMessage.to = MESSAGE_ENTITY.TradingCokpit @@ -473,7 +523,7 @@ exports.newUserBot = function newUserBot(bot, logger) { // 3) Create the signal on the cockpit await createSignal(simulatorExecutorMessage) } else { - logWarn("manageCloneInAutopilotOff -> Nothing to do, there isn't a buy or sell opportunity.") + logWarn("openTradeAutopilotOff -> Nothing to do, there isn't a buy or sell opportunity.") } } @@ -607,6 +657,11 @@ exports.newUserBot = function newUserBot(bot, logger) { function getSimulatorEngineMessageFromFile(indicatorFileContent) { try { let lastIndexIndicatorFile = indicatorFileContent.length - 1 + + if (indicatorFileContent[lastIndexIndicatorFile] === undefined) { + logWarn('getSimulatorEngineMessageFromFile -> Recived file does not contain any record yet.') + return + } let lastAvailableDateTime = indicatorFileContent[lastIndexIndicatorFile][0] if (bot.processDatetime.valueOf() <= lastAvailableDateTime || !isExecutionToday()) { @@ -618,12 +673,14 @@ exports.newUserBot = function newUserBot(bot, logger) { 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 10 minutes top - let maxTolerance = 10 * 60 * 1000 + // Running live we will process last available Indicator Message only if it's delayed 30 minutes top + let maxTolerance = 30 * 60 * 1000 if (bot.processDatetime.valueOf() <= (lastAvailableDateTime + maxTolerance)) { + let minutesDelayed = (bot.processDatetime.valueOf() - lastAvailableDateTime) / 60 / 1000 + logWarn('getSimulatorEngineMessageFromFile -> Last available indicator is delayed: ' + minutesDelayed.toFixed(2) + ' minutes.') return getMessage(indicatorFileContent[lastIndexIndicatorFile][2]) } else { - logWarn('getSimulatorEngineMessageFromFile -> Last available indicator older than 10 minutes.') + logWarn('getSimulatorEngineMessageFromFile -> Last available indicator older than 30 minutes: ' + JSON.stringify(indicatorFileContent[lastIndexIndicatorFile])) } } } catch (error) { diff --git a/src/utils/getDevTeamHost.js b/src/utils/getDevTeamHost.js index c244cf8e1..2066fc4fb 100644 --- a/src/utils/getDevTeamHost.js +++ b/src/utils/getDevTeamHost.js @@ -8,10 +8,8 @@ export default async function getDevTeamHost(devTeamName, accessKey, ownerKey) { } else { host = teamCache.get(devTeamName + '.ownerKey') } - if (host) { - if (host.accessKey === accessKey || host.ownerKey === ownerKey) { - return host - } + if (host && (host.accessKey === accessKey || host.ownerKey === ownerKey)) { + return host } else { let ecosystem = await Ecosystem.find() From 77e4b641d56f2da39109dc358af4ba25438258ff Mon Sep 17 00:00:00 2001 From: Matias Date: Sat, 13 Jul 2019 23:23:11 -0300 Subject: [PATCH 31/34] Fixing minor issues --- src/errors/index.js | 2 +- src/storage/templates/TradingBot/User.Bot.js | 393 +++++++++++-------- src/utils/getDevTeamHost.js | 6 +- 3 files changed, 228 insertions(+), 173 deletions(-) diff --git a/src/errors/index.js b/src/errors/index.js index e9999e0a1..4680e2202 100644 --- a/src/errors/index.js +++ b/src/errors/index.js @@ -2,7 +2,7 @@ export class AuthenticationError extends Error { constructor(message) { super(message) this.code = 401 - this.message = 'Autentification not found, you have to be authentificated to perform this action.' + this.message = 'Authentication not found, you have to be authenticated to perform this action.' } } diff --git a/src/storage/templates/TradingBot/User.Bot.js b/src/storage/templates/TradingBot/User.Bot.js index 4eab68256..fb8ba4b08 100644 --- a/src/storage/templates/TradingBot/User.Bot.js +++ b/src/storage/templates/TradingBot/User.Bot.js @@ -23,7 +23,8 @@ exports.newUserBot = function newUserBot(bot, logger) { return { initialize: initialize, start: start, - setAutopilot: setAutopilot + setAutopilot: setAutopilot, + executorLogic: executorLogic } function initialize(pAssistant, pGenes, callBackFunction) { @@ -101,212 +102,257 @@ exports.newUserBot = function newUserBot(bot, logger) { try { let indicatorFileContent = await getIndicatorFile() let simulatorEngineMessage = getSimulatorEngineMessageFromFile(indicatorFileContent) - let autopilotResponse = await getAutopilot() - let stopLoss, takeProfit, receivedMessageId + let stopLoss = getValueFromPreviousExecution('lastStopLoss') + let takeProfit = getValueFromPreviousExecution('lastTakeProfit') + let openTradeSimulatorMessageId = getValueFromPreviousExecution('openTradeSimulatorMessageId') + if (simulatorEngineMessage === undefined) { - logWarn('start -> Simulator message not available. Will proceed to check SL and TP from previous execution.') - stopLoss = getValueFromPreviousExecution('lastStopLoss') - takeProfit = getValueFromPreviousExecution('lastTakeProfit') - receivedMessageId = 0 + if (openTradeSimulatorMessageId > 0) { + logWarn('executorLogic -> Simulator message not available. Will proceed to check if the trade needs to be closed.') + let simulatorMessajeNotFound = -1 + await closeTrade(stopLoss, takeProfit, simulatorMessajeNotFound) + } } else { - logInfo('Processing simulator record: ' + JSON.stringify(simulatorEngineMessage)) - receivedMessageId = simulatorEngineMessage.id - + logInfo('executorLogic -> Processing simulator record: ' + JSON.stringify(simulatorEngineMessage)) // Verify if there is an inconsistent state between the simulator and executor + let isMessageAnOrder = (simulatorEngineMessage.messageType === MESSAGE_TYPE.Order) let isMessageAnOrderUpdate = (simulatorEngineMessage.messageType === MESSAGE_TYPE.OrderUpdate) let isMessageAnOrderClose = (simulatorEngineMessage.messageType === MESSAGE_TYPE.OrderClose) - let isStopLoss = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) - let isTakeProfit = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) - - let isATradeOpen = (getValueFromPreviousExecution('lastSimulatorEngineMessageId') > 0) let isMessageAHeartBeat = (simulatorEngineMessage.messageType === MESSAGE_TYPE.HeartBeat) - if (isATradeOpen && isMessageAHeartBeat) { - logWarn('An inconsisten state was detected: there is an open trade and a close trade was expected.') - simulatorEngineMessage = getSkippedMessageOrderClose(indicatorFileContent) - if (simulatorEngineMessage === undefined) { - logWarn('There was not a Skipped Message Order Close. Closing the position the best possible.') - stopLoss = assistant.getMarketRate() - takeProfit = assistant.getMarketRate() - } else { - let lastSimulatorEngineMessageId = getValueFromPreviousExecution('lastSimulatorEngineMessageId') - if (simulatorEngineMessage.id > lastSimulatorEngineMessageId) { - logWarn('The Skipped Message Order Close was found and using it to set the SL and TP.') - } else { - logWarn('One Message Order Close was found but is older than the Open Order. Closing the position the best possible.') + + let existsAnOpenTrade = (openTradeSimulatorMessageId > 0) + if (existsAnOpenTrade) { + if (isMessageAnOrder && simulatorEngineMessage.id === openTradeSimulatorMessageId) { + logInfo('We just put an order with the latest simualtor record available, checking SL and TP.') + } else if (isMessageAnOrderUpdate) { + await openOrMoveTrade(simulatorEngineMessage) + } else if (isMessageAnOrderClose) { + assistant.rememberThis('closeTradeSimulatorMessageId', simulatorEngineMessage.id) + let isStopLoss = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) + let isTakeProfit = (simulatorEngineMessage.order.exitOutcome === ORDER_EXIT_OUTCOME.StopLoss) + if (isStopLoss) { stopLoss = assistant.getMarketRate() + takeProfit = 0 + } else if (isTakeProfit) { + stopLoss = 0 takeProfit = assistant.getMarketRate() } + } else if (isMessageAHeartBeat) { + logWarn('An inconsisten state was detected: there is an open trade and a close trade was expected.') + let skippedSimulatorEngineMessage = getSkippedMessageOrderClose(indicatorFileContent) + if (skippedSimulatorEngineMessage === undefined) { + logWarn('There was not a Skipped Message Order Close. Closing the position the best possible.') + stopLoss = assistant.getMarketRate() + takeProfit = 0 + } else { + if (skippedSimulatorEngineMessage.id > openTradeSimulatorMessageId) { + logWarn('The Skipped Message Order Close was found and using it to set the SL and TP.') + stopLoss = skippedSimulatorEngineMessage.order.stop + takeProfit = skippedSimulatorEngineMessage.order.takeProfit + } else { + logWarn('One Message Order Close was found but is older than the Open Order. Closing the position the best possible.') + stopLoss = assistant.getMarketRate() + takeProfit = 0 + } + } + } else { + logError('Not managed condition.') } - } - - if (isMessageAnOrderUpdate) { - stopLoss = simulatorEngineMessage.order.stop - takeProfit = simulatorEngineMessage.order.takeProfit - } else if (isMessageAnOrderClose && isStopLoss) { - stopLoss = assistant.getMarketRate() - takeProfit = 0 - } else if (isMessageAnOrderClose && isTakeProfit) { - stopLoss = 0 - takeProfit = assistant.getMarketRate() - } - assistant.rememberThis('lastStopLoss', stopLoss) - assistant.rememberThis('lastTakeProfit', takeProfit) - } - - // Checking Stop Loss - let assetABalance = assistant.getAvailableBalance().assetA - let currentRate = assistant.getMarketRate() - - if (stopLoss > 0 && assetABalance > 0 && currentRate >= stopLoss) { - logInfo('Closing trade with Stop Loss.') - try { - let position = await createBuyPosition(currentRate) - - let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = receivedMessageId - simulatorExecutorMessage.order.id = position.id - simulatorExecutorMessage.order.rate = currentRate - simulatorExecutorMessage.order.stop = stopLoss - simulatorExecutorMessage.order.takeProfit = 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) - assistant.rememberThis('lastSimulatorEngineMessageId', 0) - assistant.rememberThis('lastStopLoss', 0) - assistant.rememberThis('lastTakeProfit', 0) - } catch (error) { - let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = receivedMessageId - simulatorExecutorMessage.from = MESSAGE_ENTITY.TradingAssistant - simulatorExecutorMessage.to = MESSAGE_ENTITY.SimulationExecutor - simulatorExecutorMessage.order.rate = position.rate - simulatorExecutorMessage.order.stop = stopLoss - simulatorExecutorMessage.order.takeProfit = 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 (takeProfit > 0 && assetABalance > 0 && currentRate <= takeProfit) { - logInfo('Closing trade with Take Profit.') - try { - let position = await createBuyPosition(currentRate, assetABalance) - - let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = receivedMessageId - simulatorExecutorMessage.order.id = position.id - simulatorExecutorMessage.order.rate = currentRate - simulatorExecutorMessage.order.stop = stopLoss - simulatorExecutorMessage.order.takeProfit = 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) - assistant.rememberThis('lastSimulatorEngineMessageId', 0) - assistant.rememberThis('lastStopLoss', 0) - assistant.rememberThis('lastTakeProfit', 0) - } catch (error) { - let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = receivedMessageId - simulatorExecutorMessage.from = MESSAGE_ENTITY.TradingAssistant - simulatorExecutorMessage.to = MESSAGE_ENTITY.SimulationExecutor - simulatorExecutorMessage.order.rate = currentRate - simulatorExecutorMessage.order.stop = stopLoss - simulatorExecutorMessage.order.takeProfit = takeProfit - simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Buy - simulatorExecutorMessage.order.size = assetABalance - simulatorExecutorMessage.order.status = ORDER_STATUS.Rejected - simulatorExecutorMessage.order.statusDetails = error.message - - let message = createMessageFromObject(simulatorExecutorMessage) - assistant.addExtraData(message) - } finally { - return - } - } - - if (simulatorEngineMessage !== undefined) { - if (autopilotResponse.autopilot || autopilotControl) { - await manageCloneInAutopilotOn(simulatorEngineMessage) + assistant.rememberThis('lastStopLoss', stopLoss) + assistant.rememberThis('lastTakeProfit', takeProfit) + await closeTrade(stopLoss, takeProfit, openTradeSimulatorMessageId) } else { - await manageCloneInAutopilotOff(simulatorEngineMessage) + if (isMessageAnOrder) { + await openOrMoveTrade(simulatorEngineMessage) + } else { + logInfo('Nothing to do, there is not an open order.') + } } } } catch (error) { - logError('Error on executorLogic: ' + error) - logError('Stack: ' + JSON.stringify(error.stack)) + logError('Error on executorLogic: ' + JSON.stringify(error)) throw error } } - async function manageCloneInAutopilotOn(simulatorEngineMessage) { - let assetBBalance = assistant.getAvailableBalance().assetB + async function closeTrade(stopLoss, takeProfit, openTradeSimulatorMessageId) { + let assetABalance = assistant.getAvailableBalance().assetA let currentRate = assistant.getMarketRate() - if (simulatorEngineMessage.messageType === MESSAGE_TYPE.Order - && simulatorEngineMessage.order.direction === ORDER_DIRECTION.Sell) { - - let lastSimulatorEngineMessageId = getValueFromPreviousExecution('lastSimulatorEngineMessageId') - if (lastSimulatorEngineMessageId >= simulatorEngineMessage.id) { - logInfo("manageCloneInAutopilotOn -> Order message was previously processed.") - return + // Check completion of current positions at the exchange + let closeTradeSimulatorMessageId = getValueFromPreviousExecution('closeTradeSimulatorMessageId') + let positions = assistant.getPositions() + let isPositionExecuted = false + for (let i = 0; i < positions.length; i++) { + const position = positions[i]; + if (position.id === closeTradeSimulatorMessageId && position.status === 'executed') { + isPositionExecuted = true + break } + } + let wasExecuted = closeTradeSimulatorMessageId > 0 && assistant.getAvailableBalance().assetA === 0 && assistant.getAvailableBalance().assetB > 0 + if (isPositionExecuted || wasExecuted) { + assistant.rememberThis('openTradeSimulatorMessageId', 0) + assistant.rememberThis('closeTradeSimulatorMessageId', 0) + assistant.rememberThis('openTradePositionId', 0) + assistant.rememberThis('lastStopLoss', 0) + assistant.rememberThis('lastTakeProfit', 0) + + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.id = openTradeSimulatorMessageId + simulatorExecutorMessage.order.id = positions.length > 0 ? positions[0].id : 0 + simulatorExecutorMessage.order.rate = currentRate + simulatorExecutorMessage.order.stop = stopLoss + simulatorExecutorMessage.order.takeProfit = 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) + } + + // Checking Stop Loss + if (stopLoss > 0 && closeTradeSimulatorMessageId > 0) { + logInfo('Closing trade with Stop Loss.') try { - let position = await createSellPosition(currentRate, assetBBalance) + let position = await createBuyPosition(currentRate) + assistant.rememberThis('closeTradePositionId', position.id) + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = simulatorEngineMessage.id + simulatorExecutorMessage.id = openTradeSimulatorMessageId 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 + simulatorExecutorMessage.order.stop = stopLoss + simulatorExecutorMessage.order.takeProfit = 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) - assistant.rememberThis('lastSimulatorEngineMessageId', simulatorEngineMessage.id) - assistant.rememberThis('lastStopLoss', simulatorEngineMessage.order.stop) - assistant.rememberThis('lastTakeProfit', simulatorEngineMessage.order.takeProfit) + } catch (error) { let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = receivedMessageId + simulatorExecutorMessage.id = openTradeSimulatorMessageId 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.stop = stopLoss + simulatorExecutorMessage.order.takeProfit = 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 (takeProfit > 0 && closeTradeSimulatorMessageId > 0) { + logInfo('Closing trade with Take Profit.') + try { + let position = await createBuyPosition(currentRate, assetABalance) + assistant.rememberThis('closeTradePositionId', position.id) + + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.id = openTradeSimulatorMessageId + simulatorExecutorMessage.order.id = position.id + simulatorExecutorMessage.order.rate = currentRate + simulatorExecutorMessage.order.stop = stopLoss + simulatorExecutorMessage.order.takeProfit = 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.id = openTradeSimulatorMessageId + simulatorExecutorMessage.from = MESSAGE_ENTITY.TradingAssistant + simulatorExecutorMessage.to = MESSAGE_ENTITY.SimulationExecutor + simulatorExecutorMessage.order.rate = currentRate + simulatorExecutorMessage.order.stop = stopLoss + simulatorExecutorMessage.order.takeProfit = takeProfit + simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Buy + simulatorExecutorMessage.order.size = assetABalance simulatorExecutorMessage.order.status = ORDER_STATUS.Rejected simulatorExecutorMessage.order.statusDetails = error.message let message = createMessageFromObject(simulatorExecutorMessage) assistant.addExtraData(message) + } finally { + return } + } + + } + + async function openOrMoveTrade(simulatorEngineMessage) { + autopilotControl = true // Force autopilot on for now + if (!autopilotControl) { + autopilotControl = await getAutopilot() + } + if (autopilotControl.autopilot || autopilotControl) { + await openTradeAutopilotOn(simulatorEngineMessage) } else { - logInfo("manageCloneInAutopilotOn -> Nothing to do, there isn't a buy or sell opportunity.") + await openTradeAutopilotOff(simulatorEngineMessage) } } - async function manageCloneInAutopilotOff(simulatorEngineMessage) { + async function openTradeAutopilotOn(simulatorEngineMessage) { + let assetBBalance = assistant.getAvailableBalance().assetB + let currentRate = assistant.getMarketRate() + + try { + // Create a new position or move existing position to complete the order execution + let position = await createSellPosition(currentRate, assetBBalance) + assistant.rememberThis('openTradePositionId', position.id) + assistant.rememberThis('openTradeSimulatorMessageId', simulatorEngineMessage.id) + assistant.rememberThis('lastStopLoss', simulatorEngineMessage.order.stop) + assistant.rememberThis('lastTakeProfit', simulatorEngineMessage.order.takeProfit) + + let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() + simulatorExecutorMessage.id = simulatorEngineMessage.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.id = simulatorEngineMessage.id + 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 + simulatorExecutorMessage.order.statusDetails = error.message + + let message = createMessageFromObject(simulatorExecutorMessage) + assistant.addExtraData(message) + } + } + + async function openTradeAutopilotOff(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 @@ -318,7 +364,7 @@ exports.newUserBot = function newUserBot(bot, logger) { if (acceptedSignals !== undefined && acceptedSignals.length > 0) { for (let acceptedSignal of acceptedSignals) { try { - logInfo('manageCloneInAutopilotOff -> Executing manual authorized signal.') + logInfo('openTradeAutopilotOff -> Executing manual authorized signal.') // 1) Save audit containing what we received from the cockpit let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() @@ -332,6 +378,10 @@ exports.newUserBot = function newUserBot(bot, logger) { // 2) Execute the order in the market as received let position = await createSellPosition(acceptedSignal.orderData.rate, acceptedSignal.orderData.size) + assistant.rememberThis('openTradePositionId', position.id) + assistant.rememberThis('openTradeSimulatorMessageId', simulatorEngineMessage.id) + assistant.rememberThis('lastStopLoss', simulatorEngineMessage.order.stop) + assistant.rememberThis('lastTakeProfit', simulatorEngineMessage.order.takeProfit) // 3) Save the audit of the order placed simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() @@ -386,7 +436,7 @@ exports.newUserBot = function newUserBot(bot, logger) { let inProcessSignals = await getSignalsByCloneId(ORDER_STATUS.Placed) if (inProcessSignals !== undefined && inProcessSignals.length > 0) { for (let inProcessSignal of inProcessSignals) { - logInfo('manageCloneInAutopilotOff -> Checking in process signal.') + logInfo('openTradeAutopilotOff -> Checking in process signal.') // 1) Check order status on the market let position = checkSignalCompletion() if (position || position === undefined) { @@ -423,7 +473,7 @@ exports.newUserBot = function newUserBot(bot, logger) { 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.') + logInfo('openTradeAutopilotOff -> Signal waiting for approval found, updating it with latest market data.') let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() simulatorExecutorMessage.to = MESSAGE_ENTITY.TradingCokpit @@ -473,7 +523,7 @@ exports.newUserBot = function newUserBot(bot, logger) { // 3) Create the signal on the cockpit await createSignal(simulatorExecutorMessage) } else { - logWarn("manageCloneInAutopilotOff -> Nothing to do, there isn't a buy or sell opportunity.") + logWarn("openTradeAutopilotOff -> Nothing to do, there isn't a buy or sell opportunity.") } } @@ -607,6 +657,11 @@ exports.newUserBot = function newUserBot(bot, logger) { function getSimulatorEngineMessageFromFile(indicatorFileContent) { try { let lastIndexIndicatorFile = indicatorFileContent.length - 1 + + if (indicatorFileContent[lastIndexIndicatorFile] === undefined) { + logWarn('getSimulatorEngineMessageFromFile -> Recived file does not contain any record yet.') + return + } let lastAvailableDateTime = indicatorFileContent[lastIndexIndicatorFile][0] if (bot.processDatetime.valueOf() <= lastAvailableDateTime || !isExecutionToday()) { @@ -618,12 +673,14 @@ exports.newUserBot = function newUserBot(bot, logger) { 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 10 minutes top - let maxTolerance = 10 * 60 * 1000 + // Running live we will process last available Indicator Message only if it's delayed 30 minutes top + let maxTolerance = 30 * 60 * 1000 if (bot.processDatetime.valueOf() <= (lastAvailableDateTime + maxTolerance)) { + let minutesDelayed = (bot.processDatetime.valueOf() - lastAvailableDateTime) / 60 / 1000 + logWarn('getSimulatorEngineMessageFromFile -> Last available indicator is delayed: ' + minutesDelayed.toFixed(2) + ' minutes.') return getMessage(indicatorFileContent[lastIndexIndicatorFile][2]) } else { - logWarn('getSimulatorEngineMessageFromFile -> Last available indicator older than 10 minutes.') + logWarn('getSimulatorEngineMessageFromFile -> Last available indicator older than 30 minutes: ' + JSON.stringify(indicatorFileContent[lastIndexIndicatorFile])) } } } catch (error) { diff --git a/src/utils/getDevTeamHost.js b/src/utils/getDevTeamHost.js index c244cf8e1..2066fc4fb 100644 --- a/src/utils/getDevTeamHost.js +++ b/src/utils/getDevTeamHost.js @@ -8,10 +8,8 @@ export default async function getDevTeamHost(devTeamName, accessKey, ownerKey) { } else { host = teamCache.get(devTeamName + '.ownerKey') } - if (host) { - if (host.accessKey === accessKey || host.ownerKey === ownerKey) { - return host - } + if (host && (host.accessKey === accessKey || host.ownerKey === ownerKey)) { + return host } else { let ecosystem = await Ecosystem.find() From a816c9d96b2c1b81df2873087b6fe8c51a4c6b6e Mon Sep 17 00:00:00 2001 From: Matias Date: Tue, 16 Jul 2019 10:14:53 -0300 Subject: [PATCH 32/34] Adding new templates --- src/storage/templates/Simulator/Commons.js | 7 +++++++ src/storage/templates/TradingBot/User.Bot.js | 13 ++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/storage/templates/Simulator/Commons.js b/src/storage/templates/Simulator/Commons.js index cb6a0f0e8..7d9509461 100644 --- a/src/storage/templates/Simulator/Commons.js +++ b/src/storage/templates/Simulator/Commons.js @@ -1487,9 +1487,15 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { if (type === '"Take Position"') { messageType = MESSAGE_TYPE.Order; orderId++; + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> addRecord -> Taking Position Now. "); } + } else { if (type === '"Close@TakeProfit"' || type === '"Close@StopLoss"') { messageType = MESSAGE_TYPE.OrderClose; + + if (FULL_LOG === true) { logger.write(MODULE_NAME, "[INFO] runSimulation -> addRecord -> Closing Position Now. "); } + } else { messageType = MESSAGE_TYPE.OrderUpdate; } @@ -1550,6 +1556,7 @@ exports.newCommons = function newCommons(bot, logger, UTILITIES) { 0, "", 0, + "", "") } diff --git a/src/storage/templates/TradingBot/User.Bot.js b/src/storage/templates/TradingBot/User.Bot.js index fb8ba4b08..7e3c30019 100644 --- a/src/storage/templates/TradingBot/User.Bot.js +++ b/src/storage/templates/TradingBot/User.Bot.js @@ -124,9 +124,9 @@ exports.newUserBot = function newUserBot(bot, logger) { let existsAnOpenTrade = (openTradeSimulatorMessageId > 0) if (existsAnOpenTrade) { - if (isMessageAnOrder && simulatorEngineMessage.id === openTradeSimulatorMessageId) { - logInfo('We just put an order with the latest simualtor record available, checking SL and TP.') - } else if (isMessageAnOrderUpdate) { + let isOrderMessage = isMessageAnOrder && simulatorEngineMessage.id === openTradeSimulatorMessageId + if (isOrderMessage || isMessageAnOrderUpdate) { + logInfo('Moving the position because it is the same message that caused the open trade or because it is an update.' ) await openOrMoveTrade(simulatorEngineMessage) } else if (isMessageAnOrderClose) { assistant.rememberThis('closeTradeSimulatorMessageId', simulatorEngineMessage.id) @@ -170,6 +170,7 @@ exports.newUserBot = function newUserBot(bot, logger) { logInfo('Nothing to do, there is not an open order.') } } + assistant.addExtraData(simulatorEngineMessage) // Adding the record used for the execution } } catch (error) { logError('Error on executorLogic: ' + JSON.stringify(error)) @@ -675,12 +676,14 @@ exports.newUserBot = function newUserBot(bot, logger) { } else { // Running live we will process last available Indicator Message only if it's delayed 30 minutes top let maxTolerance = 30 * 60 * 1000 + let lastAvailableDateTimeEnd = indicatorFileContent[lastIndexIndicatorFile][1] + let minutesDelayed = (bot.processDatetime.valueOf() - lastAvailableDateTimeEnd) / 60 / 1000 + logWarn('getSimulatorEngineMessageFromFile -> Last available indicator is delayed: ' + minutesDelayed.toFixed(2) + ' minutes.') if (bot.processDatetime.valueOf() <= (lastAvailableDateTime + maxTolerance)) { - let minutesDelayed = (bot.processDatetime.valueOf() - lastAvailableDateTime) / 60 / 1000 - logWarn('getSimulatorEngineMessageFromFile -> Last available indicator is delayed: ' + minutesDelayed.toFixed(2) + ' minutes.') return getMessage(indicatorFileContent[lastIndexIndicatorFile][2]) } else { logWarn('getSimulatorEngineMessageFromFile -> Last available indicator older than 30 minutes: ' + JSON.stringify(indicatorFileContent[lastIndexIndicatorFile])) + assistant.addExtraData(indicatorFileContent[lastIndexIndicatorFile]) // Adding the record used for the execution } } } catch (error) { From 03ebb720f12593c635aac4186b74e7f7d0a537b8 Mon Sep 17 00:00:00 2001 From: Matias Date: Wed, 17 Jul 2019 08:33:33 -0300 Subject: [PATCH 33/34] Fixing remove bot --- src/schema/mutations/AddBot.js | 1 + src/schema/mutations/AddTeam.js | 13 ++++++++++++- src/schema/mutations/DeleteTeam.js | 1 + src/schema/mutations/RemoveBot.js | 1 + 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/schema/mutations/AddBot.js b/src/schema/mutations/AddBot.js index a3fba7964..86d972b68 100644 --- a/src/schema/mutations/AddBot.js +++ b/src/schema/mutations/AddBot.js @@ -36,6 +36,7 @@ const resolve = async (parent, { bot }, context) => { products: [auxProduct] } auxTeam.bots.push(newBotClone) + userEcosystem.markModified("devTeams") break } } diff --git a/src/schema/mutations/AddTeam.js b/src/schema/mutations/AddTeam.js index 995f87c6e..73c7c6918 100644 --- a/src/schema/mutations/AddTeam.js +++ b/src/schema/mutations/AddTeam.js @@ -39,7 +39,17 @@ const resolve = async (parent, { team }, context) => { // Teams will be created on a local minio container let botsConfigurations = [] - await createContainer(team.codeName) + try { + await createContainer(team.codeName) + } catch (err) { + if (err.message !== 'Your previous request to create the named bucket succeeded and you already own it.') { + logger.error('addTeam -> Error: %s', err.stack) + throw err + } else { + logger.warn('addTeam -> %s', err.message) + } + } + 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) @@ -59,6 +69,7 @@ const resolve = async (parent, { team }, context) => { } userEcosystem.devTeams.push(team) + userEcosystem.markModified("devTeams") await userEcosystem.save() return team } catch (err) { diff --git a/src/schema/mutations/DeleteTeam.js b/src/schema/mutations/DeleteTeam.js index f604bdac6..6c1fbc0f8 100644 --- a/src/schema/mutations/DeleteTeam.js +++ b/src/schema/mutations/DeleteTeam.js @@ -32,6 +32,7 @@ const resolve = async (parent, { team }, context) => { teamFound = true await deleteContainer(team.codeName) userEcosystem.devTeams.splice(i, 1) + userEcosystem.markModified("devTeams") await userEcosystem.save() break } diff --git a/src/schema/mutations/RemoveBot.js b/src/schema/mutations/RemoveBot.js index 68e093e27..83249f703 100644 --- a/src/schema/mutations/RemoveBot.js +++ b/src/schema/mutations/RemoveBot.js @@ -29,6 +29,7 @@ const resolve = async (parent, { bot }, context) => { if (auxProduct.codeName === bot.productCodeName) { removedBotClone = auxBot auxTeam.bots.splice(j, 1) + userEcosystem.markModified("devTeams") break } } From 9e939c5b02e41084359c907f703348f9615398a5 Mon Sep 17 00:00:00 2001 From: Matias Date: Sat, 20 Jul 2019 07:30:15 -0300 Subject: [PATCH 34/34] USDT base asset --- src/storage/templates/TradingBot/User.Bot.js | 121 +++++++++---------- 1 file changed, 57 insertions(+), 64 deletions(-) diff --git a/src/storage/templates/TradingBot/User.Bot.js b/src/storage/templates/TradingBot/User.Bot.js index 27fcadbb6..ac5a643ea 100644 --- a/src/storage/templates/TradingBot/User.Bot.js +++ b/src/storage/templates/TradingBot/User.Bot.js @@ -126,7 +126,7 @@ exports.newUserBot = function newUserBot(bot, logger) { if (existsAnOpenTrade) { let isOrderMessage = isMessageAnOrder && simulatorEngineMessage.id === openTradeSimulatorMessageId if (isOrderMessage || isMessageAnOrderUpdate) { - logInfo('Moving the position because it is the same message that caused the open trade or because it is an update.' ) + logInfo('Moving the position because it is the same message that caused the open trade or because it is an update.') await openOrMoveTrade(simulatorEngineMessage) } else if (isMessageAnOrderClose) { assistant.rememberThis('closeTradeSimulatorMessageId', simulatorEngineMessage.id) @@ -179,11 +179,21 @@ exports.newUserBot = function newUserBot(bot, logger) { } async function closeTrade(stopLoss, takeProfit, openTradeSimulatorMessageId) { - let assetABalance = assistant.getAvailableBalance().assetA let currentRate = assistant.getMarketRate() + let wasExecuted = false + let tradeSize = assistant.getAvailableBalance().assetA + let tradeDirection = ORDER_DIRECTION.Buy + let closeTradeSimulatorMessageId = getValueFromPreviousExecution('closeTradeSimulatorMessageId') + + if (process.env.BASE_ASSET === 'assetA') { + wasExecuted = closeTradeSimulatorMessageId > 0 && assistant.getAvailableBalance().assetB === 0 && assistant.getAvailableBalance().assetA > 0 + tradeSize = assistant.getAvailableBalance().assetB + tradeDirection = ORDER_DIRECTION.Sell + } else { + wasExecuted = closeTradeSimulatorMessageId > 0 && assistant.getAvailableBalance().assetA === 0 && assistant.getAvailableBalance().assetB > 0 + } // Check completion of current positions at the exchange - let closeTradeSimulatorMessageId = getValueFromPreviousExecution('closeTradeSimulatorMessageId') let positions = assistant.getPositions() let isPositionExecuted = false for (let i = 0; i < positions.length; i++) { @@ -193,7 +203,6 @@ exports.newUserBot = function newUserBot(bot, logger) { break } } - let wasExecuted = closeTradeSimulatorMessageId > 0 && assistant.getAvailableBalance().assetA === 0 && assistant.getAvailableBalance().assetB > 0 if (isPositionExecuted || wasExecuted) { assistant.rememberThis('openTradeSimulatorMessageId', 0) @@ -208,8 +217,8 @@ exports.newUserBot = function newUserBot(bot, logger) { simulatorExecutorMessage.order.rate = currentRate simulatorExecutorMessage.order.stop = stopLoss simulatorExecutorMessage.order.takeProfit = takeProfit - simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Buy - simulatorExecutorMessage.order.size = assetABalance + simulatorExecutorMessage.order.direction = tradeDirection + simulatorExecutorMessage.order.size = tradeSize simulatorExecutorMessage.order.status = ORDER_STATUS.Placed simulatorExecutorMessage.order.exitOutcome = ORDER_EXIT_OUTCOME.StopLoss @@ -217,51 +226,24 @@ exports.newUserBot = function newUserBot(bot, logger) { assistant.addExtraData(message) } - // Checking Stop Loss - if (stopLoss > 0 && closeTradeSimulatorMessageId > 0) { - logInfo('Closing trade with Stop Loss.') - try { - let position = await createBuyPosition(currentRate) - assistant.rememberThis('closeTradePositionId', position.id) - - let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() - simulatorExecutorMessage.id = openTradeSimulatorMessageId - simulatorExecutorMessage.order.id = position.id - simulatorExecutorMessage.order.rate = currentRate - simulatorExecutorMessage.order.stop = stopLoss - simulatorExecutorMessage.order.takeProfit = 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.id = openTradeSimulatorMessageId - simulatorExecutorMessage.from = MESSAGE_ENTITY.TradingAssistant - simulatorExecutorMessage.to = MESSAGE_ENTITY.SimulationExecutor - simulatorExecutorMessage.order.rate = currentRate - simulatorExecutorMessage.order.stop = stopLoss - simulatorExecutorMessage.order.takeProfit = 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 Stop Loss & Take Profit + if ((takeProfit > 0 || stopLoss > 0) && closeTradeSimulatorMessageId > 0) { + let exitOutcome + if (takeProfit > 0) { + logInfo('Closing trade with Take Profit.') + exitOutcome = ORDER_EXIT_OUTCOME.TakeProfit + } else { + logInfo('Closing trade with Stop Loss.') + exitOutcome = ORDER_EXIT_OUTCOME.StopLoss } - } - // Checking Take Profits - if (takeProfit > 0 && closeTradeSimulatorMessageId > 0) { - logInfo('Closing trade with Take Profit.') try { - let position = await createBuyPosition(currentRate, assetABalance) + let position + if (process.env.BASE_ASSET === 'assetA') { + position = await createSellPosition(currentRate) + } else { + position = await createBuyPosition(currentRate) + } assistant.rememberThis('closeTradePositionId', position.id) let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() @@ -270,13 +252,14 @@ exports.newUserBot = function newUserBot(bot, logger) { simulatorExecutorMessage.order.rate = currentRate simulatorExecutorMessage.order.stop = stopLoss simulatorExecutorMessage.order.takeProfit = takeProfit - simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Buy - simulatorExecutorMessage.order.size = assetABalance + simulatorExecutorMessage.order.direction = tradeDirection + simulatorExecutorMessage.order.size = tradeSize simulatorExecutorMessage.order.status = ORDER_STATUS.Placed - simulatorExecutorMessage.order.exitOutcome = ORDER_EXIT_OUTCOME.TakeProfit + simulatorExecutorMessage.order.exitOutcome = exitOutcome let message = createMessageFromObject(simulatorExecutorMessage) assistant.addExtraData(message) + } catch (error) { let simulatorExecutorMessage = buildBasicSimulatorExecutorMessage() simulatorExecutorMessage.id = openTradeSimulatorMessageId @@ -285,9 +268,10 @@ exports.newUserBot = function newUserBot(bot, logger) { simulatorExecutorMessage.order.rate = currentRate simulatorExecutorMessage.order.stop = stopLoss simulatorExecutorMessage.order.takeProfit = takeProfit - simulatorExecutorMessage.order.direction = ORDER_DIRECTION.Buy - simulatorExecutorMessage.order.size = assetABalance + simulatorExecutorMessage.order.direction = tradeDirection + simulatorExecutorMessage.order.size = tradeSize simulatorExecutorMessage.order.status = ORDER_STATUS.Rejected + simulatorExecutorMessage.order.exitOutcome = exitOutcome simulatorExecutorMessage.order.statusDetails = error.message let message = createMessageFromObject(simulatorExecutorMessage) @@ -296,7 +280,6 @@ exports.newUserBot = function newUserBot(bot, logger) { return } } - } async function openOrMoveTrade(simulatorEngineMessage) { @@ -312,12 +295,24 @@ exports.newUserBot = function newUserBot(bot, logger) { } async function openTradeAutopilotOn(simulatorEngineMessage) { - let assetBBalance = assistant.getAvailableBalance().assetB let currentRate = assistant.getMarketRate() + let tradeSize = assistant.getAvailableBalance().assetB + let tradeDirection = ORDER_DIRECTION.Sell + + if (process.env.BASE_ASSET === 'assetA') { + tradeSize = assistant.getAvailableBalance().assetA + tradeDirection = ORDER_DIRECTION.Buy + } try { // Create a new position or move existing position to complete the order execution - let position = await createSellPosition(currentRate, assetBBalance) + let position + if (process.env.BASE_ASSET === 'assetA') { + position = await createBuyPosition(currentRate) + } else { + position = await createSellPosition(currentRate) + } + assistant.rememberThis('openTradePositionId', position.id) assistant.rememberThis('openTradeSimulatorMessageId', simulatorEngineMessage.id) assistant.rememberThis('lastStopLoss', simulatorEngineMessage.order.stop) @@ -329,8 +324,8 @@ exports.newUserBot = function newUserBot(bot, logger) { 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.direction = tradeDirection + simulatorExecutorMessage.order.size = tradeSize let message = createMessageFromObject(simulatorExecutorMessage) assistant.addExtraData(message) @@ -343,8 +338,8 @@ exports.newUserBot = function newUserBot(bot, logger) { 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.direction = tradeDirection + simulatorExecutorMessage.order.size = tradeSize simulatorExecutorMessage.order.status = ORDER_STATUS.Rejected simulatorExecutorMessage.order.statusDetails = error.message @@ -564,8 +559,8 @@ exports.newUserBot = function newUserBot(bot, logger) { }) let putPosition = util.promisify(assistant.putPosition) - await putPosition('buy', currentRate, amountToBuy, amountB) - return positions[0] + let position = await putPosition('buy', currentRate, amountToBuy, amountB) + return position } else { logInfo('createBuyPosition -> Not enough available balance to buy.') throw new Error('There is not enough available balance to buy.') @@ -680,8 +675,6 @@ exports.newUserBot = function newUserBot(bot, logger) { let minutesDelayed = (bot.processDatetime.valueOf() - lastAvailableDateTimeEnd) / 60 / 1000 logWarn('getSimulatorEngineMessageFromFile -> Last available indicator is delayed: ' + minutesDelayed.toFixed(2) + ' minutes.') if (bot.processDatetime.valueOf() <= (lastAvailableDateTime + maxTolerance)) { - let minutesDelayed = (bot.processDatetime.valueOf() - lastAvailableDateTime) / 60 / 1000 - logWarn('getSimulatorEngineMessageFromFile -> Last available indicator is delayed: ' + minutesDelayed.toFixed(2) + ' minutes.') return getMessage(indicatorFileContent[lastIndexIndicatorFile][2]) } else { logWarn('getSimulatorEngineMessageFromFile -> Last available indicator older than 30 minutes: ' + JSON.stringify(indicatorFileContent[lastIndexIndicatorFile]))