Kotak-Neo / kotak-neo-api

120 stars 112 forks source link

Websocket: How to subscribe to index and FnO contracts at the same time? #79

Open AlbertBGeorge opened 1 year ago

AlbertBGeorge commented 1 year ago
inst_tokens = [{"instrument_token": "44465", "exchange_segment": "nse_fo"},
{"instrument_token": "Nifty 50", "exchange_segment": "nse_cm"}]
client.subscribe(instrument_tokens = inst_tokens, isIndex=False, isDepth=False) 

Since exchange identifier are not a number in case of Indexes, how do I subscribe to indices and FnO derivative contracts/ stocks etc at the same time? If I exclude the isIndex parameter, then it doesn't subscribe to the index.

youngking1686 commented 1 year ago

welcome to Neo api :). you can subscribe index using their exchange token directly like this, it works and i am doing so, to this you can append your other instruments and subscribe.

subs = [{"instrument_token": "26000", "exchange_segment": "nse_cm"},
        {"instrument_token": "26009", "exchange_segment": "nse_cm"}]

26000 - Nifty 50 26009 - Banknifty

AlbertBGeorge commented 1 year ago

Thanks @youngking1686, from where did you get instrument tokens for indices? These will be permanent, or do they keep changing? I generally don't like to hardcode things, if you can explain the process to get index instrument tokens, it would be helpful.

youngking1686 commented 1 year ago

from the scripmaster for nse_fo, it is available as 'pAssetCode' - the underlying instrument token. Those are exchange tokens I have been using for a longtime, common across apis.