-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
428 lines (325 loc) · 12.1 KB
/
Copy pathapp.py
File metadata and controls
428 lines (325 loc) · 12.1 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import plotly.graph_objects as go
import plotly.express as px
import streamlit as st
import pandas as pd
import urllib.parse
import pandas as pd
import requests
import json
# Settings:
## Extra CSS:
st.set_page_config(page_title='Upstox Portfolio Viewer', page_icon=':bar_chart:', layout='wide')
hide_st_style = '''
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
header {visibility: hidden;}
</style>
'''
st.markdown(hide_st_style, unsafe_allow_html=True)
# @st.cache_data
# def load_instruments():
# data = pd.read_csv('NSE.csv')
# return data
def connect():
conf = get_details()
redirect_url = urllib.parse.quote(conf['rurl'], safe='')
uri = f"https://api-v2.upstox.com/login/authorization/dialog?response_type=code&client_id={conf['apiKey']}&redirect_uri={redirect_url}"
st.markdown(f'[Authorize with Upstox]({uri})')
def login(code):
conf = get_details()
conf['code'] = code
store_details(conf)
conf = get_details()
url = "https://api-v2.upstox.com/login/authorization/token"
headers = {
"accept": "application/json",
"Api-Version": "2.0",
"Content-Type": "application/x-www-form-urlencoded",
}
data = {
"code": conf['code'],
"client_id": conf['apiKey'],
"client_secret": conf['secretKey'],
"redirect_uri": conf['rurl'],
"grant_type": "authorization_code",
}
response = requests.post(url, headers=headers, data=data)
json_response = response.json()
try:
access_token = json_response['access_token']
conf['access_token'] = access_token
except Exception:
pass
# uri = f"https://upstoxapi.streamlit.app"
# st.markdown(f'[Something went wrong!!! Please restart the app]({uri})')
# st.stop()
store_details(conf)
def get_details():
with open('config.json') as f:
data = json.load(f)
return data
def store_details(conf):
with open('config.json', 'w') as f:
json.dump(conf, f)
def get_profile():
conf = get_details()
url = 'https://api-v2.upstox.com/user/profile'
headers = {
"accept": "application/json",
"Api-Version": "2.0",
"Authorization": f"Bearer {conf['access_token']}",
}
response = requests.get(url, headers=headers)
json_response = response.json()
return json_response
def pnl(data):
net_pnl = 0
initial = sum([avg_price['average_price']*avg_price['quantity'] for avg_price in data])
for pnl in data:
net_pnl += pnl['pnl']
current = initial + net_pnl
per = (current - initial) / initial
st.metric(label=f"NET PNL of Rs. {initial:.2f} /-", value=f"{current:.2f} /-", delta=f"{per*100:.2f}%")
def plot_pnl(data):
labels = [name['company_name'] for name in data]
values = [price['pnl'] for price in data]
df1 = pd.DataFrame(list(zip(labels, values)), columns=['Companies -->', 'PNLs -->'])
df1.sort_values(by=['PNLs -->'], inplace=True)
fig1 = px.bar(
df1,
x = 'PNLs -->',
y = 'Companies -->',
orientation="h",
title="<b>PNL per Company:</b>",
)
fig1.update_layout(
plot_bgcolor="rgba(0,0,0,0)",
xaxis=(dict(showgrid=True))
)
st.plotly_chart(fig1, use_container_width=True)
st.markdown(f'Net PNL: {sum(values):.2f} /-')
def get_holdings():
conf = get_details()
url = 'https://api-v2.upstox.com/portfolio/long-term-holdings'
headers = {
"accept": "application/json",
"Api-Version": "2.0",
"Authorization": f"Bearer {conf['access_token']}",
}
response = requests.get(url, headers=headers)
json_response = response.json()
return json_response
def get_investments_plot_by_price(data):
labels = [name['company_name'] for name in data]
values = [avg_price['average_price']*avg_price['quantity'] for avg_price in data]
qts = [qt['quantity'] for qt in data]
fig1 = go.Figure(data=[go.Pie(labels=labels, values=values, hole=.3)])
fig1.update_layout(plot_bgcolor='rgba(0,0,0,0)', title="<b>Investments per Company by Price Weightage:</b>")
df1 = pd.DataFrame(list(zip(labels, values)), columns=['Companies -->', 'Amounts -->'])
df1.sort_values(by=['Amounts -->'], inplace=True)
fig2 = px.bar(
df1,
x = 'Amounts -->',
y = 'Companies -->',
orientation="h",
title="<b>Investments per Company by Amount:</b>",
)
fig2.update_layout(
plot_bgcolor="rgba(0,0,0,0)",
xaxis=(dict(showgrid=True))
)
df2 = pd.DataFrame(list(zip(labels, qts)), columns=['Companies -->', 'Quantity -->'])
df2.sort_values(by=['Quantity -->'], inplace=True)
fig3 = px.bar(
df2,
x = 'Quantity -->',
y = 'Companies -->',
orientation="h",
title="<b>Shares per Company by Quantity:</b>",
)
fig3.update_layout(
plot_bgcolor="rgba(0,0,0,0)",
xaxis=(dict(showgrid=True))
)
st.subheader('Distribution by Invested Amount')
l, r = st.columns(2)
with l:
st.plotly_chart(fig1, use_container_width=True)
with r:
st.plotly_chart(fig3, use_container_width=True)
st.plotly_chart(fig2, use_container_width=True)
st.markdown(f'Total Amount Invested: {sum(values):.2f} /-')
def get_sell_charges(ins_token, quan, price, prod, temp=None):
conf = get_details()
url = 'https://api-v2.upstox.com/charges/brokerage'
headers = {
"accept": "application/json",
"Api-Version": "2.0",
"Authorization": f"Bearer {conf['access_token']}",
}
params = {
"instrument_token": ins_token,
"quantity": quan,
"product": prod,
"transaction_type": "SELL",
"price": price
}
response = requests.get(url, headers=headers, params=params)
json_response = response.json()
dp_charge = 18.5
grace = 20
return json_response['data']['charges']['total'] + dp_charge + grace
def get_all_sell_estimates(data):
labels = [name['company_name'] for name in data]
ins_tokens = [tok['instrument_token'] for tok in data]
qts = [qt['quantity'] for qt in data]
ltp = [lt['last_price'] for lt in data]
byp = [bp['average_price'] for bp in data]
prod = [p['product'] for p in data]
ttax = 0
for l, a, b, c, p, bp in zip(labels, ins_tokens, qts, ltp, prod, byp):
sell_charge = get_sell_charges(a, b, c, p, l)
ttax += sell_charge
net_pnl = 0
initial = sum([avg_price['average_price']*avg_price['quantity'] for avg_price in data])
for pnl in data:
net_pnl += pnl['pnl']
current = initial + net_pnl - ttax
per = (current - initial) / initial
st.write(f"NET PNL (with other charges {ttax}) of Rs. {initial:.2f}/- is: {(current - initial):.2f}/- (PORTFOLIO VALUE: {current:.2f} | {per*100:.2f}%)")
# def get_sell_charges(ins_token, quan, price):
# conf = get_details()
# url = 'https://api-v2.upstox.com/charges/brokerage'
# headers = {
# "accept": "application/json",
# "Api-Version": "2.0",
# "Authorization": f"Bearer {conf['access_token']}",
# }
# params = {
# "instrument_token": ins_token,
# "quantity": quan,
# "product": "D",
# "transaction_type": "SELL",
# "price": price
# }
# response = requests.get(url, headers=headers, params=params)
# json_response = response.json()
# st.write(json_response)
# return json_response['data']['charges']['total']
# def get_all_sell_estimates(data):
# labels = [name['company_name'] for name in data]
# ins_tokens = [tok['instrument_token'] for tok in data]
# qts = [qt['quantity'] for qt in data]
# ltp = [lt['last_price'] for lt in data]
# charges = []
# for a, b, c in zip(ins_tokens, qts, ltp):
# charges.append(get_sell_charges(a, b, c))
# st.write(labels)
# st.write(ltp)
# st.write(charges)
def get_ltps(symbs):
to_fetch = ','.join(symbs)
conf = get_details()
url = 'https://api-v2.upstox.com/market-quote/ltp'
headers = {
"accept": "application/json",
"Api-Version": "2.0",
"Authorization": f"Bearer {conf['access_token']}",
}
params = {
"symbol": to_fetch
}
response = requests.get(url, headers=headers, params=params)
json_response = response.json()
return json_response
def get_wannabe_investments_plot_by_price(data, symbs, quantity):
# labels = [name['company_name'] for name in data]
# values = [price['last_price']*(quantity - price['quantity']) if (price['last_price']*(quantity - price['quantity'])) > 0 else 0 for price in data]
# qts = [quantity - qt['quantity'] if (quantity - qt['quantity']) > 0 else 0 for qt in data]
labels = []
values = []
qts = []
for name in data:
if name['company_name'] in symbs:
labels.append(name['company_name'])
values.append(name['last_price']*(quantity - name['quantity']) if (name['last_price']*(quantity - name['quantity'])) > 0 else 0)
qts.append(quantity - name['quantity'] if (quantity - name['quantity']) > 0 else 0)
extra_labels = set(symbs) - set(labels)
if extra_labels:
ltps = get_ltps(extra_labels)
# st.write(ltps)
for k, v in ltps['data']:
if k in extra_labels:
labels.append(k)
values.append(quantity*v['last_price'])
fig1 = go.Figure(data=[go.Pie(labels=labels, values=values, hole=.3)])
fig1.update_layout(plot_bgcolor='rgba(0,0,0,0)', title="<b>Investments per Company by Price Weightage Required:</b>")
df1 = pd.DataFrame(list(zip(labels, values)), columns=['Companies -->', 'Amounts -->'])
df1.sort_values(by=['Amounts -->'], inplace=True)
fig2 = px.bar(
df1,
x = 'Amounts -->',
y = 'Companies -->',
orientation="h",
title="<b>Investments per Company by Amount Required:</b>",
)
fig2.update_layout(
plot_bgcolor="rgba(0,0,0,0)",
xaxis=(dict(showgrid=True))
)
df2 = pd.DataFrame(list(zip(labels, qts)), columns=['Companies -->', 'Quantity -->'])
df2.sort_values(by=['Quantity -->'], inplace=True)
fig3 = px.bar(
df2,
x = 'Quantity -->',
y = 'Companies -->',
orientation="h",
title="<b>Shares per Company by Quantity Required:</b>",
)
fig3.update_layout(
plot_bgcolor="rgba(0,0,0,0)",
xaxis=(dict(showgrid=True))
)
st.subheader('Distribution by Amount Required')
l, r = st.columns(2)
with r:
st.plotly_chart(fig1, use_container_width=True)
with l:
st.plotly_chart(fig3, use_container_width=True)
st.plotly_chart(fig2, use_container_width=True)
st.markdown(f'Total Amount Required: {sum(values):.2f} /-')
response = st.experimental_get_query_params()
if 'code' in response:
st.sidebar.markdown('In case of any errors: [restart-app](https://upstoxapi.streamlit.app)')
login(response['code'][0])
st.success('Login Successfull!')
# ins_data = load_instruments()
data = get_holdings()
# st.write(data)
profile = get_profile()
# st.write(profile)
st.header(f"Welcome {profile['data']['user_name']}")
pnl(data['data'])
plot_pnl(data['data'])
st.markdown('##')
with st.expander('Show Holdings'):
get_investments_plot_by_price(data['data'])
st.markdown('##')
# get_all_sell_estimates(data['data'])
# with st.expander('Show Goals'):
st.subheader('Set Goals Here:')
# ops = list(ins_data['tradingsymbol'].unique())
# ops.extend([name['company_name'] for name in data['data']])
symbs = st.multiselect(
'Select The Appropriate Symbols:',
options= [name['company_name'] for name in data['data']],
default= [name['company_name'] for name in data['data']]
)
quantity = st.slider('Quantity(s) [ALL]:', 1, 100, value=10, step=1)
get_wannabe_investments_plot_by_price(data['data'], symbs, quantity)
st.markdown('##')
get_all_sell_estimates(data['data'])
st.markdown('##')
else:
connect()