Official Python SDK for Alice Blue API
Project description
Official Python SDK for Alice Blue API
The Official Python library for communicating with the Alice Blue APIs.
Alice Blue Python library 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.
- Author: CodiFi
Installation
This module is installed via pip:
pip install pya3
To force upgrade existing installations:
pip uninstall pya3
pip --no-cache-dir install --upgrade pya3
Prerequisites
Python 3.x
Getting started with API
Overview
There is only one class in the whole library: AliceBlue
. The get_session_id
static method is used to retrieve a Session ID from the alice blue server. A Session ID is valid until the trading account logout.
With a Session ID, you can instantiate an AliceBlue object. Ideally you only need to create a Session ID once every time login the trading account. After you have the Session ID, 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
Get a Session ID
- Import pya3
from pya3 import *
Create AliceBlue Object
- You can create an AliceBlue object with your
UserID
andAPI Key
.
alice = Aliceblue(user_id='username',api_key='API_KEY')
- You can get a Session ID by running following command.
print(alice.get_session_id()) # Get Session ID
- You can run commands here to check your connectivity
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
Get tradable instruments
Symbols can be retrieved in multiple ways. Once you have the master contract loaded for an exchange, you can get an instrument in many ways.
Get a single instrument by it's name:
tatasteel_nse_eq = alice.get_instrument_by_symbol('NSE', 'TATASTEEL')
reliance_nse_eq = alice.get_instrument_by_symbol('NSE', 'RELIANCE')
ongc_bse_eq = alice.get_instrument_by_symbol('BSE', 'ONGC')
india_vix_nse_index = alice.get_instrument_by_symbol('NSE', 'India VIX')
sensex_nse_index = alice.get_instrument_by_symbol('BSE', 'SENSEX')
nifty50_nse_index = alice.get_instrument_by_symbol('NSE', 'Nifty 50')
banknifty_nse_index = alice.get_instrument_by_symbol('NSE', 'Nifty Bank')
Get a single instrument by it's token number (generally useful only for BSE Equities):
ongc_bse_eq = alice.get_instrument_by_token('BSE', 500312)
reliance_bse_eq = alice.get_instrument_by_token('BSE', 500325)
acc_nse_eq = alice.get_instrument_by_token('NSE', 22)
Search for symbols
Search for multiple instruments by matching the name. This works case insensitive and returns all instrument which has the name in its symbol.
all_sensex_scrips = alice.search_instruments('BSE', 'sEnSeX')
print(all_sensex_scrips)
The above code results multiple symbol which has 'sensex' in its symbol.
[Instrument(exchange='BSE', token=1, symbol='SENSEX', name='SENSEX', expiry=None, lot_size=None), Instrument(exchange='BSE', token=540154, symbol='IDFSENSEXE B', name='IDFC Mutual Fund', expiry=None, lot_size=None), Instrument(exchange='BSE', token=532985, symbol='KTKSENSEX B', name='KOTAK MAHINDRA MUTUAL FUND', expiry=None, lot_size=None), Instrument(exchange='BSE', token=538683, symbol='NETFSENSEX B', name='NIPPON INDIA ETF SENSEX', expiry=None, lot_size=None), Instrument(exchange='BSE', token=535276, symbol='SBISENSEX B', name='SBI MUTUAL FUND - SBI ETF SENS', expiry=None, lot_size=None)]
Search for multiple instruments by matching multiple names
multiple_underlying = ['BANKNIFTY','NIFTY','INFY','BHEL']
all_scripts = alice.search_instruments('NFO', multiple_underlying)
Place an order
Place limit, market, SL, SL-M, AMO, BO, CO orders
print (alice.get_profile())
# TransactionType.Buy, OrderType.Market, ProductType.Delivery
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
alice.place_order(transaction_type = TransactionType.Buy,
instrument = alice.get_instrument_by_symbol('NSE', 'INFY'),
quantity = 1,
order_type = OrderType.Market,
product_type = ProductType.Delivery,
price = 0.0,
trigger_price = None,
stop_loss = None,
square_off = None,
trailing_sl = None,
is_amo = False)
)
# TransactionType.Buy, OrderType.Market, ProductType.Intraday
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
alice.place_order(transaction_type = TransactionType.Buy,
instrument = alice.get_instrument_by_symbol('NSE', 'INFY'),
quantity = 1,
order_type = OrderType.Market,
product_type = ProductType.Intraday,
price = 0.0,
trigger_price = None,
stop_loss = None,
square_off = None,
trailing_sl = None,
is_amo = False)
)
# TransactionType.Buy, OrderType.Market, ProductType.CoverOrder
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%3%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
alice.place_order(transaction_type = TransactionType.Buy,
instrument = alice.get_instrument_by_symbol('NSE', 'INFY'),
quantity = 1,
order_type = OrderType.Market,
product_type = ProductType.CoverOrder,
price = 0.0,
trigger_price = 7.5, # trigger_price Here the trigger_price is taken as stop loss (provide stop loss in actual amount)
stop_loss = None,
square_off = None,
trailing_sl = None,
is_amo = False)
)
# TransactionType.Buy, OrderType.Limit, ProductType.BracketOrder
# OCO Order can't be of type market
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%4%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
alice.place_order(transaction_type = TransactionType.Buy,
instrument = alice.get_instrument_by_symbol('NSE', 'INFY'),
quantity = 1,
order_type = OrderType.Limit,
product_type = ProductType.BracketOrder,
price = 8.0,
trigger_price = None,
stop_loss = 6.0,
square_off = 10.0,
trailing_sl = None,
is_amo = False)
)
# TransactionType.Buy, OrderType.Limit, ProductType.Intraday
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%5%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
alice.place_order(transaction_type = TransactionType.Buy,
instrument = alice.get_instrument_by_symbol('NSE', 'INFY'),
quantity = 1,
order_type = OrderType.Limit,
product_type = ProductType.Intraday,
price = 8.0,
trigger_price = None,
stop_loss = None,
square_off = None,
trailing_sl = None,
is_amo = False)
)
# TransactionType.Buy, OrderType.Limit, ProductType.CoverOrder
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%6%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
alice.place_order(transaction_type = TransactionType.Buy,
instrument = alice.get_instrument_by_symbol('NSE', 'INFY'),
quantity = 1,
order_type = OrderType.Limit,
product_type = ProductType.CoverOrder,
price = 7.0,
trigger_price = 6.5, # trigger_price Here the trigger_price is taken as stop loss (provide stop loss in actual amount)
stop_loss = None,
square_off = None,
trailing_sl = None,
is_amo = False)
)
###############################
# TransactionType.Buy, OrderType.StopLossMarket, ProductType.Delivery
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
alice.place_order(transaction_type = TransactionType.Buy,
instrument = alice.get_instrument_by_symbol('NSE', 'INFY'),
quantity = 1,
order_type = OrderType.StopLossMarket,
product_type = ProductType.Delivery,
price = 0.0,
trigger_price = 8.0,
stop_loss = None,
square_off = None,
trailing_sl = None,
is_amo = False)
)
# TransactionType.Buy, OrderType.StopLossMarket, ProductType.Intraday
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%8%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
alice.place_order(transaction_type = TransactionType.Buy,
instrument = alice.get_instrument_by_symbol('NSE', 'INFY'),
quantity = 1,
order_type = OrderType.StopLossMarket,
product_type = ProductType.Intraday,
price = 0.0,
trigger_price = 8.0,
stop_loss = None,
square_off = None,
trailing_sl = None,
is_amo = False)
)
# TransactionType.Buy, OrderType.StopLossMarket, ProductType.CoverOrder
# CO order is of type Limit and And Market Only
# TransactionType.Buy, OrderType.StopLossMarket, ProductType.BO
# BO order is of type Limit and And Market Only
###################################
# TransactionType.Buy, OrderType.StopLossLimit, ProductType.Delivery
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%9%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
alice.place_order(transaction_type = TransactionType.Buy,
instrument = alice.get_instrument_by_symbol('NSE', 'INFY'),
quantity = 1,
order_type = OrderType.StopLossMarket,
product_type = ProductType.Delivery,
price = 8.0,
trigger_price = 8.0,
stop_loss = None,
square_off = None,
trailing_sl = None,
is_amo = False)
)
# TransactionType.Buy, OrderType.StopLossLimit, ProductType.Intraday
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%10%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
alice.place_order(transaction_type = TransactionType.Buy,
instrument = alice.get_instrument_by_symbol('NSE', 'INFY'),
quantity = 1,
order_type = OrderType.StopLossLimit,
product_type = ProductType.Intraday,
price = 8.0,
trigger_price = 8.0,
stop_loss = None,
square_off = None,
trailing_sl = None,
is_amo = False)
)
# TransactionType.Buy, OrderType.StopLossLimit, ProductType.CoverOrder
# CO order is of type Limit and And Market Only
# TransactionType.Buy, OrderType.StopLossLimit, ProductType.BracketOrder
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%11%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
alice.place_order(transaction_type = TransactionType.Buy,
instrument = alice.get_instrument_by_symbol('NSE', 'INFY'),
quantity = 1,
order_type = OrderType.StopLossLimit,
product_type = ProductType.BracketOrder,
price = 8.0,
trigger_price = 8.0,
stop_loss = 1.0,
square_off = 1.0,
trailing_sl = 20,
is_amo = False)
)
Place basket order
Basket order is used to buy or sell group of securities simultaneously.
order1 = { "instrument" : alice.get_instrument_by_symbol('NSE', 'INFY'),
"order_type" : OrderType.Market,
"quantity" : 1,
"transaction_type" : TransactionType.Buy,
"product_type" : ProductType.Delivery}
order2 = { "instrument" : alice.get_instrument_by_symbol('NSE', 'SBIN'),
"order_type" : OrderType.Limit,
"quantity" : 2,
"price" : 280.0,
"transaction_type" : TransactionType.Sell,
"product_type" : ProductType.Intraday}
orders = [order1, order2]
print(alice.place_basket_order(orders))
Cancel an order
alice.cancel_order('170713000075481') #Cancel an open order
Getting order history and trade details
Get order history of a particular order
print(alice.get_order_history('170713000075481'))
Get order history of all orders.
print(alice.get_order_history())
Get trade book
print(alice.get_trade_book())
Order properties as enums
Order properties such as TransactionType, OrderType, and others have been safely classified as enums so you don't have to write them out as strings
TransactionType
Transaction types indicate whether you want to buy or sell. Valid transaction types are of the following:
TransactionType.Buy
- buyTransactionType.Sell
- sell
OrderType
Order type specifies the type of order you want to send. Valid order types include:
OrderType.Market
- Place the order with a market priceOrderType.Limit
- Place the order with a limit price (limit price parameter is mandatory)OrderType.StopLossLimit
- Place as a stop loss limit orderOrderType.StopLossMarket
- Place as a stop loss market order
ProductType
Product types indicate the complexity of the order you want to place. Valid product types are:
ProductType.Intraday
- Intraday order that will get squared off before market closeProductType.Delivery
- Delivery order that will be held with you after market closeProductType.CoverOrder
- Cover orderProductType.BracketOrder
- One cancels other order. Also known as bracket order
Example strategy using alice blue API
This strategy generates a buy signal when 5-EMA > 20-EMA (golden cross) or a sell signal when 5-EMA < 20-EMA (death cross).
Read this before creating an issue
Before creating an issue in this library, please follow the following steps.
- Search the problem you are facing is already asked by someone else. There might be some issues already there, either solved/unsolved related to your problem. Go to issues
- If you feel your problem is not asked by anyone or no issues are related to your problem, then create a new issue.
- Describe your problem in detail while creating the issue. If you don't have time to detail/describe the problem you are facing, assume that I also won't be having time to respond to your problem.
- Post a sample code of the problem you are facing. If I copy paste the code directly from issue, I should be able to reproduce the problem you are facing.
- Before posting the sample code, test your sample code yourself once. Only sample code should be tested, no other addition should be there while you are testing.
- Have some print() function calls to display the values of some variables related to your problem.
- Post the results of print() functions also in the issue.
- Use the insert code feature of github to inset code and print outputs, so that the code is displayed neat.
- If you have multiple lines of code, use triple grave accent ( ``` ) to insert multiple lines of code. Example:
- Here is an example of what I'm expecting while you are creating an issue.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.