why does the code in python disconnect while fetching websocket data

genesis
i am running something like this to fetch data from websocket to csv

python gettingdata.py >> data.csv

but this automatically disconnects after some time
Tagged:
  • rakeshr
    @genesis
    Can you give us more detail about the above issue? Is there any error, while connecting to WebSocket?
  • genesis
    there is no error while connecting to websocket but it closes down after some time unknowingly
  • sujith
    This might happen when your main thread which is listening to ticks is overloaded. Try offloading your task to a secondary thread after you receive a tick.
  • genesis
    if i make it threaded like this :

    kws.connect(threaded=True)

    it exits straight away

    if i write something like this :

    count=0
    while True:
    count+=1
    if(count%2==0):
    if kws.is_connected():
    kws.set_mode(kws.MODE_FULL,tokens)
    else:
    if kws.is_connected():
    kws.set_mode(kws.MODE_FULL,tokens)
    time.sleep(0.01)

    it is not able to get data for all the symbols.

    i would also like to know if it is possible for us to tell the websocket not to get data for certain tokens after their exchange time is over , like not getting data for nse equities , fno , options after 3:30 pm , cds and its options after 5:00 pm and likewise for commodities after 11:55 pm ?
  • genesis
    @sujith , @rakeshr , it would be helpful if you could reply to this ?
  • naz
    @genesis : are you running your code on ubuntu machine. We are facing the same issue of "kws.connect(threaded=True) - exits straight away".
  • jashp
    Sensum Fintech is a company which aims to democratise algorithmic trading in India.
    Our mission is to build India’s first crowdsourced quant fund.

    We are looking for algo traders and quantitative enthusiasts who have developed trading strategies for the Indian markets. The signals should be mid frequency or low frequency and derivative strategies are eligible too. We have partnered with a wealth management firm as well as a few of India’s top brokers.

    Discretionary traders can also become a part of the company and we can help their strategies to be automated and reach a wider client base.

    The perks include capital allocation to your strategies as well as revenue sharing if your strategy is selected to be published on our platform.

    Interested people can reach me at [email protected]
    Whatsapp : 7021520557
  • zartimus
    @genesis
    Instead of redirecting stdout to file, you could simply write that logic inside your script.
    Websocket disconnects can occur due to several problems. But we do have re-connection mechanism to handle such cases.

    Speaking of reconnection , please note that doing a `ws.stop()` inside `on_close` callback will stop the underlying event loop which takes care of the websocket connection. And no reconnection will happen then. So if you have a ws.stop() inside on_close callback and still wants the reconnection, please remove it. Ref comments in https://github.com/zerodhatech/pykiteconnect#websocket-usage.
Sign In or Register to comment.