Exemplo n.º 1
0
    def __call__(self):

        start_of_trend = self._period_start.date
        end_of_trend = self._period_end.date

        asset_bucket = PeriodCollate(start_of_trend, end_of_trend, decimal_generator, split_summation,
                                     frequency=self._period_size.frequency, interval=self._period_size.interval)
        liability_bucket = PeriodCollate(start_of_trend, end_of_trend, decimal_generator, split_summation,
                                         frequency=self._period_size.frequency, interval=self._period_size.interval)
        net_bucket = PeriodCollate(start_of_trend, end_of_trend, decimal_generator, split_summation,
                                   frequency=self._period_size.frequency, interval=self._period_size.interval)

        currency = get_currency()

        # Calculate the asset balances
        for account in account_walker(self._asset_accounts):
            for key, value in asset_bucket.container.iteritems():
                balance = get_balance_on_date(account, key, currency)
                asset_bucket.container[key] += balance

        # Calculate the liability balances
        for account in account_walker(self._liability_accounts):
            for key, value in liability_bucket.container.iteritems():
                balance = get_balance_on_date(account, key, currency)
                liability_bucket.container[key] += balance

        # Now calculate the net values from the difference.
        for key, value in liability_bucket.container.iteritems():
            net_bucket.container[key] = asset_bucket.container[key] + liability_bucket.container[key]

        result = self._generate_result()
        result['data']['assets'] = sorted([dict(date=time.mktime(key.timetuple()), value=value)
                                           for key, value in asset_bucket.container.iteritems()],
                                          key=lambda s: s['date'])
        result['data']['liabilities'] = sorted([dict(date=time.mktime(key.timetuple()), value=-value)
                                                for key, value in liability_bucket.container.iteritems()],
                                               key=lambda s: s['date'])
        result['data']['net'] = sorted([dict(date=time.mktime(key.timetuple()), value=value)
                                        for key, value in net_bucket.container.iteritems()],
                                       key=lambda s: s['date'])

        inflation = get_monthly_inflation()
        starting_point = None
        inflation_data = []
        for record in result['data']['net']:
            if starting_point:
                starting_point += (starting_point * inflation)
            else:
                starting_point = record['value']

            inflation_data.append(dict(date=record['date'], value=starting_point))

        result['data']['inflation'] = inflation_data

        return result
Exemplo n.º 2
0
    def _calculate_payload(self, account_list, delta_months, trend_months, liability=False):
        currency = get_currency()
        today = date.today()
        end_of_month = date(today.year, today.month, monthrange(today.year, today.month)[1])
        total_data = dict(records=[], current_data=Decimal('0.0'),
                          deltas=[Decimal('0.0') for a in delta_months],
                          delta_sub_total=[Decimal('0.0') for a in delta_months],
                          trend=[Decimal('0.0') for a in trend_months])

        for definition in account_list:
            definition_data = dict(name=definition['name'],
                                   current_data=Decimal('0.0'),
                                   deltas=[Decimal('0.0') for a in delta_months],
                                   trend=[Decimal('0.0') for a in trend_months])

            # Get Current Data first
            for account in account_walker(definition['accounts']):
                balance = get_balance_on_date(account, end_of_month, currency)
                definition_data['current_data'] += balance
                total_data['current_data'] += balance

            # Calculate the trends
            for index, trend in enumerate(trend_months):
                for account in account_walker(definition['accounts']):
                    balance = get_balance_on_date(account, trend, currency)
                    definition_data['trend'][index] += balance
                    total_data['trend'][index] += balance

            # Calculate deltas
            for index, delta in enumerate(delta_months):
                value = Decimal(0.0)
                for account in account_walker(definition['accounts']):
                    balance = get_balance_on_date(account, delta, currency)
                    value += balance
                    total_data['delta_sub_total'][index] += balance

                try:
                    definition_data['deltas'][index] = (definition_data['current_data'] - value) / value
                    if liability:
                        definition_data['deltas'][index] = -definition_data['deltas'][index]
                except:
                    definition_data['deltas'][index] = 'N/A'

            total_data['records'].append(definition_data)

        # Calculate the deltas for the total values.
        for index, value in enumerate(total_data['delta_sub_total']):
            try:
                total_data['deltas'][index] = (total_data['current_data'] - value) / value
                if liability:
                    total_data['deltas'][index] = -total_data['deltas'][index]
            except:
                definition_data['deltas'][index] = 'N/A'

        return total_data
    def __call__(self):

        investment_value = dict()
        buckets = PeriodCollate(self._period_start.date, self._period_end.date,
                                investment_bucket_generator, store_investment, frequency=self._period_size.frequency,
                                interval=self._period_size.interval)

        start_value = Decimal('0.0')
        start_value_date = self._period_start.date - relativedelta(days=1)
        currency = get_currency()

        for account in account_walker(self._investment_accounts, ignore_list=self._ignore_accounts):
            for split in get_splits(account, self._period_start.date, self._period_end.date):
                buckets.store_value(split)

            start_value += get_balance_on_date(account, start_value_date, currency)

            for key in buckets.container.keys():
                date_value = key + relativedelta(months=1) - relativedelta(days=1)
                investment_value[key] = investment_value.get(key, Decimal('0.0')) + get_balance_on_date(account,
                                                                                                        date_value,
                                                                                                        currency)

        results = self._generate_result()
        results['data']['start_value'] = start_value

        results['data']['income'] = sorted(
            [(time.mktime(key.timetuple()), value['income']) for key, value in buckets.container.iteritems()],
            key=itemgetter(0))

        results['data']['money_in'] = sorted(
            [(time.mktime(key.timetuple()), value['money_in']) for key, value in buckets.container.iteritems()],
            key=itemgetter(0))

        results['data']['expense'] = sorted(
            [(time.mktime(key.timetuple()), value['expense']) for key, value in buckets.container.iteritems()],
            key=itemgetter(0))

        results['data']['value'] = sorted(
            [[time.mktime(key.timetuple()), value] for key, value in investment_value.iteritems()],
        )

        results['data']['basis'] = sorted(
            [[time.mktime(key.timetuple()), Decimal('0.0')] for key in buckets.container.keys()],
            key=itemgetter(0)
        )

        monthly_start = start_value
        for index, record in enumerate(results['data']['basis']):
            record[1] += (monthly_start + results['data']['income'][index][1] + results['data']['money_in'][index][1] +
                          results['data']['expense'][index][1])
            monthly_start = record[1]

        return results
Exemplo n.º 4
0
    def __call__(self):

        credit_used = Decimal('0.0')
        liquid_assets = Decimal('0.0')

        currency = get_currency()

        for credit_account in account_walker(self._credit_accounts):
            credit_used += get_balance_on_date(credit_account, PeriodStart.today.date, currency)

        for liquid_asset_account in account_walker(self._liquid_asset_accounts):
            liquid_assets += get_balance_on_date(liquid_asset_account, PeriodStart.today.date, currency)

        result = self._generate_result()
        result['data']['credit_used'] = -credit_used
        result['data']['liquid_assets'] = liquid_assets

        return result
    def __call__(self):
        account = get_account(self.account)

        balance = get_balance_on_date(account, self.when.date)

        payload = self._generate_result()
        payload["data"]["balance"] = balance
        payload["data"]["good_value"] = self.good_value
        payload["data"]["warn_value"] = self.warn_value
        payload["data"]["error_value"] = self.error_value

        return payload
    def __call__(self):
        breakdown = dict()
        today = datetime.today()
        currency = get_currency()

        for account in account_walker(self._investment_accounts, self._ignore_accounts):
            balance = get_balance_on_date(account, today, currency)
            commodity = account.GetCommodity().get_mnemonic()

            results = get_asset_allocation(commodity, balance)

            for key, value in results.iteritems():
                breakdown[key] = breakdown.get(key, Decimal('0.0')) + value

        return_value = self._generate_result()
        return_value['data']['categories'] = sorted([[key, value] for key, value in breakdown.iteritems()],
                                                    key=itemgetter(0))

        return return_value
Exemplo n.º 7
0
    def __call__(self):

        total_balance = Decimal('0.0')
        currency = get_currency()

        for account_name in self.accounts:
            multiplier = Decimal('1.0')
            if isinstance(account_name, basestring):
                account = get_account(account_name)
            else:
                account = get_account(account_name[0])
                multiplier = Decimal(account_name[1])

            balance = get_balance_on_date(account, self.as_of.date, currency)
            total_balance += (balance * multiplier)

        payload = self._generate_result()
        payload['data']['balance'] = total_balance
        payload['data']['goal'] = self.goal_amount

        return payload
    def __call__(self):
        account = get_account(self.account_name)

        data = dict(purchases=[], dividend=[], value=[])

        last_dividend = Decimal('0.0')
        last_purchase = Decimal('0.0')

        currency = get_currency()

        purchases = dict()
        dividends = dict()
        values = dict()

        for split in account.GetSplitList():
            other_account_name = get_corr_account_full_name(split)
            other_account = get_account(other_account_name)

            account_type = AccountTypes(other_account.GetType())
            date = datetime.fromtimestamp(split.parent.GetDate())

            # Store previous data
            if len(purchases):
                previous_date = date - relativedelta(days=1)
                previous_date_key = time.mktime(previous_date.timetuple())
                purchases[previous_date_key] = last_purchase
                dividends[previous_date_key] = last_dividend
                values[previous_date_key] = get_balance_on_date(account, previous_date, currency)

            # Find the correct amount that was paid from the account into this account.
            change_amount = get_decimal(split.GetValue())

            if change_amount > 0:
                # Need to get the value from the corr account split.
                for parent_splits in split.parent.GetSplitList():
                    if parent_splits.GetAccount().get_full_name() == other_account_name:
                        change_amount = -get_decimal(parent_splits.GetValue())

            if account_type == AccountTypes.mutual_fund or account_type == AccountTypes.asset:
                # Asset or mutual fund transfer
                last_purchase += change_amount
            else:
                last_dividend += get_decimal(split.GetValue())

            key = time.mktime(date.timetuple())
            purchases[key] = last_purchase
            dividends[key] = last_dividend
            values[key] = get_balance_on_date(account, date, currency)

        # Now get all of the price updates in the database.
        price_database = get_session().get_book().get_price_db()
        commodity = account.GetCommodity()
        for price in price_database.get_prices(commodity, None):
            date = time.mktime(price.get_time().timetuple())

            values[date] = max(values.get(date, Decimal('0.0')),
                               get_balance_on_date(account, price.get_time(), currency))

        data['purchases'] = sorted([(key, value) for key, value in purchases.iteritems()], key=itemgetter(0))
        data['dividend'] = sorted([(key, value) for key, value in dividends.iteritems()], key=itemgetter(0))
        data['value'] = sorted([(key, value) for key, value in values.iteritems()], key=itemgetter(0))

        results = self._generate_result()
        results['data'] = data

        return results