-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
executable file
·250 lines (209 loc) · 7.7 KB
/
common.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python3
"""Common functions."""
import functools
import multiprocessing
import os
import shutil
import tempfile
import warnings
from contextlib import contextmanager
from functools import reduce
from pathlib import Path
import pandas as pd
import yahoofinancials
import yahooquery
import yfinance
from playwright.sync_api import sync_playwright
from sqlalchemy import create_engine
from sqlalchemy import text as sqlalchemy_text
PUBLIC_HTML = f"{Path.home()}/code/accounts/web/"
PREFIX = PUBLIC_HTML
LOCKFILE = f"{PREFIX}/run.lock"
LOCKFILE_TIMEOUT = 10 * 60
SQLITE_URI = f"sqlite:///{PREFIX}sqlite.db"
SQLITE_URI_RO = f"sqlite:///file:{PREFIX}sqlite.db?mode=ro&uri=true"
SQLITE3_URI_RO = f"file:{PREFIX}sqlite.db?mode=ro"
SELENIUM_REMOTE_URL = "http://selenium:4444"
LEDGER_BIN = "ledger"
LEDGER_DIR = f"{Path.home()}/code/ledger"
LEDGER_DAT = f"{LEDGER_DIR}/ledger.ledger"
LEDGER_PRICES_DB = f"{LEDGER_DIR}/prices.db"
# pylint: disable-next=line-too-long
LEDGER_PREFIX = f"{LEDGER_BIN} -f {LEDGER_DAT} --price-db {LEDGER_PRICES_DB} -X '$' -c --no-revalued"
GET_TICKER_TIMEOUT = 30
class GetTickerError(Exception):
"""Error getting ticker."""
def get_tickers(tickers: list) -> dict:
"""Get prices for a list of tickers."""
ticker_dict = {}
for ticker in tickers:
ticker_dict[ticker] = get_ticker(ticker)
return ticker_dict
def log_function_result(name, success, error_string=None):
"""Log the success or failure of a function."""
to_sql(
pd.DataFrame(
{"name": name, "success": success, "error": error_string},
index=[pd.Timestamp.now()],
),
"function_result",
)
@functools.cache
def get_ticker(ticker):
"""Get ticker prices by trying various methods."""
get_ticker_methods = (
get_ticker_yahooquery,
get_ticker_yahoofinancials,
get_ticker_yfinance,
)
for method in get_ticker_methods:
name = method.__name__
with multiprocessing.Pool(processes=1) as pool:
async_result = pool.apply_async(method, (ticker,))
try:
return async_result.get(timeout=GET_TICKER_TIMEOUT)
except multiprocessing.TimeoutError:
log_function_result(name, False, "Timeout")
# pylint: disable-next=broad-exception-caught
except Exception as ex:
log_function_result(name, False, str(ex))
raise GetTickerError("No more methods to get ticker price")
@functools.cache
def get_ticker_yahoofinancials(ticker):
"""Get ticker price via yahoofinancials library."""
return yahoofinancials.YahooFinancials(ticker).get_current_price()
@functools.cache
def get_ticker_yahooquery(ticker):
"""Get ticker price via yahooquery library."""
return yahooquery.Ticker(ticker).price[ticker]["regularMarketPrice"]
@functools.cache
def get_ticker_yfinance(ticker):
"""Get ticker price via yfinance library."""
with warnings.catch_warnings():
# See https://github.com/ranaroussi/yfinance/issues/1837
warnings.simplefilter(action="ignore", category=FutureWarning)
return yfinance.Ticker(ticker).history(period="5d")["Close"].iloc[-1]
def read_sql_table(table, index_col="date"):
"""Load table from sqlite."""
with create_engine(SQLITE_URI_RO).connect() as conn:
return pd.read_sql_table(table, conn, index_col=index_col)
def read_sql_query(query):
"""Load table from sqlite query."""
with create_engine(SQLITE_URI_RO).connect() as conn:
return pd.read_sql_query(
sqlalchemy_text(query),
conn,
index_col="date",
parse_dates=["date"],
)
def to_sql(dataframe, table, if_exists="append", index_label="date", foreign_key=False):
"""Write dataframe to sqlite table."""
with create_engine(SQLITE_URI).connect() as conn:
if foreign_key:
conn.execute(sqlalchemy_text("PRAGMA foreign_keys=ON"))
dataframe.to_sql(table, conn, if_exists=if_exists, index_label=index_label)
conn.commit()
def write_ticker_csv(
amounts_table,
prices_table,
csv_output_path,
ticker_col_name="ticker",
ticker_amt_col="shares",
ticker_aliases=None,
ticker_prices=None,
):
"""Write ticker values to prices table and csv file.
ticker_aliases is used to map name to actual ticker: GOLD -> GC=F
"""
# Just get the latest row.
amounts_df = read_sql_query(
f"select * from {amounts_table} order by date desc limit 1"
)
if ticker_aliases:
amounts_df = amounts_df.rename(columns=ticker_aliases)
if not ticker_prices:
ticker_prices = get_tickers(amounts_df.columns)
prices_df = pd.DataFrame(
ticker_prices, index=[pd.Timestamp.now()], columns=sorted(ticker_prices.keys())
).rename_axis("date")
if ticker_aliases:
prices_df = prices_df.rename(columns={v: k for k, v in ticker_aliases.items()})
to_sql(prices_df, prices_table)
if ticker_aliases:
# Revert back columns names/tickers.
amounts_df = amounts_df.rename(
columns={v: k for k, v in ticker_aliases.items()}
)
latest_amounts = amounts_df.iloc[-1].rename(ticker_amt_col).sort_index()
latest_prices = prices_df.iloc[-1].rename("current_price").sort_index()
# Multiply latest amounts by prices.
latest_values = (latest_amounts * latest_prices.values).rename("value")
new_df = pd.DataFrame([latest_amounts, latest_prices, latest_values]).T.rename_axis(
ticker_col_name
)
new_df.to_csv(csv_output_path)
@contextmanager
def temporary_file_move(dest_file):
"""Provides a temporary file that is moved in place after context."""
with tempfile.NamedTemporaryFile(mode="w", delete=False) as write_file:
yield write_file
shutil.move(write_file.name, dest_file)
def schwab_browser_page(page):
"""Click popup that sometimes appears."""
page.get_by_text("Continue with a limited experience").click()
page.get_by_role("button", name="Accept All Cookies").click()
return page
@contextmanager
def run_with_browser_page(url):
"""Run code with a Chromium browser page."""
if not os.environ.get("SELENIUM_REMOTE_URL"):
os.environ["SELENIUM_REMOTE_URL"] = SELENIUM_REMOTE_URL
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
try:
page = browser.new_page()
page.goto(url)
yield page
finally:
browser.close()
def reduce_merge_asof(dataframes):
"""Reduce and merge date tables."""
return reduce(
lambda L, r: pd.merge_asof(L, r, left_index=True, right_index=True),
dataframes,
)
def load_sqlite_and_rename_col(table, rename_cols=None):
"""Load resampled table from sqlite and rename columns."""
dataframe = read_sql_table(table)
if rename_cols:
dataframe = dataframe.rename(columns=rename_cols)
return dataframe
def get_real_estate_df():
"""Get real estate price and rent data from sqlite."""
price_df = (
read_sql_table(
"real_estate_prices",
)[["name", "value"]]
.groupby(["date", "name"])
.mean()
.unstack("name")
)
price_df.columns = price_df.columns.get_level_values(1) + " Price"
price_df.columns.name = "variable"
rent_df = (
read_sql_table("real_estate_rents")
.groupby(["date", "name"])
.mean()
.unstack("name")
)
rent_df.columns = rent_df.columns.get_level_values(1) + " Rent"
rent_df.columns.name = "variable"
return (
reduce_merge_asof([price_df, rent_df])
.sort_index(axis=1)
.resample("D")
.mean()
.interpolate()
)
if __name__ == "__main__":
print(f'{get_ticker("SWYGX")}')