eshan6704 commited on
Commit
b5a0b36
·
verified ·
1 Parent(s): 3081989

Update daily.py

Browse files
Files changed (1) hide show
  1. daily.py +102 -6
daily.py CHANGED
@@ -3,13 +3,97 @@ import pandas as pd
3
  from ta_indi_pat import talib_df # use the combined talib_df function
4
  from common import html_card, wrap_html
5
 
6
- def fetch_daily(symbol, max_rows=200):
7
- """
8
- Fetch daily OHLCV data, calculate TA-Lib indicators + patterns,
9
- return a single scrollable HTML table.
10
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  try:
12
- # --- Fetch daily data ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  df = yf.download(symbol + ".NS", period="1y", interval="1d").round(2)
14
  if df.empty:
15
  return html_card("Error", f"No daily data found for {symbol}")
@@ -17,6 +101,18 @@ def fetch_daily(symbol, max_rows=200):
17
  # --- Standardize columns ---
18
  df.columns = ["Close", "High", "Low", "Open", "Volume"]
19
  df.reset_index(inplace=True) # make Date a column
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # --- Limit rows for display ---
22
  df_display = df.head(max_rows)
 
3
  from ta_indi_pat import talib_df # use the combined talib_df function
4
  from common import html_card, wrap_html
5
 
6
+ # -----------------------------
7
+ # Global Variables
8
+ # -----------------------------
9
+ nse_del_key_map = {
10
+ 'Symbol': "Symbol", 'Series': "Series",
11
+ 'Date': 'Date', 'Prev Close': 'Preclose',
12
+ 'Open Price': 'Open', 'High Price': 'High',
13
+ 'Low Price': 'Low', 'Last Price': 'Last',
14
+ 'Close Price': 'Close', 'Average Price': 'AvgPrice',
15
+ 'Total Traded Quantity': 'Volume',
16
+ 'Turnover ₹': 'Turnover', 'No. of Trades': "Trades",
17
+ 'Deliverable Qty': "Delivery", '% Dly Qt to Traded Qty': "Del%"
18
+ }
19
+
20
+ # -----------------------------
21
+ # Data Fetching Functions (NSE)
22
+ # -----------------------------
23
+ def url_nse_del(symbol, start_date, end_date):
24
+ base_url = "https://www.nseindia.com/api/historicalOR/generateSecurityWiseHistoricalData"
25
+ start_date_str = start_date.strftime("%d-%m-%Y")
26
+ end_date_str = end_date.strftime("%d-%m-%Y")
27
+ url = f"{base_url}?from={start_date_str}&to={end_date_str}&symbol={symbol.split('.')[0]}&type=priceVolumeDeliverable&series=ALL&csv=true"
28
+ return url
29
+
30
+ def to_numeric_safe(series):
31
+ series = series.replace('-', 0)
32
+ series = series.fillna(0)
33
+ series = series.astype(str).str.replace(',', '')
34
+ return pd.to_numeric(series, errors='coerce').fillna(0)
35
+
36
+
37
+ def nse_del(symbol, start_date_str=None, end_date_str=None):
38
+ # Default end date is today
39
+ end_date = datetime.now()
40
+ if end_date_str:
41
+ try:
42
+ end_date = datetime.strptime(end_date_str, "%Y-%m-%d")
43
+ except ValueError:
44
+ print(f"Warning: Invalid end date format '{end_date_str}'. Using today's date.")
45
+ end_date = datetime.now()
46
+
47
+ # Default start date is one year prior to end_date
48
+ start_date = end_date - timedelta(days=365)
49
+ if start_date_str:
50
+ try:
51
+ start_date = datetime.strptime(start_date_str, "%Y-%m-%d")
52
+ except ValueError:
53
+ print(f"Warning: Invalid start date format '{start_date_str}'. Using default start date.")
54
+ start_date = end_date - timedelta(days=365)
55
+
56
+ # Ensure start_date is not after end_date
57
+ if start_date > end_date:
58
+ print("Warning: Start date is after end date. Swapping dates.")
59
+ start_date, end_date = end_date, start_date
60
+
61
+ url = url_nse_del(symbol, start_date, end_date)
62
+ headers = {
63
+ 'User-Agent': 'Mozilla/5.0'
64
+ }
65
  try:
66
+ response = requests.get(url, headers=headers)
67
+ response.raise_for_status()
68
+ if response.content:
69
+ df = pd.read_csv(io.StringIO(response.content.decode('utf-8'))).round(2)
70
+ df.columns = df.columns.str.strip()
71
+ df.rename(columns=nse_del_key_map, inplace=True)
72
+
73
+ # Capitalize the first letter of ALL column names after renaming
74
+ df.columns = [col.capitalize() for col in df.columns]
75
+
76
+ # Remove 'Symbol', 'Series', 'Avgprice', and 'Last' columns (now capitalized)
77
+ df.drop(columns=['Symbol','Series','Avgprice','Last'], errors='ignore', inplace=True)
78
+
79
+ # Convert 'Date' column to datetime objects
80
+ df['Date'] = pd.to_datetime(df['Date'], format='%d-%b-%Y').dt.strftime('%Y-%m-%d')
81
+
82
+ numeric_cols = ['Close', 'Preclose', 'Open', 'High', 'Low', 'Volume', 'Delivery', 'Turnover', 'Trades']
83
+ # Ensure numeric_cols are capitalized before checking and conversion
84
+ numeric_cols_capitalized = [col.capitalize() for col in numeric_cols]
85
+ for col in numeric_cols_capitalized:
86
+ if col in df.columns:
87
+ df[col] = to_numeric_safe(df[col])
88
+ else:
89
+ df[col] = 0
90
+ return df
91
+ except Exception as e:
92
+ print(f"Error fetching data from NSE for {symbol}: {e}")
93
+ return None
94
+
95
+ def daily(symbol,source="yfinace"):
96
+ if source=="yfinance"
97
  df = yf.download(symbol + ".NS", period="1y", interval="1d").round(2)
98
  if df.empty:
99
  return html_card("Error", f"No daily data found for {symbol}")
 
101
  # --- Standardize columns ---
102
  df.columns = ["Close", "High", "Low", "Open", "Volume"]
103
  df.reset_index(inplace=True) # make Date a column
104
+ return df
105
+ if source=="NSE":
106
+ df=nse_del(symbol)
107
+
108
+ def fetch_daily(symbol, source,max_rows=200):
109
+ """
110
+ Fetch daily OHLCV data, calculate TA-Lib indicators + patterns,
111
+ return a single scrollable HTML table.
112
+ """
113
+ try:
114
+ # --- Fetch daily data ---
115
+ df=daily(symbol,source)
116
 
117
  # --- Limit rows for display ---
118
  df_display = df.head(max_rows)