Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket connection getting disconnected after 10-15 seconds. #152

Open
saugatapaul1010 opened this issue Oct 17, 2024 · 5 comments
Open

Comments

@saugatapaul1010
Copy link

Hello Breeze Team,

I am trying to get the second by second candle data for Reliance and Infosys using the Breeze web-socket, however the connection get's discontinued abruptly. I will attach the code to replicate the issue at your end, along with the error message displayed in the console. What should be the fix for this? Can anything be done at your end to improve the integrity and robustness of the web-socket data streaming functionality? Is there any alternative approach that is present ?

Code :

from breeze_connect import BreezeConnect
from datetime import datetime, timedelta

key_api = "*******************************************"
key_secret =  "*******************************************"
key_session = ********"

breeze = BreezeConnect(api_key=key_api)
breeze.generate_session(api_secret=key_secret, session_token=key_session)
breeze.ws_connect()
data = []

# Callback to receive ticks.
def on_ticks(ticks):
    data.append(ticks)
    print("Ticks: {}".format(ticks))

breeze.on_ticks = on_ticks

while True:
    #breeze.subscribe_feeds(stock_token=["4.1!1594", "4.1!2885"], interval="1second")
    breeze.subscribe_feeds(stock_token="4.1!1594", interval="1second")
    breeze.subscribe_feeds(stock_token="4.1!2885", interval="1second")

Output + Errors :

Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'INFTEC', 'low': '1974.55', 'high': '1974.55', 'open': '1974.55', 'close': '1974.55', 'volume': '1001', 'datetime': '2024-10-17 15:29:54'}
Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'RELIND', 'low': '2714.8', 'high': '2714.8', 'open': '2714.8', 'close': '2714.8', 'volume': '49', 'datetime': '2024-10-17 15:29:54'}
Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'INFTEC', 'low': '1974.05', 'high': '1974.05', 'open': '1974.05', 'close': '1974.05', 'volume': '127', 'datetime': '2024-10-17 15:29:55'}
Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'RELIND', 'low': '2713.3', 'high': '2713.3', 'open': '2713.3', 'close': '2713.3', 'volume': '162', 'datetime': '2024-10-17 15:29:55'}
Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'INFTEC', 'low': '1975.25', 'high': '1975.25', 'open': '1975.25', 'close': '1975.25', 'volume': '451', 'datetime': '2024-10-17 15:29:56'}
Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'RELIND', 'low': '2713.3', 'high': '2713.3', 'open': '2713.3', 'close': '2713.3', 'volume': '113', 'datetime': '2024-10-17 15:29:56'}
Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'INFTEC', 'low': '1974.7', 'high': '1974.7', 'open': '1974.7', 'close': '1974.7', 'volume': '52', 'datetime': '2024-10-17 15:29:57'}
Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'RELIND', 'low': '2713.3', 'high': '2713.3', 'open': '2713.3', 'close': '2713.3', 'volume': '136', 'datetime': '2024-10-17 15:29:57'}
Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'INFTEC', 'low': '1975.0', 'high': '1975.0', 'open': '1975.0', 'close': '1975.0', 'volume': '177', 'datetime': '2024-10-17 15:29:58'}
Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'INFTEC', 'low': '1975.0', 'high': '1975.0', 'open': '1975.0', 'close': '1975.0', 'volume': '177', 'datetime': '2024-10-17 15:29:58'}
Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'RELIND', 'low': '2713.3', 'high': '2713.3', 'open': '2713.3', 'close': '2713.3', 'volume': '252', 'datetime': '2024-10-17 15:29:58'}
Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'RELIND', 'low': '2713.3', 'high': '2713.3', 'open': '2713.3', 'close': '2713.3', 'volume': '252', 'datetime': '2024-10-17 15:29:58'}
Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'INFTEC', 'low': '1974.55', 'high': '1974.55', 'open': '1974.55', 'close': '1974.55', 'volume': '161', 'datetime': '2024-10-17 15:30:00'}
Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'INFTEC', 'low': '1974.55', 'high': '1974.55', 'open': '1974.55', 'close': '1974.55', 'volume': '161', 'datetime': '2024-10-17 15:30:00'}
Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'RELIND', 'low': '2713.3', 'high': '2713.3', 'open': '2713.3', 'close': '2713.3', 'volume': '468', 'datetime': '2024-10-17 15:30:00'}
Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'RELIND', 'low': '2713.3', 'high': '2713.3', 'open': '2713.3', 'close': '2713.3', 'volume': '10', 'datetime': '2024-10-17 15:30:01'}
Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'RELIND', 'low': '2713.3', 'high': '2713.3', 'open': '2713.3', 'close': '2713.3', 'volume': '468', 'datetime': '2024-10-17 15:30:00'}
Ticks: {'interval': '1second', 'exchange_code': 'NSE', 'stock_code': 'RELIND', 'low': '2713.3', 'high': '2713.3', 'open': '2713.3', 'close': '2713.3', 'volume': '10', 'datetime': '2024-10-17 15:30:01'}
Traceback (most recent call last):
Traceback (most recent call last):
  File "C:\Users\z004ns2e\AppData\Local\anaconda3\envs\trading\lib\site-packages\breeze_connect\breeze_connect.py", line 99, in watch_stream_data
    self.sio.emit('join', data)
  File "C:\Users\z004ns2e\AppData\Local\anaconda3\envs\trading\lib\site-packages\socketio\client.py", line 214, in emit
    raise exceptions.BadNamespaceError(
socketio.exceptions.BadNamespaceError: / is not a connected namespace.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "e:\Personal\Trading-Bot-Backend\src\real-time-data-stream\data_streamer_websocket.py", line 244, in <module>
  File "C:\Users\z004ns2e\AppData\Local\anaconda3\envs\trading\lib\site-packages\breeze_connect\breeze_connect.py", line 99, in watch_stream_data
    self.sio.emit('join', data)
  File "C:\Users\z004ns2e\AppData\Local\anaconda3\envs\trading\lib\site-packages\socketio\client.py", line 214, in emit
    raise exceptions.BadNamespaceError(
socketio.exceptions.BadNamespaceError: / is not a connected namespace.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "e:\Personal\Trading-Bot-Backend\src\real-time-data-stream\data_streamer_websocket.py", line 244, in <module>
    raise exceptions.BadNamespaceError(
socketio.exceptions.BadNamespaceError: / is not a connected namespace.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "e:\Personal\Trading-Bot-Backend\src\real-time-data-stream\data_streamer_websocket.py", line 244, in <module>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "e:\Personal\Trading-Bot-Backend\src\real-time-data-stream\data_streamer_websocket.py", line 244, in <module>

Traceback (most recent call last):
  File "e:\Personal\Trading-Bot-Backend\src\real-time-data-stream\data_streamer_websocket.py", line 244, in <module>
Traceback (most recent call last):
  File "e:\Personal\Trading-Bot-Backend\src\real-time-data-stream\data_streamer_websocket.py", line 244, in <module>
  File "e:\Personal\Trading-Bot-Backend\src\real-time-data-stream\data_streamer_websocket.py", line 244, in <module>
    breeze.subscribe_feeds(stock_token="4.1!1594", interval="1second")
  File "C:\Users\z004ns2e\AppData\Local\anaconda3\envs\trading\lib\site-packages\breeze_connect\breeze_connect.py", line 382, in subscribe_feeds
    self.sio_ohlcv_stream_handler.watch_stream_data(stock_token,interval)
  File "C:\Users\z004ns2e\AppData\Local\anaconda3\envs\trading\lib\site-packages\breeze_connect\breeze_connect.py", line 105, in watch_stream_data
    raise Exception(except_message.OHLC_SOCKET_CONNECTION_DISCONNECTED.value)
Exception: Failed to connect to OHLC stream

Can you please help me debug this issue? Also, I want to know what is the maximum number of instruments that I can subscribe to using a single web-socket connection? Is there any limit ? Also, is there any alternative ways to get the data for multiple instruments in real-time using stock_code, instead of stock_token ?

@Idirect-Tech
Copy link
Owner

Idirect-Tech commented Oct 17, 2024 via email

@saugatapaul1010
Copy link
Author

saugatapaul1010 commented Oct 18, 2024

Thank you. I am well aware of the errors, the reason I have started this thread is to find out a resolution to these errors. As you might have clearly seen in the code, I have connected to the web-socket using this code breeze.ws_connect(). And this is as per documentation.

Also, can you please answer the remaining questions that I have asked in my original thread?

@saugatapaul1010
Copy link
Author

I am pasting the list of packages used along with their versions, in the requirements.txt file. You may want to create a new Python environment and install these packages with the exact same version, so that the issue can be reproducible at your end.

requirements.txt file.

breeze-connect==1.0.56
psycopg2-binary==2.9.9
selenium==4.16.0
PyYAML==6.0.1
pytest==8.1.1
tqdm==4.66.2
#TA-Lib==0.4.28
Backtesting==0.3.3
plotly==5.22.0
kafka-python==2.0.2
#Kaafka Windows Version - 3.7.1
matplotlib==3.9.2```

@balaji99
Copy link

I'm facing the same issue. I subscribed to the feeds after connecting to the WebSocket. After a few ticks, the websocket disconnects.

@Idirect-Tech
Copy link
Owner

Idirect-Tech commented Nov 11, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants