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

Delayed / Missing Websocket data #163

Open
karanag opened this issue Nov 29, 2024 · 1 comment
Open

Delayed / Missing Websocket data #163

karanag opened this issue Nov 29, 2024 · 1 comment

Comments

@karanag
Copy link

karanag commented Nov 29, 2024

Hi Team,

I have been having issues with missing/delayed data. So I setup a simple script to fetch options prices from websocket and log them. The script is below.

Attached screenshot shows my console logs.
Screenshot 2024-11-29 at 12 59 12 PM

I have marked in red, quotes that are delayed. As you can see, some of them are significantly delayed like 3 to 4 minutes which is a rather long time. Ideally, at say 12.58.XX (XX being seconds), we should get quote for 12.57.00 candle for all subscribed instruments. Preferably within the first 5-10 seconds.

How can I mitigate this issue? I tried running it in parallel using threads but that doesn't improve it either.
For 5 minute quotes, data gets delayed by 4-6 minutes, so the data we get is sometimes 1 entire candle late.

Please advise, as this is causing errors in my signal generation which is messing up my trading activity.

# Initialize keys
api_key = "************************"
api_secret = "************************"
api_session = "************************"

# Import Libraries
from datetime import datetime
import time
from breeze_connect import BreezeConnect
import logging

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)

# Setup API keys
breeze = BreezeConnect(api_key=api_key)
breeze.generate_session(api_secret=api_secret, session_token=api_session)

# Define strike ranges for calls and puts
call_strikes = list(range(23600, 24400, 50))
put_strikes = list(range(23600, 24400, 50))


# Callback to handle received ticks
def on_ticks(ticks):
    # Log required fields: datetime, strike_price, right
    logging.info(f"Datetime: {ticks.get('datetime')}, Strike Price: {ticks.get('strike_price')}, Right: {ticks.get('right_type')}")

# Main Function
if __name__ == "__main__":
    print("Starting Execution \n")

    # Connect to WebSocket
    breeze.ws_connect()
    print("Connected to WebSocket")

    # Assign the on_ticks callback
    breeze.on_ticks = on_ticks

    # Create subscriptions for call and put options
    subscriptions = []
    for strike_price in call_strikes:
        subscriptions.append({
            "exchange_code": "NFO",
            "stock_code": "NIFTY",
            "product_type": "options",
            "expiry_date": datetime.strftime(datetime.now(), '%Y-%m-%d'),  # Replace with your expiry logic
            "strike_price": str(strike_price),
            "right": "call",
            "interval": "1minute"
        })

    for strike_price in put_strikes:
        subscriptions.append({
            "exchange_code": "NFO",
            "stock_code": "NIFTY",
            "product_type": "options",
            "expiry_date": datetime.strftime(datetime.now(), '%Y-%m-%d'),  # Replace with your expiry logic
            "strike_price": str(strike_price),
            "right": "put",
            "interval": "1minute"
        })
   

    # Subscribe to each feed
    for sub in subscriptions:
        answer = breeze.subscribe_feeds(
            exchange_code="NFO",
            stock_code="NIFTY",
            product_type="options",
            expiry_date="05-Dec-2024",
            strike_price=sub["strike_price"],
            right=sub["right"],
            interval="1minute"
        )
        logging.info(f"Subscribed: {answer}")

    # Keep the script running to receive data
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        print("Execution stopped.")
@karanag
Copy link
Author

karanag commented Nov 29, 2024

Screenshot 2024-11-30 at 1 32 52 AM

So shall I expect a response or spend my weekend exploring their API? I have been an ICICI customer since 2004 so not in a mood to shift unless forced to...

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

1 participant