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 to which the Price relates

Type

Commodity

currency

currency in which the Price is expressed

Type

Commodity

date

date object representing the day at which the price is relevant

Type

datetime.date

source

source of the price

Type

str

type

last, ask, bid, unknown, nav

Type

str

value

the price itself

Type

decimal.Decimal

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='', book=None)[source]

Bases: piecash._declbase.DeclarativeBaseGuid

A GnuCash Commodity.

cusip

cusip code

Type

str

fraction

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

Type

int

namespace

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

Type

str

mnemonic

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

Type

str

quote_flag

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

Type

int

quote_source

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

Type

str

quote_tz

the timezone to assign on the online quotes

Type

str

base_currency

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

Type

Commodity

accounts

list of accounts which have the commodity as commodity

Type

list of piecash.core.account.Account

transactions

list of transactions which have the commodity as currency

Type

list of piecash.core.transaction.Transaction

prices

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

Type

iterator of Price

currency_conversion(currency)[source]

Return the latest conversion factor to convert self to currency

currency

the currency to which the Price need to be converted

Type

piecash.core.commodity.Commodity

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