Type 1 - Exact Symbol Search: When the user selects type 1 and inputs a symbol ticker, the stream is returned as a result.
Type 2 - Prefix Symbol Search: When the user selects type 2 and inputs a symbol ticker prefix and stream, all symbols under that stream with the given prefix are returned.
Note: For exiting the code, enter "exit" as an input.You can use the below code for running the script:
import requests
import sys
from collections import defaultdict
from pytrie import StringTrie
streamSymbolDict = defaultdict(list)
symbolStreamDict = {}
def prefixSearch(arr,prefix):
trie=StringTrie()
for key in arr:
trie[key] = key
result = trie.values(prefix)
return result
def SymbolStreamMap():
response = requests.get("https://s3.ap-south-1.amazonaws.com/public.fyers.in/sym_details/NSE_FO_sym_master.json")")
data = response.json()
for symbolName in data:
stream = data[symbolName]['stream']
streamSymbolDict[stream].append(symbolName)
symbolStreamDict[symbolName] = stream
SymbolStreamMap()
while True:
searchType = input("Enter the type of search : ")
if searchType == "exit":
break
symbolSearch = input("\n Enter the symbol : ")
symbolSearch = symbolSearch.upper()
if len(symbolSearch) > 4:
if symbolSearch[:4] != "NSE:":
symbolSearch = "NSE:" + symbolSearch
else:
symbolSearch = "NSE:" + symbolSearch
if searchType == "1":
if symbolSearch == "exit":
break
try:
print(symbolStreamDict[symbolSearch])
except KeyError:
print("Enter a valid Symbol ticker for exact search",KeyError)
if searchType == "2":
inputStream = input("\n Enter the stream : ")
if inputStream == "exit":
continue
if symbolSearch != "":
try:
results = prefixSearch(streamSymbolDict[inputStream],symbolSearch)
print(results)
except KeyError:
print("Enter a valid Symbol ticker for exact search",KeyError)
else:
results = streamSymbolDict[inputStream]
print(results)
print("Exiting application")