Get instruments dump - AB API

Hi,

Do we have an option or a function to get a dump of all the instruments using Alice’s blue API, I need the list to create a function for my strategy, basically I need to retrieve tokens of required tickers

for reference - Kite has this function - kite.instruments(“NSE”) to fetch all the instruments on NSE or any other exchange

Any help will be appreciated
Thanks

There is nothing of that sort in Alice blue. Use Kite only.

Follow - github.com/aeron7/nsequant/blob/main/How_to_get_DataToken_of_a_stock.ipynb

So i reached out to Krishna’s Github and he told me
All master contracts are stored in 2 private variable. You can access them like this.

alice._AliceBlue__master_contracts_by_token
alice._AliceBlue__master_contracts_by_symbol

So i tried this

alice._AliceBlue__master_contracts_by_symbol
data = pd.DataFrame(alice._AliceBlue__master_contracts_by_symbol)

Output -

      Index                                    NSE
1018GS2026 GS   Instrument(exchange='NSE', token=6833, symbol='1018GS2026 GS', name='GOI LOAN 10.18% 
                            2026', expiry=None, lot_size='1')
1025GS2021 GS   Instrument(exchange='NSE', token=6819, symbol='1025GS2021 GS', name='GOI LOAN 10.25% 
                            2021', expiry=None, lot_size='1')
116GS2020 GS    Instrument(exchange='NSE', token=6814, symbol='116GS2020 GS', name='GOI LOAN 11.60% 
                           2020', expiry=None, lot_size='1')
182D010721 TB   Instrument(exchange='NSE', token=1776, symbol='182D010721 TB', name='GOI TBILL 182D- 
                            01/07/21', expiry=None, lot_size='100')
182D020921 TB   Instrument(exchange='NSE', token=2593, symbol='182D020921 TB', name='GOI TBILL 182D- 
                            02/09/21', expiry=None, lot_size='100')

I want a dataframe like this from inside the above dataframe

    Index      Exchange  token     symbol             name            expiry  lot_size 
1018GS2026 GS    NSE     6833   1018GS2026 GS  GOI LOAN 10.18% 2026    None      1
1025GS2021 GS    NSE     6819   1025GS2021 GS  GOI LOAN 10.25% 2021    None      1
116GS2020 GS     NSE     6814   116GS2020 GS   GOI LOAN 11.60% 2020    None      1
182D010721 TB    NSE     1776   182D010721 TB  GOI TBILL 182D-01/07/21 None     100
182D020921 TB    NSE     2593   182D020921 TB  GOI TBILL 182D-02/09/21 None     100

so i tried this -

  rows = []

for val in alice._AliceBlue__master_contracts_by_symbol.values():
    rows.append( [val.exchange, val.token, val.symbol, val.name,
                val.expiry, val.lot_size] )

df = pd.DataFrame( rows,
    index=alice._AliceBlue__master_contracts_by_symbol.keys(),
    columns=['exchange', 'token', 'symbol', 'name', 'expiry', 'lot_size'])

But it returns an error stating -

 AttributeError: 'collections.OrderedDict' object has no attribute 'exchange'

Bro , can you tell me what am i missing here , i have tried looking everywhere but cant solve this plus this will help every Aliceblue API user replicating kite’s kite.instruments() function

Thanks

Anyone trying to create a replica of kite.instruments() function for alice blue, use the code down below

df = pd.DataFrame(alice._AliceBlue__master_contracts_by_symbol)
df = pd.json_normalize([x._asdict() for x in df['NSE']]).set_index(df.index)

Thanks

1 Like

Thanks a lot for posting this piece. I will use it somewhere for sure !! :slight_smile:

1 Like