-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.py
executable file
·79 lines (67 loc) · 2.34 KB
/
history.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
"""Write finance history."""
import subprocess
import pandas as pd
import common
LEDGER_LIQUID_CMD = (
f"""{common.LEDGER_PREFIX} --limit 'commodity=~/^(SWVXX|\\\\$|CHF|GBP|SGD|"SPX)/' """
"--limit 'not(account=~/(Retirement|Precious Metals|Zurcher)/)' -J "
"-n bal \\(^assets or ^liabilities\\)"
)
LEDGER_COMMODITIES_CMD = (
f'{common.LEDGER_PREFIX} -J -n --limit "commodity=~/^(GLD|SGOL|SIVR)/" bal '
'^"Assets:Investments"'
)
LEDGER_ETFS_CMD = (
f'{common.LEDGER_PREFIX} --limit "commodity=~/^(SCH|SW[AIT]|IBKR)/" -J -n bal '
'^"Assets:Investments:.*Broker.*"'
)
LEDGER_IRA_CMD = (
f'{common.LEDGER_PREFIX} --limit "commodity=~/^SWYGX/" -J -n bal '
'^"Assets:Investments:Retirement:Charles Schwab IRA"'
)
LEDGER_REAL_ESTATE_CMD = f'{common.LEDGER_PREFIX} -J -n bal ^"Assets:Real Estate"'
LEDGER_UBS_PILLAR_CMD = (
f"{common.LEDGER_PREFIX} -J -n bal "
'^"Assets:Investments:Retirement:UBS Vested Benefits"'
)
LEDGER_ZURCHER_CMD = f'{common.LEDGER_PREFIX} -J -n bal ^"Assets:Zurcher Kantonal"'
def get_ledger_balance(command):
"""Get account balance from ledger."""
try:
return float(
subprocess.check_output(
f"{command} | tail -1", shell=True, text=True
).split()[1]
)
except IndexError:
return 0
def main():
"""Main."""
commodities = get_ledger_balance(LEDGER_COMMODITIES_CMD)
etfs = get_ledger_balance(LEDGER_ETFS_CMD)
total_investing = commodities + etfs
total_real_estate = get_ledger_balance(LEDGER_REAL_ESTATE_CMD)
# Retirement
schwab_ira = get_ledger_balance(LEDGER_IRA_CMD)
pillar2 = get_ledger_balance(LEDGER_UBS_PILLAR_CMD)
zurcher = get_ledger_balance(LEDGER_ZURCHER_CMD)
total_retirement = pillar2 + zurcher + schwab_ira
history_df_data = {
"total_real_estate": total_real_estate,
"total_liquid": get_ledger_balance(LEDGER_LIQUID_CMD),
"total_investing": total_investing,
"total_retirement": total_retirement,
"etfs": etfs,
"commodities": commodities,
"ira": schwab_ira,
"pillar2": pillar2,
}
history_df = pd.DataFrame(
history_df_data,
index=[pd.Timestamp.now()],
columns=history_df_data.keys(),
)
common.to_sql(history_df, "history")
if __name__ == "__main__":
main()