Kotak-Neo / kotak-neo-api

96 stars 80 forks source link

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

Open AlbertBGeorge opened 8 months ago

AlbertBGeorge commented 8 months 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 8 months 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 8 months 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 8 months 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.