Beispiel #1
0
    def get_historical_price(
        from_asset: 'Asset',
        to_asset: 'Asset',
        timestamp: Timestamp,
        max_seconds_distance: int,
        source: Optional[HistoricalPriceOracle] = None,
    ) -> Optional['HistoricalPrice']:
        """Gets the price around a particular timestamp

        If no price can be found returns None
        """
        connection = GlobalDBHandler()._conn
        cursor = connection.cursor()
        querystr = (
            'SELECT from_asset, to_asset, source_type, timestamp, price FROM price_history '
            'WHERE from_asset=? AND to_asset=? AND ABS(timestamp - ?) <= ? ')
        querylist = [
            from_asset.identifier, to_asset.identifier, timestamp,
            max_seconds_distance
        ]
        if source is not None:
            querystr += ' AND source_type=?'
            querylist.append(source.serialize_for_db())

        querystr += 'ORDER BY ABS(timestamp - ?) ASC LIMIT 1'
        querylist.append(timestamp)

        query = cursor.execute(querystr, tuple(querylist))
        result = query.fetchone()
        if result is None:
            return None

        return HistoricalPrice.deserialize_from_db(result)
Beispiel #2
0
def get_globaldb_cache_entries(from_asset: Asset, to_asset: Asset) -> List[HistoricalPrice]:
    """TODO: This should probaly be moved in the globaldb/handler.py if we use it elsewhere
    and made more generic (accept different sources)"""
    connection = GlobalDBHandler()._conn
    cursor = connection.cursor()
    query = cursor.execute(
        'SELECT from_asset, to_asset, source_type, timestamp, price FROM '
        'price_history WHERE from_asset=? AND to_asset=? AND source_type=? ORDER BY timestamp ASC',
        (
            from_asset.identifier,
            to_asset.identifier,
            HistoricalPriceOracle.CRYPTOCOMPARE.serialize_for_db(),  # pylint: disable=no-member
        ),
    )
    return [HistoricalPrice.deserialize_from_db(x) for x in query]