-
Notifications
You must be signed in to change notification settings - Fork 0
/
lus.py
310 lines (264 loc) · 9.71 KB
/
lus.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import logging
import re
import requests
import camelot
import datetime
import sys
from bs4 import BeautifulSoup
from os import path
from prisma import Prisma
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
def _get_link_to_handelsuniversum_pdf():
"""
Returns the link to the handelsuniversum pdf
The handelsuniversum pdf contains all assets which are tradable on a given date
:return: https://www.ls-x.de/link/to/todays/handelsuniversum.pdf"
"""
logging.info("Getting link to handelsuniversum pdf")
response = requests.get('https://www.ls-x.de/de/handelsuniversum')
soup = BeautifulSoup(response.content, 'html.parser')
link_to_handelsuniversum_pdf = soup.select_one(
"#page_content > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) "
"> a:nth-child(1)")
# logging.info(f"Got PDF Link: https://www.ls-x.de{link_to_handelsuniversum_pdf['href']}")
return f"https://www.ls-x.de{link_to_handelsuniversum_pdf['href']}"
def _download_handelsuniversum_pdf(link_to_pdf):
"""
Parses the handelsuniversum pdf and returns a list of all assets which are tradable on a given date
:param link_to_pdf: full link to the pdf
:return:
"""
logging.info(f"Downloading handelsuniversum pdf from {link_to_pdf}")
filename = re.search(r'\w+\.pdf$', link_to_pdf).group()
file = requests.get(link_to_pdf)
logging.info(f"Saving handelsuniversum pdf to {path.join('handelsuniversum', filename)}")
open(path.join('handelsuniversum', filename), 'wb').write(file.content)
return filename
def _parse_handelsuniversum_pdf(filename, pages='all'):
"""
:param filename: to pdf file
:return: tables
"""
logging.info(f"Parsing {filename}. Pages: {pages}. This will take a few minutes...")
tables = camelot.read_pdf(path.join('handelsuniversum', filename), pages=pages,
flavor='stream',
columns=["76.60377358490567,171.44654088050314,322.8301886792453,401.25786163522014,470.5660377358491,621.9496855345913,802.5157232704403,891.8867924528303,1061.5094339622642"])
# for _ in tables:
# print(_.df)
normalized_tables = _normalize_tables(tables)
return normalized_tables
def _normalize_tables(tables):
"""
- runs vanity check on rows
- renames columns
- removes "null" strings and replaces with None
:param tables:
:return:
"""
for table in tables:
# rename columns from 1, 2, 3 etc
table.df.columns = ['wkn', 'isin', 'name', "shortcode", "type", "index_affiliation", "market", "end_date", "minimum_trading_unit", "denomination"]
# remove all rows where ISIN is not valid
isin_regex = re.compile(r'[A-Z]{2}\w{10}$')
table.df = table.df[table.df['isin'].str.contains(isin_regex)]
# replace , and . with empty string
table.df['minimum_trading_unit'] = table.df['minimum_trading_unit'].str.replace('[\,\.]', '', regex=True)
table.df['minimum_trading_unit'] = table.df['minimum_trading_unit'].str.replace('[\,\.]', '', regex=True)
table.df['minimum_trading_unit'] = table.df['minimum_trading_unit'].replace('null', None)
# table.df["end_date"] = table.df["end_date"].apply(lambda x: datetime.datetime.strptime(x, '%d.%m.%Y'))
return tables
def _resolve_isin_to_instrument_id(isin):
"""
:param isin:
:return:
"""
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'en,en-GB;q=0.5',
# 'Accept-Encoding': 'gzip, deflate, br',
'X-Requested-With': 'XMLHttpRequest',
'DNT': '1',
'Connection': 'keep-alive',
'Referer': 'https://www.ls-x.de/de/etf/1428954',
# Requests sorts cookies= alphabetically
# 'Cookie': 'disclaimer=2015040809; baukasten=l9cn3j6584u95ti28heokrn803',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
}
params = {
'q': isin,
'localeId': '2',
}
response = requests.get('https://www.ls-x.de/_rpc/json/.lstc/instrument/search/main', params=params,
headers=headers)
instrument_id_and_category_id = (response.json()[0]["instrumentId"], response.json()[0]["categoryid"])
return instrument_id_and_category_id
def _synchronize_database_with_handelsuniversum():
"""
Synchronizes the instruments in the database
:return: Number of instruments added
"""
# get url, download the file and parse the file
url = _get_link_to_handelsuniversum_pdf()
filename = _download_handelsuniversum_pdf(url)
tables = _parse_handelsuniversum_pdf(filename, pages='all')
# connect to db
db = Prisma()
db.connect()
# synchronize db
for index, table in enumerate(tables):
logging.info(f"{index}/{len(tables)}")
# create_many is not available for SQLite, therefore we iterate through the data frame and create one by one
for index, row in table.df.iterrows():
logging.info("Creating type if not exists: " + row["type"])
res = db.type.upsert(
where={
"type": row["type"]
},
data={
"create": {
"type": row["type"],
},
"update": {
}
}
)
db.instrument.upsert(
where={
"isin": row["isin"]
},
data={
"create": {
"isin": row["isin"],
"wkn": row["wkn"],
"name": row["name"],
"shortcode": row["shortcode"],
"type": row["type"],
"index_affiliation": row["index_affiliation"],
"market": row["market"],
},
"update": {
"wkn": row["wkn"],
"name": row["name"],
"shortcode": row["shortcode"],
"type": row["type"],
"index_affiliation": row["index_affiliation"],
"market": row["market"],
}
}
)
logging.info("Creating/updating instrument: " + row["name"])
db.lastupdatehandelsuniversum.upsert(
where={
"id": 1
},
data={
"create": {
"id": 1,
"filename": filename,
},
"update": {
"filename": filename,
}
}
)
db.disconnect()
def _get_instrument_ids_for_lightstreamer():
"""
Adds all missing instrument ids to the database
:return:
"""
# connect to db
db = Prisma()
db.connect()
# get all ISINs where instrument_id is null
instruments = db.instrument.find_many(
where={ "instrument_id": None },
)
# print(instruments)
# get all instrument ids
for instrument in instruments:
logging.info(f"{instrument.isin}")
instrument_id, market_id = _resolve_isin_to_instrument_id(instrument.isin) # object is not subscripable
db.instrument.update(
where={
"isin": instrument.isin
},
data={
"instrument_id": instrument_id,
"market_id": market_id,
}
)
db.lastupdateinstrumentids.upsert(
where={
"id": 1
},
data={
"create": {
"id": 1,
"last_update": datetime.datetime.now()
},
"update": {
"last_update": datetime.datetime.now()
}
}
)
db.disconnect()
def update_check():
"""
Checks if the database is up to date
:return:
"""
link_to_pdf = _get_link_to_handelsuniversum_pdf()
filename = re.search(r'\w+\.pdf$', link_to_pdf).group()
# connect to db
db = Prisma()
db.connect()
# check last update instrument ids
res = db.lastupdateinstrumentids.find_first(
where={
"id": 1
}
)
should_update = False
if res is None:
logging.info("Instrument IDs: No last update for instrument ids found")
should_update = True
else:
# res can be null
logging.info(f"Instrument IDs: Last update: {res.last_update.date()} | Today: {datetime.date.today()}")
if res.last_update.date() < datetime.date.today():
logging.info("Last update is older than today... Will update")
should_update = True
# ---
# check last update handelsuniversum
res = db.lastupdatehandelsuniversum.find_first(
where={
"id": 1
}
)
if res is None:
logging.info("Handelsuniversum: No last update for handelsuniversum found")
should_update = True
else:
# res can be null
logging.info(f"Handelsuniversum: Last filename: {res.filename} | Today: {filename}")
if res.filename != filename:
logging.info("Handelsuniversum: Last file is older than today's file... Will update")
should_update = True
# ---
# perform update if necessary
db.disconnect()
if should_update:
update()
def update():
"""
Updates the database
:return:
"""
_synchronize_database_with_handelsuniversum()
_get_instrument_ids_for_lightstreamer()