Market Data Not Working

Hello Team Fyers, I have tried your Live Market Data code and it is not working properly. I've done all the session generation and auth_code process. But the live data is not fetching and sometimes it is fetching but only 2 or 3 ticks not continuous. Please let me know if anything to do with the code or with the authentication process. Thank you.

Code :

pip install fyers-apiv3

from fyers_apiv3 import fyersModel
import pandas as pd
import datetime as dt

user = pd.read_excel("user.xlsx")
client_id = user.iloc[0,0]
secret_key = user.iloc[0,1]
redirect_uri = "https://trade.fyers.in/api-login/redirect-uri/index.html"
response_type = "code"
grant_type = "authorization_code"

# Create a session model with the provided credentials
session = fyersModel.SessionModel(
    client_id=client_id,
    secret_key=secret_key,
    redirect_uri=redirect_uri,
    response_type=response_type
)

# Generate the auth code using the session model
response = session.generate_authcode()

print(response)

auth_code ="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhcGkubG9naW4uZnllcnMuaW4iLC...."

# Create a session object to handle the Fyers API authentication and token generation
session = fyersModel.SessionModel(
    client_id=client_id,
    secret_key=secret_key,
    redirect_uri=redirect_uri,
    response_type=response_type,
    grant_type=grant_type
)

# Set the authorization code in the session object
session.set_token(auth_code)

# Generate the access token using the authorization code
response = session.generate_token()

# Print the response, which should contain the access token and other details
print(response)

df = pd.DataFrame()
df = response
tk = df["access_token"]
print(tk)

# Initialize the FyersModel instance with your client_id, access_token, and enable async mode
fyers = fyersModel.FyersModel(client_id=client_id, is_async=False, token=tk, log_path="")

# Make a request to get the user profile information
response = fyers.get_profile()

# Print the response received from the Fyers API
print(response)

from fyers_apiv3.FyersWebsocket import data_ws


def onmessage(message):

    print("Response:", message)


def onerror(message):

    print("Error:", message)


def onclose(message):

    print("Connection closed:", message)

def onopen():

    # Specify the data type and symbols you want to subscribe to
    data_type = "SymbolUpdate"
    # data_type = "OnTrades"
    # data_type = "OnPositions"
    # data_type = "OnGeneral"
    # data_type = "OnOrders,OnTrades,OnPositions,OnGeneral"

    # Subscribe to the specified symbols and data type
    symbols = ["NSE:NIFTYBANK-INDEX"]
    wfyers.subscribe(symbols=symbols, data_type=data_type)


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


# Replace the sample access token with your actual access token obtained from Fyers
waccess_token = client_id + ":" + tk

# Create a FyersDataSocket instance with the provided parameters
wfyers = data_ws.FyersDataSocket(
    access_token=waccess_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=False,                  # 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
wfyers.connect()


I've added the output image below.

2 replies