FYERS API Utilize Free of Cost to Automate Trades in Python


Open Fyers Trading Account Please Click Here.



CMD pip Installation Type and Enter: pip install fyers_api



Please Follow the Above Video then go for the Code Below for generation of access token and Programmatic Trading:


Generate Token:


from fyers_api import accessToken
app_id = open('app_id.txt','r').read()
app_secret = open('app_secret.txt','r').read()
app_session = accessToken.SessionModel(app_id, app_secret)
response = app_session.auth()
print(response)

Place Order:


from fyers_api import fyersModel
token="Your Token "
is_async = False 
fyers = fyersModel.FyersModel(is_async)
print(fyers.place_orders(
token = token,
data = {
"symbol" : "NSE:TATAMOTORS-EQ",
"qty" : 1,
"type" : 2,
"side" : 1,
"productType" : "INTRADAY",
"limitPrice" : 0,
"stopPrice" : 0,
"disclosedQty" : 0,
"validity" : "DAY",
"offlineOrder" : "False",
"stopLoss" : 0,
"takeProfit" : 0
}
))

Generate access token directly from the web browser:



import sys
import webbrowser
from fyers_api import accessToken


def getToken(app_id, app_secret):
 """
 The variable `generateTokenUrl` will have a url like 
 https://api.fyers.in/api/v1/genrateToken?authorization_code=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqaGdqZzg3NiQ3ODVidjVANjQ3NTZ2NSZnNyM2OTg3Njc5OHhkIjoiWElHVFVYVjBSSSIsImV4cCI6MTU2MTU5NzM5Ny41NjQxNTV9.Agl-UUs63NforrUEdbG7YUlPcbFXu9hLYm4akGuIBkU&appId=Your_app_id"
  1. This function open this url in the browser. 
  2. This will ask you to login and will ask you to approve the app if it is not approved already.
  3. Once that is done, it will redirect to a url (added while app creation) with the access_token. The url will look like
     http://localhost:5000?access_token=gAAAAABc3Sh9QpE5mNx2mSz6vvvT29SAsELqkfbKQKa2977zHw3NdPBhe6jAZCBumHvYUum87j53-AzMEPXMjQw31wkRviZ1TdM5OimgTYWEEorWDmWuHnY=&user_id=FYXXXX
  4. You have to take the access_token from the url and use that token in your going forward.
 """
 appSession = accessToken.SessionModel(app_id,app_secret)

 response = appSession.auth()

 if response["code"] != 200:
  print (response)
  sys.exit()

 auth_code = response["data"]["authorization_code"]

 appSession.set_token(auth_code)

 generateTokenUrl = appSession.generate_token()
 webbrowser.open(generateTokenUrl,new=1)





def main():
 app_id = open('app_id.txt','r').read()
 app_secret = open('app_secret.txt','r').read()
 getToken(app_id, app_secret)

if __name__ == '__main__':
 main()


If you registered under my referral for opening of trading account and after successful registration when you got the Client ID and Password applied for API activation and still it is not active please do visit to my whatsapp which is available in the video description and ask for the form link will definitely help in get your API active.