Exemplo n.º 1
0
 def __init__(self, platform):
     """ Initialize object.
     """
     self._db_name = platform  # Db name. => MongoDB db name.
     self._collection = "kline"  # Table name. => MongoDB collection name.
     self._platform = platform
     self._k_to_c = {}   # Kline types cursor for db. e.g. {"BTC/USD": "kline_btc_usd"}
     self._db = MongoDBBase(self._db_name, self._collection)  # db instance
Exemplo n.º 2
0
 def __init__(self):
     """Initialize object."""
     self._db = "order"  # db name
     self._collection = "order"  # collection name
     self._db = MongoDBBase(self._db, self._collection)  # db instance
Exemplo n.º 3
0
 def __init__(self):
     """Initialize object."""
     self._db = MongoDBBase(self.DB, self.COLLECTION)  # db instance
Exemplo n.º 4
0
class KLineData:
    """ Save or fetch kline data via MongoDB.

    Data struct:
        {
            "o": open, # Open price
            "h": high, # Highest price
            "l": low, # Lowest price
            "c": close, # Close price
            "t": timestamp # Millisecond timestamp
        }

    Attributes:
        platform: Exchange platform name.
    """
    def __init__(self, platform):
        """ Initialize object.
        """
        self._db_name = platform  # Db name. => MongoDB db name.
        self._collection = "kline"  # Table name. => MongoDB collection name.
        self._platform = platform
        self._k_to_c = {
        }  # Kline types cursor for db. e.g. {"BTC/USD": "kline_btc_usd"}
        self._db = MongoDBBase(self._db_name, self._collection)  # db instance

    async def create_new_kline(self, kline: Kline):
        """ Insert kline data to db.

        Args:
            kline: kline object.

        Returns:
            kline_id: Kline id, it's a MongoDB document _id.
        """
        cursor = self._get_kline_cursor_by_symbol(kline.symbol)
        data = {
            "o": kline.open,
            "h": kline.high,
            "l": kline.low,
            "c": kline.close,
            "v": kline.volume,
            "t": kline.timestamp
        }
        #logger.info("create_new_kline ", data, caller=self)
        kline_id = await self._db.insert(data, cursor=cursor)
        return kline_id

    async def get_kline_at_ts(self, symbol, ts=None):
        """ Get a kline data, you can specific symbol and timestamp.

        Args:
            symbol: Symbol pair, e.g. ETH/BTC.
            ts: Millisecond timestamp. If this param is None, ts will be specific current timestamp.

        Returns:
            result: kline data, dict format. If no any data in db, result is None.
        """
        cursor = self._get_kline_cursor_by_symbol(symbol)
        if ts:
            spec = {"t": {"$lte": ts}}
        else:
            spec = {}
        _sort = [("t", -1)]
        result = await self._db.find_one(spec, sort=_sort, cursor=cursor)
        return result

    async def get_latest_kline_by_symbol(self, symbol):
        """ Get latest kline data by symbol.

        Args:
            symbol: Symbol pair, e.g. ETH/BTC.

        Returns:
            result: kline data, dict format. If no any data in db, result is None.
        """
        cursor = self._get_kline_cursor_by_symbol(symbol)
        sort = [("create_time", -1)]
        result = await self._db.find_one(sort=sort, cursor=cursor)
        return result

    async def get_kline_between_ts(self, symbol, start, end):
        """ Get some kline data between two timestamps.

        Args:
            symbol: Symbol pair, e.g. ETH/BTC.
            start: Millisecond timestamp, the start time you want to specific.
            end: Millisecond timestamp, the end time you want to specific.

        Returns:
            result: kline data, list format. If no any data in db, result is a empty list.
        """
        cursor = self._get_kline_cursor_by_symbol(symbol)
        spec = {"t": {"$gte": start, "$lte": end}}
        fields = {"create_time": 0, "update_time": 0}
        _sort = [("t", 1)]
        datas = await self._db.get_list(spec,
                                        fields=fields,
                                        sort=_sort,
                                        cursor=cursor)
        return datas

    def _get_kline_cursor_by_symbol(self, symbol):
        """ Get a cursor name by symbol, we will convert a symbol name to a collection name.
            e.g. ETH/BTC => kline_eth_btc

        Args:
            symbol: Symbol pair, e.g. ETH/BTC.

        Returns:
            cursor: DB query cursor name.
        """
        cursor = self._k_to_c.get(symbol)
        if not cursor:
            s = symbol.replace("/", "").replace("-", "")
            collection = "kline_{}".format(s)
            cursor = self._db.new_cursor(self._db_name, collection)
            self._k_to_c[symbol] = cursor
        return cursor
Exemplo n.º 5
0
 def __init__(self, collection, host=None, port=None, username=None, password=None, ):
    self._db = 'robot_table'
    self._collection = collection
    self._db = MongoDBBase(self._db, self._collection, host=host, port=port,username=username, password=password)