Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for indirect commodity quoting #243

Open
punchdeerflyscorpion opened this issue Nov 1, 2024 · 0 comments
Open

Add support for indirect commodity quoting #243

punchdeerflyscorpion opened this issue Nov 1, 2024 · 0 comments

Comments

@punchdeerflyscorpion
Copy link

Hi,
GnuCash supports indirect commodity quoting. For example, if the price database contains the valuation of a hypothetical instrument X in EUR and the valuation of EUR to USD, then gnucash is able to provide the value of instrument X in USD. Currently Piecash raises the GncConversionError (in Commodity.currency_conversion) if there is no direct quote in price database.

I am currently using the following modification to the currency_conversion method:

def currency_conversion(self, currency):
        """
        Return the latest conversion factor to convert self to currency

        Attributes:
            currency (:class:`piecash.core.commodity.Commodity`): the currency to which the Price need to be converted

        Returns:
            a Decimal that can be multiplied by an amount expressed in self.commodity to get an amount expressed in currency

        Raises:
            GncConversionError: not possible to convert self to the currency

        """
        # conversion is done from self.commodity to commodity (if possible)
        sc2c = self.prices.filter_by(currency=currency).order_by(Price.date.desc()).first()
        if sc2c:
            return sc2c.value

        # conversion is done directly from commodity to self.commodity (if possible)
        c2sc = currency.prices.filter_by(currency=self).order_by(Price.date.desc()).first()
        if c2sc:
            return Decimal(1) / c2sc.value

        **"""
        Indirect conversion workaround
        """

	priced_in = []
        ind = 0
        for pr in self.prices:
            if pr.currency not in priced_in:
                priced_in.append(pr.currency)
        for curr in priced_in:
            ind = self.currency_conversion(curr) * curr.currency_conversion(currency)
            if ind:
                return ind

        """
        End workaround
        """**

        raise GncConversionError("Cannot convert {} to {}".format(self, currency))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant