ShoonyaApi Py
ShoonyaApi Py
Build
to build this package and install it on your server please use
pip install -r requirements.txt
API
ShoonyaApi
- login
- logout
Symbols
- searchscrip
- get_security_info
- get_quotes
- get_time_price_series
- get_daily_price_series
- get_option_chain
- modify_order
- cancel_order
- exit_order
- product_convertion
- get_orderbook
- get_tradebook
- get_singleorderhistory
- get_limits
Websocket API
- start_websocket
- subscribe
- unsubscribe
Example
- getting started
- Market Functions
Request Details :
vc* Vendor code provided by noren team, along with connection URLs
imei* Send mac if users logs in for desktop, imei is from mobile
addldivinf Optional field, Value must be in below format:
source API
Response Details :
prarr Json array of Product Obj with enabled products, as defined below.
actid Account id
email Email Id
brkname Broker id
Request Details :
Response Details :
Response data will be in json format with below fields.
Request Details :
exch* NSE / NFO / Exchange (Select from ‘exarr’ Array provided in User Details
BSE / MCX response)
dscqty Disclosed quantity (Max 10% for NSE, and 50% for MCX)
prd* C/M/H Product name (Select from ‘prarr’ Array provided in User Details
response, and if same is allowed for selected, exchange. Show
product display name, for user to select, and send corresponding
prd in API call)
ret* DAY / EOS / Retention type (Show options as per allowed exchanges)
IOC
tsym2 Trading symbol of second leg, mandatory for price type 2L and 3L
(use url encoding to avoid special char error for symbols like
M&M)
trantype2 Transaction type of second leg, mandatory for price type 2L and
3L
qty2 Quantity for second leg, mandatory for price type 2L and 3L
prc2 Price for second leg, mandatory for price type 2L and 3L
tsym3 Trading symbol of third leg, mandatory for price type 3L (use url
encoding to avoid special char error for symbols like M&M)
Response Details :
Response data will be in json format with below fields.
Request Details :
exch* Exchange
Response Details :
Response data will be in json format with below fields.
Request Details :
Response Details :
Response data will be in json format with below fields.
prd* H/B Allowed for only H and B products (Cover order and bracket order)
Response Details :
Response data will be in json format with below fields.
Request Details :
exch* Exchange
actid* Account id
Response Details :
Response data will be in json format with below fields.
Request Details :
Response Details :
Response data will be in json Array of objects with below fields in case of success.
prd Display product alias name, using prarr returned in user details.
status
uid
actid
amo Yes / No
pp Price precision
ti Tick size
ls Lot size
norentm
ordenttm
exch_tm
snonum This field will be present for product H and B; and only if it is
profit/sl order.
Response data will be in json format with below fields in case of failure:
Request Details :
Response Details :
Response data will be in json Array of objects with below fields in case of success.
prd Display product alias name, using prarr returned in user details.
actid
pp Price precision
ti Tick size
ls Lot size
flid Fill ID
token Token
Response data will be in json format with below fields in case of failure:
Response Details :
Response data will be in json Array of objects with below fields in case of success.
prd Display product alias name, using prarr returned in user details.
status
actid
amo Yes / No
pp Price precision
ti Tick size
ls Lot size
norentm
ordenttm
exch_tm
Response data will be in json format with below fields in case of failure:
Request Details :
Response Details :
Response data will be in json format with below fields in case of Success:
Notes:
Valuation : btstqty + holdqty + brkcolqty + unplgdqty + benqty + Max(npoadqty, dpqty) - usedqty
Salable: btstqty + holdqty + unplgdqty + benqty + dpqty - usedqty
Exch_tsym object:
pp Price precision
ti Tick size
ls Lot size
Response data will be in json format with below fields in case of failure:
Request Details :
Response Details :
Response data will be in json format with Array of Objects with below fields in case of success.
actid Account Id
lp LTP
rpnl RealizedPNL
urmtom UnrealizedMTOM.
openbuyqty
opensellqty
openbuyamt
opensellamt
openbuyavgprc
opensellavgprc
mult
pp
prcftr gnpn/(gdpd).
ti Tick size
ls Lot size
Response data will be in json format with below fields in case of failure:
]
Sample Failure Response :
{
"stat":"Not_Ok",
"request_time":"14:14:11 26-05-2020",
"emsg":"Error Occurred : 5 \"no data\""
}
product_type strin True retreives the delivery holdings or for a given product
g
-------------------------Cash Additional
Fields-------------------------------
-------------------------Margin
Utilized----------------------------------
-------------------------Margin Used
components---------------------
-------------------------Additional Risk
Limits---------------------------
-------------------------Additional Risk
Indicators---------------------------
if ret != None:
symbols = ret['values']
for symbol in symbols:
print('{0} token is {1}'.format(symbol['tsym'], symbol['token']))
Example 2:
api.searchscrip(exchange='NSE', searchtext='REL')
Request Details :
exch Exchange (Select from ‘exarr’ Array provided in User Details response)
Response Details :
Response data will be in json format with below fields.
pp Price precision
ti Tick size
ls Lot size
Request Details :
exch Exchange
Response Details :
Response data will have below fields.
seg Segment
isin ISIN
ti Tick Size
ls Lot Size
pp Price precision
mult Multiplier
Request Details :
exch Exchange
Response Details :
Response data will be in json format with below fields.
seg Segment
isin ISIN
pp Price precision
ls Lot Size
ti Tick Size
mult Multiplier
token Token
lp LTP
o Open Price
v Volume
Request Details :
exch* Exchange
token*
intrv “1”, ”3”, “5”, “10”, “15”, “30”, Candle size in minutes (optional field, if not given
“60”, “120”, “240” assume to be “1”)
Response Details :
Response data will be in json format in case for failure.
v volume
oi oi
exchange strin False Exchange (UI need to check if exchange in NFO / CDS /
g MCX / or any other exchange which has options, if not don't
allow)
tradingsymbol strin False Trading symbol of any of the option or future. Option chain
g for that underlying will be returned. (use url encoding to
avoid special char error for symbols like M&M)
count int True Number of strike to return on one side of the mid price for
PUT and CALL. (example cnt is 4, total 16 contracts will be
returned, if cnt is is 5 total 20 contract will be returned)
in the example above we see first message t='tk' with all the values, 2nd message has lasttradeprice avg
price and few other fields with value changed.. note bp1 isnt sent as its still 118.45
in the next tick ( 3rd message) only last price is changed to 118.6
Subscription Acknowledgement:
Number of Acknowledgements for a single subscription will be the same as the number of scrips
mentioned in the key (k) field.
| Json Fields | Possible value | Description|
| --- | --- | --- |
| t | tk |‘tk’ represents touchline acknowledgement |
| e |NSE, BSE, NFO ..|Exchange name |
| tk |22|Scrip Token |
| pp |2 for NSE, BSE & 4 for CDS USDINR|Price precision |
| ts | | Trading Symbol |
| ti | | Tick size |
| ls | | Lot size |
| lp | |LTP |
| pc | |Percentage change |
| v | | volume |
| o | | Open price |
| h | | High price |
| l | | Low price |
| c | | Close price |
| ap | | Average trade price |
| oi | | Open interest |
| poi | | Previous day closing Open Interest |
| toi | | Total open interest for underlying |
| bq1 | | Best Buy Quantity 1 |
| bp1 | | Best Buy Price 1 |
| sq1 | | Best Sell Quantity 1 |
| sp1 | | Best Sell Price 1|
TouchLine subscription Updates :
Accept for t, e, and tk other fields may / may not be present.
| Json Fields | Possible value | Description|
| --- | --- | --- |
| t | tf |‘tf’ represents touchline acknowledgement |
| e |NSE, BSE, NFO ..|Exchange name |
| tk | 22 |Scrip Token |
| lp | |LTP |
| pc | |Percentage change |
| v | | volume |
| o | | Open price |
| h | | High price |
| l | | Low price |
| c | | Close price |
| ap | | Average trade price |
| oi | | Open interest |
| poi | | Previous day closing Open Interest |
| toi | | Total open interest for underlying |
| bq1 | | Best Buy Quantity 1 |
| bp1 | | Best Buy Price 1 |
| sq1 | | Best Sell Quantity 1 |
| sp1 | | Best Sell Price 1|
#credentials
user = '< user id>'
u_pwd = '< password >'
factor2 = 'second factor'
vc = 'vendor code'
app_key = 'secret key'
imei = 'uniq identifier'
Search Scrips
The call can be made to get the exchange provided token for a scrip or alternately can search for a partial
string to get a list of matching scrips
Trading Symbol:
SymbolName + ExpDate + 'F' for all data having InstrumentName starting with FUT
SymbolName + ExpDate + 'P' + StrikePrice for all data having InstrumentName starting with OPT and
with OptionType PE
SymbolName + ExpDate + 'C' + StrikePrice for all data having InstrumentName starting with OPT and
with OptionType C
For MCX, F to be ignored for FUT instruments
api.searchscrip(exchange='NSE', searchtext='REL')
Security Info
This call is done to get the properties of the scrip such as freeze qty and margins
api.get_security_info(exchange='NSE', token='22')
def event_handler_feed_update(tick_data):
print(f"feed update {tick_data}")
def open_callback():
global feed_opened
feed_opened = True
api.start_websocket( order_update_callback=event_handler_order_update,
subscribe_callback=event_handler_feed_update,
socket_open_callback=open_callback)
while(feed_opened==False):
pass
api.place_order(buy_or_sell='B', product_type='C',
exchange='NSE', tradingsymbol='INFY-EQ',
quantity=1, discloseqty=0,price_type='LMT', price=1500,
trigger_price=None,
retention='DAY', remarks='my_order_001')
api.place_order(buy_or_sell='B', product_type='C',
exchange='NSE', tradingsymbol='INFY-EQ',
quantity=1, discloseqty=0,price_type='MKT', price=0,
trigger_price=None,
retention='DAY', remarks='my_order_001')
api.place_order(buy_or_sell='B', product_type='C',
exchange='NSE', tradingsymbol='INFY-EQ',
quantity=1, discloseqty=0,price_type='SL-LMT', price=1500,
trigger_price=1450,
retention='DAY', remarks='my_order_001')
api.place_order(buy_or_sell='B', product_type='H',
exchange='NSE', tradingsymbol='INFY-EQ',
quantity=1, discloseqty=0,price_type='LMT', price=1500,
trigger_price=None,
retention='DAY', remarks='my_order_001', bookloss_price =
1490)
api.place_order(buy_or_sell='B', product_type='B',
exchange='NSE', tradingsymbol='INFY-EQ',
quantity=1, discloseqty=0,price_type='LMT', price=1500,
trigger_price=None,
retention='DAY', remarks='my_order_001', bookloss_price =
1490, bookprofit_price = 1510)
Modify Order
Modify a New Order by providing the OrderNumber
Cancel Order
Cancel a New Order by providing the Order Number
api.cancel_order(orderno=orderno)
def event_handler_order_update(order):
print(f"order feed {order}")
def open_callback():
global feed_opened
feed_opened = True
api.start_websocket( order_update_callback=event_handler_order_update,
subscribe_callback=event_handler_feed_update,
socket_open_callback=open_callback)
while(feed_opened==False):
pass
Author
Kumar Anand
License
Copyright (C) 2021 Kambala Solutions Pvt Ltd- All Rights Reserved
Copying of this file, via any medium is strictly prohibited.
Proprietary and confidential.
All file transfers are logged.