-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_upstox_mapping.py
More file actions
230 lines (184 loc) · 7.47 KB
/
update_upstox_mapping.py
File metadata and controls
230 lines (184 loc) · 7.47 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
#!/usr/bin/env python3
"""
update_upstox_mapping.py
Automatically update MongoDB instrument_keys collection with new IPO symbols.
This script:
1. Reads recent IPOs from MongoDB ipos collection
2. Uses Upstox API to find instrument keys for missing symbols
3. Updates MongoDB instrument_keys collection automatically
"""
import os
import pandas as pd
import requests
import time
from datetime import datetime
from dotenv import load_dotenv
import logging
# Load environment
load_dotenv()
# Logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(message)s")
logger = logging.getLogger(__name__)
# Configuration
UPSTOX_ACCESS_TOKEN = os.getenv('UPSTOX_ACCESS_TOKEN')
def get_isin_from_nse(symbol):
"""Get ISIN code from NSE equity list"""
try:
url = "https://nsearchives.nseindia.com/content/equities/EQUITY_L.csv"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
session = requests.Session()
session.headers.update(headers)
# Establish session
session.get("https://www.nseindia.com", timeout=15)
resp = session.get(url, timeout=45)
resp.raise_for_status()
from io import StringIO
df = pd.read_csv(StringIO(resp.text))
# Find symbol column
symbol_col = None
isin_col = None
for col in df.columns:
col_upper = col.upper()
if 'SYMBOL' in col_upper:
symbol_col = col
elif 'ISIN' in col_upper:
isin_col = col
if symbol_col and isin_col:
match = df[df[symbol_col] == symbol]
if not match.empty:
isin = match[isin_col].iloc[0]
return isin
return None
except Exception as e:
logger.debug(f"Error getting ISIN from NSE for {symbol}: {e}")
return None
def search_instrument_key(symbol, listing_date=None):
"""Search for instrument key using Upstox API - requires ISIN code"""
if not UPSTOX_ACCESS_TOKEN:
logger.warning("UPSTOX_ACCESS_TOKEN not found - cannot search for instrument keys")
return None
try:
headers = {
'Accept': 'application/json',
'Api-Version': '2.0',
'Authorization': f'Bearer {UPSTOX_ACCESS_TOKEN}'
}
logger.info(f"Searching for {symbol}...")
# Step 1: Get ISIN code from NSE (same as existing mappings use)
isin = get_isin_from_nse(symbol)
if not isin:
logger.warning(f"Could not find ISIN for {symbol} from NSE - skipping")
return None
# Use ISIN in instrument key format (same as existing mappings: NSE_EQ|INE...)
instrument_key = f"NSE_EQ|{isin}"
logger.info(f"Found ISIN for {symbol}: {isin}")
# Step 2: Get company name from NSE data (if available)
name = symbol # Default to symbol
try:
url = "https://nsearchives.nseindia.com/content/equities/EQUITY_L.csv"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
session = requests.Session()
session.headers.update(headers)
session.get("https://www.nseindia.com", timeout=15)
resp = session.get(url, timeout=45)
resp.raise_for_status()
from io import StringIO
df = pd.read_csv(StringIO(resp.text))
# Find columns
symbol_col = None
name_col = None
for col in df.columns:
col_upper = col.upper()
if 'SYMBOL' in col_upper:
symbol_col = col
elif 'NAME' in col_upper and 'COMPANY' in col_upper:
name_col = col
if symbol_col and name_col:
match = df[df[symbol_col] == symbol]
if not match.empty:
name = match[name_col].iloc[0]
except:
pass # Use symbol as name if we can't fetch
# Return mapping in same format as existing ones (match_type: 'exact')
logger.info(f"Created mapping for {symbol}: {instrument_key}")
return {
'ipo_symbol': symbol,
'upstox_symbol': symbol,
'name': name,
'instrument_key': instrument_key,
'match_type': 'exact' # Same as existing mappings
}
except Exception as e:
logger.error(f"Error searching for {symbol}: {e}")
return None
def update_mapping_from_db():
"""Update MongoDB instrument_keys collection with new IPO symbols from ipos_col."""
try:
from db import ipos_col, upsert_instrument_key, ensure_indexes, get_instrument_key_mapping
ensure_indexes()
if ipos_col is None:
logger.warning("ipos_col not available — cannot update mappings")
return 0
# Get all IPO symbols from MongoDB
docs = list(ipos_col.find({}, {"_id": 0, "symbol": 1}))
if not docs:
logger.warning("ipos_col is empty — no recent IPOs to process")
return 0
recent_symbols = [d["symbol"] for d in docs if d.get("symbol")]
# Get already-mapped symbols from instrument_keys collection
existing_mapping = get_instrument_key_mapping()
existing_symbols = set(existing_mapping.keys())
new_mappings = []
updated_count = 0
for symbol in recent_symbols:
if symbol in existing_symbols:
continue
logger.info(f"Processing {symbol}...")
mapping = search_instrument_key(symbol)
if mapping:
new_mappings.append(mapping)
updated_count += 1
upsert_instrument_key(
ipo_symbol=mapping['ipo_symbol'],
instrument_key=mapping['instrument_key'],
isin=mapping['instrument_key'].split('|')[-1] if '|' in mapping['instrument_key'] else None,
name=mapping.get('name', mapping['ipo_symbol']),
match_type=mapping.get('match_type', 'exact')
)
logger.info(f"✅ Added mapping for {symbol}")
else:
logger.warning(f"⚠️ Could not find mapping for {symbol} - skipping")
time.sleep(0.2)
logger.info(f"[MongoDB] Upserted {updated_count} new instrument key mappings")
return updated_count
except Exception as e:
logger.error(f"Error updating mapping from DB: {e}")
return 0
# Keep backward-compatible alias (deprecated CSV ref)
def update_mapping_csv():
return update_mapping_from_db()
def main():
"""Main function"""
try:
print("="*60)
print("Upstox Mapping Updater")
print("="*60)
except:
print("="*60)
print("Upstox Mapping Updater")
print("="*60)
print(f"Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("="*60)
print()
if not UPSTOX_ACCESS_TOKEN:
print("WARNING: UPSTOX_ACCESS_TOKEN not found!")
print(" Mapping update will be limited.")
print(" Set UPSTOX_ACCESS_TOKEN in .env file for full functionality.")
print()
updated = update_mapping_csv()
print()
print("="*60)
print(f"Update Complete: {updated} new mappings added")
print("="*60)
if __name__ == "__main__":
main()