-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathledger_prices_db.py
More file actions
executable file
·65 lines (53 loc) · 2.08 KB
/
Copy pathledger_prices_db.py
File metadata and controls
executable file
·65 lines (53 loc) · 2.08 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
#!/usr/bin/env python
"""Update prices.db with latest data."""
import typing
from datetime import datetime
import pandas as pd
import common
import etfs
import homes
import stock_options
DATE_FORMAT = "%Y/%m/%d %H:%M:%S"
NOW = datetime.now().strftime(DATE_FORMAT)
def main():
"""Main."""
with common.temporary_file_move(common.LEDGER_PRICES_DB) as output_file:
# Commodities
for row in etfs.get_etfs_df().itertuples():
output_file.write(f"P {NOW} {row.Index} ${row.current_price}\n")
# Forex values
forex_df = common.read_sql_table("forex").resample("D").last()
for row in forex_df.itertuples():
timestamp = f"{row.Index}".replace("-", "/")
if pd.notna(row.CHFUSD):
output_file.write(f"P {timestamp} CHF ${row.CHFUSD}\n")
if pd.notna(row.SGDUSD):
output_file.write(f"P {timestamp} SGD ${row.SGDUSD}\n")
# Properties
real_estate_df = homes.get_real_estate_df()
for p in homes.PROPERTIES:
col = f"{p.name} Price"
output_file.write(
f'P {NOW} "{p.address}" ${real_estate_df[col].iloc[-1]:.0f}\n'
)
# Stock options
o: stock_options.OptionsAndSpreads = stock_options.get_options_and_spreads()
options_df = o.all_options
commodities_written = set()
for idx, row in options_df.iterrows():
idx = typing.cast(tuple, idx)
name = idx[1]
if name in commodities_written:
continue
value = row["value"] / row["count"]
output_file.write(f'P {NOW} "{name}" ${value:.2f}\n')
commodities_written.add(name)
# Futures
for row in o.futures.futures_df.reset_index().itertuples():
if row.commodity in commodities_written:
continue
value = row.current_price * row.multiplier # type: ignore
output_file.write(f'P {NOW} "{row.commodity}" ${value:.2f}\n')
commodities_written.add(row.commodity)
if __name__ == "__main__":
main()