how to set Gnucash commodities (exchange rates) from Python?

John Ralls jralls at ceridwen.us
Sat Jan 3 11:06:00 EST 2015


> On Jan 3, 2015, at 1:24 AM, Felix Schwarz <felix.schwarz at oss.schwarz.eu> wrote:
> 
> 
> I'm trying to import some transactions into my gnucash db using Python. Adding
> the transactions itself is no problem but some of them refer to accounts with
> different currencies and I don't know how to set the exchange rate using the
> Python API.
> 
> For example I have a EUR and a USD account and I transfer some USD to EUR. If
> I don't care about commities gnucash will create an "imbalance" account.
> 
> After reading some examples it seems as if gnucash has a "global" (per gnucash
> file) internal price database which contains triples
>  (commodity_1, commodity_2, date) -> rate
> 
> Is just adding a new triple to the price database is enough for a
> multi-currency transaction? Or do I have to set the correct commodity also in
> the splits/transaction?
> 
> 
> Assuming my understanding of gnucash's commodities is not flawed there are two
> aspects of commodities which worry me a bit:
> In general gnucash seems to assume that there is one "true" exchange rate for
> any given point in time.
> 1. What is the time resolution for prices? I might have different
>   transactions on the same day with different rates.
> 2. Similarly currency exchange rates are not the same everywhere even at the
>   exact same point in time. For example PayPal has (significantly worse)
>   rates than my bank. Is it possible somehow to set a specific rate
>   for a transaction?
>   The only workaround I could think of for this would be to create a new
>   commodity (e.g. "PayPal USD") but that seems quite messy.

There is no API for setting price/exchange rate in a split because it's not a stored parameter, it's the ratio between the split's "amount" expressed in the split's account's commodity/currency and the split's "value" expressed in the transaction's currency/commodity. 

That's separate from the price database, which isn't used for transactions except that the last entered price is provided as the default value. As of 2.6 the GUI has hooks to post a split's price into the price db but that isn't present in the API, you'll have to code it yourself.

Since the useful date for pricing purposes is the posted date, which is only a date, if you enter in the GUI multiple transactions with different prices on the same day, each successive entry will overwrite the price in the pricedb and you'll end up with only the last one. The pricedb itself stores date-time, so you can use 1 second resolution when posting to it from your own code if you want to track the multiple prices outside of the transactions.

Regards,
John Ralls




More information about the gnucash-user mailing list