-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathweb_order_socket.py
executable file
·108 lines (76 loc) · 3.25 KB
/
web_order_socket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import time
import threading
from my_fyers_model import client_id
from my_fyers_model import MyFyersModel
from my_fyers_model import get_access_token
from fyers_apiv3.FyersWebsocket import order_ws
def onTrade(message):
"""
Callback function to handle incoming messages from the FyersDataSocket WebSocket.
Parameters:
message (dict): The received message from the WebSocket.
"""
print("Trade Response:", message)
def onOrder(message):
"""
Callback function to handle incoming messages from the FyersDataSocket WebSocket.
Parameters:
message (dict): The received message from the WebSocket.
"""
print("Order Response:", message)
def onPosition(message):
"""
Callback function to handle incoming messages from the FyersDataSocket WebSocket.
Parameters:
message (dict): The received message from the WebSocket.
"""
print("Position Response:", message)
def onGeneral(message):
"""
Callback function to handle incoming messages from the FyersDataSocket WebSocket.
Parameters:
message (dict): The received message from the WebSocket.
"""
print("General Response:", message)
def on_error(message):
"""
Callback function to handle WebSocket errors.
Parameters:
message (dict): The error message received from the WebSocket.
"""
print("Error:", message)
def on_close(message):
"""
Callback function to handle WebSocket connection close events.
"""
print("Connection closed:", message)
def on_open():
"""
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 = "OnOrders"
# data_type = "OnTrades"
# data_type = "OnPositions"
# data_type = "OnGeneral"
data_type = "OnOrders,OnTrades,OnPositions,OnGeneral"
fyers.subscribe(data_type=data_type)
# Keep the socket running to receive real-time data
fyers.keep_running()
# Replace the sample access token with your actual access token obtained from Fyers
access_token = "XCXXXXXXM-100:eyJ0tHfZNSBoLo"
# Create a FyersDataSocket instance with the provided parameters
fyers = order_ws.FyersOrderSocket(
access_token=access_token, # Your access token for authenticating with the Fyers API.
write_to_file=False, # A boolean flag indicating whether to write data to a log file or not.
log_path="logs", # The path to the log file if write_to_file is set to True (empty string means current directory).
on_connect=on_open, # Callback function to be executed upon successful WebSocket connection.
on_close=on_close, # Callback function to be executed when the WebSocket connection is closed.
on_error=on_error, # Callback function to handle any WebSocket errors that may occur.
on_general=onGeneral, # Callback function to handle general events from the WebSocket.
on_orders=onOrder, # Callback function to handle order-related events from the WebSocket.
on_positions=onPosition, # Callback function to handle position-related events from the WebSocket.
on_trades=onTrade # Callback function to handle trade-related events from the WebSocket.
)
# Establish a connection to the Fyers WebSocket
fyers.connect()