What is url of streaming option data ?
I tried with url as “wss:/stream.stocknote.com” but it does not returning any data whereas same url is working to stream equity/ index data.
To stream option data, please use the symbol code. For example, for NIFTY08AUG2425050 PE, the contract symbol code is 51809_NFO, which can be found in the ScripMaster.csv file.
To stream tick data for the Nifty 50 index, utilize the Listing ID from the Index Quote API response attribute named “listingId”. This ID allows you to access real-time market information for the Nifty 50 index.
To stream tick data for the Nifty 50 index, utilize the Listing ID from the Index Quote API response attribute named “listingId”. This ID allows you to access real-time market information for the Nifty 50 index.
For your convenience, here are sample Python codes to stream tick data for the index (Banknifty or Nifty). The codes include the Listing IDs for NIFTY 50 (“listingId”: “-21”) and Nifty Bank (“listingId”: “-22”).
Note: Copy the code below into a Python file, update the “XXXX” placeholders with your credentials, and run it in any Python environment.
import json
import threading
import time
import websocket
from typing import Any
from snapi_py_client.snapi_bridge import StocknoteAPIPythonBridge
samco = StocknoteAPIPythonBridge()
login = json.loads(samco.login(body={
“userId”: “XXXX”,
“password”: “XXXX”,
“yob”: “XXXX”}))
session = login[‘sessionToken’]
print(session)
samco.set_session_token(sessionToken=session)
def on_message(ws, msg):
print("Message Arrived: " + msg)
def on_error(ws, error):
print(error)
def on_close(ws):
print(“Connection Closed”)
print(“Retry : %s” % time.ctime())
time.sleep(1)
connect_websocket()
def on_open(ws):
print(“Sending json”)
data=‘{“request”:{“streaming_type”:“quote”, “data”:{“symbols”:[{“symbol”:“51809_NFO”},{“symbol”:“50703_NFO”},{“symbol”:“-22”},{“symbol”:“-25”}]},“request_type”:“subscribe”, “response_format”:“json”}}’
ws.send(data)
ws.send(“\n”)
headers = {‘x-session-token’: session}
websocket.enableTrace(True)
def connect_websocket():
ws = websocket.WebSocketApp(“wss://stream.stocknote.com”, on_open=on_open, on_message=on_message, on_error=on_error, on_close=on_close, header=headers)
wst = threading.Thread(target=ws.run_forever())
wst.daemon = True
wst.start()
ws.run_forever()
if name == “main”:
try:
connect_websocket()
except Exception as err:
print(err)
print(“Connection failed”)
For more information, you can refer to the Trade API documentation: StockNote API Documentation
The StockNote Bridge is a wrapper for the Trade API, simplifying API usage with basic knowledge of Python or Java SDKs. It allows you to develop your own trading strategies and platforms without dealing with complex programming intricacies.
For StockNote Bridge(TradeAPI), you can download and access the documentation for the respective platforms via the following links:
• Python Bridge: GitHub - Python SDK
• Java Bridge: GitHub - Java SDK
• NodeJS Bridge: GitHub - NodeJS SDK
Please feel free to reach out if you have any further questions or need additional support.
Thank you for contacting us.
Sir how to write this streaming data into excel file through xlwings with filter for required headers. Please share code
import json
import threading
import time
import websocket
import pandas as pd
from typing import Any
from snapi_py_client.snapi_bridge import StocknoteAPIPythonBridge
Initialize the SAMCO API client
samco = StocknoteAPIPythonBridge()
login = json.loads(samco.login(body={
“userId”: “XXXXXX”,
“password”: “XXXXXXXX”,
“yob”: “XXXX”
}))
session = login[‘sessionToken’]
print(session)
samco.set_session_token(sessionToken=session)
List to store the incoming messages
messages_list = [ ]
def on_message(ws, msg):
print("Message Arrived: " + msg)
# Append each message to the list
messages_list.append(json.loads(msg))
def on_error(ws, error):
print(error)
def on_close(ws):
print(“Connection Closed”)
print(“Retrying: %s” % time.ctime())
time.sleep(1)
connect_websocket()
def on_open(ws):
print(“Sending json”)
data = ‘{“request”:{“streaming_type”:“quote”, “data”:{“symbols”:[{“symbol”:“51809_NFO”},{“symbol”:“50703_NFO”},{“symbol”:“-22”},{“symbol”:“-25”}]}, “request_type”:“subscribe”, “response_format”:“json”}}’
ws.send(data)
ws.send(“\n”)
headers = {‘x-session-token’: session}
websocket.enableTrace(True)
def connect_websocket():
ws = websocket.WebSocketApp(“wss://stream.stocknote.com”,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close,
header=headers)
wst = threading.Thread(target=ws.run_forever)
wst.daemon = True
wst.start()
ws.run_forever()
def save_to_excel():
# Convert the list of messages to a pandas DataFrame
df = pd.DataFrame(messages_list)
# Save DataFrame to an Excel file
df.to_excel(‘websocket_messages.xlsx’, index=False)
print(“Messages saved to websocket_messages.xlsx”)
if name == “main”:
try:
connect_websocket()
except Exception as err:
print(err)
print(“Connection failed”)
# After some time or condition, you can save the messages to Excel
time.sleep(5) # Wait for 5 seconds before saving, or use a condition to exit
save_to_excel()
Note : The code doesn’t use xlwings, but I saved the data in .xlsx format.