Find Tokens for Any Symbols using Python

What are TOKENS ?

Token is a unique number given by the exchanges to a particular instrument traded in the stock market.
Tokens are extremely essential as they’re used by all the brokers and it is very easy to identify any stock by it’s token number. By finding the token you can get the exact symbol name of that instrument across any broker.

How To find the required token ?

In this example, we’re going to use the instruments file provided to us by Angel One.
Angel One provides the entire detailed file for all the instruments here.
Using this link you can get all the data for any instrument.
Now let’s check how to find the name of the token using python.

Let’s Import all the necessary libraries.

import pandas as pd
import requests
import json

After importing these libraries, we need to get all the contracts in the form of a pandas dataframe

url = 'https://margincalculator.angelbroking.com/OpenAPI_File/files/OpenAPIScripMaster.json'
d = requests.get(url).json()
tokendf = pd.DataFrame.from_dict(d)

After getting all the tokens in a dataframe, it gets very easy to find the exact token for any symbol.
Let’s check how we can find token for ASHOKLEY.

def get_token(stk): 
   m = tokendf.loc[tokendf['symbol'] == stk].iloc[0]['token']

print(get_token('ASHOKLEY'))

In the same way we can find symbol of any token using the below code.

def get_symbol(token): 
   m = tokendf.loc[tokendf['token'] == token].iloc[0]['symbol']

print(get_symbol('212'))
Scroll to Top