Skip to main content

Websocket

Subscribe Feed

Websocket Subscribe Feed will provide multiple instruments available actions and possible values at once.

Websockets running as main process without thread

Tabbed Interface
"""Websockets"""
from thefirstock import thefirstock


handler = thefirstock.WebSocketHandler("{userId}")
listOfTradingSymbol = ["Nifty Bank", "Nifty 50"]


def subscribe_feed_data(data):
    thefirstock.subscribe_symbol(handler, "ACC-EQ")
    print(data)


# Modify the websocket_connection function to accept the handler as an argument
thread = thefirstock.websocket_connection(
    handler,
    listOfTradingSymbol,
    socket_connection=1,
    activate_sub_feed=True,
    callback_sub_feed=subscribe_feed_data
)

Websockets running as background process using thread

Tabbed Interface
"""Websockets"""
from thefirstock import thefirstock


handler = thefirstock.WebSocketHandler("PV0013")
listOfTradingSymbol = ["Nifty Bank", "Nifty 50"]


def subscribe_feed_data(data):
    print(data)


# Modify the websocket_connection function to accept the handler as an argument
thread = thefirstock.websocket_connection(
    handler,
    listOfTradingSymbol,
    socket_connection=1,
    activate_sub_feed=True,
    callback_sub_feed=subscribe_feed_data,
    run_in_background=True
)

if thread:
    thread.join()
  • In the above code you can send the trading symbol in the list to subscribe for the feed,
  • You can choose the callback_sub_feed either to 1 or 2 and can maintain two connection simultaneously.
  • Once the above steps are done, you can create a method and provide it as a callback to the callback_sub_feed then this will start
    posting the feed to that method.

Acknowledgement Response

{'t': 'tk', 'e': 'NSE', 'tk': '26009', 'ts': 'Nifty Bank', 'pp': '2', 'ls': '1', 'ti': '0.05', 'lp': '50580.45', 'pc': '5.06', 'ft': '1702707042', 'o': '48362.45', 'h': '52708.85', 'l': '47885.10', 'c': '48143.55', 'toi': '13542150'}

Feed Response

{
  't': 'tf',
  'e': 'NSE',
  'tk': '22',
  'ft': '1664340733',
  'v': '2371.20',
  'bp1': '2371.20',
  'sp1': '2371',
  'bq1': '49',
  'sq1': '10'
}
Parameter Description
t type
tf tick feed
tk tick acknowledgement
e exchange
tk token
pc percentage change
cv change value
v volume
o open
h high
l low
c close
ap average price
symbol Symbol Name