Alice Blue Utilize Free of Cost API to Automate Trades in Python





Open Alice Blue Trading Account Please Click Here.



CMD pip Installation Type and Enter: pip install alice_blue



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



from alice_blue import *
import csv
access_token=AliceBlue.login_and_get_access_token(username='username',password='password',twoFA='twoFA',api_secret='api_secret')
print(access_token)
with open('access_token.txt','w') as wr1:
    wr=csv.writer(wr1)
    wr.writerow([access_token])
access_token=open('access_token.txt','r').read().strip()   
alice=AliceBlue(username='username',password='password',access_token=access_token, master_contracts_to_download=['NSE', 'BSE'])
tatasteel_nse_eq = alice.get_instrument_by_symbol('NSE', 'TATASTEEL')
print(tatasteel_nse_eq)


Manage Alice Blue Login Credentials as well as API Secret Confidentially with the help of Python inbuilt Module Function DBM(Database Manager).


First create the Database and then go for Read and Write in which 'c' does all the work if database not created it will create them and then with the help of 'r' we will read our credentials and utilize them wherever it is required and now the value dbm while write comes with 'b as byte which won't be true if we pass the same in our required credentials so we will go for decode() function which will decode and erase 'b and now we can utilize as required without any trouble or error and if you don't have trading account please follow above link.


To create database with login credentials and api go for below code with your username,password,twoFA and api_secret:


import dbm
with dbm.open('alicedbm','c') as db:
    db['username']='your_username'
    db['password']='your_password'
    db['twoFA']='your_twoFA'
    db['api_secret']='your_api_secret'


After create of database with login credentials and api go for below code to create access token and same utilize for programmatic/algo trading in python as explained in video so follow them properly for instructions.


from alice_blue import *
import csv
import dbm
db=dbm.open('your_db_stored_path/alicedb','r')
access_token=AliceBlue.login_and_get_access_token(username=db['username'].decode(),password=db['password'].decode(),twoFA=db['twoFA'].decode(),api_secret=db['api_secret'].decode())
print(access_token)
with open('access_token.txt','w') as wr1:
    wr=csv.writer(wr1)
    wr.writerow([access_token])
access_token=open('access_token.txt','r').read().strip()   
alice=AliceBlue(username=db['username'].decode(),password=db['password'].decode(),access_token=access_token, master_contracts_to_download=['NSE', 'BSE'])
tatasteel_nse_eq = alice.get_instrument_by_symbol('NSE', 'TATASTEEL')
print(tatasteel_nse_eq)


Get Strike List's of Trading Symbol for Options Trading in Python for that Follow Below Video then Go for the Code.



import requests
import io
from pprint import pprint
import pandas as pd
instruments=requests.get('https://api.kite.trade/instruments').content
instruments=pd.read_csv(io.StringIO(instruments.decode('utf-8')))
instruments_str_match=instruments[instruments['segment'].str.match('NFO-OPT')]
strike=instruments_str_match[instruments_str_match['tradingsymbol'].str.match('TATAMOTORS')]
strike_price=strike['strike']
print(strike_price)

Search Instruments Fetch Symbol,Expiry and Lot Size for that Follow Below Video then Go for the Code.



from alice_blue import *
import pandas as pd
username=open('username.txt','r').read().strip()
password=open('password.txt','r').read().strip()
access_token=open('access_token.txt','r').read().strip()
alice=AliceBlue(username,password,access_token,['NFO','NSE'])
instruments=alice.search_instruments('NFO','BANKNIFTY')
instruments=pd.DataFrame(instruments)
tradingsymbol=instruments[0:1]['symbol'].to_string(index=False)
print(tradingsymbol)