Idirect-Tech / Breeze-Python-SDK

The official Python client library for the ICICI Securities trading APIs
MIT License
61 stars 30 forks source link

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

Open saugatapaul1010 opened 6 days ago

saugatapaul1010 commented 6 days ago

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 commented 6 days ago

Dear Client,

As can be seen from below trail mail, there are 2 errors

1. Badname space error : This error usually comes when you are subscribing the feeds without been connected to websocket.

2. Failed to connect to OHLC stream - This error usually comes when you have low internet connectivity because of which connection to WebSocket gets disconnected.

[E29CF836]

Thanks & Regards,

Hiten Solanki,

Team Breeze API,

ICICI Securities


From: Saugata Paul @.> Sent: Thursday, October 17, 2024 5:24 PM To: Idirect-Tech/Breeze-Python-SDK @.> Cc: Subscribed @.***> Subject: [Idirect-Tech/Breeze-Python-SDK] Websocket connection getting disconnected after 10-15 seconds. (Issue #152)

CAUTION: This email originated from outside the organization! Do not click links, open attachments or reply, unless you recognize the sender's email address! Use 'Report suspicious Email' button in the toolbar to report Phishing mails. Beware!

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 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 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

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

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

— Reply to this email directly, view it on GitHubhttps://github.com/Idirect-Tech/Breeze-Python-SDK/issues/152, or unsubscribehttps://github.com/notifications/unsubscribe-auth/A3TQZ44523FLQSZMIYUXHTDZ36QRHAVCNFSM6AAAAABQDSFDF6VHI2DSMVQWIX3LMV43ASLTON2WKOZSGU4TINJQGEZDAOI. You are receiving this because you are subscribed to this thread.Message ID: @.***>

Print this mail only if absolutely necessary. Save Paper. Save Trees." "The information contained in this e-mail and any attachments to this message are intended for the exclusive use of the intended recipient and may contain proprietary, confidential or legally privileged information. If you are not the intended recipient, please note that you are not authorised to disseminate, distribute or copy this e-mail or any parts of it or act upon/rely on the contents of this e-mail in any manner. Please notify the sender immediately by e-mail and destroy all copies of this e-mail and any attachments. Please also note that ICICI Bank or its subsidiaries and associated companies, (collectively "ICICI Group"), are unable to exercise control or ensure the integrity of/over the contents of the information contained in e-mail transmissions and that any views expressed in this e-mail are not endorsed by/binding on the ICICI Group unless the sender does so expressly with due authority of ICICI Group. Before opening any attachments please check them for viruses and defects and please note that ICICI Group accepts no liability or responsibility for any damage caused by any virus that may be transmitted by this email. Any unauthorized use of this email or any attachment(s) may be treated as infringement of right(s) including right of privacy under the applicable laws of India including Information and Technology Act, 2000. Thank you for your cooperation. Regulatory Disclosures are available on https://www.icicisecurities.com (for institutional business) and www.icicidirect.com (for retail business).

saugatapaul1010 commented 5 days ago

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 commented 5 days ago

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```