code
import os
from fyers_apiv3 import fyersModel
from fyers_apiv3.FyersWebsocket import data_ws
log_path_ref ="*********"
app_id = "************"
app_secret = "***********"
redirect_url = "https://trade.fyers.in/api-login/redirect-uri/index.html"
def get_access_toekn():
if not os.path.exists("access_token.txt"):
session = fyersModel.SessionModel(client_id=app_id, secret_key=app_secret,redirect_uri=redirect_url, response_type="code", grant_type="authorization_code")
response = session.generate_authcode()
print("Login Url : ", response)
auth_code = input("Enter Auth Code : ")
session.set_token(auth_code)
access_token = session.generate_token()["access_token"]
with open("access_token.txt", "w") as f:
f.write(access_token)
else:
with open("access_token.txt", "r") as f:
access_token = f.read()
return access_token
access_token = get_access_toekn()
fyers = fyersModel.FyersModel(client_id=app_id, is_async=False, token=access_token, log_path=log_path_ref) #"C:/apiV2/" ,"/home/JafarSadhique/"
print(fyers.get_profile())
ws_access_token = f"{app_id}:{access_token}"
def onmessage(message):
"""
Callback function to handle incoming messages from the FyersDataSocket WebSocket.
Parameters:
message (dict): The received message from the WebSocket.
"""
print("Response:", message)
def onerror(message):
"""
Callback function to handle WebSocket errors.
Parameters:
message (dict): The error message received from the WebSocket.
"""
print("Error:", message)
def onclose(message):
"""
Callback function to handle WebSocket connection close events.
"""
print("Connection closed:", message)
def onopen():
"""
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"
# Subscribe to the specified symbols and data type
symbols = ["NSE:NIFTY50-INDEX", "NSE:NIFTYBANK-INDEX"]
fyers.subscribe(symbols=symbols, data_type=data_type)
# Keep the socket running to receive real-time data
fyers.keep_running()
# Create a FyersDataSocket instance with the provided parameters
fyers = data_ws.FyersDataSocket(
access_token=ws_access_token, # Access token in the format "appid:accesstoken"
log_path=log_path_ref, # 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=onopen, # Callback function to subscribe to data upon connection.
on_close=onclose, # Callback function to handle WebSocket connection close events.
on_error=onerror, # Callback function to handle WebSocket errors.
on_message=onmessage # Callback function to handle incoming messages from the WebSocket.
)
response = fyers.connect()
print(response)
Show this error when subscribing ...
Error: failed CONNECT via proxy status: 403
Attempting reconnect 1 of 5... None Error: failed CONNECT via proxy status: 403
Attempting reconnect 2 of 5... Error: failed CONNECT via proxy status: 403
Attempting reconnect 3 of 5... Error: failed CONNECT via proxy status: 403
Attempting reconnect 4 of 5... Error: failed CONNECT via proxy status: 403
Attempting reconnect 5 of 5... Error: failed CONNECT via proxy status: 403
Max reconnect attempts reached. Connection abandoned.
what is solution ?