topsecrettraders commited on
Commit
6d17eec
·
verified ·
1 Parent(s): cea344a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -8
app.py CHANGED
@@ -150,24 +150,37 @@ def fetch_series(
150
  if not instruments:
151
  return JSONResponse({"error": f"No data found for expiry {expiry}", "labels":[], "P":[]})
152
 
153
- # 2. Use user-selected instrument as Reference. Fallback to auto-detect only if empty.
154
  ref_instrument = instrument
155
  if not ref_instrument:
156
- base_keys =[k for k in instruments if k.endswith('-INDEX') or k.endswith('-EQ') or k.endswith('FUT')]
157
- ref_instrument = base_keys[0] if base_keys else instruments[0]
 
 
 
 
 
 
158
 
159
  if ref_instrument not in instruments:
160
  return JSONResponse({"error": f"Ref Instrument '{ref_instrument}' not found in DB for this expiry. Please re-select from dropdown.", "labels": [], "P":[]})
161
 
162
- # 3. Build Strike Map strictly in Python (handles Fyers weird dates correctly)
163
  strike_map = {}
164
  for k in instruments:
165
- match = re.search(r'(\d+)(CE|PE)$', k)
166
- if match:
167
- strike_map[k] = float(match.group(1))
 
 
 
 
 
 
 
168
 
169
  if not strike_map:
170
- return JSONResponse({"error": "No options available to build ATM map.", "labels": [], "P":[]})
171
 
172
  # 4. Call Postgres with parsed Strike Map
173
  params = {
 
150
  if not instruments:
151
  return JSONResponse({"error": f"No data found for expiry {expiry}", "labels":[], "P":[]})
152
 
153
+ # 2. Intelligent Default Selection (Spot/Equity > Future > First Available)
154
  ref_instrument = instrument
155
  if not ref_instrument:
156
+ spot_keys =[k for k in instruments if k.endswith('-INDEX') or k.endswith('-EQ')]
157
+ fut_keys = [k for k in instruments if 'FUT' in k]
158
+ if spot_keys:
159
+ ref_instrument = spot_keys[0]
160
+ elif fut_keys:
161
+ ref_instrument = fut_keys[0]
162
+ else:
163
+ ref_instrument = instruments[0]
164
 
165
  if ref_instrument not in instruments:
166
  return JSONResponse({"error": f"Ref Instrument '{ref_instrument}' not found in DB for this expiry. Please re-select from dropdown.", "labels": [], "P":[]})
167
 
168
+ # 3. Build Strike Map correctly handling Fyers date strings (e.g. NIFTY2630224000CE -> 24000)
169
  strike_map = {}
170
  for k in instruments:
171
+ if k.endswith('CE') or k.endswith('PE'):
172
+ # Fyers string: 2 digits(YY) + 3 Alphanumeric(M DD/MMM) + Strike + CE/PE
173
+ match = re.search(r'\d{2}[A-Z0-9]{3}(\d+(\.\d+)?)(?:CE|PE)$', k)
174
+ if match:
175
+ strike_map[k] = float(match.group(1))
176
+ else:
177
+ # Fallback
178
+ match2 = re.search(r'(\d+(\.\d+)?)(?:CE|PE)$', k)
179
+ if match2:
180
+ strike_map[k] = float(match2.group(1))
181
 
182
  if not strike_map:
183
+ return JSONResponse({"error": "No options available to build ATM map.", "labels":[], "P":[]})
184
 
185
  # 4. Call Postgres with parsed Strike Map
186
  params = {