-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
605 lines (546 loc) · 22.4 KB
/
app.py
File metadata and controls
605 lines (546 loc) · 22.4 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent / "src"))
import streamlit as st
import pandas as pd
from datetime import datetime, date, timedelta
import os
import requests
from mtfck.db import get_connection, close_connection
from mtfck.mtfck import (
get_next_available_date,
get_prev_available_date,
get_top5_amt_financed,
get_top5_amt_financed_pct_change,
get_newly_added_stocks,
get_top_exposure_stocks,
nse,
calculate_returns,
get_ffmc_and_exposure,
fetch_industry_data,
)
import plotly.graph_objects as go
def download_database():
db_path = "mtf_data/stock_data.parquet"
summary_path = "mtf_data/daily_summary.parquet"
db_url = "https://github.com/eggmasonvalue/MTFDB/raw/main/stock_data.parquet"
summary_url = "https://github.com/eggmasonvalue/MTFDB/raw/main/daily_summary.parquet"
os.makedirs("mtf_data", exist_ok=True)
with st.spinner("Downloading latest database from cloud..."):
try:
close_connection()
r = requests.get(db_url, stream=True)
r.raise_for_status()
with open(db_path, "wb") as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
r_summary = requests.get(summary_url, stream=True)
r_summary.raise_for_status()
with open(summary_path, "wb") as f:
for chunk in r_summary.iter_content(chunk_size=8192):
f.write(chunk)
except Exception as e:
st.error(f"Failed to sync database: {e}")
# Auto-download on first load if missing
if not os.path.exists("mtf_data/stock_data.parquet"):
download_database()
@st.cache_data(ttl=86400)
def get_cached_industry_data():
"""Fetch and cache industry mapping data for 24 hours."""
return fetch_industry_data()
def get_available_dates():
try:
conn = get_connection()
df = conn.sql("SELECT DISTINCT date FROM stock_data ORDER BY date").df()
return pd.to_datetime(df["date"]).dt.strftime("%Y-%m-%d").tolist()
except Exception:
return []
def get_unique_industries(industry_data):
"""Extract unique industries from the JSON dataset and format them for display."""
industries = set()
for _, details in industry_data.items():
if isinstance(details, list) and len(details) > 0:
basic_industry = details[-1]
hierarchy = " > ".join(details[:-1]) if len(details) > 1 else "Unknown"
# Pad with 60 non-breaking spaces to push hierarchy out of view
# Streamlit truncates the view, but keeps it searchable and visible on hover tooltip
padding = chr(160) * 60
formatted_string = f"{basic_industry}{padding}[{hierarchy}]"
industries.add(formatted_string)
return sorted(list(industries))
st.set_page_config(page_title="MTF Analytics Dashboard", layout="wide")
st.markdown(
"""
<h1 style='text-align: center; font-family: "Impact"; font-size: 3em; font-weight: bold; color: blue; margin-bottom: 0.5em; letter-spacing: 0.05em;'>
MTFCK!
</h1>
""",
unsafe_allow_html=True,
)
industry_data = get_cached_industry_data()
# --- Sidebar Controls ---
with st.sidebar:
st.header("Database Sync")
# Check sync status
db_path = "mtf_data/stock_data.parquet"
if os.path.exists(db_path):
mtime = os.path.getmtime(db_path)
last_synced = datetime.fromtimestamp(mtime).strftime('%Y-%m-%d %I:%M %p')
st.caption(f"Last Synced: {last_synced}")
else:
st.caption("Database not downloaded yet.")
sync_clicked = st.button("Sync Database from Cloud", width="stretch")
if sync_clicked:
download_database()
st.rerun()
st.markdown("---")
st.header("Analysis Controls")
dates = get_available_dates()
if dates:
min_date = min(dates)
max_date = max(dates)
else:
min_date = max_date = date.today().strftime("%Y-%m-%d")
from_date = st.date_input(
"From Date",
value=datetime.strptime(str(min_date), "%Y-%m-%d").date(),
key="from_date",
)
to_date = st.date_input(
"To Date", value=datetime.strptime(str(max_date), "%Y-%m-%d").date(), key="to_date"
)
top_n = st.slider(
"Number of Top Stocks", min_value=3, max_value=20, value=5, step=1
)
industries = get_unique_industries(industry_data)
selected_industries = st.multiselect(
"Industry Filter (optional)", industries, default=[]
)
function = st.selectbox(
"Analysis Type",
[
"Top by Amount Financed",
"Top by % Change in Amount Financed",
"Newly Added MTF Stocks",
"Top by Exposure %",
],
)
if function == "Top by Exposure %" and not selected_industries:
st.warning(
"For Exposure % analysis, please choose one or more industry filters to avoid long load time."
)
run_analysis = st.button("Run Analysis", width="stretch")
st.markdown("---")
# --- Trends Section ---
st.header("Trends")
show_net_outstanding_clicked = st.button(
"Show Total Outstanding Trend", key="net_outstanding_btn_sidebar"
)
trend_symbol_input = st.text_input(
"Enter Symbol for Amount Financed Trend", key="trend_symbol_input"
).upper()
show_trend_clicked = st.button(
"Show Amount Financed Trend", key="trend_btn_sidebar"
)
st.markdown(f"**Selected Range:** {from_date} to {to_date}")
# --- Trend Analysis Logic ---
def get_amt_financed_trend(symbol, from_date, to_date):
conn = get_connection()
df = conn.execute(
"SELECT date, amt_financed FROM stock_data WHERE symbol = ? AND date BETWEEN ? AND ? ORDER BY date",
(symbol, from_date, to_date)
).df()
if not df.empty:
df["date"] = pd.to_datetime(df["date"])
df["amt_financed_cr"] = df["amt_financed"] / 100
return df
# --- Analysis Output Section ---
if "last_range" not in st.session_state:
st.session_state["last_range"] = {}
if "results" not in st.session_state:
st.session_state["results"] = {}
def cache_key(function, from_date_db, to_date_db, top_n, selected_industries):
industries_key = (
",".join(sorted(selected_industries)) if selected_industries else "ALL"
)
return f"{function}|{from_date_db}|{to_date_db}|{top_n}|{industries_key}"
from_date_db = get_next_available_date(from_date)
to_date_db = get_prev_available_date(to_date)
if not from_date_db or not to_date_db:
st.warning("No data available for the selected range. Please sync the database.")
st.stop()
if from_date_db and to_date_db:
key = cache_key(function, from_date_db, to_date_db, top_n, selected_industries)
current_range = (
from_date_db,
to_date_db,
top_n,
tuple(sorted(selected_industries)) if selected_industries else (),
)
last_range = st.session_state["last_range"].get(function)
rerun_needed = (last_range != current_range) or (key not in st.session_state["results"])
# --- Always get the latest analysis result for the current controls ---
if run_analysis:
if (
rerun_needed
or "analysis_df" not in st.session_state
or st.session_state.get("analysis_key") != key
):
if function == "Top by Amount Financed":
df = get_top5_amt_financed(
to_date_db, top_n, selected_industries, from_date_db, industry_data
)
df["Amount Financed (₹ Cr)"] = df["amt_financed"] / 100
df["Free Float Market Cap (₹ Cr)"] = (
df["Free Float Market Cap (₹ Lakhs)"] / 100
)
df = df.rename(
columns={
"symbol": "Symbol",
"industry": "Industry",
"Exposure (%)": "Exposure (%)",
}
)
elif function == "Top by % Change in Amount Financed":
df = get_top5_amt_financed_pct_change(
from_date_db, to_date_db, top_n, selected_industries, industry_data
)
df["Amount Financed Start (₹ Cr)"] = df["amt_financed_from"] / 100
df["Amount Financed End (₹ Cr)"] = df["amt_financed_to"] / 100
df["Free Float Market Cap (₹ Cr)"] = (
df["Free Float Market Cap (₹ Lakhs)"] / 100
)
df = df.rename(
columns={
"symbol": "Symbol",
"industry": "Industry",
"pct_change": "% Change",
"Exposure (%)": "Exposure (%)",
}
)
elif function == "Newly Added MTF Stocks":
df = get_newly_added_stocks(from_date_db, to_date_db, selected_industries, industry_data)
df["Amount Financed Start (₹ Cr)"] = df["amt_financed_from"] / 100
df["Amount Financed End (₹ Cr)"] = df["amt_financed_to"] / 100
df["Free Float Market Cap (₹ Cr)"] = (
df["Free Float Market Cap (₹ Lakhs)"] / 100
)
df = df.rename(
columns={
"symbol": "Symbol",
"industry": "Industry",
"Exposure (%)": "Exposure (%)",
}
)
elif function == "Top by Exposure %":
df = get_top_exposure_stocks(to_date_db, top_n, selected_industries, industry_data)
df["Amount Financed (₹ Cr)"] = df["amt_financed"] / 100
df["Free Float Market Cap (₹ Cr)"] = (
df["Free Float Market Cap (₹ Lakhs)"] / 100
)
df = df.rename(
columns={
"symbol": "Symbol",
"industry": "Industry",
"Exposure (%)": "Exposure (%)",
}
)
else:
df = pd.DataFrame()
st.session_state["analysis_df"] = df
st.session_state["analysis_key"] = key
else:
df = st.session_state["analysis_df"]
# --- Dropdown below graph, button below dropdown, then subheader, then table ---
if function == "Top by Amount Financed":
st.subheader(f"Top {top_n} Stocks by Amount Financed on {to_date_db}")
st.dataframe(
df[
[
"Symbol",
"Industry",
"Amount Financed (₹ Cr)",
"Free Float Market Cap (₹ Cr)",
"Exposure (%)",
"1yr Return (%)",
"3yr Return (%) (CAGR)",
"Point-to-Point Return (%)",
]
],
use_container_width=True,
)
elif function == "Top by % Change in Amount Financed":
st.subheader(
f"Top {top_n} Stocks by % Change in Amount Financed ({from_date_db} to {to_date_db})"
)
st.dataframe(
df[
[
"Symbol",
"Industry",
"Amount Financed Start (₹ Cr)",
"Amount Financed End (₹ Cr)",
"% Change",
"Free Float Market Cap (₹ Cr)",
"Exposure (%)",
"1yr Return (%)",
"3yr Return (%) (CAGR)",
"Point-to-Point Return (%)",
]
],
use_container_width=True,
)
elif function == "Newly Added MTF Stocks":
st.subheader(f"Newly Added MTF Stocks ({from_date_db} to {to_date_db})")
st.dataframe(
df[
[
"Symbol",
"Industry",
"Amount Financed Start (₹ Cr)",
"Amount Financed End (₹ Cr)",
"Free Float Market Cap (₹ Cr)",
"Exposure (%)",
"1yr Return (%)",
"3yr Return (%) (CAGR)",
"Point-to-Point Return (%)",
]
],
use_container_width=True,
)
elif function == "Top by Exposure %":
st.subheader(f"Top {top_n} Stocks by Exposure % on {to_date_db}")
st.dataframe(
df[
[
"Symbol",
"Industry",
"Amount Financed (₹ Cr)",
"Free Float Market Cap (₹ Cr)",
"Exposure (%)",
]
],
use_container_width=True,
)
# --- Trend Analysis Display ---
if show_trend_clicked:
trend_df = get_amt_financed_trend(trend_symbol_input, from_date_db, to_date_db)
if not trend_df.empty:
ptp_return, one_year_return, three_year_cagr = None, None, None
ffmc_lakhs, exposure_pct = None, None
try:
ptp_return, one_year_return, three_year_cagr = calculate_returns(
trend_symbol_input, to_date_db, from_date_db
)
if not trend_df.empty:
amt_field = "amt_financed"
last_row = trend_df.iloc[-1]
ffmc_lakhs, exposure_pct = get_ffmc_and_exposure(
{"symbol": trend_symbol_input, amt_field: last_row[amt_field]},
amt_field,
)
except Exception:
ptp_return, one_year_return, three_year_cagr = None, None, None
ffmc_lakhs, exposure_pct = None, None
ffmc_cr = ffmc_lakhs / 100 if ffmc_lakhs is not None else None
st.markdown(
f"<div style='font-weight:bold;font-size:1.5em'>{trend_symbol_input}</div>",
unsafe_allow_html=True,
)
col1, col2, col3, col4, col5 = st.columns(5)
col1.markdown(
f"<b>Free Float Market Cap:</b><br>{ffmc_cr:.2f} Cr"
if ffmc_cr is not None
else "<b>Free Float Market Cap:</b><br>N/A",
unsafe_allow_html=True,
)
col2.markdown(
f"<b>Exposure (%):</b><br>{exposure_pct:.2f}%"
if exposure_pct is not None
else "<b>Exposure (%):</b><br>N/A",
unsafe_allow_html=True,
)
col3.markdown(
f"<b>P2P Return:</b><br>{ptp_return:.2f}%"
if ptp_return is not None
else "<b>P2P Return:</b><br>N/A",
unsafe_allow_html=True,
)
col4.markdown(
f"<b>1yr Return:</b><br>{one_year_return:.2f}%"
if one_year_return is not None
else "<b>1yr Return:</b><br>N/A",
unsafe_allow_html=True,
)
col5.markdown(
f"<b>3yr CAGR:</b><br>{three_year_cagr:.2f}%"
if three_year_cagr is not None
else "<b>3yr CAGR:</b><br>N/A",
unsafe_allow_html=True,
)
trend_df["pct_change"] = trend_df["amt_financed_cr"].pct_change() * 100
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=trend_df["date"],
y=trend_df["amt_financed_cr"],
mode="lines+markers",
name="Amount Financed (₹ Cr)",
line=dict(color="blue", width=2),
hovertemplate="<b>Date:</b> %{x}<br><b>Amount Financed:</b> ₹%{y:.2f} Cr<br><b>% Change:</b> %{customdata:.2f}%",
customdata=trend_df["pct_change"],
)
)
price_data = []
curr_from = pd.to_datetime(from_date_db).date()
end_to = pd.to_datetime(to_date_db).date()
while curr_from <= end_to:
curr_to = min(curr_from + timedelta(days=90), end_to)
try:
chunk = nse.fetch_equity_historical_data(
trend_symbol_input,
from_date=curr_from,
to_date=curr_to,
)
if isinstance(chunk, list):
price_data.extend(chunk)
except Exception:
pass
curr_from = curr_to + timedelta(days=1)
if price_data:
price_df = pd.DataFrame(price_data)
timestamp_col = "mtimestamp" if "mtimestamp" in price_df.columns else "CH_TIMESTAMP"
closing_col = "chClosingPrice" if "chClosingPrice" in price_df.columns else "CH_CLOSING_PRICE"
if timestamp_col in price_df.columns and closing_col in price_df.columns:
price_df["date"] = pd.to_datetime(
price_df[timestamp_col], errors="coerce"
)
price_df = price_df.sort_values("date")
price_df["pct_change"] = price_df[closing_col].pct_change() * 100
fig.add_trace(
go.Scatter(
x=price_df["date"],
y=price_df[closing_col],
mode="lines+markers",
name="Closing Price",
line=dict(color="orange", width=2, dash="dot"),
yaxis="y2",
hovertemplate="<b>Date:</b> %{x}<br><b>Closing Price:</b> ₹%{y:.2f}<br><b>% Change:</b> %{customdata:.2f}%",
customdata=price_df["pct_change"],
)
)
fig.update_layout(
yaxis2=dict(
title="Closing Price (₹)",
overlaying="y",
side="right",
showgrid=False,
)
)
fig.update_layout(
title=f"Amount Financed Trend for {trend_symbol_input}",
xaxis_title="Date",
yaxis_title="Amount Financed (₹ Cr)",
xaxis_range=[trend_df["date"].min(), trend_df["date"].max()],
yaxis_range=[
trend_df["amt_financed_cr"].min(),
trend_df["amt_financed_cr"].max(),
],
width=800,
height=400,
hovermode="x unified",
)
st.plotly_chart(fig, use_container_width=True)
else:
st.subheader(
f"No trend data available for {trend_symbol_input} in the selected range."
)
# --- Net Outstanding Trend Display ---
if show_net_outstanding_clicked:
conn = get_connection()
df_chart = conn.execute(
"SELECT date, net_outstanding_end FROM daily_summary WHERE date BETWEEN ? AND ? ORDER BY date",
(from_date_db, to_date_db)
).df()
if not df_chart.empty:
df_chart["date"] = pd.to_datetime(df_chart["date"])
df_chart["net_outstanding_end_cr"] = df_chart["net_outstanding_end"] / 100
df_chart["pct_change"] = df_chart["net_outstanding_end_cr"].pct_change() * 100
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=df_chart["date"],
y=df_chart["net_outstanding_end_cr"],
mode="lines+markers",
name="Net Outstanding End (₹ Cr)",
line=dict(color="green", width=2),
hovertemplate="<b>Date:</b> %{x}<br><b>Net Outstanding:</b> ₹%{y:.2f} Cr<br><b>% Change:</b> %{customdata:.2f}%",
customdata=df_chart["pct_change"],
)
)
price_list = []
try:
curr_from = pd.to_datetime(df_chart["date"].min()).date()
end_to = pd.to_datetime(df_chart["date"].max()).date()
while curr_from <= end_to:
curr_to = min(curr_from + timedelta(days=90), end_to)
chunk = nse.fetch_historical_index_data(
index="NIFTY TOTAL MARKET",
from_date=curr_from,
to_date=curr_to,
)
if isinstance(chunk, list):
price_list.extend(chunk)
curr_from = curr_to + timedelta(days=1)
except Exception as e:
print(f"Error fetching index data: {e}")
price_df = pd.DataFrame(price_list)
if not price_df.empty and "EOD_CLOSE_INDEX_VAL" in price_df.columns:
timestamp_col = (
"EOD_TIMESTAMP" if "EOD_TIMESTAMP" in price_df.columns else "mTIMESTAMP"
)
if timestamp_col in price_df.columns:
price_df["date"] = pd.to_datetime(
price_df[timestamp_col], format="%d-%b-%Y", errors="coerce"
)
price_df = price_df.sort_values("date")
price_df["pct_change"] = (
price_df["EOD_CLOSE_INDEX_VAL"].pct_change() * 100
)
fig.add_trace(
go.Scatter(
x=price_df["date"],
y=price_df["EOD_CLOSE_INDEX_VAL"],
mode="lines+markers",
name="NIFTY TOTAL MARKET",
line=dict(color="blue", width=2, dash="dot"),
yaxis="y2",
hovertemplate="<b>Date:</b> %{x}<br><b>Index Close:</b> %{y:.2f}<br><b>% Change:</b> %{customdata:.2f}%",
customdata=price_df["pct_change"],
)
)
fig.update_layout(
yaxis2=dict(
title="NIFTY TOTAL MARKET Close",
overlaying="y",
side="right",
showgrid=False,
)
)
fig.update_layout(
title="Net Outstanding End (Daily Trend)",
xaxis_title="Date",
yaxis_title="Net Outstanding End (₹ Cr)",
xaxis_range=[df_chart["date"].min(), df_chart["date"].max()],
yaxis_range=[
df_chart["net_outstanding_end_cr"].min(),
df_chart["net_outstanding_end_cr"].max(),
],
width=800,
height=400,
hovermode="x unified",
)
st.plotly_chart(fig, use_container_width=True)
else:
st.info("No summary data found.")
st.caption("NSE MTF Analytics Dashboard")