Automatically Generate Access token in Fyers Python API | Algo Trading in India using Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome to suralyan in today's video i am going to tell you how to create the access token for fires python api automatically and to use it for a good rating so keep watching friends we all know that algo trading is going to be the future of trading and if you are new to a good trading there is a lot to learn about i will be continuously making videos about algo trading so if you want to learn about it subscribe to my channel and hit the bell icon for notifications in today's video we will learn how we can automatically create such token for fires python api which later can be used to place automatic orders on your fires account jirodha charges 2000 rupees per month for its api but fires api is free for all of its customer so if you don't have an account with the fires i will leave the link in the description below moving on to today's topic you need an access token to place automatic orders on fires you can use this as token for the whole day the token expires every morning between 7 am to 8 am so every day before the trading starts you need to create a token and in this tutorial i am going to teach you how you can do that automatically by just running one script so let's head to fire starting and we need to open files dot in and in products we will be clicking on files api and then down here we will be clicking on request access now it will ask you for the username and the password but since i am already logged in you didn't ask me and it will show your apps here on the dashboard uh you might not have any apps yet so your dashboard would be empty so what you have to do is you have to click on the create app and here you have to give a name to your app so i will give it tutorial app and then you have to set a redirect url you can set the url as https 127.0.0.1 and in description you can give the description of your app okay and then we will click on create app once you will click click on the create app you will get an app id and secret id and you must know that the status of your app should be approved then only you would be able to place the order with those app id and secret id it automatically gets approved if it is not approved or pending just wait for it to get approved and so we will be using this app id and the secret id now we will be moving to the coding part for that i will go to my terminal i will open a new window and make it large so that you can see it so here i will move to my desktop and hit and create a new directory and then cd into the directory now i will be creating a virtual environment virtual environment helps you to keep different version of applications differently on your system i have already created a video about virtual environment i will leave the link in the description and you can check it out so now we will be doing production 3.0 minus when dot this will create a virtual environment now the virtual environment is created now i will activate it and the virtual environment is activated so now we will be doing all the necessary installs so first of all we need to install the fires api and i'll be doing fifth install fires underscore api this will install the python files api and further we will be using selenium webdriver to open the browser and get our rss token and for that we will be using doing fifth install selenium so now both the modules have been installed now i will create a new file and start coding so here we will do the necessity import the first one would be from fires underscore api import access token from fires api import fires model and then from selenium import web drive and one more thing about selenium you must have the chromium or gecko driver in your path directly i will leave the links to the drivers in the description below if you are having any issue running them feel free to ask in the comment section and now we will create a new function the name of the function would be generate token url so function generate token url it will take two arguments that is app underscore id and secret underscore id and down here we will start a session app underscore session is equal to access token dot session model and will give and secret ideas variables and then we'll look for the response and if response code is not equal to 200 it means that if we didn't get any response 200 is okay response we will be doing says start exit it means we will be exiting it and we also need to import says it's an inbuilt module so you don't need to install anything and we can also print that error is not equal to 200 and otherwise if we don't if we get a 200 response a little bit so underscore code is equal to response and then response would be a dictionary so we'll be thinking data key and inside data there would be another dictionary and that would be authorization underscore code so we basically need this authorization code and then we will say app underscore session dot set and then we'll generate a url and that would be generate token url is equal to app underscore session dot generate underscore token so basically we will get a url and then that's linear browser will open it so now we will try to open it up so here we will be saying the driver dot get this is our app id f id is equal to this and create endless define a mean function and in the main function we'll be running the general token underscore url function with our app id and the secret id and here if name mean is good and one thing we forgot to do is we have to create this driver so up here will be saying driver is equal to web driver dot i want to open it in mozilla so i'll be using firefox we can also use chrome instead of it for that you have to do chrome here and you must be sure that the chromium driver or the gecko driver firefox are in your path folder so let's save it and run it so here you can see that it has opened the mozilla firefox and now it will open a url but i think i have got some error okay so response code is not equal to zero so i need one i made a typo here sorry for that windows and let's create it for once and rerun it so now you can see that it opened up the fires login page and authorization code which was generated as here and you just need to log into it to get your access token uh but since we need to automate this process so we will be using automation so now we need to identify four elements on this page the first id the password the pan card and the submit button and so i will right click on it and say inspect element and then i have to find the ids of all these elements for example fires id the id is files underscore id you can see it here the ids files underscore id similarly if you go to password the id is password and if you go to pan the id is pan card and if you go to submit button the ids button underscore id so these are the four id which we need in our code so let's close it and go back to our code and here now i will set user underscore name is equal to driver dot find underscore element underscore by id and here we will and i will be making four such lines so here i'll be saying password and password is password itself and here i'll be saying pan card and the pan card with the pan card and here i will be saying submit button and the submit button has an id of button underscore id so i have this button on this variety so i'll save it now we need to send the values to this so i am using a material called python decouple to save my username and pan card because i don't want to share it with the public and for that i will be doing an install that is fifth install python underscore decouple so it has installed python the form decouple import config and then what you have to do is you have to open up uh you have to create a dot env file in the same directory in which your script says and in this here you will be writing username is equal to and then enter you go into the name here and then password is equal to your password and your pan card you don't need to add any kind of space or any kind of commas here you just have to create it like this and then save it and close it at the very top i will do all these so i'll be saying username is to config username password is equal to config password and pan card is equal to config pan card you can also do the similar thing with your app id and secret id it keeps your all your credentials in a dot enemy file and you don't have to hard code them into your script and it is better to do that in this way so now here down under i will be doing user under dot send underscore keys and i'll be sending the username password dot password and then pan dots and underscore keys and card and then we have to click the submit button so we will submit underscore button dot click and then we need to add some sleep time so that our url is redirected to a url containing the system so let's add a sleep time of 30 seconds and for that we have to import the time module also you don't need to install anything it's an input module so input time here save it and then down under here android sleep 30 then we'll be saying url underscore underscore driver and this module will be returning a url with token so this is what will happen and it will give us a url which will contain rss token and then we have to us extract that as to confirm the url so now i will add a print statement here so that we can see it and let's save it i will open the me file and enter my credential i won't show you here so let's okay so on now i will run it again so basically it will open the fires api and it will enter my credential for some reason i'm getting some error what i have done is i have added the codes here with these are variables so i have to remove the boots and save it and now going back to the terminal i will run it and what it will do is it will open the mozilla firefox and enter my credential i will hiding the credentials because i don't want it you to know the credential and then see it will login success and it it will open this page this page has a url which contains the access token which is needed for making trades on the files api so our print statement it will get exited in 30 seconds because we have here the url has got an access token has token is from this equal to that is it starts with g and it goes up to this and and the last equal to is included in rss token the first one is not and now we will write program which will automatically extract this from the whole url and let's go back to our code and here we will say define extract token it will take the full url which was returned by the above function and then we will be using rejects rejects is a complex module i generally do the search for the matching rejects and then do it so you just have to copy it so here what i'll be doing is i'll be saying assess we will be adding https 127.0.0.1 this is the placeholder which we added in our application and then we will be adding this assess underscore token is equal to and then just close this bracket and then add a full stop a star and a question mark and then again start of bracket then a posture mark and then is equal to and user underscore id is equal to and then it will contain your user id we will be running it on the full url and group 0 so this is our re just see this bracket closes here and this column closes here so it appears fine you just need to add your user id here i would be adding it before running it but i can't show it to you so i'm not adding it right now so and then we will be doing we will be saving it as a pickle basically this is token would be a string and pickle is an english library you just have to use import pickle and what it does is it saves the variable or save the value as it is so we will open up we will save it open okay first we will see if there is an ss token pixel so basically it will be creating a pixel file and it would be a writable file as that and here i made a typo it should do one and down here will be saying pickle so this is it and otherwise we will be saying no token generated so basically what it would be doing is it would be extracting the access token from this from this url and then saving it as a pickle so we don't need a print statement anymore because it would be saved as a pickle so i will remove the print statement so now we will be changing our main function here we'll be saying full underscore url is equal to this it will give us a url and then here under we will be running extract token and we will be passing the full underscore url into it i have filled my login id in the rejects function and now let's save it and go back and run it so it will open it with my credentials i will be hiding and now the says token has been generated we just have to wait for 30 seconds to get this window close and then it will save it as a pickle just go to the terminal okay still okay now it's finished it has been closed and we haven't got any errors so let's open up our folder and here you can see that we have got a files underscore notification file this one is basically rss token it can be used and to demonstrate that we go back to the terminal and we'll create a new file called place underscore and here we will be doing certain imports the first one would be underscore api import access token in fact we won't be creating any access token so we don't need this import here so we just needing the file model and the other one is important and now we will be saying with open file under put to condor pixel so it will be loading the article and it will be cycling it was too fun and then we will be saying is a thing a request so here the first fire model has f small and the second one has f and m as capitals we can see our profile by simply saying print fires dot get under profile and token directly token but i'm not i won't be showing you the file but instead we can do is we can do funds so instead of profile i will say funds and we can also check out our holdings so here we can say holdings so this will show me the funds and the holdings and similarly we can also place an order using this token so let's save it and python please order dot ui and if you run it see you can we have got the print statement the first one shows me the funds and the second one shows me the folding of my account so here we haven't created any uh token again we are just using the token which we have saved and now you can use this token for the whole day and it will expire between seven and eight am next day and next day you will again have to get your asses token so i am using a trade setup on a raspberry pi and i have scheduled this script to create the access token every morning at 8 30 am and then i can use it for the rest of the day and this is how the automation happens since currently the market is not running i'm making this video in the evening in my next video i will show you how you can place order using this fire api this is it for today's video if you have enjoyed it please hit like button and as i already told you in my next video i will tell you how to place an order using fires api and in order to be notified about that video please subscribe to my channel and hit the bell icon see you in the next video till then bye and take care
Info
Channel: Saral Gyaan
Views: 4,152
Rating: 4.826087 out of 5
Keywords: how to generate access token in python, fyers api india, fyers python api, how to use fyers api in python to do algo trading, free api for algo trading in india, algo trading using python, use python to do algo trade in India, fyers, fyers api tutorial, free fyers api, fyers api, India Algo Trading, indian algo trading, algo trading in india, automatic trading in india, algo trading strategy india, fyers stock brokers, open an account fyers, fyers algo trades, Fyers live chart
Id: xPszIoIZO5g
Channel Id: undefined
Length: 27min 35sec (1655 seconds)
Published: Sun Sep 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.