Getting Started with Alice Blue API

Aliceblue has a set of REST-like HTTP APIs that expose many capabilities required to build a complete stock market investment and trading platform. It lets you execute orders in real-time (equities, commodities, mutual funds), manage user portfolios, stream live market data over WebSockets, and more.

The Alice Blue Python library created by Krishna provides an easy-to-use wrapper over the HTTPS APIs.
 

The HTTP calls have been converted to methods and JSON responses are wrapped into Python-compatible objects.

Websocket connections are handled automatically within the library.

We are strategic partner of Aliceblue. We are responsible for this git as well as our primary task is to create an ecosystem of Algo trading on the Indian Stock Market. If You’ve opened an Alice account through us, You will get access to our private codes, tutorials, and a lot more.

Installation

This module is installed via pip:

pip install alice_blue

To force upgrade existing installations:

pip uninstall alice_blue
pip --no-cache-dir install --upgrade alice_blue

Getting Started with API

Overview

There is only one class in the whole library: AliceBlue. The login_and_get_access_token() static method is used to retrieve an access token from the alice blue server. An access token is valid for 24 hours. With an access token, you can instantiate an AliceBlue object. Ideally you only need to create an access_token once every day. After you have the access token, you can store it separately for re-use.

REST Documentation

The original REST API that this SDK is based on is available online. Alice Blue API REST documentation

Using the API

Logging

The whole library is equipped with python’s logging moduele for debugging. If more debug information is needed, enable logging using the following code.

import logging<br />logging.basicConfig(level=logging.DEBUG)

Get api_secret

api_secret is unique for each and every account. You need to enable API trading and get api_secret from Alice Blue. Please contact alice blue for getting api_secret.

We are creating OAuth Application. Three variables are needed for OAuth Applications.

  • Client ID/App ID
  • Client Secret/APP Secret/API Secret
  • Redirect URL

By default, When You ask from Aliceblue, they will make the Client ID the same as your Alice User ID and Redirect URL as localhost

If You’ve opened an account through Unofficed, Please contact us for activation of API and sample code.

Get an access token

#1 Import alice_blue
from alice_blue import *
#2 Create access_token using login_and_get_access_token() function 
access_token = AliceBlue.login_and_get_access_token(username='username', password='password', twoFA='a', api_secret='api_secret')
but in case your client_id and redirect_url is different, You need to redo these above step as following –
#Declaring User Details
username = ''
password = ''
twoFA=''

#Declaring OAuth App Details
client_id = ''
client_secret = ''
redirect_url= ''

access_token = AliceBlue.login_and_get_access_token(username=username, password=password, twoFA=twoFA, api_secret=client_secret, redirect_url=redirect_url, app_id=client_id)
For betting clarity, the variable names are declared in the starting itself instead of the single line.

Create AliceBlue Object

Once you have your access_token, you can create an AliceBlue object with your access_tokenusername and password.

alice = AliceBlue(username='username', password='password', access_token=access_token)

Final Python Code to Connect Alice Blue API

The Final python code will look something like this –
import logging
logging.basicConfig(level=logging.DEBUG)

from alice_blue import *

#Fill These Details Yourself.
##Ask AliceBlue Support to give you Client ID, Client Secret and Redirect URL.
###If You're Unofficed Client (Signed up from https://www.unofficed.com/aliceblue/), Ask me.

#Declaring User Details
username = ''
password = ''
twoFA=''

#Declaring OAuth App Details
client_id = ''
client_secret = ''
redirect_url= ''

#Getting the Access Token
access_token = AliceBlue.login_and_get_access_token(username=username, password=password, twoFA=twoFA, api_secret=client_secret, redirect_url=redirect_url, app_id=client_id)

#Getting the Alice Object
alice = AliceBlue(username=username, password=password, access_token=access_token, master_contracts_to_download=['NSE', 'BSE', 'MCX', 'NFO'])

print(alice.get_balance()) # get balance / margin limits
print(alice.get_profile()) # get profile
print(alice.get_daywise_positions()) # get daywise positions
print(alice.get_netwise_positions()) # get netwise positions
print(alice.get_holding_positions()) # get holding positions

Jupyter Notebook / Google Collab Version

The Final Jupyter version of the python code is made in Google Collab as following. It will show you both the input and output. Feel free to make a copy of it and tinker with it –