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.

Connect to fyers API and print Profile information

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.

https://fyers.in/?s=ok&code=200&auth_code=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhcGkubG9naW4uZnllcnMuaW4iLCJpYXQiOjE2NDc1MTE3MDcsImV4cCI6MTY0NzU0MTcwNywibmJmIjoxNjQ3NTExMTA3LCJhdWQiOiJbXCJ4OjBcIiwgXCJ4OjFcIiwgXCJ4OjJcIiwgXCJkOjFcIiwgXCJkOjJcIiwgXCJ4OjFcIiwgXCJ4OjBcIl0iLCJzdWIiOiJhdXRoX2NvZGUiLCJkaXNwbGF5X25hbWUiOiJYTTE1ODgyIiwibm9uY2UiOiJzYW1wbGVfbm9uY2UiLCJhcHBfaWQiOiI1UU41RjNXR1Y2IiwidXVpZCI6IjdiYTBiNTEyZTcyZTQxYzA4M2Q4Mzc3ZjFhNTc5ZTc0IiwiaXBBZGRyIjoiMC4wLjAuMCIsInNjb3BlIjoiIn0.E0yqpkJPmUQn0pf1A3Q61ee_PB_4Wpw3Irl8co9cW38&state=sample_state

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

Popular posts from this blog

Zerodha Algo Trading Python, How to connect to kiteconnect?

How to Place Order in Zerodha using Python

How to place order in zerodha when RSI crosses below 30? Algo Trading Python