backend / index.py
eshan6704's picture
Update index.py
e11cd25 verified
raw
history blame
2.16 kB
import io
import requests
import pandas as pd
import yfinance as yf
from datetime import datetime, timedelta
from common import html_card, wrap_html
from ta_indi_pat import talib_df
import datetime
date = datetime.date(2025, 11, 27) # Trying a past date where data is likely available
df = nse_preopen_df("NIFTY")
df_bhav, act_date = fetch_bhavcopy_df(date)
df_ce, df_pe = fetch_option_chain_df("NIFTY")
df_m, df_a, df_meta, df_data = nse_index_df("NIFTY 50")
fno = nse_fno_df("RELIANCE")
def fetch_index(max_rows=200):
"""
Fetch NIFTY 50 (^NSEI) 1-year OHLCV data from Yahoo Finance,
add TA-Lib indicators + candlestick patterns,
return HTML table inside a scrollable container.
"""
try:
# ----------------------------------
# Fetch NIFTY 50 data
# ----------------------------------
df = yf.download("^NSEI", period="1y", interval="1d").round(2)
if df.empty:
return html_card("Error", "No data found for NIFTY 50 (^NSEI).")
# Standardize column names
df.columns = ["Close", "High", "Low", "Open", "Volume"]
df.reset_index(inplace=True) # make Date a column
# Limit display rows
df_display = df.head(max_rows)
# ----------------------------------
# Generate TA-Lib indicators
# ----------------------------------
combined_df = talib_df(df_display)
# ----------------------------------
# Convert to HTML
# ----------------------------------
table_html = combined_df.to_html(
classes="table table-striped table-bordered",
index=False
)
scrollable_html = f"""
<div style="overflow-x:auto; overflow-y:auto; max-height:650px; border:1px solid #ccc; padding:8px;">
{table_html}
</div>
"""
content = f"""
<h2>NIFTY 50 (^NSEI) β€” Daily (OHLCV + Indicators + Patterns)</h2>
{html_card("Technical Analysis Table", scrollable_html)}
"""
return wrap_html(content, title="NIFTY 50 Daily Data")
except Exception as e:
return html_card("Error", str(e))