-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb_data_socket.py
118 lines (96 loc) · 3.62 KB
/
web_data_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
109
110
111
112
113
114
115
116
117
118
import time
import threading
import pandas as pd
import pandas_ta as ta
from datetime import datetime
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 data_ws
fy_model = MyFyersModel()
time_frame = 60
live_data = pd.DataFrame(columns=['datetime', 'symbol', 'ltp'])
def on_message(message):
"""
Callback function to handle incoming messages from the FyersDataSocket WebSocket.
Parameters:
message (dict): The received message from the WebSocket.
"""
print(message)
# global live_data
# c = datetime.now()
# current_time = c.strftime('%Y/%m/%d %H:%M:%S')
# symb = message['symbol']
# ltp = message['ltp']
# # print(f"success : {message}")
# new_row = {'datetime': current_time, 'symbol': symb, 'ltp': ltp}
# live_data.loc[len(live_data)] = new_row
#
# open_dict = {
# 'ltp': 'first'
# }
#
# low_dict = {
# 'ltp': 'min'
# }
#
# high_dict = {
# 'ltp': 'max'
# }
#
# close_dict = {
# 'ltp': 'last'
# }
#
# data = live_data.copy()
# data['datetime'] = pd.to_datetime(data['datetime'])
# data = data.set_index('datetime')
#
# one_minute = pd.DataFrame(columns=['open', 'high', 'low', 'close'])
# one_minute['open'] = data.resample('1min').agg(open_dict).dropna()
# one_minute['high'] = data.resample('1min').agg(high_dict).dropna()
# one_minute['low'] = data.resample('1min').agg(low_dict).dropna()
# one_minute['close'] = data.resample('1min').agg(close_dict).dropna()
# df = one_minute.copy()
# df['rsi_close_9'] = ta.rsi(df.close, length=9, scalar=100, talib=True, drift=1, offset=0)
# print(df)
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 = "SymbolUpdate"
# data_type = "DepthUpdate"
# Subscribe to the specified symbols and data type
symbols = ['NSE:NIFTYBANK-INDEX']
fyers.subscribe(symbols=symbols, 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 = f"{client_id}:{get_access_token()}"
# 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="logs", # 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=on_open, # Callback function to subscribe to data upon connection.
on_close=on_close, # Callback function to handle WebSocket connection close events.
on_error=on_error, # Callback function to handle WebSocket errors.
on_message=on_message # Callback function to handle incoming messages from the WebSocket.
)
# Establish a connection to the Fyers WebSocket
fyers.connect()