-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathlookupTablesCreator.py
More file actions
566 lines (490 loc) · 29.3 KB
/
Copy pathlookupTablesCreator.py
File metadata and controls
566 lines (490 loc) · 29.3 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
"""
Author: Rajesh Thennan
Source: https://github.com/rthennan/ZerodhaWebsocket
Downloads Instrument List from Zerodha
Lists the exchange tokens (instrument_token) to be subscribed, based on the lookup table Generated / Updated by nifty500Updater
Estimate current and next expiry dates for Nifty and BankNifty Options
Generates Instrument Token Lists for
- NiftyOptions - current and next expiry
- BankNiftyOptions - current and Next expiry
Creates lookup Dictionaries that will be used for exchangeToken => TableName and exchangeToken => Symbol in the actual ticker
Creates DB and one table for storing daily tick. Can be used for live queries during market hours
Table:
{nifty500DBName}.dailytable
Example queries to get data from live DB (live Ticks):
SELECT timestamp, price FROM dailytable WHERE tradingsymbol='NIFTY 50' order by timestamp DESC LIMIT 5;
SELECT timestamp, price FROM dailytable WHERE tablename='NIFTY' order by timestamp DESC LIMIT 5;
SELECT instrument_token, timestamp, price, volume FROM dailytable WHERE tradingsymbol='NIFTY24APRFUT' order by timestamp DESC LIMIT 5;
SELECT timestamp, price, volume FROM dailytable WHERE tablename='NIFTYFUT' order by timestamp DESC LIMIT 5;
SELECT timestamp, price, volume FROM dailytable WHERE instrument_token=13368834 order by timestamp DESC LIMIT 5;
nifty500DBName configured in dasConfig.json
Some of the operations for Nifty500 token and Options tokens,
lookup table creation and SQL table creation could have been combined.
combined vs 3 separate lookup tables for ~1300 tokens doesn't have any measurable performance difference.
But splitting them on purpose, for readability.
Also, if you don't want a part of the ticker,
you could just remove it from here and the main ticker - DAS_ticker
Check _InstrumentsSubscribed.log to see the list of symbols subscribed to
#Change log - 2024-04-08:
- DAS_Ticker now stores live ticks into one table now. This is to reduce IOPS
- Hence only creating one table, instead of the previous 'one table per instrument' approach
#Change log - 2024-11-18
- getBankNiftyExpiry now takes 'offsetMonth' and returns this month or next month expiry.
- This change is required as BankNifty expiries are no longer weekly.
- Also simplifying getNiftyExpiry to accept an 'offsetWeek' and return weekly expiries accordingly
#Change log - 2025-08-06:
- makedirs(lookupDirectory,exist_ok=True) in the off chance that the lookup directory wasn't created.
- unlikely,first, standalone run for lookupTableCreator
- getNiftyExpiry accepts 'offsetExpiry' and returns this or next expiry.
- agnostic of weekly or monthly or fortnightly or whatever the puck SEBI decided to do
- Similarly, getBankNiftyExpiry accepts 'offsetExpiry'
ChangeLog - 2026-05-19:
- Adding Sensex Futures and Options
"""
import pandas as pd
import numpy as np
import MySQLdb
from datetime import datetime as dt, date, timedelta
from dateutil.relativedelta import relativedelta, TH, WE
from os import path,makedirs
import json
from DAS_gmailer import DAS_mailer
import traceback
from DAS_errorLogger import DAS_errorLogger
from time import sleep
import sys
import urllib.request
from isDasConfigDefault import isDasConfigDefault
numbOfRetries = 5
configFile = 'dasConfig.json'
with open(configFile,'r') as configFile:
dasConfig = json.load(configFile)
destinationEmailAddress = dasConfig['destinationEmailAddress']
mysqlHost = dasConfig['mysqlHost']
mysqlUser = dasConfig['mysqlUser']
mysqlPass = dasConfig['mysqlPass']
mysqlPort = dasConfig['mysqlPort']
dailyTableName = 'dailytable'
nifty500DBName = dasConfig['nifty500DBName']
lookupDirectory = 'lookupTables'
makedirs(lookupDirectory,exist_ok=True)
n500instrumentLookupFile = 'lookupTables_Nifty500.csv'
n500InstrumentFilePath = path.join(lookupDirectory,n500instrumentLookupFile)
def lookupTableCreatorLogger(txt):
print(dt.now(),txt)
logDirectory = path.join('Logs',str(date.today())+'_DAS_Logs')
if not path.exists(logDirectory):
makedirs(logDirectory)
logFile = path.join(logDirectory,f'DAS_lookupTableCreator_logs_{str(date.today())}.log')
logMsg = '\n'+str(dt.now())+' ' + txt
with open(logFile,'a') as f:
f.write(logMsg)
def insrumentListLogger(txt):
logDirectory = path.join('Logs',str(date.today())+'_DAS_Logs')
if not path.exists(logDirectory):
makedirs(logDirectory)
logFile = path.join(logDirectory,f'DAS_InstrumentsSubscribed_{str(date.today())}.log')
logMsg = '\n'+str(dt.now())+' ' + txt
with open(logFile,'a') as f:
f.write(logMsg)
def downloadZerodhaInstrumentFile():
zerodhaDumpUrl = 'https://api.kite.trade/instruments'
zerdhaIstrumentDumpFileName = 'zerodhaInstrumentDump.csv'
zerdhaIstrumentDumpFilePath = path.join('lookupTables',zerdhaIstrumentDumpFileName)
for attempt in range(1,numbOfRetries+1):
try:
urllib.request.urlretrieve(zerodhaDumpUrl, zerdhaIstrumentDumpFilePath)
msg = 'Downloaded Instrument dump file from Zerodha'
lookupTableCreatorLogger(msg)
return None
except Exception as e:
msg = f'Downloading zerodhaInstrumentsDump from {zerodhaDumpUrl} Failed. Attempt No :{attempt} . Exception-> {e} Traceback : {traceback.format_exc()}.\nWill retry after 30 seconds'
lookupTableCreatorLogger(msg)
DAS_errorLogger('lookupTablesCreator - '+msg)
sleep(30)
else:
break
else:
msg = f'Downloading zerodhaInstrumentsDump from {zerodhaDumpUrl} Failed after {numbOfRetries} attempts. Exiting'
lookupTableCreatorLogger(msg)
DAS_errorLogger('lookupTablesCreator - '+msg)
sys.exit()
#zerdhaIstrumentDumpFile is used multiple times
#Nifty 500 - Once for getting instrument_tokens
#Nifty Options - Thrice - Once for each each expiry and once for getting instrument_tokens
#BankNifty Options - Thrice - Once for each each expiry and once for getting instrument_tokens
#So retaining and using a local file if it is fresh
def getZerodhaInstDump():
zerdhaIstrumentDumpFileName = 'zerodhaInstrumentDump.csv'
zerdhaIstrumentDumpFilePath = path.join('lookupTables',zerdhaIstrumentDumpFileName)
if path.exists(zerdhaIstrumentDumpFilePath):
oneHourAgo = dt.now() - timedelta(hours=1)
#if file exists and is fresh (not older than an hour), use the local file.
zerodhaDumpModifiedTime = dt.fromtimestamp(path.getmtime(zerdhaIstrumentDumpFilePath))
if zerodhaDumpModifiedTime >= oneHourAgo:
msg = f'local zerdhaIstrumentDumpFile {zerdhaIstrumentDumpFileName} is fresh. Using it.'
lookupTableCreatorLogger(msg)
else:
msg = f'local zerodhaInstrumentDumpFile {zerdhaIstrumentDumpFileName} is older than 1 hour. Downloading a new one'
lookupTableCreatorLogger(msg)
downloadZerodhaInstrumentFile()
else:
msg = f'local zerodhaInstrumentDumpFile {zerdhaIstrumentDumpFileName} not found. Downloading a new one'
lookupTableCreatorLogger(msg)
downloadZerodhaInstrumentFile()
return pd.read_csv(zerdhaIstrumentDumpFilePath)
def getNiftyExpiry(offsetExpiry): #offsetExpiry=0 => this expiry. offsetExpiry=1 => next expiry
#Uses global variable zerodhaInstrumentsDump
#Filtering Nifty Options from zerodhaInstrumentsDump
zerodhaInstrumentsDump = getZerodhaInstDump()
niftyOptions = zerodhaInstrumentsDump[zerodhaInstrumentsDump['segment'].isin(['NFO-OPT'])]
niftyOptions = niftyOptions[niftyOptions['name'].isin(['NIFTY'])]
#Filtering just CE as we interested only in the expiry dates now and not the actual instruments
niftyOptions = niftyOptions[niftyOptions['instrument_type'].isin(['CE'])]
niftyExpiryDates = sorted(list(niftyOptions['expiry'].unique()))
return str(niftyExpiryDates[offsetExpiry])
def getBankNiftyExpiry(offsetExpiry): #offsetExpiry=0 => this expiry. offsetExpiry=1 => next expiry
#Uses global variable zerodhaInstrumentsDump
#Filtering Nifty Options from zerodhaInstrumentsDump
zerodhaInstrumentsDump = getZerodhaInstDump()
bankNiftyOptions = zerodhaInstrumentsDump[zerodhaInstrumentsDump['segment'].isin(['NFO-OPT'])]
bankNiftyOptions = bankNiftyOptions[bankNiftyOptions['name'].isin(['BANKNIFTY'])]
#Filtering just CE as we interested only in the expiry dates now and not the actual instruments
bankNiftyOptions = bankNiftyOptions[bankNiftyOptions['instrument_type'].isin(['CE'])]
bankNiftyExpiryDates = sorted(list(bankNiftyOptions['expiry'].unique()))
return str(bankNiftyExpiryDates[offsetExpiry])
def getSensexExpiry(offsetExpiry): #offsetExpiry=0 => this expiry. offsetExpiry=1 => next expiry
#Uses global variable zerodhaInstrumentsDump
#Filtering Nifty Options from zerodhaInstrumentsDump
zerodhaInstrumentsDump = getZerodhaInstDump()
sensexOptions = zerodhaInstrumentsDump[zerodhaInstrumentsDump['segment'].isin(['BFO-OPT'])]
sensexOptions = sensexOptions[sensexOptions['name'].isin(['SENSEX'])]
#Filtering just CE as we interested only in the expiry dates now and not the actual instruments
sensexOptions = sensexOptions[sensexOptions['instrument_type'].isin(['CE'])]
sensexExpiryDates = sorted(list(sensexOptions['expiry'].unique()))
return str(sensexExpiryDates[offsetExpiry])
def lookupTablesCreatorNifty500():
try:
'''
=========================================
Creating Lookup Tables for Nifty500 - Start
=========================================
'''
msg = 'DAS - Nifty 500 Lookup Table Creation Started'
lookupTableCreatorLogger(msg)
##Reading Nifty500 instruments +Index + IndexFuture to be subscribed
##Reading the lookup table again to create lookup dictionary
n500InstrumentSymbolsTables = pd.read_csv(n500InstrumentFilePath)
n500InstrumentSymbols = n500InstrumentSymbolsTables['Symbol'].values.tolist()
#Downloading Zerodha Instrument dump
zerodhaInstrumentsDump = getZerodhaInstDump()
## retaining instrument_token and tradingsymbol only in the instrument dump
nifty500Instruments = zerodhaInstrumentsDump[zerodhaInstrumentsDump['tradingsymbol'].isin(n500InstrumentSymbols)]
#Filtering further.
#'exchange'.isin(['NSE','NFO']
#'instrument_type'.isin(['EQ','FUT']
# isin(['BFO-FUT', 'INDICES'] to retain sensex future and index
# =============================================================================
# nifty500Instruments = nifty500Instruments[
# (
# (nifty500Instruments['exchange'].isin(['NSE', 'NFO'])) &
# (nifty500Instruments['instrument_type'].isin(['EQ', 'FUT']))
# ) |
# (
# nifty500Instruments['segment'].isin(['BFO-FUT', 'INDICES'])
# )
# ]
# =============================================================================
#alternatively, just removing BSE listings for N500. This retains Index and Futures for Nifty, BankNifty and Sensex
nifty500Instruments = nifty500Instruments[
~(
(nifty500Instruments['segment'] == 'BSE') &
(nifty500Instruments['exchange'] == 'BSE')
)
]
##Retaining instrument_token and tradingsymbol only from the instrument dump
nifty500Instruments = nifty500Instruments[['instrument_token','tradingsymbol']]
nifty500Instruments = nifty500Instruments.drop_duplicates()
#Creating a lookup dictionary for Symbols. Lookup the exchange token, get the Symbol response
n500TokenSymbolDict= nifty500Instruments.set_index('instrument_token')['tradingsymbol'].to_dict()
#Saving the exchange_token:Symbol Dictionary
np.save(path.join(lookupDirectory,'nifty500TokenSymbolDict.npy'), n500TokenSymbolDict)
msg = 'Saved n500TokenTable exchange_token:Symbol Dictionary => nifty500TokenSymbolDict.npy'
lookupTableCreatorLogger(msg)
##Creating a lookup dictionary - Lookup Symbol, get TableName
nifty500TableNameLookup = n500InstrumentSymbolsTables.set_index('Symbol')['TableName'].to_dict()
#In the original instrument dump, replacing the symbol with the table name.
#This way, in the ticker, any response for the instrument_token is stored to its table directly.
#This is necessary as the table name is not the same as the symbol name for a few instruments:
#Symbol has special character (M&M)
#The tradingsymbol column now holds the Table Name
nifty500Instruments = nifty500Instruments.rename(columns={'tradingsymbol': 'TableName'})
nifty500Instruments.replace(nifty500TableNameLookup,inplace=True)
#Creating a lookup dictionary from this.
#Lookup the exchange token, get the table name as response
#The tradingsymbol column now holds the Table Name
n500TokenTableDict= nifty500Instruments.set_index('instrument_token')['TableName'].to_dict()
#Saving the exchange_token:TableName Dictionary
np.save(path.join(lookupDirectory,'nifty500TokenTableDict.npy'), n500TokenTableDict)
msg = 'Saved n500TokenTable exchange_token:TableName Dictionary => nifty500TokenTableDict.npy'
lookupTableCreatorLogger(msg)
#Separating Indexes NIFTY and BANKNIFTY from the main list
#They have just two columns in the feed - timestamp and price.
#Don't have to be removed from the main list.
#just a list to check and create different table structure at EoD
indexInstruments = nifty500Instruments.loc[nifty500Instruments['TableName'].isin(['NIFTY', 'BANKNIFTY','SENSEX'])]
indexInstruments.drop('TableName',axis=1).to_csv(path.join(lookupDirectory,'indexTokenList.csv'),index=False)
msg = 'Saved indexTokenList.csv to differentiate Nifty, BankNifty and Sensex Indexes from the other instruments for SQL store as they only have timestamp and price'
lookupTableCreatorLogger(msg)
#Saving the main exchange_token list to subscribe later
nifty500Instruments.drop('TableName',axis=1).to_csv(path.join(lookupDirectory,'nifty500TokenList.csv'),index=False)
msg = 'Saved nifty500TokenList.csv'
lookupTableCreatorLogger(msg)
msg = f'Nifty 500 - Subscribing to {len(nifty500Instruments)} Instruments'
lookupTableCreatorLogger(msg)
insrumentListLogger(msg)
# Creating a string from the 'TableName' column where each item is on a new line
n500InstrumentNameString = '\n'.join(n500InstrumentSymbols)
msg = 'Nifty 500 Instruments Subscribed :\n'+n500InstrumentNameString
insrumentListLogger(msg)
return True
except Exception as e:
msg = f"Instrument Token Lookup Table Creator - Nifty 500 - failed with exception {e}. Traceback : {str(traceback.format_exc())}"
lookupTableCreatorLogger(msg)
DAS_errorLogger('lookupTablesCreator - '+msg)
DAS_mailer('DAS Nifty 500 Lookup Table Creator Failed',msg)
return False
'''
=========================================
Creating Lookup Tables for Nifty500 - End
=========================================
'''
def lookupTablesCreatorNiftyOptions():
'''
=========================================
Creating Lookup Tables for Nifty Options - Start
=========================================
'''
msg = 'DAS - Nifty Options Lookup Table Creation Started'
lookupTableCreatorLogger(msg)
try:
niftyThisExpiry = getNiftyExpiry(0) #This Expiry
niftyNextExpiry = getNiftyExpiry(1) #Next Expiry
#Downloading Zerodha Instrument dump
zerodhaInstrumentsDump = getZerodhaInstDump()
#Filtering Nifty Options from zerodhaInstrumentsDump
niftyOptionsDF = zerodhaInstrumentsDump[
(zerodhaInstrumentsDump['segment'] == 'NFO-OPT') &
(zerodhaInstrumentsDump['name'] == 'NIFTY')
]
#Retain this and Next Expiry only
niftyOptionsDF = niftyOptionsDF[niftyOptionsDF['expiry'].isin([niftyThisExpiry,niftyNextExpiry])]
##Retaining instrument_token and tradingsymbol only from the instrument dump
niftyOptionsDF = niftyOptionsDF[['instrument_token','tradingsymbol']]
niftyOptionsDF = niftyOptionsDF.drop_duplicates()
#Trading Symbol can be used as tableName as Index option symbols do not have special characters or spaces
niftyOptionsDF = niftyOptionsDF.rename(columns={'tradingsymbol': 'TableName'})
##Creating a lookup dictionary - Lookup instrument_token, get TableName
niftyOptionsTokenTable = niftyOptionsDF.set_index('instrument_token')['TableName'].to_dict()
#Saving the exchange_token:TableName Dictionary
#Same dictionary can be used for instrument_token:Symbol lookup for options
np.save(path.join(lookupDirectory,'niftyOptionsTokenTableDict.npy'), niftyOptionsTokenTable)
msg = 'Saved niftyOptionsTokenTable exchange_token:TableName Dictionary => niftyOptionsTokenTableDict.npy'
lookupTableCreatorLogger(msg)
#Saving niftyOptions instrument_token list to subscribe.
#This will also be used to save nifty option ticks to a separate DB
niftyOptionsDF.drop('TableName',axis=1).to_csv(path.join(lookupDirectory,'niftyOptionsTokenList.csv'),index=False)
msg = f'Subscribing to {len(niftyOptionsDF)} Nifty Options. Saved niftyOptionsTokenList.csv'
lookupTableCreatorLogger(msg)
insrumentListLogger(msg)
# Creating a string from the 'TableName' column where each item is on a new line
niftyOptionsNameString = '\n'.join(niftyOptionsDF['TableName'].astype(str))
msg = 'Nifty Option Instruments Subscribed :\n'+niftyOptionsNameString
insrumentListLogger(msg)
return True
except Exception as e:
msg = f"Instrument Token Lookup Table Creator - Nifty Options - failed with exception {e}. Traceback : {str(traceback.format_exc())}"
lookupTableCreatorLogger(msg)
DAS_errorLogger('lookupTablesCreator - '+msg)
DAS_mailer('DAS Nifty Options Lookup Table Creator Failed',msg)
return False
'''
=========================================
Creating Lookup Tables for Nifty Options - End
=========================================
'''
def lookupTablesCreatorBankNiftyOptions():
'''
=========================================
Creating Lookup Tables for BankNifty Options - Start
=========================================
'''
msg = 'DAS - BankNifty Options Lookup Table Creation Started'
lookupTableCreatorLogger(msg)
try:
bankNiftyThisExpiry = getBankNiftyExpiry(0) #This Expiry
bankNiftyNextExpiry = getBankNiftyExpiry(1) #Next Expiry
#Downloading Zerodha Instrument dump
zerodhaInstrumentsDump = getZerodhaInstDump()
#Filtering BankNifty Options from zerodhaInstrumentsDump
bankNiftyOptionsDF = zerodhaInstrumentsDump[
(zerodhaInstrumentsDump['segment'] == 'NFO-OPT') &
(zerodhaInstrumentsDump['name'] == 'BANKNIFTY')
]
#Retain this and Next Expiry only
bankNiftyOptionsDF = bankNiftyOptionsDF[bankNiftyOptionsDF['expiry'].isin([bankNiftyThisExpiry,bankNiftyNextExpiry])]
##Retaining instrument_token and tradingsymbol only from the instrument dump
bankNiftyOptionsDF = bankNiftyOptionsDF[['instrument_token','tradingsymbol']]
bankNiftyOptionsDF = bankNiftyOptionsDF.drop_duplicates()
#Trading Symbol can be used as tableName as Index option symbols do not have special characters or spaces
bankNiftyOptionsDF = bankNiftyOptionsDF.rename(columns={'tradingsymbol': 'TableName'})
##Creating a lookup dictionary - Lookup instrument_token, get TableName
bankNiftyOptionsTokenTable = bankNiftyOptionsDF.set_index('instrument_token')['TableName'].to_dict()
#Saving the exchange_token:TableName Dictionary
#Same dictionary can be used for instrument_token:Symbol lookup for options
np.save(path.join(lookupDirectory,'bankNiftyOptionsTokenTableDict.npy'), bankNiftyOptionsTokenTable)
msg = 'Saved bankNiftyOptionsTokenTable exchange_token:TableName Dictionary => bankNiftyOptionsTokenTableDict.npy'
lookupTableCreatorLogger(msg)
#Saving BankNiftyOptions instrument_token list to subscribe.
#This will also be used to save banknifty option ticks to a separate DB
bankNiftyOptionsDF.drop('TableName',axis=1).to_csv(path.join(lookupDirectory,'bankNiftyOptionsTokenList.csv'),index=False)
msg = f'Subscribing to {len(bankNiftyOptionsDF)} BankNifty Options. Saved bankNiftyOptionsTokenList.csv'
lookupTableCreatorLogger(msg)
insrumentListLogger(msg)
# Creating a string from the 'TableName' column where each item is on a new line
bankNiftyOptionsNameString = '\n'.join(bankNiftyOptionsDF['TableName'].astype(str))
msg = 'Bank Nifty Option Instruments Subscribed :\n'+bankNiftyOptionsNameString
insrumentListLogger(msg)
return True
except Exception as e:
msg = f"Instrument Token Lookup Table Creator - BankNifty Options - failed with exception {e}. Traceback : {str(traceback.format_exc())}"
lookupTableCreatorLogger(msg)
DAS_errorLogger('lookupTablesCreator - '+msg)
DAS_mailer('DAS BankNifty Options Lookup Table Creator Failed',msg)
return False
'''
=========================================
Creating Lookup Tables for Bank Nifty Options - End
=========================================
'''
def lookupTablesCreatorSensexOptions():
'''
=========================================
Creating Lookup Tables for Sensex Options - Start
=========================================
'''
msg = 'DAS - Sensex Options Lookup Table Creation Started'
lookupTableCreatorLogger(msg)
try:
sensexThisExpiry = getSensexExpiry(0) #This Expiry
sensexNextExpiry = getSensexExpiry(1) #Next Expiry
#Downloading Zerodha Instrument dump
zerodhaInstrumentsDump = getZerodhaInstDump()
#Filtering Sensex Options from zerodhaInstrumentsDump
sensexOptionsDF = zerodhaInstrumentsDump[
(zerodhaInstrumentsDump['segment'] == 'BFO-OPT') &
(zerodhaInstrumentsDump['name'] == 'SENSEX')
]
#Retain this and Next Expiry only
sensexOptionsDF = sensexOptionsDF[sensexOptionsDF['expiry'].isin([sensexThisExpiry,sensexNextExpiry])]
##Retaining instrument_token and tradingsymbol only from the instrument dump
sensexOptionsDF = sensexOptionsDF[['instrument_token','tradingsymbol']]
sensexOptionsDF = sensexOptionsDF.drop_duplicates()
#Trading Symbol can be used as tableName as Index option symbols do not have special characters or spaces
sensexOptionsDF = sensexOptionsDF.rename(columns={'tradingsymbol': 'TableName'})
##Creating a lookup dictionary - Lookup instrument_token, get TableName
sensexOptionsTokenTable = sensexOptionsDF.set_index('instrument_token')['TableName'].to_dict()
#Saving the exchange_token:TableName Dictionary
#Same dictionary can be used for instrument_token:Symbol lookup for options
np.save(path.join(lookupDirectory,'sensexOptionsTokenTableDict.npy'), sensexOptionsTokenTable)
msg = 'Saved sensexOptionsTokenTable exchange_token:TableName Dictionary => sensexOptionsTokenTableDict.npy'
lookupTableCreatorLogger(msg)
#Saving SensexOptions instrument_token list to subscribe.
#This will also be used to save sensex option ticks to a separate DB
sensexOptionsDF.drop('TableName',axis=1).to_csv(path.join(lookupDirectory,'sensexOptionsTokenList.csv'),index=False)
msg = f'Subscribing to {len(sensexOptionsDF)} Sensex Options. Saved sensexOptionsTokenList.csv'
lookupTableCreatorLogger(msg)
insrumentListLogger(msg)
# Creating a string from the 'TableName' column where each item is on a new line
sensexOptionsNameString = '\n'.join(sensexOptionsDF['TableName'].astype(str))
msg = 'Sensex Option Instruments Subscribed :\n'+sensexOptionsNameString
insrumentListLogger(msg)
return True
except Exception as e:
msg = f"Instrument Token Lookup Table Creator - Sensex Options - failed with exception {e}. Traceback : {str(traceback.format_exc())}"
lookupTableCreatorLogger(msg)
DAS_errorLogger('lookupTablesCreator - '+msg)
DAS_mailer('DAS Sensex Options Lookup Table Creator Failed',msg)
return False
'''
=========================================
Creating Lookup Tables for Sensex Options - End
=========================================
'''
def lookupTablesCreator():
if isDasConfigDefault():
msg = 'DAS Config has defaults. accessTokenReq is exiting'
lookupTableCreatorLogger(msg)
DAS_errorLogger(msg)
return False
#On failure, function would have exited at the False return.
#Hence no Else required here
n500TablesCreated = lookupTablesCreatorNifty500()
niftyOptionsTablesCreated = lookupTablesCreatorNiftyOptions()
bankOptionsTablesCreated = lookupTablesCreatorBankNiftyOptions()
sensexOptionsTablesCreated = lookupTablesCreatorSensexOptions()
if not all([n500TablesCreated, niftyOptionsTablesCreated, bankOptionsTablesCreated,sensexOptionsTablesCreated]):
msg = 'One or more Lookup Table Creation Activities FAILED!!!'
lookupTableCreatorLogger(msg)
DAS_errorLogger('lookupTablesCreator - '+msg)
return False
if n500TablesCreated and niftyOptionsTablesCreated and bankOptionsTablesCreated and sensexOptionsTablesCreated:
#Create {nifty500DBName}
conn = MySQLdb.connect(host = mysqlHost, user = mysqlUser, passwd = mysqlPass, port=mysqlPort)
c = conn.cursor()
c.execute(f'CREATE DATABASE IF NOT EXISTS {nifty500DBName}')
##Create Daily table - {nifty500DBName}.{dailyTableName}
c.execute(f'''
CREATE TABLE IF NOT EXISTS {nifty500DBName}.{dailyTableName} (
instrument_token BIGINT(20),
tradingsymbol VARCHAR(100),
tablename VARCHAR(100),
dbname VARCHAR(100),
timestamp DATETIME, price DECIMAL(19,2),
qty INT UNSIGNED,
avgPrice DECIMAL(19,2),
volume BIGINT,
bQty INT UNSIGNED,
sQty INT UNSIGNED,
open DECIMAL(19,2),
high DECIMAL(19,2),
low DECIMAL(19,2),
close DECIMAL(19,2),
changeper DECIMAL(60,10),
lastTradeTime DATETIME,
oi INT,
oiHigh INT,
oiLow INT,
bq0 INT UNSIGNED, bp0 DECIMAL(19,2), bo0 INT UNSIGNED,
bq1 INT UNSIGNED, bp1 DECIMAL(19,2), bo1 INT UNSIGNED,
bq2 INT UNSIGNED, bp2 DECIMAL(19,2), bo2 INT UNSIGNED,
bq3 INT UNSIGNED, bp3 DECIMAL(19,2), bo3 INT UNSIGNED,
bq4 INT UNSIGNED, bp4 DECIMAL(19,2), bo4 INT UNSIGNED,
sq0 INT UNSIGNED, sp0 DECIMAL(19,2), so0 INT UNSIGNED,
sq1 INT UNSIGNED, sp1 DECIMAL(19,2), so1 INT UNSIGNED,
sq2 INT UNSIGNED, sp2 DECIMAL(19,2), so2 INT UNSIGNED,
sq3 INT UNSIGNED, sp3 DECIMAL(19,2), so3 INT UNSIGNED,
sq4 INT UNSIGNED, sp4 DECIMAL(19,2), so4 INT UNSIGNED,
UNIQUE (instrument_token, timestamp),
INDEX tablenameindex (tablename),
INDEX symbolindex (tradingsymbol),
INDEX instrument_token_index (instrument_token),
INDEX timestamp_index (timestamp)
)''')
msg = 'DAS - All Lookup Table Creation Activities Successful'
lookupTableCreatorLogger(msg)
return True
#Catch all
return False
if __name__ == '__main__':
lookupTablesCreator()