Livefeed using sockets


  • I'm having issue fetching the live feed using the API. I tried the sample code given on the Github but somehow I'm not able to get the live feed. Below is the code I used. If anybody knows how to resolve this, please let me know.

    CODE

    import logging
    from smartapi import SmartConnect, SmartSocket
    
    obj = SmartConnect(api_key="API_KEY")
    data = obj.generateSession("CLIENT_ID","PWD")
    tokens = obj.generateToken(data['data']['refreshToken'])
    
    feedToken = tokens['data']['feedToken']
    token = 'nse_cm|2885'
    
    logging.debug(feedToken)
    
    ss = SmartSocket(feedToken, 'CLIENT_ID')
    
    def on_tick(ws, tick):
    	logging.debug("Ticks: {}".format(ticks))
    
    def on_connect(ws, response):
    	logging.debug("on connect")
    	ws.send_request(token)
    
    def on_error(ws, code, reason):
    	logging.debug("Connection error: {code} - {reason}".format(code=code, reason=reason))
    
    def on_close(ws, code, reason):
    	logging.debug("on close")
    	ws.stop()
    
    
    ss.on_ticks = on_tick
    ss.on_connect = on_connect
    ss.on_error = on_error
    ss.on_close = on_close
    
    ss.connect(threaded = True)
    
    while True:
    	def on_ticks(ws, ticks):
    		helper_method(ticks)
    
    	def helper_method(ticks):
    		print("Ticks received from while loop: {}".format(tick))
    
    	ss.on_ticks = on_ticks
    

    RESPONSE

    PAYLOAD eJyLrlZKzFayUsrPVtJRKkksBrGT84Ds3OJ0EDM/Ly81uSQ1Rak2FgAaGQ1z
    DATA b'x\x9c\x8b\xaeVJ\xccV\xb2R\xca\xcfV\xd2Q*I,\x06\xb1\x93\xf3\x80\xec\xdc\xe2t\x103?//5\xb9$5E\xa96\x16\x00\x1a\x19\rs'
    PAYLOAD eJyLrlZKzFayUsrPVtJRKkksBrGT84Ds3OJ0EDM/Ly81uSQ1Rak2FgAaGQ1z
    DATA b'x\x9c\x8b\xaeVJ\xccV\xb2R\xca\xcfV\xd2Q*I,\x06\xb1\x93\xf3\x80\xec\xdc\xe2t\x103?//5\xb9$5E\xa96\x16\x00\x1a\x19\rs'
    

    After this response, nothing happens.


  • @bhaveshjain hi bhavesh please provide a clear understanding of how to stream tick data, as i have been through documentation but it is not clear how to do the same , as mentioned by @rjbanna i'm also getting the same error , if there is any other method to do the same please let us know,


  • @bhaveshjain Any updates on this?


  • Hi @rjbanna @dorje ,

    I have tried to implement live feed using NodeJS SDK & getting the below-mentioned output. If you are receiving a similar result, your code is perfect & it should provide you actual data while the market is live (9:00 - 3:30). You just have to re-run code when the market is live, you will get output for every seconds on your terminal/console.

    Console Output:
    ak:'nk'
    msg:'cn'
    task:'cn'

    d4c75061-b5e4-4e1c-8eb9-d134424c5208-image.png

    Hope this might help you.

    -vishant


  • @vishant No, I'm not getting the desired output. I had tried the given Python code during the market hours also, but it was giving me the same error. May be it is the issue with Python SDK or I'm missing some parameters.


  • Hi @rjbanna,

    I am using Node SDK - https://github.com/angelbroking-github/smartapi-javascript

    Also, I am using the same code which is mentioned in the link above.

    let web_socket = new WebSocket({
    client_code: "CLIENT_CODE",
    feed_token: "FEED_TOKEN",
    script: "nse_cm|2885&nse_cm|1594" //exchange|token for multi stocks use & seperator
    });

    web_socket.connect();
    web_socket.on('tick', receiveTick)

    function receiveTick(data) {
    console.log("Receive stock ticks::", data)
    }

    You can also try copy pasting the code from Python SDK, it should work.


  • @vishant I tried the same code before also but it's not working.


  • It looks like issue is with the python, i think they have not started python websocket streaming service, there is no word from @bhaveshjain regarding this matter, and there is no proper documentation for python ,
    so I'm hoping @bhaveshjain could clarify this matter, and how long it will take when service will resume for python.


  • @bhaveshjain ok ..


  • @bhaveshjain Sure, I'll check it and let you know soon.


  • @bhaveshjain I checked the websocket API and it is working as expected. There is one typo in the README.md file on Github. In the code feedToken=obj.getFeedToken(), the function name is misspelt. In the smartConnect.py file, the function name is getfeedToken(). with the "feed" in lowercase where as in the example it is "Feed" with first letter capital. So kindly resolve that issue or else people will receive the following error "'SmartConnect object has no attribute 'getFeedToken'". Thanks


  • @bhaveshjain Can you please elaborate following terms that I receive as a response from socket? They are to, lo, sp, c(if this is close, then which close is it referring to?), tk, bs, bp, cng, bq, ap, nc, name, tbq.


  • Hi @rjbanna,

    Request you to raise this same query as a general discussion. So that other people will also be informed about this.


  • @vishant I have created the new thread for the same.


  • Hi @admin @bhaveshjain I am using latest SMARTAPI-PYTHON and getting the below output only for token="nse_cm|3045". I have tried few more ticks as well but still getting the same output.

    Ticks: [{'ak': 'nk', 'msg': 'hb', 'task': 'hb'}]
    Ticks: [{'ak': 'nk', 'msg': 'hb', 'task': 'hb'}]
    Ticks: [{'ak': 'nk', 'msg': 'hb', 'task': 'hb'}]
    Ticks: [{'ak': 'nk', 'msg': 'hb', 'task': 'hb'}]
    Ticks: [{'ak': 'nk', 'msg': 'hb', 'task': 'hb'}]
    Ticks: [{'ak': 'nk', 'msg': 'hb', 'task': 'hb'}]

    Please let me know how can I get data.


  • Hi @dushyant , I suppose you're passing wrong feedToken. Please review.


  • @admin @vishant .

    I am facing the exact issue faced by @dushyant . Even I used the same token: 3045 which is the token for SBIN-EQ.

    {"token":"3045","symbol":"SBIN-EQ","name":"SBIN","expiry":"","strike":"-1.000000","lotsize":"1","instrumenttype":"","exch_seg":"NSE","tick_size":"5.000000"}

    The data is from the instruments API. which means the instrument data is wrong?

    BR
    Dheepi


  • Hi @dheepi , please share your contact details on api@angelbroking.com


  • @admin Thanks for your help. The issue is solved.


  • @admin
    Getting same error
    Co No:- 8000662244
    mail:- mangroliyadj6293@gmail.com
    Client id : D108750