LTP getting mixed up with different symbols

Hello Team,

I am having weired scenario when I am trying to subscribe multiple symbol at some intervals. For first symbol it works fine but for next symbols its mixing ltp prices with each other symbols., Please find below sample code,

from fyers_apiv3.FyersWebsocket import data_ws
import time

def fetch_data_for_symbol(symbol):
    def onmessage(message):
        """
        Callback function to handle incoming messages from the FyersDataSocket WebSocket.

        Parameters:
            message (dict): The received message from the WebSocket.

        """
        print("Response:", message)

    def onerror(message):
        """
        Callback function to handle WebSocket errors.

        Parameters:
            message (dict): The error message received from the WebSocket.

        """
        print("Error:", message)

    def onclose(message):
        """
        Callback function to handle WebSocket connection close events.
        """
        print("Connection closed:", message)

    def onopen():
        """
        Callback function to subscribe to data type and symbols upon WebSocket connection.

        """
        # Specify the data type and symbols you want to subscribe to
        data_type = "SymbolUpdate"

        # Subscribe to the specified symbols and data type
        fyers.unsubscribe(symbols=symbol, data_type=data_type)
        time.sleep(3)
        fyers.subscribe(symbols=symbol, data_type=data_type)

        # Keep the socket running to receive real-time data
        fyers.keep_running()

# Replace the sample access token with your actual access token obtained from Fyers
    access_token = "VWXXXXXJV-100:XXXX"

    # Create a FyersDataSocket instance with the provided parameters
    fyers = data_ws.FyersDataSocket(
        access_token=access_token,  # Access token in the format "appid:accesstoken"
        log_path="",  # Path to save logs. Leave empty to auto-create logs in the current directory.
        litemode=True,  # Lite mode disabled. Set to True if you want a lite response.
        write_to_file=False,  # Save response in a log file instead of printing it.
        reconnect=True,  # Enable auto-reconnection to WebSocket on disconnection.
        on_connect=onopen,  # Callback function to subscribe to data upon connection.
        on_close=onclose,  # Callback function to handle WebSocket connection close events.
        on_error=onerror,  # Callback function to handle WebSocket errors.
        on_message=onmessage  # Callback function to handle incoming messages from the WebSocket.
    )

    # Establish a connection to the Fyers WebSocket
    fyers.connect()

symbols = ['MCX:CRUDEOIL24SEPFUT', 'MCX:CRUDEOIL24SEP6000CE']
for symbol in symbols:
    print(symbol)
    fetch_data_for_symbol([symbol])
    time.sleep(10)

Output of the program:

MCX:CRUDEOIL24SEPFUT

Response: {‘type’: ‘cn’, ‘code’: 200, ‘message’: ‘Authentication done’, ‘s’: ‘ok’}

Response: {‘type’: ‘lit’, ‘code’: 200, ‘message’: ‘Lite Mode On’, ‘s’: ‘ok’}

Response: {‘type’: ‘sub’, ‘code’: 200, ‘message’: ‘Subscribed’, ‘s’: ‘ok’}

Response: {‘ltp’: 6129.0, ‘symbol’: ‘MCX:CRUDEOIL24SEPFUT’, ‘type’: ‘sf’}

MCX:CRUDEOIL24SEP6000CE

Response: {‘type’: ‘cn’, ‘code’: 200, ‘message’: ‘Authentication done’, ‘s’: ‘ok’}

Response: {‘type’: ‘lit’, ‘code’: 200, ‘message’: ‘Lite Mode On’, ‘s’: ‘ok’}

Response: {‘type’: ‘sub’, ‘code’: 200, ‘message’: ‘Subscribed’, ‘s’: ‘ok’}

Response: {‘ltp’: 263.3, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}

Response: {‘ltp’: 6131.0, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}

Response: {‘ltp’: 263.5, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}

Response: {‘ltp’: 6131.0, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}

Response: {‘ltp’: 263.5, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}

Response: {‘ltp’: 6131.0, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}

Response: {‘ltp’: 263.5, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}

Response: {‘ltp’: 6131.0, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}

Response: {‘ltp’: 263.5, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}

If you see carefully : its mixing prices of CRUDEOIL24SEPFUT & CRUDEOIL24SEP6000CE

Hey @amit_moghe

Could you please DM your details so that we can connect over a call and understand this better

kindly post full response of JSON message.

I call them bad ticks and I am currently filtering them with.

## validTick is defined as market open timestamp. This works

if self.validTick > msg['exch_feed_time']: return

Can also dynamically update the validTick timestamp to say a few minutes before current time. Some illiquid scrips in MCX trade very slowly. Ideally tick should not be more than 1-2 secs behind current time.

Fyers websocket data corruption

I am also facing the same issue, sometimes nifty index ltp is being assigned to the options key triggering exit in my strategy.

Hey @9IC04gMybe ,

Could you please DM the code snippet used to derive the LTP of the symbols

Hi @WWhYB2qrsx, I have sent you the details. Do let me know if there is any flaw in the code.

Hi @WWhYB2qrsx ,

Please look at the logs below,
I had Nifty-Index and few options data subscribed. Later I unsubscribed the options data. Hence only Nifty-index was getting fetched.
when connection was reset, it started giving inappropriate LTP . Hope this helps.

{‘ltp’: 23515.75, ‘symbol’: ‘NSE:NIFTY50-INDEX’, ‘type’: ‘if’}

{‘ltp’: 23515.6, ‘symbol’: ‘NSE:NIFTY50-INDEX’, ‘type’: ‘if’}

{‘ltp’: 23515.45, ‘symbol’: ‘NSE:NIFTY50-INDEX’, ‘type’: ‘if’}

{‘ltp’: 23515.9, ‘symbol’: ‘NSE:NIFTY50-INDEX’, ‘type’: ‘if’}

{‘ltp’: 23515.25, ‘symbol’: ‘NSE:NIFTY50-INDEX’, ‘type’: ‘if’}

{‘ltp’: 23514.8, ‘symbol’: ‘NSE:NIFTY50-INDEX’, ‘type’: ‘if’}

{‘ltp’: 23514.55, ‘symbol’: ‘NSE:NIFTY50-INDEX’, ‘type’: ‘if’}

message [WinError 10054] An existing connection was forcibly closed by the remote host

message [Errno 11001] getaddrinfo failed

updated list [‘NSE:NIFTY50-INDEX’]

message [Errno 11001] getaddrinfo failed

updated list [‘NSE:NIFTY50-INDEX’]

{‘type’: ‘cn’, ‘code’: 200, ‘message’: ‘Authentication done’, ‘s’: ‘ok’}

‘symbol’

{‘type’: ‘lit’, ‘code’: 200, ‘message’: ‘Lite Mode On’, ‘s’: ‘ok’}

‘symbol’

updated list [‘NSE:NIFTY50-INDEX’]

{‘type’: ‘sub’, ‘code’: 200, ‘message’: ‘Subscribed’, ‘s’: ‘ok’}

‘symbol’

{‘ltp’: 23511.1, ‘symbol’: ‘NSE:NIFTY50-INDEX’, ‘type’: ‘if’}

{‘ltp’: 23511.0, ‘symbol’: ‘NSE:NIFTY2520623300PE’, ‘type’: ‘sf’}

INAPPROPRIATE LTP {‘ltp’: 23511.0, ‘symbol’: ‘NSE:NIFTY2520623300PE’, ‘type’: ‘sf’}

{‘ltp’: 23511.35, ‘symbol’: ‘NSE:NIFTY2520623300PE’, ‘type’: ‘sf’}

INAPPROPRIATE LTP {‘ltp’: 23511.35, ‘symbol’: ‘NSE:NIFTY2520623300PE’, ‘type’: ‘sf’}

{‘ltp’: 23510.35, ‘symbol’: ‘NSE:NIFTY2520623300PE’, ‘type’: ‘sf’}

Hi @9IC04gMybe

I posted the same issue in another post for this same issue recently also. @Chirag helped me out with a workaround(Please check the comments section). He pointed out that whenever we get such bad tick, the exch_time_stamp is wrong. Its somewhere in the 1970 range. So like Chirag pointed, I built a filter to check if the timestamp is right. If yes, I proceed with the tick.

But the issue still exists.

https://fyers.in/community/t/data-mismatch-during-subscription-disconnection/13485