Need assistance on WebSocket integration.

Currently, our strategy relies on the alerts API to compare the Last Traded Price (LTP) with the first candle’s High in order to trigger buy or sell actions.

However, due to limitations associated with using alerts, we aim to transition towards WebSocket integration. If anyone have a template code for comparing the LTP against our specified parameter and to store the same in DB or temp variables in Python, it would be highly helpful for us to proceed further.

Else, can someone please provide your inputs on how to bring on (implementing) this functionality?

Regards

Archana

Hi Archana, I’m not completely understanding this requirement and would like to get in touch with you to help you out implementing this functionality.

Please reach out to me by messaging me directly, will solve your query 1:1. We’ve implemented similar kind of algorithms for our users which are successfully running and generating returns for them.

Kindly DM me via Fyers community itself.

P.S. - I’ll be posting the solution/relevant details for solving the above problem upon communication with the user in this thread itself.

I am trying to compare the LTP with the first candle’s high in real time. I thought to resolute the real-time LTP to the desired time frame candle and compare it with the first candle high to trigger the order. But I have no idea how to do the same through the WebSocket.

First start the code at 9:15 AM and store the value of the 1st candle high as per your desired timeframe in a variable. Once you’ve it stored in the variable, start the websocket now, the websocket will constantly provide you with LTP data which you can compare to the global fixed variable stored earlier. Based on this you can create your code as per the normal websocket code.

Hi Archit,

Is it possible for a quick conversation over the phone please.

Kindly provide your mail id or request you to DM me your contact details to [email protected]

Hi Archit,

If possible, can you please send the Template code which would be highly helpful.

Regards

Archana

Hi Archit,

Good morning.

Request you to revert pls. Tks in advance.

I don’t have a template code for this issue…all our codes are developed with specific use case for our clients. You can contact me and based on your requirement, we can develop the code for your use case.

Hi Archit,

Thanks for your response. Please let me know on how to connect (personally) with you for a discussion on our requirement.

My mail id is [email protected].

Regards

Archana

Hi Archana,

Here is a sample code to keep gettting LTP of 2 stocks. You can add more stocks in the list in onopen() function.

The LTP is in variable message[‘ltp’]

from fyers_apiv3.FyersWebsocket import data_ws

client_id = open("client_id.txt",'r').read()
access_token = open("access_token.txt",'r').read()

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)
    print(message['symbol'], message['ltp'])

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
    symbols = ['NSE:SBIN-EQ', 'NSE:ADANIENT-EQ']
    fyers.subscribe(symbols=symbols, data_type=data_type)

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

# 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=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
fyers.connect()

Hi Aseem,

Thank you so much for your post !!!