-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_html_yf.py
executable file
·183 lines (156 loc) · 5.07 KB
/
gen_html_yf.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
#!/usr/bin/env python3
# pip3 install yfinance
import os
import sys
import json
import time
from pytz import timezone
from datetime import datetime
import yfinance as yf
def bailmsg(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
exit(1)
def write_to_file(text, filename):
with open(filename, 'w') as f: f.write(text)
def gen_html_table(tbl):
table_name,symbols = tbl.split('=')
symb_list = symbols.split(',')
html = '<table>'
html += '<tr>'
html += f'<th><u>{table_name}</u></th>'
for symb in symb_list:
html += f'<th id={symb}_th><a href=https://finance.yahoo.com/quote/{symb}>{symb}</a></th>'
html += '</tr>'
html += '<tr>'
html += '<td>$</td>'
for symb in symb_list:
html += f'<td id={symb}_currentPrice></td>'
html += '</tr>'
html += '<tr>'
html += '<td>%</td>'
for symb in symb_list:
html += f'<td id={symb}_move></td>'
html += '</tr>'
html += '<tr>'
html += '<td>Close</td>'
for symb in symb_list:
html += f'<td id={symb}_previousClose></td>'
html += '</tr>'
mc_toggle = os.getenv('DISPLAY_MARKET_CAP')
if (mc_toggle is not None) and (mc_toggle == '1'):
html += '<tr>'
html += '<td>MCap</td>'
for symb in symb_list:
html += f'<td id={symb}_mcap></td>'
html += '</tr>'
fpe_toggle = os.getenv('DISPLAY_FPE_RATIO')
if (fpe_toggle is not None) and (fpe_toggle == '1'):
html += '<tr>'
html += '<td>FPE</td>'
for symb in symb_list:
html += f'<td id={symb}_forwardPE></td>'
html += '</tr>'
pst12_toggle = os.getenv('DISPLAY_PST12_RATIO')
if (pst12_toggle is not None) and (pst12_toggle == '1'):
html += '<tr>'
html += '<td>PST12</td>'
for symb in symb_list:
html += f'<td id={symb}_priceToSalesTrailing12Months></td>'
html += '</tr>'
peg_toggle = os.getenv('DISPLAY_PEG_RATIO')
if (peg_toggle is not None) and (peg_toggle == '1'):
html += '<tr>'
html += '<td>PEG</td>'
for symb in symb_list:
html += f'<td id={symb}_pegRatio></td>'
html += '</tr>'
urls_toggle = os.getenv('OTHER_URLS')
if (urls_toggle is not None) and (urls_toggle == '1'):
html += '<tr>'
html += '<td>URLS</td>'
for symb in symb_list:
html += f'<td><a href=https://finance.yahoo.com/quote/{symb}>YF</a>/<a href=https://gurufocus.com/stock/{symb}>GF</a></td>'
html += '</tr>'
html += '</table>'
html += '</br>'
return html
def gen_html_head(json_update_interval):
html = '<head>'
html += '<title>Stonk Stalker</title>'
if os.getenv('DARKMODE'):
html += '<link rel="stylesheet" href="static/dashboard-dark.css">'
else:
html += '<link rel="stylesheet" href="static/dashboard.css">'
html += '<script src="static/jquery-3.7.0.min.js"></script>'
html += '<script src="static/symbols.js"></script>'
html += '<script>$(document).ready(function(){ time_init(); });</script>'
html += '<script>$(document).ready(function(){ symbol_init(' + json_update_interval + '); });</script>'
html += '</head>'
return html
def gen_html_body():
html = '<body>'
html += '<center>'
html += '<h2>Stonk Stalker</h2>'
html += '<h3><div id="time"></div></h3>'
for tbl in tables_list:
html += gen_html_table(tbl)
html += '<a href="https://github.com/Fullaxx/stonk_stalker">Source Code on GitHub</a>'
html += '</center>'
html += '</body>'
return html
def gen_index_html(tables_list, json_update_interval):
html = '<!DOCTYPE html>'
html += '<html lang="en">'
html += gen_html_head(json_update_interval)
html += gen_html_body()
html += '</html>'
write_to_file(html, 'index.html')
def delete_if_exists(stock_d, key):
if key in stock_d:
del stock_d[key]
def load_market_data(symb_list, marketdb, request_delay):
for symb in symb_list:
symb_data = {}
res = yf.Ticker(symb)
delete_if_exists(res.info, 'companyOfficers')
delete_if_exists(res.info, 'longBusinessSummary')
marketdb[symb] = res.info
market_str = json.dumps(marketdb)
filename = f'market.json'
print(f'Writing {filename} ...')
write_to_file(market_str, filename)
# Amount of time to wait in between yfinance requests
time.sleep(request_delay)
# This function is pretty generic and could use some better logic to determine an active trading session
#def trading_is_active():
# active = False
# tz = timezone('US/Eastern')
# now = datetime.now(tz)
# day = now.strftime("%a")
# hour = int(now.strftime("%H"))
# if (day == 'Sun'): return False
# if (day == 'Sat'): return False
# if ((hour >= 9) and (hour <= 16)):
# active = True
# return active
if __name__ == '__main__':
wwwdir = os.getenv('WWWDIR')
if wwwdir is not None: os.chdir(wwwdir)
ticker_tables = os.getenv('TICKER_TABLES')
if ticker_tables is None: bailmsg('Set TICKER_TABLES')
json_update_interval = os.getenv('JSON_UPDATE')
if json_update_interval is None: json_update_interval = '1000'
sleep_time = 30
if os.getenv('SLEEP_TIME') is not None:
sleep_time = int(os.getenv('SLEEP_TIME'))
symb_list = []
tables_list = ticker_tables.split(';')
for tbl in tables_list:
symbols = tbl.split('=')[1]
symb_list += symbols.split(',')
marketdb = {}
gen_index_html(tables_list, json_update_interval)
while True:
load_market_data(symb_list, marketdb, sleep_time)
# sleep_time = 30 if trading_is_active() else 30
# time.sleep(sleep_time)