piecash.core.commodity module

exception piecash.core.commodity.GncCommodityError[source]

Bases: piecash._common.GnucashException

exception piecash.core.commodity.GncPriceError[source]

Bases: piecash._common.GnucashException

class piecash.core.commodity.Price(commodity, currency, date, value, type='unknown', source='user:price')[source]

Bases: piecash._declbase.DeclarativeBaseGuid

A single Price for a commodity.

commodity

Commodity – commodity to which the Price relates

currency

Commodity – currency in which the Price is expressed

date

datetime.date – date object representing the day at which the price is relevant

source

str – source of the price

type

str – last, ask, bid, unknown, nav

value

decimal.Decimal – the price itself

object_to_validate(change)[source]

yield the objects to validate when the object is modified (change=”new” “deleted” or “dirty”).

For instance, if the object is a Split, if it changes, we want to revalidate not the split but its transaction and its lot (if any). split.object_to_validate should yeild both split.transaction and split.lot

validate()[source]

This must be reimplemented for object requiring validation

class piecash.core.commodity.Commodity(namespace, mnemonic, fullname, fraction=100, cusip='', quote_flag=0, quote_source=None, quote_tz=None, book=None)[source]

Bases: piecash._declbase.DeclarativeBaseGuid

A GnuCash Commodity.

cusip

str – cusip code

fraction

int – minimal unit of the commodity (e.g. 100 for 1/100)

namespace

str – CURRENCY for currencies, otherwise any string to group multiple commodities together

mnemonic

str – the ISO symbol for a currency or the stock symbol for stocks (used for online quotes)

quote_flag

int – 1 if piecash/GnuCash quotes will retrieve online quotes for the commodity

quote_source

str – the quote source for GnuCash (piecash always use yahoo for stock and quandl for currencies

quote_tz

str – the timezone to assign on the online quotes

base_currency

Commodity – The base_currency for a commodity:

  • if the commodity is a currency, returns the “default currency” of the book (ie the one of the root_account)
  • if the commodity is not a currency, returns the currency encoded in the quoted_currency slot
accounts

list of piecash.core.account.Account – list of accounts which have the commodity as commodity

transactions

list of piecash.core.transaction.Transaction – list of transactions which have the commodity as currency

prices

iterator of Price – iterator on prices related to the commodity (it is a sqlalchemy query underneath)

currency_conversion(currency)[source]

Return the latest conversion factor to convert self to currency

currency

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
update_prices(start_date=None)[source]

Retrieve online prices for the commodity:

  • for currencies, it will get from quandl the exchange rates between the currency and its base_currency
  • for stocks, it will get from yahoo the daily closing prices expressed in its base_currency
Parameters:
  • start_date (datetime.date) – prices will be updated as of the start_date. If None, start_date is today
  • 7 days. (-) –

Note

if prices are already available in the GnuCash file, the function will only retrieve prices as of the max(start_date, last quoted price date)

Todo

add some frequency to retrieve prices only every X (week, month, …)

object_to_validate(change)[source]

yield the objects to validate when the object is modified (change=”new” “deleted” or “dirty”).

For instance, if the object is a Split, if it changes, we want to revalidate not the split but its transaction and its lot (if any). split.object_to_validate should yeild both split.transaction and split.lot

validate()[source]

This must be reimplemented for object requiring validation