Skip to content

Commit 1ad89d1

Browse files
authored
Merge pull request #28 from FyersDev/tbtexample
chore: added tbt sample code
2 parents 1290893 + c3cb476 commit 1ad89d1

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from fyers_apiv3.FyersWebsocket.tbt_ws import FyersTbtSocket, SubscriptionModes
2+
3+
def on_depth_update(ticker, message):
4+
"""
5+
Callback function to handle incoming messages from the FyersDataSocket WebSocket.
6+
7+
Parameters:
8+
ticker (str): The symbol for which the message is received.
9+
message (dict): The received message from the WebSocket.
10+
11+
"""
12+
print("Depth Response:", ticker, message)
13+
14+
15+
def onerror_message( message):
16+
"""
17+
Callback function to handle incoming messages from the FyersDataSocket WebSocket.
18+
19+
Parameters:
20+
message (str): error message from the server
21+
22+
"""
23+
print("server returned error:", message)
24+
25+
26+
def onerror(message):
27+
"""
28+
Callback function to handle WebSocket errors.
29+
30+
Parameters:
31+
message (dict): The error message received from the WebSocket.
32+
33+
34+
"""
35+
print("Error:", message)
36+
37+
38+
def onclose(message):
39+
"""
40+
Callback function to handle WebSocket connection close events.
41+
"""
42+
print("Connection closed:", message)
43+
44+
45+
def onopen():
46+
"""
47+
Callback function to subscribe to data type and symbols upon WebSocket connection.
48+
49+
"""
50+
print("Connection opened")
51+
# Specify the data type and symbols you want to subscribe to
52+
mode = SubscriptionModes.DEPTH
53+
Channel = '1'
54+
# Subscribe to the specified symbols and data type
55+
symbols = ['NSE:NIFTY25MARFUT']
56+
57+
fyers.subscribe(symbol_tickers=symbols, channelNo=Channel, mode=mode)
58+
fyers.switchChannel(resume_channels=[Channel], pause_channels=[])
59+
60+
# Keep the socket running to receive real-time data
61+
fyers.keep_running()
62+
63+
64+
# Replace the sample access token with your actual access token obtained from Fyers
65+
access_token = "XCXXXXXXM-100:eyJ0tHfZNSBoLo"
66+
67+
fyers = FyersTbtSocket(
68+
access_token=access_token, # Your access token for authenticating with the Fyers API.
69+
write_to_file=False, # A boolean flag indicating whether to write data to a log file or not.
70+
log_path="", # The path to the log file if write_to_file is set to True (empty string means current directory).
71+
on_open=onopen, # Callback function to be executed upon successful WebSocket connection.
72+
on_close=onclose, # Callback function to be executed when the WebSocket connection is closed.
73+
on_error=onerror, # Callback function to handle any WebSocket errors that may occur.
74+
on_depth_update=on_depth_update, # Callback function to handle depth-related events from the WebSocket
75+
on_error_message=onerror_message # Callback function to handle server-related erros from the WebSocket.
76+
)
77+
78+
79+
# Establish a connection to the Fyers WebSocket
80+
fyers.connect()
81+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from fyers_apiv3.FyersWebsocket.tbt_ws import FyersTbtSocket, SubscriptionModes
2+
3+
# Replace the sample access token with your actual access token obtained from Fyers
4+
access_token = "XCXXXXXXM-100:eyJ0tHfZNSBoLo"
5+
6+
# Create a FyersDataSocket instance with the provided parameters
7+
fyers = FyersTbtSocket(
8+
access_token=access_token, # Your access token for authenticating with the Fyers API.
9+
write_to_file=True, # A boolean flag indicating whether to write data to a log file or not.
10+
log_path="", # The path to the log file if write_to_file is set to True (empty string means current directory).
11+
)
12+
13+
14+
# Establish a connection to the Fyers WebSocket
15+
fyers.connect()
16+
17+
mode = SubscriptionModes.DEPTH
18+
Channel = '1'
19+
# Subscribe to the specified symbols and data type
20+
symbols = ['NSE:NIFTY25MARFUT']
21+
22+
fyers.subscribe(symbol_tickers=symbols, channelNo=Channel, mode=mode)
23+
fyers.switchChannel(resume_channels=[Channel], pause_channels=[])
24+
25+
# Keep the socket running to receive real-time data
26+
fyers.keep_running()
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from fyers_apiv3.FyersWebsocket.tbt_ws import FyersTbtSocket, SubscriptionModes
2+
3+
def on_depth_update(ticker, message):
4+
"""
5+
Callback function to handle incoming messages from the FyersDataSocket WebSocket.
6+
7+
Parameters:
8+
ticker (str): The symbol for which the message is received.
9+
message (dict): The received message from the WebSocket.
10+
11+
"""
12+
print("Depth Response:", ticker, message)
13+
14+
15+
def onerror_message( message):
16+
"""
17+
Callback function to handle incoming messages from the FyersDataSocket WebSocket.
18+
19+
Parameters:
20+
message (str): error message from the server
21+
22+
"""
23+
print("server returned error:", message)
24+
25+
26+
def onerror(message):
27+
"""
28+
Callback function to handle WebSocket errors.
29+
30+
Parameters:
31+
message (dict): The error message received from the WebSocket.
32+
33+
34+
"""
35+
print("Error:", message)
36+
37+
38+
def onclose(message):
39+
"""
40+
Callback function to handle WebSocket connection close events.
41+
"""
42+
print("Connection closed:", message)
43+
44+
45+
def onopen():
46+
"""
47+
Callback function to subscribe to data type and symbols upon WebSocket connection.
48+
49+
"""
50+
print("Connection opened")
51+
# Specify the data type and symbols you want to subscribe to
52+
mode = SubscriptionModes.DEPTH
53+
Channel = '1'
54+
# Subscribe to the specified symbols and data type
55+
symbols = ['NSE:NIFTY25MARFUT']
56+
57+
fyers.subscribe(symbol_tickers=symbols, channelNo=Channel, mode=mode)
58+
fyers.switchChannel(resume_channels=[Channel], pause_channels=[])
59+
60+
# Keep the socket running to receive real-time data
61+
fyers.keep_running()
62+
63+
64+
# Replace the sample access token with your actual access token obtained from Fyers
65+
access_token = "XCXXXXXXM-100:eyJ0tHfZNSBoLo"
66+
67+
fyers = FyersTbtSocket(
68+
access_token=access_token, # Your access token for authenticating with the Fyers API.
69+
write_to_file=False, # A boolean flag indicating whether to write data to a log file or not.
70+
log_path="", # The path to the log file if write_to_file is set to True (empty string means current directory).
71+
on_open=onopen, # Callback function to be executed upon successful WebSocket connection.
72+
on_close=onclose, # Callback function to be executed when the WebSocket connection is closed.
73+
on_error=onerror, # Callback function to handle any WebSocket errors that may occur.
74+
on_depth_update=on_depth_update, # Callback function to handle depth-related events from the WebSocket
75+
on_error_message=onerror_message # Callback function to handle server-related erros from the WebSocket.
76+
)
77+
78+
79+
# Establish a connection to the Fyers WebSocket
80+
fyers.connect()
81+

0 commit comments

Comments
 (0)