V3 FYERS API Utilize Free of Cost to Automate Trades in Python:
Following Instructions from the above video and code we are able to generate access token from fyers api v2 but to generate access token in api v3 we need to follow different syntax in python.
Follow Instructions from the above video and then go for the input and code to generate access token from fyers api v3 for that we need to follow different syntax and command in python given below.
CMD : pip install fyers_apiv3
Change API URL : https://api-t1.fyers.in/api/v3/generate-authcode?client_id=your_app_id&redirect_uri=https://myapi.fyers.in/docs/&response_type=code&state=None
How to Place Market Bracket Order in Option Utilizing Fyers API:
Follow the Above Video then go for the Code Input Everything Follows the Same from the Below Code of Placing StopLoss Limit Orders Just for Few Changes Follow the Code After Print Statement Such as StopLoss and TakeProfit
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()
FYERS API V2:
CMD:pip install fyers-apiv2
Following Instructions from the above video and code we are able to generate access token from fyers api v1 but to generate access token in api v2 we need to follow different syntax in python but instructions will be the same as stated in the video the most important part is that we need to create an app under api version 2.If you don't have an account please do Open Fyers Trading Account for that Please Click Here. After Opening Fyers trading account from the link please visit https://myapi.fyers.in/dashboard/ When you click on api version 2 the 2 most important thing is app name and redirect url which you can choose of your choice eg. ABCD as app name and in redirect url it can be your choice as well but it should be perfect and that syntax should be followed in every redirect url in your python code while trading i'm using documentation url fyers represent each and every syntax as well usage that will be https://myapi.fyers.in/docs/ after this maintain a description and go ahead with app permissions and accept the API usage terms and conditions as well as app permissions and go ahead with create app click on them and your fyers app is ready for trading in python and meantime don't worry about the inactive status after when you start with python generating access token the app get in active status.
After the app is created you will get an App id and Secret id copy them and store them in a text file and required folder for later access now if you check with api docs there is a list of codes in generation of access token in which we can skip few steps in which we need a url which gives a auth_code from web browser for that reason please follow the url as such 'https://api.fyers.in/api/v2/generate-authcode?client_id=App_id&redirect_uri=https://myapi.fyers.in/docs/&response_type=code&state=None' now the client id will be yours app id which you received after creation of app and redirect url as of your choice or you can use the redirect url of fyers docs as well.
Now put the auth code you received from web browser in python code and generate the access token and saved them in required text file and folder for later access.If you face any error regarding api access it might be issue due to older installation so please completely wipe them by utilizing pip uninstall in cmd and then go directly installation with pip install fyers-apiv2 and follow the below code as follows.
from fyers_api import fyersModel
from fyers_api import accessToken
client_id='your_app_id'
secret_key='your_secret_id'
redirect_uri='https://myapi.fyers.in/docs/'
session=accessToken.SessionModel(client_id=client_id,
secret_key=secret_key,redirect_uri=redirect_uri,
response_type='code',grant_type='authorization_code')
auth_code='you received from web browser'
session.set_token(auth_code)
response = session.generate_token()
access_token = response["access_token"]
print(access_token)
After generation of access token things will get simpler just add the token and client id in required field and call the model and utilize them for placing orders and accessing the details of postions,trades and data as well for writing strategies and implementing strategy as follows.
Fetch Weekly Expiry Current Index Option Strike Price Symbol Utilizing Fyers API V2:
import pandas as pd
import csv
wr=csv.writer(open('symbol.csv','w'))
index_symbol='BANKNIFTY'
instruments=pd.read_csv('https://public.fyers.in/sym_details/NSE_FO.csv',header=None)
ism=instruments[instruments[13]=='{}'.format(index_symbol)]
index_value=ism.index[0]
tradingsymbol=ism[1][index_value]
tradingsymbol=tradingsymbol[:-9]
symbol=ism[ism[1].str.match('{}'.format(tradingsymbol))].reset_index(drop=True)[9]
for x in range(len(symbol)):
with open('symbol.csv','a') as csvfile:
wr=csv.writer(csvfile)
wr.writerow([symbol[x][0:3],symbol[x][4:]])
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 will definitely help to get your API active.