Beispiel #1
0
    def convert_currency(self, from_currency, to_currency, amount):
        log.debug("Converting %f %s to %s, rates: %s", amount, from_currency,
                  to_currency, [market.rate for market in self.market_feeds])
        if from_currency == to_currency:
            return round(amount, 8)

        rates = []
        for market in self.market_feeds:
            if (market.has_rate and market.is_online and
                    market.rate.currency_pair == (from_currency, to_currency)):
                rates.append(market.rate.spot)

        if rates:
            return round(amount * Decimal(median(rates)), 8)

        raise CurrencyConversionError(
            f'Unable to convert {amount} from {from_currency} to {to_currency}'
        )
    def convert_currency(self, from_currency, to_currency, amount):
        rates = [market.rate for market in self.market_feeds]
        log.debug("Converting %f %s to %s, rates: %s" %
                  (amount, from_currency, to_currency, rates))
        if from_currency == to_currency:
            return amount

        for market in self.market_feeds:
            if (market.rate_is_initialized() and market.is_online() and
                    market.rate.currency_pair == (from_currency, to_currency)):
                return amount * Decimal(market.rate.spot)
        for market in self.market_feeds:
            if (market.rate_is_initialized() and market.is_online()
                    and market.rate.currency_pair[0] == from_currency):
                return self.convert_currency(
                    market.rate.currency_pair[1], to_currency,
                    amount * Decimal(market.rate.spot))
        raise CurrencyConversionError(
            f'Unable to convert {amount} from {from_currency} to {to_currency}'
        )