def _net_worth_in_intervals(self, interval): interval_tuples = self._interval_tuples(interval, self.entries) interval_totals = [] end_dates = [p[1] for p in interval_tuples] for (begin_date, end_date), holdings_list in \ zip(interval_tuples, holdings_at_dates(self.entries, end_dates, self.price_map, self.options)): totals = {} for currency in self.options['operating_currency']: currency_holdings_list = \ holdings.convert_to_currency(self.price_map, currency, holdings_list) if not currency_holdings_list: continue holdings_ = holdings.aggregate_holdings_by( currency_holdings_list, operator.attrgetter('cost_currency')) holdings_ = [holding for holding in holdings_ if holding.currency and holding.cost_currency] # If after conversion there are no valid holdings, skip the # currency altogether. if holdings_: totals[currency] = holdings_[0].market_value interval_totals.append({ 'date': end_date - datetime.timedelta(1), 'totals': totals }) return interval_totals
def test_holdings_at_dates(load_doc): """ plugin "auto_accounts" 2016-01-01 * Equity:Unknown Assets:Cash 5000 USD 2016-01-02 * Assets:Account1 15 HOOL {123 USD} Assets:Cash 2016-01-03 * Assets:Account1 10 HOOL {130 USD} Assets:Cash """ entries, errors, options = load_doc price_map = prices.build_price_map(entries) dates = [ datetime.date(2016, 1, 1), datetime.date(2016, 1, 2), datetime.date(2016, 1, 3), datetime.date(2016, 1, 4), ] number_of_holdings = list( map(len, list(holdings_at_dates(entries, dates, price_map, options)))) assert number_of_holdings == [0, 1, 2, 3]
def net_worth_at_intervals(self, interval): interval_tuples = self._interval_tuples(interval) dates = [p[1] for p in interval_tuples] return [{ 'date': date, 'balance': self._holdings_to_net_worth(holdings_list), } for date, holdings_list in zip(dates, holdings_at_dates(self.entries, dates, self.price_map, self.options))]
def _net_worth_in_periods(self): month_tuples = self._interval_tuples('month', self.entries) monthly_totals = [] end_dates = [p[1] for p in month_tuples] for (begin_date, end_date), holdings_list in \ zip(month_tuples, holdings_at_dates(self.entries, end_dates, self.price_map, self.options)): totals = {} for currency in self.options['operating_currency']: currency_holdings_list = \ holdings.convert_to_currency(self.price_map, currency, holdings_list) if not currency_holdings_list: continue holdings_list = holdings.aggregate_holdings_by( currency_holdings_list, operator.attrgetter('cost_currency')) holdings_list = [holding for holding in holdings_list if holding.currency and holding.cost_currency] # If after conversion there are no valid holdings, skip the # currency altogether. if holdings_list: totals[currency] = holdings_list[0].market_value monthly_totals.append({ 'begin_date': begin_date, 'end_date': end_date, 'totals': totals }) return monthly_totals