# The Issue:
### In short: We need a way to map one instrument to the other j…ust using API and not by manually maintaining a mapping at our end. Say mapping an instrument to it's underlying (or vice-a-versa) or other instrument (say: from options to it's future and vice-a-versa)
NIFTY's Banking Index and other indices are polyonymous (having or known by various names).
It is refereed by all these names:
+ BANK NIFTY
+ NIFTY BANK
+ NSE BANK
+ BANKNIFTY
+ NIFTYBANK
This is causing issues with coding a well designed software (specifically one without "hardcoding").
pya3 API provides a function to lookup an instrument from instrument's directory, provided it's name or token.
```python
alice.get_instrument_by_symbol('INDICES','NIFTY BANK')
```
Now, this `"NIFTY BANK"` is no where in the FNO.csv. Its only in NSE.csv and INDICES.csv
Hence, it becomes difficult to programmatically lookup the underlying of an instrument from FNO.csv. The implementer of this library must hardcode or maintain a mapping at their end.
NSE.csv:
```csv
#HEADER: Exch,Exchange Segment,Group Name,Symbol,Token,Instrument Type,Instrument Name,Formatted Ins Name,Trading Symbol,Lot Size,Tick Size
NSE,nse_idx,,NIFTY 50,26000,INDEX,,NIFTY 50,,,
NSE,nse_idx,,NIFTY BANK,26009,INDEX,,NIFTY BANK,,,
NSE,nse_idx,,NIFTY MIDCAP 100,26011,INDEX,,NIFTY MIDCAP 100,,,
NSE,nse_idx,,NIFTY FIN SERVICE,26037,INDEX,,NIFTY FIN SERVICE,,,
```
INDICES.csv:
```csv
#HEADER: exch,symbol,token
NSE,NIFTY 50,26000
NSE,NIFTY BANK,26009
NSE,NIFTY MIDCAP 100,26011
NSE,NIFTY FIN SERVICE,26037
```
NFO.csv:
```csv
#HEADER: Exch,Exchange Segment,Symbol,Token,Instrument Type,Option Type,Strike Price,Instrument Name,Formatted Ins Name,Trading Symbol,Expiry Date,Lot Size,Tick Size
NFO,nse_fo,BANKNIFTY,35004,OPTIDX,CE,43300,BANKNIFTY,BANKNIFTY MAR 43300 CE,BANKNIFTY28MAR24C43300,2024-03-28,15,0.05
NFO,nse_fo,BANKNIFTY,35005,OPTIDX,PE,43300,BANKNIFTY,BANKNIFTY MAR 43300 PE,BANKNIFTY28MAR24P43300,2024-03-28,15,0.05
```
Consider this piece of code which aims to get all the symbols that are in F&O and subscribes on `websocket` to get their close price:
```python
# Import Pandas
import pandas as pd
# Read CSV into a pandas DataFrame
df = pd.read_csv('NFO.csv')
# Get all symbols in FNO
fno_symbols = sorted(df['Symbol'].unique().tolist())
# Look up their underlying and subscribe to get last traded price
for symbol in fno_symbols:
alice.subscribe([ alice.get_instrument_by_symbol('NSE', instrument) ])
```
If we have symbol naming consistency, the above simple piece of code just works without any ifs and buts.
However, because we don't, we need to maintain a mapping like:
```python
symbol_mapping = {
"BANKNIFTY" : "NIFTY BANK",
"NIFTY" : "NIFTY 50",
"FINNIFTY" : "NIFTY FIN SERVICE",
"MIDCPNIFTY" : "NIFTY MIDCAP 100",
...
...
...
}
```
and then code it as such:
```python
# Import Pandas
import pandas as pd
# Read CSV into a pandas DataFrame
df = pd.read_csv('NFO.csv')
# Get all symbols in FNO
fno_symbols = sorted(df['Symbol'].unique().tolist())
# Maintain mapping
symbol_mapping = {
"BANKNIFTY" : "NIFTY BANK",
"NIFTY" : "NIFTY 50",
"FINNIFTY" : "NIFTY FIN SERVICE",
"MIDCPNIFTY" : "NIFTY MIDCAP 100",
}
# Look up their underlying and subscribe to get last traded price
for symbol in fno_symbols:
if symbol in symbol_mapping:
alice.subscribe([ alice.get_instrument_by_symbol('NSE', symbol_mapping[symbol]) ])
else:
alice.subscribe([ alice.get_instrument_by_symbol('NSE', symbol) ])
```
and when looking up instruments by symbol, we need to use this mapping each time.
And in all the modules of the software - not just at one place.
The issue is not just single fold, the issue is - this is NOT at all a "good software design" and this is also NOT a "one time thing" to maintain. What if this mapping changes? Broker or Exchange itself changes the name (which has happened with various indices recently). Client side code breaks, we again need to change the hardcoded values and maintain a mapping of the new symbols - aka., a very poor software design that breaks with minor changes.
As the codebase grows, maintaining such simple things becomes huge and if you fail to maintain some day, you may be shooting orders on the wrong instrument (the extent of disaster cannot be estimated).
Hence, a sane and simple thing to do is fix the design itself so everything just falls right in place with least overhead and efforts.
# **The Solution**
The way I see this can be quickly fixed is to make use of the same Symbols that are there in FNO.csv into NSE.csv and INDICES.csv:
For INDICES.csv,
Introduce a new column with ```Instrument Name``` heading and use the same Symbol that is in NFO.csv ("BANKNIFTY", "NIFTY", "FINNIFTY", "MIDCPNIFTY", ....) under ```Symbol``` column of INDICES.csv and move the current names under ```Symbol``` to ```Instrument Name``` column
Existing: INDICES.csv
```CSV
#HEADER: exch,symbol,token
NSE,NIFTY 50,26000
NSE,NIFTY BANK,26009
NSE,NIFTY MIDCAP 100,26011
NSE,NIFTY FIN SERVICE,26037
```
New: INDICES.csv
```CSV
#HEADER: exch,symbol,Instrument Name,token
NSE,NIFTY,NIFTY 50,26000
NSE,BANKNIFTY,NIFTY BANK,26009
NSE,MIDCPNIFTY,NIFTY MIDCAP 100,26011
NSE,FINNIFTY,NIFTY FIN SERVICE,26037
```
For NSE.csv,
Have the same Symbol from NFO.csv ("BANKNIFTY", "NIFTY", "FINNIFTY", "MIDCPNIFTY", ....) under ```Symbol``` column and move the current names to ```Instrument Name``` column
Existing: NSE.csv
```CSV
#HEADER: Exch,Exchange Segment,Group Name,Symbol,Token,Instrument Type,Instrument Name,Formatted Ins Name,Trading Symbol,Lot Size,Tick Size
NSE,nse_idx,,NIFTY 50,26000,INDEX,,NIFTY 50,,,
NSE,nse_idx,,NIFTY BANK,26009,INDEX,,NIFTY BANK,,,
NSE,nse_idx,,NIFTY MIDCAP 100,26011,INDEX,,NIFTY MIDCAP 100,,,
NSE,nse_idx,,NIFTY FIN SERVICE,26037,INDEX,,NIFTY FIN SERVICE,,,
```
New: NSE.csv
```CSV
#HEADER: Exch,Exchange Segment,Group Name,Symbol,Token,Instrument Type,Instrument Name,Formatted Ins Name,Trading Symbol,Lot Size,Tick Size
NSE,nse_idx,,NIFTY,26000,INDEX,NIFTY 50,NIFTY 50,,,
NSE,nse_idx,,BANKNIFTY,26009,INDEX,NIFTY BANK,NIFTY BANK,,,
NSE,nse_idx,,MIDCPNIFTY,26011,INDEX,NIFTY MIDCAP 100,NIFTY MIDCAP 100,,,
NSE,nse_idx,,FINNIFTY,26037,INDEX,NIFTY FIN SERVICE,NIFTY FIN SERVICE,,,
```
This is similar to an SQL table join, where you can lookup and join similar data.
Please help fix this. Thank you so much!