forked from mk12/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetquotes.py
More file actions
executable file
·116 lines (97 loc) · 4.12 KB
/
getquotes.py
File metadata and controls
executable file
·116 lines (97 loc) · 4.12 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
import argparse
import datetime
from lxml import html
from pathlib import Path
import requests
PRICE_DB_FILE = Path.home() / ".local/share/ledger/pricedb"
BASE = "USD"
CURRENCIES = ["CAD", "EUR"]
MUTUAL_FUNDS = {"VMFXX": "0033", "VTSAX": "0585", "VTIAX": "0569", "VBTLX": "0584"}
def fmt_currency(currency):
return "$" if currency == "CAD" else currency
def get_business_day(date):
days = {5: 1, 6: 2}.get(date.weekday(), 0)
return date - datetime.timedelta(days=days)
def get_exchange_rates(date):
date_str = date.strftime("%Y-%m-%d")
params = {"base": BASE, "symbols": ",".join(CURRENCIES)}
r = requests.get(f"https://api.exchangeratesapi.io/{date_str}", params=params)
rates = r.json()["rates"]
for currency in CURRENCIES:
yield fmt_currency(BASE), fmt_currency(currency), str(rates[currency])
def get_fund_prices(date):
date_str = get_business_day(date).strftime("%m/%d/%Y")
for symbol, number in MUTUAL_FUNDS.items():
params = {
"radio": 1,
"results": "get",
"FundType": "VanguardFunds",
"FundIntExt": "INT",
"FundId": number,
"fundName": number,
"fundValue": number,
"radiobutton2": 1,
"beginDate": date_str,
"endDate": date_str,
}
r = requests.get(
"https://personal.vanguard.com/us/funds/tools/pricehistorysearch",
params=params,
)
tree = html.fromstring(r.content)
mode = 0
price = None
for cell in tree.xpath("//td/text()"):
cell = cell.strip()
if not cell:
continue
if mode == 0:
if cell.startswith("Fund Inception Date"):
mode = 1
elif mode == 1:
if cell.startswith("$"):
price = cell[1:]
break
if not price:
raise Exception(f"Failed to find price for {symbol}")
yield symbol, fmt_currency(BASE), price
def get_trust_select_prices(date):
symbol = "VTRTS"
r = requests.get(
"https://institutional.vanguard.com/web/cf/product-details/model.json?paths=%5B%5B%5B%27allFundCharacteristicsName%27%2C%27allFundName%27%5D%5D%2C%5B%5B%27benchmarkAnalyticsLatestData%27%2C%27dailyNavPriceLatest%27%2C%27fundAnalyticsLatestRiskData%27%2C%27fundDailyYieldLatest%27%5D%2C%271685%27%5D%2C%5B%5B%27fiftyTwoWeekNavPrice%27%2C%27fundAnalyticsSpecialDateLatest%27%2C%27holdingDetailsDates%27%5D%2C%271685%2C%27%5D%2C%5B%27frapiBenchmarkContent%27%2C%271685%2C%27%2C%270031%27%5D%2C%5B%27frapiContentWithCodes%27%2C%271685%2C%27%2C%27N001%27%5D%2C%5B%27dailyNavHistory%27%2C%271685%2C%27%2C%5B%272015-06-30%27%2C%272018-03-15%27%5D%2C%272019-09-15%27%5D%5D&method=get"
)
price = r.json()["jsonGraph"]["dailyNavPriceLatest"]["value"][0]["priceItem"][0][
"price"
]
yield symbol, fmt_currency(BASE), str(price)
def main():
parser = argparse.ArgumentParser(description="generate price data for ledger")
parser.add_argument("-c", "--clear", action="store_true", help="clear file")
args = parser.parse_args()
parent = PRICE_DB_FILE.parent
if not parent.exists():
parent.mkdir(parents=True)
date = datetime.date.today()
date_str = date.strftime(r"%Y/%m/%d")
with open(PRICE_DB_FILE, "r") as file:
lines = file.readlines()
lines.sort()
with open(PRICE_DB_FILE, "w") as file:
if not args.clear:
for line in lines:
line = line.strip()
if not line:
continue
if line.startswith(f"P {date_str} "):
break
print(line, file=file)
for get in [get_exchange_rates, get_fund_prices, get_trust_select_prices]:
for price in get(date):
line = f"P {date_str} 00:00:00 {' '.join(price)}"
print(line)
print(line, file=file)
if __name__ == "__main__":
print("If it doesn't work, look at prices in the Holdings tab")
print(f"and manually enter in {PRICE_DB_FILE}.\n")
main()