Automate Fyers API Login with Python: A Step-by-Step Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
excuse me I have doubt yeah able to get the recordings of this session yes all the recordings will be uploaded on the portal okay all right so where you will find the recording once you go to uh python for finance and algo trading I've added a new section fires 2024 in this you'll find the recording of today's session like all the fire session I will be conducting all the recordings will find here okay all right so let's start with today's session so what are the few things that you need before attending the session the first thing is you need a fire account this is the first thing and second is you need uh vs code all right these two things you will be needing and we will be needing few more things but I will be telling you how to download those stuff all right so let's start now so once you go to your fir account okay so first let me show you what uh what do you see when you go to fires.in if you go to fires official website this is how their official webs it looks like you have to click on product if you want to trade you have to click on Fir web uh different uh Sun so today I will be covering the automation tomorrow I will be covering the fundamental okay and we will be having few more sessions I guess around four to five session we will be having I'll cover each and everything that you need to know in fire API okay all right so once you go to uh the product page you'll find this fire web so if you click on it click on access fires web this is where you can uh do the manual based trading okay and whenever you place any order you see the orders in this section whenever you your AUD AUD your orders are filled you see it in position you also see it in trade once the order is filled you also see the stock in the holding and in the fund you can see how much fund you have so right now I have 495 rupees in my account okay uh so so this is the basic stuff now if you go to products again uh you need to go to API so scroll down this is where we need to go fires API click on it click on request access once you click on request access click on get getting started so I've already created multiple accounts so I will be creating a new account today so just click on create app you can upload an image but not needed let's call it uh fires 2024 okay redirect URL you can have whatever URL you want but if you look at documentation and if you read at redirect URL they have specified something let me show you if you go so this is if you want to go to this page you have to go to the homepage uh click on request access and click on V3 docs okay so in V3 docs you'll find the documentation on how to use the API all right now if you read about it if you scroll down scroll down a bit more they have told us how to create a app see individual app uh you have to click on dashboard create an app uh description redirect URL now for redirect URL they have specified the user will be redirected to the redirect URI after successful logging in um let's see if they have something else here who the Au code redirect URL there uh this is where the user will be redirected to after successful login example this uh this should be the same as what you have provided the time of app creation so you can have it this also but what I'll do I'll have google.com now this is not recommended but let's have it as google.com it doesn't matter which URL you give so so I'll just copy this link I'll just paste the redirect URI okay so what is this URL used for once you verify yourself uh the fires API will redirect you to this google.com Okay so I'll write fires 2024 demo all right so for web hook click on profile transaction info everything will take I accept the API okay and create an API Web book what it is asking [Music] I'll just remove it delete just delete the web hook click on create an API okay so API was created if you scroll down at extreme bottom you can see FS 2024 so when I click you can see we have secret ID we have app ID right so let me just take a look okay all right so this is the first step you need to do just go to your fires API setting and make a new app okay this is how the new app will look and you need few important information like app ID and secret ID these two things you need okay so what I'll do I'll go to vs code let me make a new file now let's call it credential py okay this will be my credentials. py and I'll make another file let's call it login automation py now what is this login automation now see each day when if you wish to trade via API you need to verify yourself without verifying yourself you cannot trade so there are few steps that you need to take in order to verify yourself so all this step either you can take manually or you can automate the entire stuff by using selenium so I'll show you both how to do the manual thing and I'll show you how to do it with selenium okay at least we will try to finish this much today okay all right so uh what are the things you need to okay one more thing now let's say in credential if I if I make any variable like if I make a is equal to 100 I'll write B is equal to 200 I'll write uh secret equal to I'll just copy this and I'll paste it here okay so I've created three variable I saved the file and now if I go to login automation if I want to access these variables what I'll do I'll just write here import credentials as CR okay now in credential what have I have three variable so whatever variable I want so I'll write print CR dot if I write secret I'll find the secret if I run the code you see I was able to import the secret from this file so this is how you import works okay if there are two files if you want to import something from a file just write import file name as you can write uh alas whatever you want then that variable dot whatever variable you want let's say I want this b a so I'll write cr. a and if I run the code you see it is showing 100 so this is how you import variables from another file so what I will do all of my credential I'll write it in this file not needed not mandatory you can I can write the same credential here also but in future if you wish to send any data to me so I don't want to send me your credentials okay so it's better if you make a separate file for credentials whatever sensitive information we want for this entire strategy implementation all the important stuff it's it's better if you write it in a credential py file okay now uh what are the things I need I need the secret ID right so let just give me a second I'll just copy all the important variables so these are few important variables we need first we need client ID okay so I'll just copy the client ID from here this is my client ID copy and paste it okay first variable done second is I want secret key so I'll just copy the secret key copy and paste it here done then the third thing is I need this redirect URL URI so it's the same thing okay this link so I'll just copy this link again and I'll paste it here done now there are few three more variable I'll talk about these three variable in a few minute but for now let's just focus on these three uh variables okay so either I can directly paste it here okay like either you can import it or you can directly write it write it here it's totally up to you so for now to keep things simple I'll keep it in the same file okay I'm not importing anything okay but keep in mind that it's always better that you keep all the important data in another file and you import it in the current file okay so I have all the important three things now what to do next now let's try to understand how to authenticate yourself okay now if you go to official documentation just read about it some basic stuff they have provided if you scroll down they have provided us with the diagram here okay after creating an app this is the diagram they they have provided us okay so two diagram one is this and one is the above one uh this step is H so this is how the entire flow looks like so first we visit we go to apps we go to the fires login uh we get two things we get client ID and we get client URI okay once you get client ID and client URI you need to generate a link okay once you click on that link that link will give you Au code okay I'll repeat again once you have client ID client URI by using both the uh credential you'll generate a link by using a link you'll get a o code after verifying yourself once you have the O code then you have to take the O code and you have to mix it with something see what you have to miss once you get the O code you have to take o code and app hash ID app ID mix both of them and again visit the site so site will give you access token okay I'll I'll just write it and show you okay what we have to do first we need let's call it client ID and I need access I need uh Secret client ID and secret so if you have both of them you have to visit the site you have to login yourself okay once you log in yourself it will give you something which is called uh Au code Au code okay so this is your python file so you have to take the O code plus you have to take your app ID app ID okay by taking both again you have to go to the website okay so this website will give you access token so this is the flow of getting the access token so this access token is very important without this access token you cannot place order you cannot get historical data you cannot do anything so before moving to using the API the first step is to get the access token now getting the access token itself is a long process now keep in mind that this is not same for all the broker all the broker will ask you to get some credential okay okay because how will broker know that you are the one who is fetching who is requesting for the data so you need some credential to prove that this request is by you okay so if you go for some other broker like kotak API so in kotak API this is not it's not this long okay if you take IC direct it's quite long it's as long as this one okay but uh if you ask me like fires has a extra layer of security okay it's like more secure as compared to if you compare with other Brokers okay so now let's let me show you how to do this okay first step is we need to take the client ID and secret key and we need to visit the login page this is the first step we need to do so how will you get the link okay so to get the link if you scroll up we need to import we need to install one Library first so I'll just click on this P fire fires python Library so this is the library we need okay so I'll just copy the code now before installing the library I'll create a new virtual environment okay so how to create a virtual environment I'll explain you one more time so first you need to write here uh pip Python Das m v EnV space let's call it fires V3 V3 V uh V only let's call it fire V okay I need to write Python 3 so if you're a Windows user you have to write python if you're a Mac User you have to write Python 3 so it will create create a virtual environment uh I'll just click on yes so now you see I've have created a virtual environment Firs V and the virtual environment is also selected now my when I open a new terminal you see this fires we written here it means the the virtual environment was selected okay now if I type here pip freeze pip three freeze you see no libraries installed yet okay so what is virtual environment for those of you who don't know I'll tell you virtual environment is like a separate interpreter in which you'll install all the libraries okay once you have all the libraries your code will refer to your interpreter your interpreter this small interpreter that you have created will execute this code so all the necessary Library should be there in that interpreter okay now why I'm doing this it's something I've already explained you can watch my videos to understand that okay so this is how you create a in Virtual environment so I have named the virtual environment as fire V okay and in this virtual environment I'll install the library how to install very easy just copy this code and paste it here now make sure if you're a Mac User write pip 3 if you're a Windows user just write pip install so this will install the library okay so the first step of uh using the API is installing the library okay so I installed the library now now I'll go to the API documentation again and if I scroll down I'll find some sample code see they have provided a sample code this is the sample code so I'll just copy this sample code okay copy it and paste it here all right so the library is installed so I'll just close the terminal uh I'll remove the output this is was the output so I have removed it so this is the sample code that they have provided us what they want they want first uh they have first imported the python okay one more thing in fires they have two fires uh for Library one is for version two one is for version three don't install version two by mistake make sure that you are on version 3 V3 docs okay all right so you can see fires V3 we are importing now this is not being selected why because my inter my uh vs code is not able to figure out if this interpreter has this library or not that's the reason why it is not showing so I'll just save and open it again it's still not showing oh uh one more thing see when I created this interpreter I have used 3.12 okay now it might work or it might not work I don't know all right so I will not take the risk and I'll try making another in another virtual environment by using 39.6 okay because I'm not 100% sure that if I use 3.12 virtual environment everything will work so I need to create a virtual environment again okay I'll repeat again what I have done I have created a virtual environment but this virtual environment was created by using python 3.12 you see 3.12 is written I want to create a virtual environment by using 39.6 so I have to delete it now okay so what I'll do I'll just do a right click delete it okay um now what I'll do I'll create a new terminal again I'll restart the terminal first how to restart the terminal how to restart the entire vs code okay to restart the entire vs code go to view click on command pallet and just search for reload window this will reload the entire interpreter uh entire vs code okay uh again I'll create a new interpreter go to terminal now how do you select The Interpreter see how do you select uh you need to write this entire address it is/ user /b/ Python 3 right that's what I'm going to write here I'll write here Python 3 uh no not Python 3 I'll write here in codes I write slash user slash bin SL Python 3 okay or no this is my interpreter Das m space V and V Space fires V press enter okay now huh now you see a new interpreter was selected okay see a new interpreter was created fir V now when I click on select interpreter look at the version of this uh fire V fire V version is 39.6 so I used 3.96 interpreter to create this virtual environment okay all right so now I am I'm done creating the virtual environment now let me quickly install the library to I can just press up Arrow key okay so this is the library I want to I've installed the library it will take few seconds now it has installed now you see when I open F or login automation nothing shows here it means it is working fine okay but still just in case I'll just restart my entire window wa why it is not selecting python 3. I'll just restart the window go to view command pallet and reload window okay now fires no environment is selected huh see 3.9.3 is selected and still it is still not able to detect it I'll I'll uh just uh restart it okay I'll just restart it one more time reload window H uh I don't know for some reason it is not able to detect it I'll just check it I'll just go to terminal you can see fir Fe I'll write pip 3 freeze pip 3 freeze I press enter oh nothing is installed you see when I when I type P3 freeze nothing is installed so I need to install it again for some reason the library was not installed properly I'll just install it again I'll copy the command paste it here make sure that I change this to pip 3 enter now how do you know if a given library is installed or not you just type pip three freeze okay I'll open the terminal again now if I write pip 3 freeze if I press enter you see it is installed if I scroll up uh you can see PES uh- API V3 okay 3.0.7 version is installed so now I know that it is installed and you can see uh that under Mark is not visible now okay that under Mark is gone okay so this was the first step that is installing the library now we need to create a link okay how do you create a link so we have all the credential so I'll just remove these credentials for now okay so I already have the variable we'll take this and put it at the top so for now we don't need this import okay so till here everything is clear so there are two more variables that they have created one is response type code and one is state is equal to sample State I don't know what it is they have just provided us now in this fires model class they have created a so in this firus model uh Library they have a class called a session model okay and in this session model these are all the argument you need to send first is client ID then there is secret key then redirect URI and response type this response type is code and then you need to call the function generate Au code so this generate Au code will give you some response and this response will be a link I'll repeat again so first we are importing the library then we are calling the session model class in session model class we have to send these argument that is client ID secret key redirect URI and response type once you send all the argument uh you create a session object now on this session object you have to call the generate o code function this generate o code will generate a link for us so let's run the code and see if I run the code you see it generated a link okay I'll just copy this link and I will paste it here okay so when you open this link for the first time this is how the user interface will look like okay so I'll just click on I accept I accept the API usage and click on uh continue as this so this is my name of the account so you see it redirected me to google.com now once you go to google.com you need to copy the URL it might look that nothing has has changed but this URL has changed look at the URL it is not just google.com it is google.com/ something something something so I'll just copy this link okay now I'll do one thing I'll create a Jupiter file now uh login. ipy NB okay and let's uh save it Au code oh no not Au code huh I'll u i let's call it URI equal to this so this is the URL that I've got okay now if I run the code now it will ask me to select The Interpreter python environment I'll select the same it will install some Library okay so till here anyone has any doubt how how I got the URL if anyone has any doubt you can ask me right now do do you want us to do thisly no it's fine even if you don't do it right now it's fine I'll send you all the code that I have just uh do it after the session ends okay I'll tell you what are the things you need to change only those thing uh do those changes and run the code and we'll have to uh like follow the same process for for other broker also right yes for other Brokers also you have to follow the same process I for the document right yes as for the document anyone else has any other doubt Niha vun Vidya Sagar vul Sunny so uh I mean this URL uh is something similar to the top right no this is different top is different okay okay I'll come to top in few minutes okay yeah anyone else okay all right uh see now I've got the URL now if I click on this URL okay I'll just I have copied the URL now I'll open the URL H this is the URL I've got now I want you to look at the URL carefully okay see first there is http google.com then there is slash there is s equal to okay and code equal to 200 and O code equal to this this is how the URL is being divided what I need is after that odd code equal to whatever content I have right so that content I need okay I'll repeat again in this URL whatever content I have after OD code equal to okay that entire content I need so what I'll do I'll just uh split the entire string okay so I'll write URI dot split and I'll split it along this o code equal to okay so I'll write oore code equal to okay so let's run the code now I have two I have a list now okay I have a list where first is this and second is this so what I need is this second value now in this second also I don't need this uh and State equal to none this is not needed okay so now again I'll slice it I'll slice the second part so let me uh make a variable S1 equal to this so S1 is a list in S1 there are two element the first element is not needed in the second element uh it's a list and at the end I have and start equal to none so again I'll split it okay I'll write S1 dot split uh no I'll write s1's square bracket 1 first first uh element is a URL second element is the the main content right so I'll write S1 S1 square bracket 1 then I'll write do split okay in Split I'll write and state okay I'll write and uh State okay so let's run the code and see see when I run the code now this is the list I'm getting this is the first element and the second element is equal to none so what I want in this I want the first element so let 12 braet 0 now if I run the code again see I've got the O code okay I'll just explain it one more time what I did just now so what happened when I got this link okay after logging in on the website I got a link and the link was something like this it was google.com something something and there was O code equal to so let's say something something and there was and uh what was the last thing and state equal to equal to 9 this is how the string was okay so what I did first I divided it along this so I got uh this entire content okay now then again I divided into two part one is uh whatever you have after equal to none and whatever I have this content so this is what this is my Au code okay when you verify yourself you'll get a link and in that link there are there will be multiple stuff what you need you only need the O code and I just took that o code from that link okay so the the content that you see in front of you this is nothing but the O code clear everyone how I got the O code now what to do let's uh I'll just save it in a variable let's call this variable as o equal to this okay now if I just uh write code again O So in O variable I have the entire uh o code okay all right now what to do once you have the O code what you need to do so again we will go to API okay so once you have the O code if you scroll down they have again provided another python code so this is the python code I'll just copy it okay now I'll paste it here instead of pasting it on uh login automation let's paste it here so this is the code I'll remove the unnecessary part now to run this code you need this Au code see they have provided o code equal to something something without this Au code you cannot run this uh run this uh code okay so what I'll do um I need these three I need to change these three line so what I'll do I'll just press enter I need to change these three line right so I'll just go to login automation I'll copy the three uh variable that I need and I'll paste it here okay what else I need to change I need to change this o code o code equal to instead of this random thing I'll write Au okay so this is the changes you need to do I changed client ID secret key URL and I changed this o code equal to what whatever o I have got okay now once I have the O again I need to call the session model function now in this session model I need to send client ID I need to send secret key I need to send redirect URI I need to send response type response type is code and Grand type is authorization code okay I need to send everything and then I need to uh call the function set token in set token I need to send the O code after that I'll call the generate token function and it will generate something okay see now what is happening now just let me just run this code the moment moment I run the code you'll see that in this response I'll have the access token if I run the code you see in this response it is saying s okay code 200 message this and access token this now whenever you get 200 in future whenever you do any API call and you get 200 it means the call was successful okay if you get something else other than 200 it means it didn't work properly okay so uh if you have the Au code if you have the O code you just need to call you just need to run this code if you run this much code it will give you access token now you still now you have got the access token but that access token is in the response variable okay so see if I just print response again this is how the response variable looks like okay now this is a dictionary you can see it's a dictionary there are multi multiple things here the most important thing is access token so you will write square bracket you'll copy the key and and you'll paste the key like this okay if I run the code this is my access token so I'll just save it in a variable access token equal to this okay uh now if I run this code I've got the access token now I'll just write access token if I run the code I've got the access token so this is the main access token that you need I'll repeat again what we did just now first we got the initial code uh which had a function to generate a link so we generated a link once we got the link We verified yourself after verifying yourself it got me another it gave me some other link so in that link there was a o code okay so I sliced the entire uh string and I got my desired o code once I have the O code then I need to call few more functions like I just need to call uh the session model function and just call the generate token function okay so you don't need to remember anything just copy the C and paste uh copy the code and paste it and execute it now when this generate token was called so it gave me some response and this is how the response looks like so it's a dictionary where there are four to three to four key value pair so one of the key value pair is this 200 whenever you see 200 it means the request was successful okay and one of the key value pair is access token colon the value so this value is what we need so I saved this value in the access token variable okay now once you have the access token now you can call any function that is available on the fire API so let me show you if I go here uh I'll scroll down I'll just call some random function like uh uh Holdings okay now if I want to know holding what function I have to call I have to call if you go to python you need to call this function called as Holdings so I'll just copy this code I'll come here I'll add a new cell I'll paste it so first you need to create a fires model where you need to send client ID access token that you just got and there are few more argument for now I'm leaving it as it is uh and you have to create a fire years object once you have created the fires object you need to call the holding function so holding function will again give you some response see you see again 200 it means it was successful and this is my overall holding you see I have I scroll right there is one stock that I have that is haware okay so this is how you uh authenticate yourself and you can call any function you want all right uh till here anyone has any doubt you you can ask me right now amol Punit uh vinit vun Sani Shas just give me a thumbs up if everything is fine okay great all right uh now let's do one thing let's generate the link one more time now you might think that everything is done but this uh access token that you have found okay this access token is only valid for a single day okay now this might be different for other broker but for fires this access token is valid for a single day so each day you have toy yourself get the O code after getting the art code you need to get the access token okay I'll just repeat one more time so each day what you have to do uh you have to take client ID plus secret go to the website okay login yourself okay after login it will give you Au code okay take the O code again request take the O code plus client ID uh you don't need to login anything okay here you don't need to do anything here you have to verify yourself here you just need to call the function and in return it will give you access token okay so these step you need to do every day all right now uh let me show you something that if I call the link again okay let me call for the link again I'll go to to go to the login automation file and I'll run the code again now again I've got the link now I'll copy this link okay and I'll go to a website I'll open this link again the second time opening now the moment I open it second time it works fine because of the cache but now I'll try to open it in a incognito mode I'll open a private window I'll paste it here you see what what happens it asks me to verify myself okay I need to write my client ID I need to write my uh password I need to write my top three things I need to write see if I click on login to client ID I need to write the client ID first okay so let me get my client ID so my client ID is this okay so I'll just paste it somewhere here so this is my uh user name equal to this now don't get confused with this client ID and this client ID okay so here even though it is showing client Cent ID but it is asking me this username it is not asking me this client ID okay both of them are different so I'll just copy this I'll paste it here okay now when I click on continue then it will ask me top okay now this is a step which you which we can automate and which we will automate but to do this step you need the top code okay now where can you find this top code if you go to again go to Fire's website so this is our fire website looks like go to products scroll down and go to your fires my account okay access my account and in access my account uh click on here see this is my uh username click here click on manage account now when you click on manage account you'll find something called as tofa okay this is where you need to go okay so I don't want to enable it okay so I you I can disable and enable it again uh but it would be do you want do you want me to disable and enable it again or should I move forward you guys can write it in the chat room if you want I will disable and enable it again to show you how it works if you already know then we can move forward so what happens is when you do it you get a code okay and you want that code okay I'll I'll do it no worry I'll disable and enable it okay so first I'll disable it uh are you sure you want to disable yes yes disabled now I'll enable it the moment I click on enable this is how it looks like okay I need to scan this code on my okay where to scan so there is something called as Google Authenticator app Google Authenticator so this is the app that you need to download okay just download the app and you'll find an option to scan the QR code okay H where did it go ah here okay so you need to scan this QR code or you just click here to copy the key this is the key we need so I'll just copy this key and I'll paste it here let's name the variable topor key equal to this okay this is my top key uh now I need the six-digit top key and my pin 4digit pin okay so how to get the uh top so let's make a new file let's call it top pin. py okay so to get top there is a library we need to install if you just search for top python this is the library okay P OTP is the library name uh if you go to p.org scroll down this is how you install it pip install byotp so I'll just write here pip 3 install Pi OTP press enter so this will install the library now the library is installed now I'll just write import Pi OTP as TP okay and in TP we have a function TP Dot top and here I need to send this top okay so I'll just copy this thing I I'll just copy this entire thing paste it here I will send this dop key and let's save it in the variable uh k equal to this okay now if I just print this K you'll see that by using this top key it will give me some uh six-digit key now wait dot uh wait I need to verify oh no I need to call the Now function okay dot now so now if I run the code you see it is giving me uh six-digit code see I'll run it again each time I run it like after 30 to 60 seconds the code will change see if I run it again it's still the same but after a few second it will change I'll run it after a few second so this is how you can use this key to generate a OTP six-digit OTP okay so I'll just type this thing over there I'll copy this I'll go to my fires here uh I'll just run it one more okay now it has changed it is 519 519 617 617 and my four-digit pin is 1998 so click on enable so this has enabled the top okay so now you see I have updated my my top key so I'll run the code again see whenever I run this code it will give me a top okay so now let's generate the link one more time okay so now I'll go to your automation link I'll run the code again again it has given me a link I'll copy this link I'll again go to incognito I'll paste it here whenever you open this link this is how it will show so first we need to click on login with client ID I need to to paste my client ID first so I'll just copy this and paste your client ID uh client ID means the client name click on continue it will ask me 6-digit top go to top file run the code this is my top 439 762 43976 2 confirm top then I need to write my PIN sign in so now once I uh write those three things then it will take me to the google.com again I'll copy the URL okay again I'll go to this IP my uh Jupiter file okay again we'll follow the same step so first thing is I'll change this link I'll paste it here okay now if I run it this is how the link looks like we need to get the Au code from this link so I'll just run this again so this is my o code is saved in AU varibles if I run Au variable this is how Au code looks like now I'll again call this function to generate the access token Okay so so if I run the code you'll see it will give me access token now it is showing me a code equal to 200 so if I run this code I have saved my new access token clear everyone till here any doubt now keep in mind uh that OD code that we generate okay that odd code can only be used once how how let me prove it to you see if I run the code again okay if I run the same code again again it will not work so I'll call this function again this entire code now when I run the code you see it is showing error your o code has expired so whenever you verify yourself you get the O code and that o code can only be used once okay if you try to run the same o code twice it will not work you have o code you can only use the O code once after using it once you get the access token and and this access token is valid for the entire day okay I'll repeat again now this might not be same for other Brokers but for fir years it is okay so when you when you get this o code when you get this o code it is you can only use it once only once you can use after using it this access token that you get you can use it for entire day okay now what I want to do I want to automate this entire step okay I don't want to manually go write all the three credentials and then get the access token right I don't want to do this I want to automate this entire step so now I'm going to do that okay so before I move to the automation if anyone has any doubt in any step you can ask me right now vul Punit shinas have doubt yeah yes uh other than TP can we use the OTP uh for the automation process no mobile mobile OTP cannot be automated I have like I'm using API Bridge of fire on daily basis okay and there I need to log in with the OTP okay and at same time for the this like to access it Alo trading I need TP then how can I manage it if there any way you can just uh tell me I think in fires you can activate anyone anyone at a time yes yes that is the issue that's I'm asking is there any way to handle it I don't know actually you might have to ask fires you know you might have to call and ask this if there is anything they can do this is actually same across other Brokers as well so if I unable TP login instead of some other kind of OTP they'll disable all other methods yes you you're asking me or you're telling me like no no I'm updating you like that's the same experience with me as well it like we are only allowed to have one OTP method yeah see I'll tell you why uh mobile OTP p is not possible okay why it is not possible I'll tell I'll explain you that what happens when you activate top right so this is your mobile and this is Google server okay so what this Google server is doing uh this is the fires uh fires uh let's say broker okay now this fires broker has given permission to Google Okay so gole gole will generate a a unique OTP every 30 second and this unique OTP will be redirected to the mobile app okay now the point of contact here is Google server Google server is doing the work so what we are doing we are having our own python code okay and this python code is getting the data from the same Google server so this mobile app and this python code that you have we have written is getting the code from the same server so it is possible now in Mobile mobile OTP it's different right now in Mobile OTP we have fires okay and now we have a OTP provider OTP provider now there are lot of other OTP provider out there in the market you can just search about it now the problem is this OTP provider will directly send you the code to your Mobile SIM okay we are talking about Sim here here the code was being sent to mobile app right here the OTP is being sent to your sim right so we need a way to we need a way to connect our python code to this OTP provider which is not possible okay the OTP provide provider should have a API to make this possible it totally depends on the vendor that the fires are using fire might be using some uh top OTP vendor that vendor might not might not have this facility okay so this is the reason why it is not possible to automate the OTP part you can automate only the top login because we have a Google server Okay Google server is updating the OTP but in case of OTP provider I don't know where that server is who that server who that OTP provider is okay if they have some API or not if they have any API to get the same OTP by using a python code then it would be possible but I don't think that fires will ever reveal that because it's a security concern excuse me I have one solution for this every time I generate I have one soltion for this is it possible or not I get the mobile OTP every time on WhatsApp it's same like two three seconds delay then by using WhatsApp API can we use it in Code by using AP or anything that I can get from the WhatsApp and then I can use here yeah this might be possible you can send that to a telegram bot telegram bot will take the input yeah this this other way around it it might be possible but you have to do it manually then in Mobile you the moment you get the OTP you have to copy the OTP send it to the telegram app telegram board that you have created so the moment you send anything in the telegram bot your python will detect it and you can verify yourself this is possible okay I have never tried it but seems like a possible thing to do yeah I will try and then if there is anything I will ask you is it sure sure okay now uh let's try to automate the steps now to automate the step we need a selenium library now what's a selenium Library uh it's a library which is used to automate the login uh login like any type of web automation we can do by using selenium selenium python okay so this is the this is their website if you click on installation they'll tell you how to install this is what you need paper install selenium let me close this terminal open the terminal paste it here now since it's a Mac I'll write p 3 so we install the selenium Library we'll go to the website uh if you click on get started they provided us with some sample code so I'll just copy this sample code okay copy this and create a new file let's call it s demo. py now this file I'm creating to check if my selenium is working properly or not so I'll paste it here and I'll run the code the moment I run the code it will throw an error see if I run the the code it will throw an error now why it is throwing an error because whenever you use the selenium Library you need something called as driver okay and driver is dependent on the browser that you have now on my computer I have uh Brave browser and Chrome browser if you want you can do it for Firefox also but now for this session I'm going to show you how to do it for Chrome browser okay so what you will do first here you see there is a function web driver. Firefox you'll change this you'll write web driver. Chrome okay and inside Chrome you have to specify the address of the driver okay now what is a driver driver is a you can say it's a software okay which is used for automation stuff now how to download this driver I'll tell you so you'll go to Google or no no need to go to Google also if you go to installation if you scroll down they have provided they would have provided the link huh see they have provided a link for driver so click on Chrome so this is the official Chrome website uh where they allow us to download something okay so I'll click on latest dashboard okay you'll go come here now you'll install what is this what happened it actually executed the code how I don't know somehow it activated it uh executed the code but yeah let it be so you'll go you'll download the correct version this is something you have to figure out on your own which is the correct version uh what I'll do I'll download my correct version that is I'll go to Mac this is Chrome wait I'll scroll down okay never uh stable you should always download a stable version okay don't download beta version or Dev version and all okay download a stable version in stable version you can see there are multiple stuff so this is the Chrome driver this is what we need Chrome driver headless we need headless shell okay so in headless uh mine is this I guess see even I can be wrong I might download a wrong one so I have two options I four option this this this and this I don't know which one to download so I'll download one randomly okay so let's download this one I'll go for this one now if this doesn't work I might change I might have to download this one this one okay [Music] um okay let's let me just download this one okay this looks more convenient so I'll just copy this link I'll just paste it here press enter it will auto it should automatically download see it is automatically downloading I'll download it in the same folder FS 2024 save it's a zip file by the way see it's it would be a zip file that is being downloaded okay so this driver is needed if you wish to automate any process okay all right so what we will do we will extract right now it is being downloading it's downloading right now once it is downloaded we extract it there will be uh some file uh inside that extracted folder so that file will save it in the same directory okay so let it download then I'll show you it's almost downloaded okay now I'll just extract it open with archer will extract the file if I go inside I think I downloaded the wrong version Chrome headless okay let me download the other one I might have downloaded the wrong version so I'll download this one copy paste save again I'll just delete it what did I delete H I deleted the right stuff okay now it is being downloaded uh it's it's done oh it's great it's already done so I'll do a right click open with archiver h this is what I need okay this uh Chrome driver is what I need so I'll just copy this and I'll save it in the same folder now you can delete the other file that we downloaded okay so I'll come back here to my vs code you see I have a chrome driver here you'll just do a right click okay like this is the option thing you can just do a right click do copy path okay and again come here uh login automation no not loog log cell demo go to cell demo and just paste the address here okay that's it now I'll run the code again is throwing an error it is saying string object has no attribute capabilities or I I'll not write anything for now let me just try this okay it is opening uh even the address should work I'll write add equal to this for okay I'll do one more thing I'll have another driver here this is how this chrome driver looks like Okay so this also I downloaded the version is Google Chrome for testing. apppp so it's all about downloading the right version okay if this does if the code doesn't work it means you have downloaded something wrong so like this looks different right this looks like testing. app and that looks different uh I don't know why I'll just download something else okay I'll do one thing okay let let me download one more thing one more time so I think I downloaded this one right now I'll try this one like this is always a confusing thing to do which one to download so I always try multiple thing whichever works works I'll just download it one more time so the point being that the code should work okay I'll show you one more interesting thing I'll just copy this file now okay see I'll I'll just copy this file and I'll paste it here H so I have two Chrome driver this is also Chrome driver that I downloaded this is also Chrome driver that I downloaded okay so now I'll copy the address of this copy path I'll write a equal to this okay uh maybe I should write it in triple quotes now I'll run the code again it should work oh even this is not working okay I might have done some other mistake even this is not working like if I don't write any address it is working so it is working if I don't write any address but even address should work okay I'll do one thing so what uh argument we need to send we need to send option service keep alive option service okay so I think it is not needed maybe in the latest version they don't need the address okay so they automatically take the address they don't have anything called path and all right no they don't so I don't think you have to send the address anymore okay in the previous version you have to but in the new version you don't okay I think any Chrome will work okay so just so to let you know I'll delete one so this definitely works I've tried it first now I'm not sure if this new one will work or not so let's try this new one okay this is what I want to try so see if I run the code it is working it is opening uh this website that is uh python.org I'll show you some other website I'll try google.com let's try to see if we can open google.com or not so I'll just paste it here and run the code you see it should go to google.com see it is going to google.com so it means it is working fine okay now what I want to do I want to login the I want to automate the login stuff right so let's go to login automation file so till here we are getting the link okay so till here we are getting the link so uh I'll make a variable uh U link equal to this response okay and let's copy the code of from here copy this and paste it here okay so I already have the link so instead of this link this Google.com I'll write here LK link okay uh and let's remove the remaining stuff for now I'll write time do sleep so I need to import one more library that is time import time okay and I'll write time do sleep for 5 Second okay so what I'm doing the link that I've got I'm going to open that link and I'll sleep for 5 seconds then we'll close the file or close the driver okay let's run the code if I run the code it will go to this link it will wait for 5 Second and it will close the window okay so we have reach still here now what I want to do uh I want to go to this link let's increase the second I want to go to the link first I need to click on this login with client ID okay after clicking on this login to client ID I need to click on something else right so let's try to do that okay so I've already written the code for that so you don't need to worry about about it so this is the [Music] code so this is the code it will sleep for one second it will search for this client ID see I I'll show you how it looks uh I'll comment it I'll run the code again I'll just copy this link close this and let's paste it in a Cognito mode see when you go here you need the link of this login with client ID right so how do you get it just do inspect click on this over on this login with client ID this is where this is the U this is the HTML code to click on this login with client ID so just do a right click click on copy click on copy xath that's it so if I paste this this thing here this is how this xart look like and the same path is here see log with xath is this and this both of them are exactly same okay so I found the xath then I'm calling the find element function conment okay so this is my path then I'm finding this element uh then I'm clicking on it okay because I have to find this element then I have to click on on this okay after clicking this is this it will change like this okay then again I'll sleep for one second then I'll find the client ID so this will change initially it was number now it is client ID again I need uh the the X path for this client ID right so again I'll do the same thing I'll click here click here again do a right click click on copy copy xath so I've already done it so you don't have to do it I I'm not doing it again I'm finding the xath then I'm sending this uh X my user id after sending my user ID I need to press enter right so that's what I'm doing here okay I'm going to send keys. return return means press enter and then sleep for one second so these are all the things it will do now so let's see if this works or not so I'll just run the code again see it will click on login with client ID it will send the client ID see it it worked right now I need to write six top okay so till here everything has worked I'll repeat again what I have done just now I visited the link I clicked on uh login with client ID I I've written the client ID then I've clicked on continue how did I click on continue again I did the same thing I got the X paath for this continue okay see this is the xath just do a right click copy xath okay now after writing client ID I need to write top correct so to get top I need this code top pin right so I'll just copy these two line I need to copy this also login automation let's paste it here and let's paste these two line here okay so in this uh I think I should name it t here because here I have named it t yeah t I'll name T let's call this T so I have top now I just I don't even need this because I already have created a variable here top key so all my credentials are here okay so I have my top also top is also there now I need to write a code to type those top okay so this is not an easy task but I've already done it so this is how you type it okay so this code will find the xath for all the uh six boxes it will send uh the each digit in all those boxes and then it will uh click on it see at the end it is clck it will click on it then it will sleep for one second so let's see if this is working or not it will go it will first click on login to client ID it will add my client ID it will write the 6-digit TP done okay now the last step is I need to write the four-digit pin okay so for that I need to write the four-digit pin so this is my four-digit pin copy it and paste it here this is pin 1 pin 2 pin 3 pin 4 and this is the code to type four-digit pin okay very easy I'm just uh doing the same thing but see here I was using X paath here I'm using ID small change because X paath was not working so I had to use uh ID okay now after writing it is clicking it will click on the submit button see if I run the code it will verify myself it will first click on client ID then it will write my client ID write six digigit write 14 done it has visited google.com Okay now after visiting google.com I need to get the URL okay the URL that I have at the top right so how do I get that so to get the URL we have a function that is uh d. getcurrent URL so I'll copy this and instead of here I'll paste it here okay this driver do current URL will give will give me the current URL and then from the current URL I'm getting the desired Au code okay so I'll just print the O code here print o code now if I run the code you see it will generate the O code and it will print the O code also it will write client ID it will write six digigit it will write this okay I need to add a sleep also I removed the Sleep always add a sleep okay because you never know it it might not work uh time do sleep for 1 second we'll try it again take go link copy the link you see this is my o code see e and all this is my o code I found the O code okay I've already shown you how do you convert o code to access token that is already done now can this entire thing be done headlessly what is headless now see when I run the code it opens the browser it means it's a head it's a it's a uh what I can say I don't know it uses the head okay if you don't want if you want to do the same thing without opening the browser there is a way to do it okay so what we do is we uh just write this code just write these three lines of code okay so just go to where you are writing so instead of this line so instead of this line you write this okay I need to import also I need to import one more line that is this I need to import option need to create an option argument and just write add argument headless equal to new and then just write option equal to option so these three line of code will uh do the entire thing without opening the browser okay so let me now before uh showing you this so I'll just comment this for now I'll show you in a few minute but before that uh I need to get the access token right I found the O code now I need to get the access token and I need to save it in a file so I've already done it so see the same code same code that I have written here uh if you go to login. iynb see I have once you have the O code you just need to run this code right so I'll just copy this I'll go to login automation I can close it it's not a problem okay so this code okay I don't need to okay I don't need these thing Au code is being saved in AU code so I'll remove this line also okay this will generate Au code and it will save it in okay now fin I have got the response I'll get the access token also okay so in Access token variable I found the access token what I'll do now I'll save it in a file I'll write a with open let's open a file let's call it access.txt I'll write it I'll write w as K I'll write k. write and we'll write this access token so what will happen the moment access token is generated this code that is from line1 to 123 will generate the access token from o code once I've found the O code I'll just save this o code in a file easy let's run the code and see if it works or not it will open the browser it will click on log to client ID it will write my client ID it will write my four-digit pin it will get the link and you see if you click on access.txt I found the access token now before doing anything I told you that you need access token and we have the access token in access.txt files okay now can we do this headlessly yes now I'll uncomment these three line of code okay now if I uncomment this and if I run the code again it should work without opening the browser let's see uh wait I need to comment I need to comment this of course see it did the same thing without opening the I I'll I'll show you one more time if I run the code will do the entire thing without opening the browser see it will it will take few second around 10 second but in 10 second you'll see something here you'll see the access token you see I found the same access token this was the response and it saved see I'll show you one more time I'll just delete this now there is no access.txt file right if I run this same code again it will actually create the file for me around 5 to 10 second it will take does the text F will always replace or will openend the next response it will always see when you use uh W right so it removes the entire thing and it writes it again so each time it will keep every day it will erase entirely and it will write it again from the start okay so this is this is my entire login automation step okay so if you want to do it headlessly even that is possible you just need to write these three line of code okay if you want don't want to do it just uncomment line 44 and uh comment the other three line of code so this is how we automate the login process okay any doubt till here the only major concern uh that you would be having if you when you want to run the same code in your file what are the changes you will do first is you need a driver Chrome driver so you need to download the Chrome driver correctly that will be a one difficult task for you after that just change the credential okay these are the credentials we need I'll make sure that the credentials are not repeated uh okay so these this also you can okay this pin also you need to change whatever your PIN is and the rest of the things will remain the same okay I'll repeat again what thing you have to change you have to change your this credentials your client ID secret you redirect URI username top uh pin uh after changing this you need to change your four-digit pin pin 1 pin 2 pin 3 pin 4 and that's it the moment you run the code it will automatically generate the access token for you okay now without this access token you cannot move forward this was the first step of our uh API that was to get the access token okay and we have created a lot of files I'll just delete all the unnecessary file now see all the file like top is just a helper you can I will delete it cell demo is also a helper um file I just wanted to show you if it's working or not this is also a helper okay it is also not needed this is the main file login automation I have added everything in this file okay uh this is also not needed it's a log file credential okay now this credential how you will use uh if you don't whenever you are sending you don't have to send these things okay just just save it in a file and import it I'll just show you that also see I'll what I'll do these are the the important stuff right so I'll just take it here paste it so these are the credential I'll copy this I'll paste it in credentials. py I'll save the file and here instead of I'll write import credentials as CS CD you can call it whatever you want and write here CD do client ID here you'll write CD uh dot Secret key CD do U redirect URI oh this also I need right uh user I should have having username CD dot do username okay this is cd. top key this is cd. pin one pin 2 this is pin three this is pin 4 see if I run now I'll just delete this file I'll I'll show you by running it again again it will work similarly now make sure that all the files all the file which file uh this chrome driver file and this credential file are in the same directory and this login automation all the three files should be in the same folder they shouldn't be anywhere else only then this code will work okay so any doubt till here if you have any doubt you can ask me right now Punit Sagar Vidya sagur Akash SN rabindra writing down that P over there is it safe like if I'm having a lot of funds in my account safe for thing like that like don't share the file I'm sharing it because I'm going to change it after this session even though I saving it in my PC is it safe to like if someone else is coming and if if someone else is opening the file no then it's not suggested okay all right so I will be changing the pin so that's why I'm showing you after the entire entire API part is done okay now let's discuss about orders also today okay so after learning about orders I'll end the session okay so I have already created a file the file name is orders 1. iynb I'll just paste it here so this entire file entire code I will be giving you don't worry so now whenever you want to do anything the first step will be this okay you'll open the file I'm see I'm opening with open access.txt I'm reading the file and I'm saving it in access token variable and then you need to copy the client ID also so if you go to login automation uh if you go to credential this is the client ID okay you need this information also so I'll copy this I'll go to orders and I'll paste this here okay uh so these this is the first step you will do so I'll just run the code fires so I've selected now let's see if it if it is working or not so before running this oh I think I can run this let's run the code you see code 200 whenever you see 200 it means it is working fine so I just called the method position okay so what is position if you go to documentation again let's close this for now you go [Music] to here uh if you you need to go to transaction info in transaction info you have a position okay if you call the position method this is the data it will give you okay and you can easily convert this data into a data frame which I'll show you tomorrow okay uh now now I know that whenever I see 200 it means it is working fine okay so now I'll show you how to place different types of order okay so before I start with the order if anyone want to ask anything you can ask me right now all right let's let me explain you the different types of order then I'll show you how to code it okay so now this is something most of you would already be knowing but in case if you are a developer and you have never uh traded before for them I'm showing you this what are the different types of order so there are the first is Market order the second is limit order uh the third is stop order uh the fifth will be cover order and the last will be bracket order so these are the five orders that I will be showing you now sadly I don't know any Indian broker which has trailing order okay this is something which is not available in any Indian broker but it is available in Interactive Broker okay so I will show you the all the five order but this is something that is not available uh so if you want to implement the trailing stuff in your strategy so you have to manually code it okay now I'll just give you a brief overview about all the different types of order First Market order very easy whatever the price of price is at the that particular instance when you call the method it will take a trade now there are three things when you when you place a trade okay so this is broker this is exchange okay now lot of broker have a software to figure out that whatever order you are placing is correct or incorrect okay so when you have an API so when you send the request if you send something wrong okay so it will be rejected by the broker itself okay let's say you have sent something right okay it is correct so broker will send to exchange okay and if exchange if the order is filled so you can say so here it will show you filled okay this is how order works so you send it to the broker broker send it to The Exchange if order accepts the order if uh then the order will be filled and uh in in your broker Your Position will be added okay that stock Position will be added here let's say this is uh Amazon okay so when you place a market order whatever the price it will get it will give you that price now what is limit order now see limit can be by limit okay let's forget about that let's say the market price is 100 okay now when you're buying a stock you want the best price let's say you want 98 price so what you will do you'll place a limit order okay you'll place a limit order at 98 so what will happen it will not uh your order won't be filled instantly so exchange will wait till the price drops from 100 to 98 the moment it drops to 98 your order will be filled so in your position you will be having the order exactly at 98 or your order won't be filled two things will happen if it's a intraday order order will be cancelled automatically by the end of the day or the order will remain in your account for I don't know how many days in fires but like for elongated period of time the order will uh still be visible in your order book okay so this is how you place a limit order that is you want a fixed price and you specify that price price so uh broker will send order with that price this is what a limit order is now what is stop order now stop order is a little confusing thing why because this is not this is the the reason why you use limit order is to get a best price okay the purpose of a limit order is to get the best price purpose of a stop order is to save your capital okay stop order is being used with an assumption that you already have a trade and you want to minimize your losses let's say you have bought a stock at 100 okay now you will profit when the stock moves up let's say it has moved to 105 so you'll make a profit of 5 percentage okay but let's say the stock moves down okay it has moved to 95 right so you don't want to Bear more than 5% loss so what you will do you'll have a stop loss now this stop loss is what you can also call it buy no this you can also call it sell stop order okay stop-loss and sell stop order is the same thing so sell stop order you will place what will happen when you place a sell stop order at 95 the order won't be filled until the price reaches 95 the moment the price reaches 95 this stop loss order will be converted to a either a market order or a limit order depending on how you have coded okay if you have coded Market order it will be converted to a market order if you have coded in a limit order it will convert into a limit order now let's say you have converted into a market order so what will happen your position which was at 100 will be closed at any price it might close at 94 it might close at 96 it might close go at 97 also you don't know okay when you have a market sell stop order so see in stop order there are two thing one is market sell stop order and one is limit sell stop order okay now there are two more things one is Market buy stop order order and there is limit by stop order okay now when will you use this okay you'll use this let's say you have sold a stock at 100 okay you have sold it at 100 now you'll only make profit when the price will move down if it reaches 95 that is where you'll make money right but if this price reaches up let's say it is 105 so now you're Lo losing it you're losing money and you don't want to lose more than 5% so what you'll do you'll place a uh Market by stop order okay at 105 so what will happen the moment reaches the the moment the market reaches 105 your order will be filled now it's a market order so you don't know at what price it will be filled either it will be filled at one4 or it will be filled at 10 6 you don't know okay but if it's a limit buy stop order so it will be exactly filled at that price okay so stop order is also clear now the next is third one is no this is a fourth one right fourth one is cover order now what's a cover order in cover order we place two orders okay one will be uh you can say a market order so let's this is buy and one will be a stop-loss order for risk or you can say save money save okay two order will be placed at the same time okay now this cover order will automatically close your position when your uh loss is more than a certain limit which you have determined when you have placed a stop loss let's say say you have placed a stop loss of 5% okay so 100 100 is the price stop loss at 95 so it will the stop loss Will Remain at 95 no matter how your price fluctuate your position won't be closed the moment it reaches 95 that is when your position will be automatically be closed now when I say Position will be closed it means if you have a buy order it will automatically sell it if it's a sell order it will automatically buy it okay and the last one is bracket order okay so in bracket order what it will do it will place a market order let's say okay it will have two more legs one will be stop-loss leg and one will be take profit leg now the benefit of bracket order is that you not only have a defined loss you also have a defined profit profit okay and if any of the leg is triggered then the other leg is automatically closed like let's say your profit target has hit so your stop- loss would will automatically be closed all right let's say if your stop- loss got hit then your take profit would automatically be closed this is the benefit of using a bracket order clear everyone all the different types of order is clear any out in this I think this would be enough for today it's I've already taken quite a lot of time so we'll discuss the code tomorrow okay how to place different types of order we will be discussing tomorrow so before I end the session if anyone has any doubt you can ask me right now okay I have a question one second uh in different type of order see the market order uh will code it's in Market order is a market price yes it will hit right but in other other order type we have to mention the price or percentage in the code right price yes you have to specify okay I'll show you tomorrow how to do that thanks okay uh play right I don't know uh Sagar what is play right
Info
Channel: FessorPro
Views: 5,320
Rating: undefined out of 5
Keywords:
Id: gP1r0DbzBC4
Channel Id: undefined
Length: 92min 14sec (5534 seconds)
Published: Mon Feb 26 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.