-
Notifications
You must be signed in to change notification settings - Fork 0
/
as_of_now.py
323 lines (263 loc) · 8.9 KB
/
as_of_now.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
311
312
313
314
315
316
317
318
319
320
321
322
323
import os
import json
import requests
from copy import deepcopy
from flask import Flask, render_template, redirect
from datetime import datetime
from credentials import demat, dp_id, client_id, username, password
root = "https://webbackend.cdsc.com.np/api"
wacc_url = "/myPurchase/waccReport/"
portfolio_url = "/meroShareView/myPortfolio/"
login_url = "/meroShare/auth/"
_annotation_file = "scrip_annotation.json"
_token_file = "auth_token.txt"
_headers = {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "en-US,en;q=0.9,ne-NP;q=0.8,ne;q=0.7",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Type": "application/json",
"Origin": "https://meroshare.cdsc.com.np",
"Pragma": "no-cache",
"Referer": "https://meroshare.cdsc.com.np/",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-site",
"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Mobile Safari/537.36",
"sec-ch-ua": '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
"sec-ch-ua-mobile": "?1",
"sec-ch-ua-platform": '"Android"',
}
print_keys = [
"scrip",
"scrip_desc",
"qty",
"annotate_ipo",
"cost_per_unit",
"cost",
"ltp_closing",
"ltp",
"eps_closing",
"eps_ltp",
"gain_closing",
"gain_ltp",
"gain_closing_percent",
"gain_ltp_percent",
"_",
"scrip",
"trend_gain",
"annotate_target_price",
"closing_target_gain",
"ltp_target_gain",
]
annotate_keys = ["ipo", "target_price"]
numeric_keys = [
x
for x in print_keys
if x not in ["scrip", "scrip_desc", "_", "annotate_ipo", "annotate_target_price"]
]
try:
_auth_token = open(_token_file, "r").read() if _token_file else None
except Exception as exc:
open(_token_file, "w+").write("")
_auth_token = None
from requests import session
this_session = session()
afile_obj = open(_annotation_file, "r") if os.path.exists(_annotation_file) else None
annotation_data = json.load(afile_obj) if afile_obj else {}
def annotate(scrip):
global annotation_data
return annotation_data.get(demat, {}).get(scrip.lower(), {})
def login(force=False):
global _auth_token
if _auth_token and not force:
return _auth_token
url = root + login_url
login_data = {"clientId": client_id, "username": username, "password": password}
response = this_session.post(url, headers=_headers, json=login_data)
resp_headers = response.headers
if response.status_code != 200:
raise Exception("Login Failed")
_auth_token = resp_headers.get("Authorization")
open(_token_file, "w+").write(_auth_token)
return _auth_token
def logged_in_headers():
auth_token = login()
headers = deepcopy(_headers)
headers.update({"Authorization": auth_token})
return headers
def get_wacc():
url = root + wacc_url
response = this_session.post(
url, headers=logged_in_headers(), json={"demat": demat}
)
return response.json()
def get_portfolio():
url = root + portfolio_url
response = this_session.post(
url,
headers=logged_in_headers(),
json={
"sortBy": "script",
"demat": [demat],
"clientCode": dp_id,
"page": 1,
"size": 200,
"sortAsc": True,
},
)
return response.json()
def process_annotation(akey, aval):
if akey == "ipo":
return "IPO" if aval == True else "SEC"
elif akey == "target_price":
return float(aval) if aval else None
return aval # default
def compare():
wacc = get_wacc()
portfolio = get_portfolio()
wacc_key_maps = {
"scrip": "scrip",
"totalQuantity": "qty",
"averageBuyRate": "cost_per_unit",
"totalCost": "cost",
"lastModifiedDate": "last_modified",
"demat": "demat",
}
wacc_summary = {}
for w in wacc.get("waccReportResponse", []):
for key, value in wacc_key_maps.items():
w[value] = w.pop(key)
_scrip = w["scrip"]
_demat = w["demat"]
wacc_summary[f"{_demat}_{_scrip}"] = deepcopy(w)
_annotations = annotate(_scrip)
for akey in annotate_keys:
wacc_summary[f"{_demat}_{_scrip}"][f"annotate_{akey}"] = process_annotation(
akey, _annotations.get(akey)
)
portfolio_key_maps = {
"currentBalance": "qty",
"lastTransactionPrice": "ltp",
"previousClosingPrice": "ltp_closing",
"script": "scrip",
"scriptDesc": "scrip_desc",
}
for pp in portfolio.get("meroShareMyPortfolio", []):
for key, value in portfolio_key_maps.items():
pp[value] = pp.pop(key)
_scrip = pp["scrip"]
portfolio_key = f"{demat}_{_scrip}"
portfolio_cost_per_unit = wacc_summary[portfolio_key]["cost_per_unit"]
portfolio_qty = wacc_summary[portfolio_key]["qty"]
eps_closing = float(pp["ltp_closing"]) - float(portfolio_cost_per_unit)
eps_ltp = float(pp["ltp"]) - float(portfolio_cost_per_unit)
gain_closing = eps_closing * float(portfolio_qty)
gain_ltp = eps_ltp * float(portfolio_qty)
wacc_summary[portfolio_key].update(
{
"ltp": pp["ltp"],
"ltp_closing": pp["ltp_closing"],
"scrip_desc": pp["scrip_desc"],
"eps_closing": eps_closing,
"eps_ltp": eps_ltp,
"gain_closing": gain_closing,
"gain_ltp": gain_ltp,
"gain_closing_percent": ((eps_closing / portfolio_cost_per_unit) * 100),
"gain_ltp_percent": ((eps_ltp / portfolio_cost_per_unit) * 100),
"trend_gain": eps_ltp - eps_closing,
"css_class": "",
}
)
if "annotate_target_price" in wacc_summary[portfolio_key]:
target_price = wacc_summary[portfolio_key]["annotate_target_price"]
try:
closing_target_gain = float(pp["ltp_closing"]) - target_price
ltp_target_gain = float(pp["ltp"]) - target_price
wacc_summary[portfolio_key]["closing_target_gain"] = closing_target_gain
wacc_summary[portfolio_key]["ltp_target_gain"] = ltp_target_gain
wacc_summary[portfolio_key][
"css_class"
] = f" target-{'gain' if ltp_target_gain > 0 else 'loss'}"
except TypeError:
wacc_summary[portfolio_key]["closing_target_gain"] = None
wacc_summary[portfolio_key]["ltp_target_gain"] = None
for key, value in wacc_summary.items():
for k in numeric_keys:
if k == "qty":
continue
try:
wacc_summary[key][k] = float(wacc_summary[key][k]).__round__(2)
except TypeError:
pass
except KeyError:
pass
return {
"portfolio": wacc_summary,
"summary": {
"investment": sum([x["cost"] for x in wacc_summary.values()]),
"cost": sum(
[
x["cost"]
for x in filter(
lambda x: x.get("scrip_desc") is not None, wacc_summary.values()
)
]
),
"ltp": sum([x.get("gain_ltp", 0) for x in wacc_summary.values()]),
"ltp_closing": sum(
[x.get("gain_closing", 0) for x in wacc_summary.values()]
),
},
}
app = Flask(__name__)
@app.route("/")
def compare_html():
comparision = compare()
nowtime = datetime.now()
if not comparision:
# reset login tokens
open(_token_file, "w").write("")
_auth_token = None
return render_template(
"/portfolio.html",
portfolio=comparision["portfolio"],
summary=comparision["summary"],
keys=print_keys,
now=nowtime,
)
@app.route("/json")
def compare_json():
comparision = compare()
nowtime = datetime.now()
if not comparision:
# reset login tokens
open(_token_file, "w").write("")
_auth_token = None
return {
"portfolio": comparision["portfolio"],
"summary": comparision["summary"],
"keys": print_keys,
"now": nowtime,
}
@app.route("/reset")
def reset_token():
try:
login(force=True)
except Exception as exc:
print(exc)
return "Unable to reset login. Please refresh and retry in a while."
logged_in_headers()
return redirect("/")
if __name__ == "__main__":
app.run(port=5001, debug=True)
"""
52 weeks average
Turnover
Symbol Turnover LTP % Change High Low Open Qty
position in the market
AGM DIVIDEND RIGHT
https://merolagani.com/AnnouncementSummary.aspx?actType=AGM&fiscalYear=
https://merolagani.com/MarketSummary.aspx?type=turnovers
and transactions
"""