Esempio n. 1
0
    def collection(cls, stale_ok=False, majority=False):
        """Return collection instance."""

        kwargs = {}
        if stale_ok:
            kwargs["read_preference"] = ReadPreference.SECONDARY_PREFERRED
        elif majority:
            kwargs["write_concern"] = cls.__write_majority__

        return aiomotor.AsyncIOMotorCollection(cls.database(),
                                               cls.__collection__, **kwargs)
Esempio n. 2
0
    async def upsert_historical_data(self, data):
        """
        Upsert historical data

        :param dict data:
        :return:
        """
        collection = amotor.AsyncIOMotorCollection(self.db, HISTORICAL_DATA_COLLECTION)
        data_filter = {"symbol": data.get("symbol"), "datetime": data.get("datetime")}
        r = await collection.update_one(filter=data_filter, update={"$set": data}, upsert=True)
        logger.info("ASYNC | {0} data for {1}".format("added" if bool(r.upserted_id) else "updated", data.get("symbol")))
Esempio n. 3
0
    async def get_last_n_historical_data(self, symbol, days=1):
        """
        Returns the data related to a symbol for the last number of `n` trading days

        :param str symbol:
        :param int days:
        :return: list of dict
        """

        logger.info("ASYNC | Getting last {0} data for {1}".format(days, symbol))
        collection = amotor.AsyncIOMotorCollection(self.db, HISTORICAL_DATA_COLLECTION)
        return await collection.find({"symbol": symbol}).sort("datetime", -1).limit(days).to_list(days)
Esempio n. 4
0
    async def add_historical_data(self, list_of_data):
        """
        Add historical data.

        :param list list_of_data: a list of dictionaries containing symbol trade info.
        """

        try:
            collection = amotor.AsyncIOMotorCollection(self.db, HISTORICAL_DATA_COLLECTION)
            await collection.insert_many(list_of_data)
            logger.info(
                "ASYNC | {0} data inserted into collection '{1}'".format(len(list_of_data),
                                                                         SYMBOL_INFO_COLLECTION))
        except Exception as e:
            logger.error("ASYNC | " + str(e))
Esempio n. 5
0
    async def upsert_symbol_info(self, symbol_info):
        """
        Upsert symbol info into the db

        :param dict symbol_info: e.g. {"symbol": "واحیا", "ISIN": "IRO7VHYP0001"}
        :return:
        """
        try:
            collection = amotor.AsyncIOMotorCollection(self.db, SYMBOL_INFO_COLLECTION)
            symbol = symbol_info.get("symbol")
            if not symbol:
                return
            r = await collection.update_one({"symbol": symbol}, {"$set": symbol_info}, upsert=True)
            logger.info("ASYNC | {0} info for {1}: {2}".format(
                "added" if bool(r.upserted_id) else "updated", symbol, symbol_info))
        except Exception as e:
            logger.error(str(e))
Esempio n. 6
0
File: db.py Progetto: JoshOY/vj4
 def __new__(cls, name):
     if name not in cls._instances:
         cls._instances[name] = motor_asyncio.AsyncIOMotorCollection(
             Database(), name)
     return cls._instances[name]