-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathledger_amounts.py
More file actions
executable file
·67 lines (59 loc) · 1.94 KB
/
Copy pathledger_amounts.py
File metadata and controls
executable file
·67 lines (59 loc) · 1.94 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
import subprocess
from collections import defaultdict
from concurrent.futures import ThreadPoolExecutor
from typing import Optional
import common
LEDGER_COMMODITY_CMD = (
f"{common.LEDGER_BIN} -f {common.LEDGER_DAT} "
'--balance-format "%(S(display_total))" -n -c bal'
)
LEDGER_LIMIT_ETFS = (
f"""--limit 'commodity!~/{common.CURRENCIES_REGEX}/ and commodity=~/^[A-Z]+/'"""
)
def get_commodity_amounts(ledger_args: str) -> dict[str, float]:
process = subprocess.run(
f"{LEDGER_COMMODITY_CMD} {ledger_args}",
shell=True,
check=True,
text=True,
capture_output=True,
)
lines = process.stdout.splitlines()
df_data = {}
for line in lines:
shares, ticker = line.split(maxsplit=1)
ticker = ticker.strip('"')
df_data[ticker] = float(shares)
return df_data
def get_etfs_amounts(account: Optional[str] = None) -> dict[str, float]:
with ThreadPoolExecutor() as exc:
results = []
if account:
results.append(
exc.submit(
get_commodity_amounts,
LEDGER_LIMIT_ETFS
+ f' --limit "account=~/^Assets:Investments:{account}/"',
)
)
else:
results.append(
exc.submit(
get_commodity_amounts,
LEDGER_LIMIT_ETFS
+ ' --limit "account=~/^Assets:Investments:(Charles Schwab .*Brokerage|Interactive Brokers)/"',
)
)
results.append(
exc.submit(
get_commodity_amounts,
LEDGER_LIMIT_ETFS
+ ' --limit "account=~/^Assets:Investments:Retirement:Charles Schwab IRA/"',
)
)
combined = defaultdict(float)
for r in results:
for k, v in r.result().items():
combined[k] += v
return combined