Fyers Algo Trading. Connect Python to API.
If you are beginner and wants to create Algorithmic Trading for FREE, this is the place for you. Here I explain each step of connecting Algo to API here.

Before proceeding to the coding section please complete all the installation steps. If you have already completed the installation proceed to next step. Otherwise Click Here to learn the installation.
STEP 1: CODE TO CONNECT TO FYERS API
Please copy the code from here
from fyers_api import fyersModel
from fyers_api import accessToken
session=accessToken.SessionModel(client_id="APP ID",
secret_key="SECRET ID",redirect_uri="https://trade.fyers.in/",
response_type="code", grant_type="authorization_code",
state="abcdefg",scope="",nonce="")
response = session.generate_authcode()
auth_code="auth_code"
session.set_token(auth_code)
response = session.generate_token()
access_token = response["access_token"]
fyers = fyersModel.FyersModel(client_id="APP ID",
token=access_token,log_path="E:/python")
profile=fyers.get_profile()
print(profile["data"]["name"])
In order to connect to fyers API . We need 3 things.
- App ID
- Secret Key
- Auth_code
Let us see how to find each one in detail
STEP 2: APP ID
Goto to Dashboard
Copy the APP ID and replace the field client_id

STEP 3: SECRET KEY:
Goto to Dashboard
Copy the Secret Id and replace the field secret_key

STEP 4: Authentication Code:
In order to get the authentication code we need to authorize our login to fyers. First we should create a URL to authorize our login.
https://api.fyers.in/api/v2/generate-authcode?client_id=XXXXX&redirect_uri=https://fyers.in&response_type=code&state=sample_state&nonce=sample_nonce
Replace XXXXX by our Client ID
Copy and paste this URL in browser, you are redirected to a URL like below.

Copy the auth_code value from the redirected url and replace auth_code in the program.
Now we all set to run our code. Goto command prompt and type the command : python <location>\filename.py
You can see the name linked to your Demat account.

More profile information
profile=fyers.get_profile()
name=profile["data"]["name"]
display name=profile["data"]["display_name"]
email=profile["data"]["email_id"]
fyers ID=profile["data"]["fy_id"]
pwd_change_date=profile["data"]["pwd_change_date"]
pwd_to_expire=profile["data"]["pwd_to_expire"]
I hope you all have learned how to connect to fyers using python. Please watch the video below for more information.
If any queries please let me know in comments.
Thanks,
Mukesh Mohanan
Comments
Post a Comment