This class handles all database related tasks, such as connecting to the database, inserting, updating and deleting records.

Note that in this class, the terms and variables 'trade id', 'trade ref' are used interchangeably. They are the same thing, which is a stringified mongoDB ObjectId.

Active trades reside the 'trade' collection, and referenced by its _id field, while historical trades reside in the 'trade_historical' collection, and referenced by its trade_ref field.

Once a trade is recorded as a historical trade, the original active trade id is used as the trade ref in the historical trade collection.

Hierarchy

  • default

Constructors

  • Creates a new instance of the Database class

    Parameters

    • systemConfig: {}

      Contains the concatinated configuration from the config.yml and strategy.yml files

      Returns default

    Properties

    collectionPrefix: string

    Contains the database collection prefix

    db: {} = {}

    Contains the database connections

    Type declaration

      mongoClient: any

      Contains the MongoDB client

      systemConfig: any

      Contains the concatinated configuration from the config.yml and strategy.yml files

      utilities: default

      Contains the utilities class

      Methods

      • Adds a 'global' log entry. These logs are not tied to a trade. For trade logs, use addTradeLog() instead.

        Parameters

        • title: string

          The title of the log

        • type: string

          The type of the log. This type isn't 'typed' so you can use whatever you want.

        • data: any = {}

          The data to store with the log

        Returns Promise<void>

      • Adds order details to a trade and stores them in the database

        Parameters

        • tradeRef: string

          The trade reference to get the trade for, this is a stringified mongo object id

        • orderId: string

          The order id as it is returned by the exchange. Always stored as a string.

        • type: OrderDetailsType

          The type of the order (OrderDetailsType)

        • status: OrderDetailsStatus

          The status of the order (OrderDetailsStatus)

        • exchangeDetails: {}

          The details returned by the exchange

          Returns Promise<void>

        • Adds a log entry for a trade. For log entries that are not tied to a trade, use addLog() instead.

          Parameters

          • tradeRef: string

            The trade reference to get the trade for, this is a stringified mongo object id

          • title: string

            The title of the log

          • type: TradeLogType

            The type of the log (TradeLogType)

          • data: any = {}

            The data to store with the log

          Returns Promise<void>

        • Connects to the database

          Parameters

          • showOutputMessages: boolean

            true / false, whether to show output messages or not

          Returns Promise<void>

        • Counts all active trades.

          Returns Promise<any>

        • Counts trades by a custom query, either active or historical.

          Parameters

          • query: {}

            The query to use for counting trades

            • isHistorical: boolean

              If the trades are historical or not

            Returns Promise<any>

          • Creates a new trade. This is used to track the state of a trade, along with other trade related data.

            Parameters

            • exchangeId: string

              The exchange id

            • symbol: string

              The symbol

            • interval: string

              The interval

            • state: string = "start"

              The state

            • direction: string = "none"

              The direction

            Returns Promise<any>

          • Deletes all trades from the database, both active and historical.

            Returns Promise<void>

          • Deletes a shortlist item.

            Parameters

            • id: string

              The shortlist id, this is the name of the shortlist

            • symbol: string

              The symbol of the coin

            • query: {} = {}

              The query

              • dbName: string = "default"

                The database name

              Returns Promise<void>

            • Deletes a trade from the database. This can delete both active and historical trades.

              Parameters

              • id: string

                The trade id

              • isHistorical: boolean = false

                If the trade is historical or not

              Returns Promise<void>

            • Checks if a trade exists by it's signature.

              Parameters

              • tradeSignature: string

                The trade signature

              Returns Promise<boolean>

            • Ensures that a connection to the specified database is established

              Parameters

              • dbName: string

                The name of the database to connect to

              Returns Promise<void>

            • Queries for all active and historical trades, and concatenates their logs.

              Returns Promise<any>

            • Counts the number of trades in the database with the given direction.

              Parameters

              • direction: any = null

                The direction of the trades to count

              Returns Promise<any>

              A status object summarizing the allocation statistics

            • This method allows you to get any data from any collection. It will return an array of results that match the query from the collection specified.

              You can override the database name if you want to query a different database that hold trade information from other bots.

              Parameters

              • collection: string

                The collection name

              • query: {} = {}

                The query

                • dbName: string = "default"

                  The database name

                Returns Promise<any>

              • Fetches data from collections in the database which are organized by keys

                Parameters

                • key: string

                  The key of the data to get

                • dbName: string = "default"

                Returns Promise<any>

              • Parameters

                • collection: string
                • query: {} = {}
                  • dbName: string = "default"

                  Returns Promise<any>

                • Gets the order details for an order

                  Parameters

                  • orderId: string

                    The order id as it was returned by the exchange. Always stored as a string.

                  Returns Promise<any>

                • Fetches all orders for a trade reference.

                  Parameters

                  • tradeRef: string

                    The trade reference to get the trade for, this is a stringified mongo object id

                  Returns Promise<any>

                • Fetches a shortlist by id.

                  You can override the database name if you want shortlists from other bots.

                  Parameters

                  • id: string

                    The shortlist id

                  • query: {} = {}

                    The query object

                    • sort: {} = {}

                      The sort object

                      • dbName: string = "default"

                        The database name

                      Returns Promise<any>

                    • Gets a trade by it's trade reference. This may be either an active or historical trade.

                      Parameters

                      • tradeRef: any
                      • isHistorical: boolean = false

                      Returns Promise<any>

                      The trade object

                    • Returns a trade by it's signature.

                      The Utilities.getTradeSignature() method can be used to generate a signature.

                      Parameters

                      • signature: string

                      Returns Promise<any>

                    • Parameters

                      • tradeRef: string

                      Returns Promise<any>

                    • Fetches multiple trades from the database, using a custom query.

                      This is a standard MongoDB query object. An example query would be:

                      { "exchangeId": "binance", "symbol": "BTC/USDT" }

                      Parameters

                      • query: {} = {}

                        The query

                        • page: number = 0

                          The page number

                        • results: number = 0

                          The number of results per page

                        • isHistorical: boolean = false

                          If the trades are historical or not

                        • hoursBack: number = 0

                          The number of hours back to fetch trades from

                        • sortBy: string = "stateAt"

                          The field to sort by

                        Returns Promise<any>

                      • Dublicates the trade and adds it to the historical collection along with the original trade id as tradeRef.

                        Parameters

                        • trade: any

                          Trade object

                        Returns Promise<void>

                      • This method allows you to set any data in any collection, in any database. It will look to existing data by the query, and if it exists, it will update it. If it doesn't exist, it will insert it.

                        Parameters

                        • collection: string

                          The collection name

                        • query: {}

                          The query

                          • data: {}

                            The data to set

                            • dbName: string = "default"

                              The database name

                            Returns Promise<any>

                          • Sets data in collections in the database which are organized by keys. This can be any data which isn't related to a trade.

                            Parameters

                            • key: string

                              The key of the data to set

                            • value: any

                              The value of the data to set

                            • dbName: string = "default"

                            Returns Promise<void>

                          • Sets a shortlist item.

                            Parameters

                            • id: string

                              The shortlist id, this is the name of the shortlist

                            • symbol: string

                              The symbol of the coin

                            • data: {} = {}

                              The data to set

                              • dbName: string = "default"

                                The database name

                              Returns Promise<any>

                            • Updates the order details for a trade

                              Parameters

                              • orderId: string

                                The order id as it was returned by the exchange. Always stored as a string.

                              • status: OrderDetailsStatus

                                The status of the order (OrderDetailsStatus)

                              • exchangeDetails: {}

                                The details returned by the exchange

                                Returns Promise<void>

                              • This updates an active trade with any data in the data object.

                                Parameters

                                • id: any

                                  The trade id

                                • data: {}

                                  The data to update

                                  Returns Promise<void>