-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlibcashdrag.py
50 lines (40 loc) · 1.99 KB
/
libcashdrag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/env python3
import fava_investor.common.libinvestor as libinvestor
from beancount.core.inventory import Inventory
def find_cash_commodities(accapi, options):
"""Build list of commodities that are considered cash"""
meta_label = options.get('metadata_label_cash', 'asset_allocation_Bond_Cash')
cash_commodities = []
for commodity, declaration in accapi.get_commodity_directives().items():
if declaration.meta.get(meta_label, 0) == 100:
cash_commodities.append(f'^{commodity}$')
operating_currencies = accapi.get_operating_currencies()
cash_commodities += map(lambda cur: f'^{cur}$', operating_currencies)
cash_commodities = set(cash_commodities)
commodities_pattern = '(' + '|'.join(cash_commodities) + ')'
return commodities_pattern, operating_currencies[0]
def find_loose_cash(accapi, options):
"""Find uninvested cash in specified accounts"""
currencies_pattern, main_currency = find_cash_commodities(accapi, options)
sql = """
SELECT account AS account,
CONVERT(sum(position), '{main_currency}') AS position
WHERE account ~ '{accounts_pattern}'
AND not account ~ '{accounts_exclude_pattern}'
AND currency ~ '{currencies_pattern}'
GROUP BY account
ORDER BY position DESC
""".format(main_currency=main_currency,
accounts_pattern=options.get('accounts_pattern', '^Assets'),
accounts_exclude_pattern=options.get('accounts_exclude_pattern', '^ $'), # TODO
currencies_pattern=currencies_pattern,
)
rtypes, rrows = accapi.query_func(sql)
if not rtypes:
return [], {}, [[]]
rrows = [r for r in rrows if r.position != Inventory()]
threshold = options.get('min_threshold', 0)
if threshold:
rrows = [r for r in rrows if r.position.get_only_position().units.number >= threshold]
footer = libinvestor.build_table_footer(rtypes, rrows, accapi)
return [('Cash Drag Analysis', (rtypes, rrows, None, footer))]