Once the broker_connection handle is ready, it can be used to query the list containing all the financial instruments provided by the broker.
Getting ready
Make sure the broker_connection object is available in your Python namespace. Refer to the previous recipe in this chapter to set up this object.
How to do it…
We execute the following steps to complete this recipe:
- Display all the instruments:
>>> instruments = broker_connection.get_all_instruments()
>>> instruments
You will get an output similar to the following. The exact output may differ for you:
instrument_token exchange_token tradingsymbol name last_price expiry strike tick_size lot_size instrument_type segment exchange
0 267556358 1045142 EURINR20AUGFUT EURINR 0.0 2020-08-27 0.0 0.0025 1 FUT BCD-FUT BCD
1 268660998 1049457 EURINR20DECFUT EURINR 0.0 2020-12-29 0.0 0.0025 1 FUT BCD-FUT BCD
2 266440966 1040785 EURINR20JULFUT EURINR 0.0 2020-07-29 0.0 0.0025 1 FUT BCD-FUT BCD
3 266073606 1039350 EURINR20JUNFUT EURINR 0.0 2020-06-26 0.0 0.0025 1 FUT BCD-FUT BCD
4 265780742 1038206 EURINR20MAYFUT EURINR 0.0 2020-05-27 0.0 0.0025 1 FUT BCD-FUT BCD
... ... ... ... ... ... ... ... ... ... ... ... ...
64738 978945 3824 ZODJRDMKJ ZODIAC JRD-MKJ 0.0 0.0 0.0500 1 EQ NSE NSE
64739 2916865 11394 ZOTA ZOTA HEALTH CARE 0.0 0.0 0.0500 1 EQ NSE NSE
64740 7437825 29054 ZUARI-BE ZUARI AGRO CHEMICALS 0.0 0.0 0.0500 1 EQ NSE NSE
64741 979713 3827 ZUARIGLOB ZUARI GLOBAL 0.0 0.0 0.0500 1 EQ NSE NSE
64742 4514561 17635 ZYDUSWELL ZYDUS WELLNESS 0.0 0.0 0.0500 1 EQ NSE NSE
64743 rows × 12 columns
- Print the total number of instruments:
>>> print(f'Total instruments: {len(instruments)}')
We get the following output (your output may differ):
Total instruments: 64743
How it works…
The first step fetches all the available financial instruments using the get_all_instruments() method of broker_connection. This method returns a pandas.DataFrame object. This object is assigned to a new variable, instruments, which is shown in the output of step 1. This output may differ for you as new financial instruments are frequently added and existing ones expire regularly. The final step shows the total number of instruments provided by the broker.