Spaces:
Running
Running
Update intraday.py
Browse files- intraday.py +26 -89
intraday.py
CHANGED
|
@@ -1,102 +1,39 @@
|
|
| 1 |
# intraday.py
|
| 2 |
-
|
| 3 |
import yfinance as yf
|
| 4 |
import pandas as pd
|
|
|
|
| 5 |
from chart_builder import build_chart
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
# Fetch + Clean intraday dataset
|
| 10 |
-
# -------------------------------
|
| 11 |
-
def _fetch_intraday(symbol, interval="5m", period="1d"):
|
| 12 |
-
yfs = f"{symbol}.NS"
|
| 13 |
-
|
| 14 |
-
try:
|
| 15 |
-
df = yf.download(
|
| 16 |
-
yfs,
|
| 17 |
-
interval=interval,
|
| 18 |
-
period=period,
|
| 19 |
-
progress=False
|
| 20 |
-
)
|
| 21 |
-
except Exception as e:
|
| 22 |
-
return None, str(e)
|
| 23 |
-
|
| 24 |
-
if df is None or df.empty:
|
| 25 |
-
return None, "No intraday data returned"
|
| 26 |
-
|
| 27 |
-
# Clean index timestamps
|
| 28 |
-
df.index = pd.to_datetime(df.index)
|
| 29 |
-
|
| 30 |
-
# Remove timezone if present
|
| 31 |
-
try:
|
| 32 |
-
df.index = df.index.tz_localize(None)
|
| 33 |
-
except:
|
| 34 |
-
pass
|
| 35 |
-
|
| 36 |
-
# Round values
|
| 37 |
-
df = df.round(2)
|
| 38 |
-
|
| 39 |
-
return df, None
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
# -------------------------------
|
| 43 |
-
# Main intraday function (UI return)
|
| 44 |
-
# -------------------------------
|
| 45 |
-
def fetch_intraday(symbol, interval="5m", period="1d"):
|
| 46 |
"""
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
period = 1d,5d,1mo
|
| 50 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
return {
|
| 56 |
-
"html": f"<div class='group'><h2>Intraday Error</h2><p>{err}</p></div>",
|
| 57 |
-
"data": {}
|
| 58 |
-
}
|
| 59 |
-
|
| 60 |
-
if df is None or df.empty:
|
| 61 |
-
return {
|
| 62 |
-
"html": f"<div class='group'><h2>No Intraday Data for {symbol}</h2></div>",
|
| 63 |
-
"data": {}
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
# Build chart using indicator engine
|
| 67 |
-
chart_html = build_chart(df)
|
| 68 |
-
|
| 69 |
-
# Convert last rows to table
|
| 70 |
-
table_html = df.tail(200).to_html(
|
| 71 |
-
classes="styled-table",
|
| 72 |
-
border=0
|
| 73 |
-
)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
<h2>Intraday Chart — {symbol} ({interval}, {period})</h2>
|
| 78 |
-
{chart_html}
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
font-size: 13px;
|
| 88 |
-
}}
|
| 89 |
-
.styled-table td, .styled-table th {{
|
| 90 |
-
border: 1px solid #ddd;
|
| 91 |
-
padding: 6px;
|
| 92 |
-
text-align: right;
|
| 93 |
-
}}
|
| 94 |
-
.styled-table tr:nth-child(even) {{background: #f9f9f9;}}
|
| 95 |
-
</style>
|
| 96 |
-
</div>
|
| 97 |
-
"""
|
| 98 |
|
| 99 |
-
|
| 100 |
-
"
|
| 101 |
-
"data": df.tail(200).to_dict()
|
| 102 |
-
}
|
|
|
|
| 1 |
# intraday.py
|
|
|
|
| 2 |
import yfinance as yf
|
| 3 |
import pandas as pd
|
| 4 |
+
from common import format_large_number, wrap_html, make_table
|
| 5 |
from chart_builder import build_chart
|
| 6 |
|
| 7 |
+
# ============================================================
|
| 8 |
+
# INTRADAY DATA PROCESSING
|
| 9 |
+
# ============================================================
|
| 10 |
|
| 11 |
+
def fetch_intraday(symbol, indicators=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
"""
|
| 13 |
+
Fetch intraday (5-min) data for a symbol from Yahoo Finance,
|
| 14 |
+
format it, apply indicators, and return full HTML.
|
|
|
|
| 15 |
"""
|
| 16 |
+
yfsymbol = f"{symbol}.NS"
|
| 17 |
+
try:
|
| 18 |
+
# Fetch 1-day intraday 5-min interval
|
| 19 |
+
df = yf.download(yfsymbol, period="1d", interval="5m").round(2)
|
| 20 |
+
if df.empty:
|
| 21 |
+
return wrap_html(f"<h1>No intraday data available for {symbol}</h1>")
|
| 22 |
|
| 23 |
+
# Reset MultiIndex if exists
|
| 24 |
+
if isinstance(df.columns, pd.MultiIndex):
|
| 25 |
+
df.columns = df.columns.get_level_values(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Build chart with indicators
|
| 28 |
+
chart_html = build_chart(df, indicators=indicators, volume=True)
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Format last 50 rows for table
|
| 31 |
+
table_html = make_table(df.tail(50))
|
| 32 |
|
| 33 |
+
# Wrap in full HTML
|
| 34 |
+
full_html = wrap_html(f"{chart_html}<h2>Recent Intraday Data (last 50 rows)</h2>{table_html}",
|
| 35 |
+
title=f"Intraday Data for {symbol}")
|
| 36 |
+
return full_html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return wrap_html(f"<h1>Error fetching intraday data for {symbol}</h1><p>{str(e)}</p>")
|
|
|
|
|
|