Shoonya Python API
Shoonya Python API
Shoonya Python API
Product Of
Contents
ShoonyaApi-Py .............................................................................................................................................. 1
Introduction .............................................................................................................................................. 3
Build .......................................................................................................................................................... 3
Login ...................................................................................................................................................... 3
Orders and Trades................................................................................................................................. 3
Holdings and Limits ............................................................................................................................... 5
Symbols and Contract Information ..................................................................................................... 11
WebSocket API .................................................................................................................................... 17
Example - Getting Started ....................................................................................................................... 17
Example Symbol/Contract : Example_market.py ................................................................................... 18
Search Scrips ....................................................................................................................................... 18
Security Info ........................................................................................................................................ 19
Subscribe to a live feed ....................................................................................................................... 20
Example - Orders and Trades : example_orders.py ................................................................................ 21
Place Order.......................................................................................................................................... 21
Modify Order....................................................................................................................................... 21
Cancel Order ....................................................................................................................................... 22
Subscribe to Order Updates ................................................................................................................ 22
Introduction
Api used to connect to Shoonya OMS
Build
to build this package and install it on your server please use
pip install -r requirements.txt
Login
login(userid, password, twoFA, vendor_code, api_secret, imei)
connect to the broker, only once this function has returned successfully can any other operations be
performed
Param Type Optional Description
userid string False user credentials
password string False password encrypted
twoFA string False OTP/TOTP
vendor_code string False vendor code shared
api_secret string False your secret
imei string False imei identification
cancel_order(orderno)
cancel an order
Param Type Optional Description
orderno string False orderno with status open
exit_order(orderno)
exits a cover or bracket order
Param Type Optional Description
orderno string False orderno with status open
prd string False Allowed for only H and B products (Cover order and bracket order)
get_holdings(product_type)
retrieves the holdings as a list
Param Type Optional Description
product_type string True retreives the delivery holdings or for a given product
get_positions()
retrieves the positions cf and day as a list
Param Type Optional Description
No Parameters
get_limits
retrieves the margin and limits set
Param Type Optional Description
product_type string True retreives the delivery holdings or for a given product
segment string True CM / FO / FX
exchange string True Exchange NSE/BSE/MCX
the response is as follows,
Param Type Optional Description
stat Ok or False Limits request success or failure
Not_Ok indication.
actid string True Account id
prd string True Product name
seg string True Segment CM / FO / FX
exch string True Exchange
-------------------------Cash Primary Fields---------
----------------------
cash string True Cash Margin available
payin string True Total Amount transferred using
Payins today
payout string True Total amount requested for
withdrawal today
-------------------------Cash Additional Fields------
-------------------------
brkcollamt string True Prevalued Collateral Amount
unclearedcash string True Uncleared Cash (Payin through
cheques)
daycash string True Additional leverage amount /
Amount added to handle system
errors - by broker.
-------------------------Margin Utilized---------------
-------------------
marginused string True Total margin / fund used today
mtomcurper string True Mtom current percentage
-------------------------Margin Used components-
--------------------
cbu string True CAC Buy used
csc string True CAC Sell Credits
rpnl string True Current realized PNL
unmtom string True Current unrealized mtom
marprt string True Covered Product margins
span string True Span used
expo string True Exposure margin
premium string True Premium used
varelm string True Var Elm Margin
grexpo string True Gross Exposure
greexpo_d string True Gross Exposure derivative
scripbskmar string True Scrip basket margin
addscripbskmrg string True Additional scrip basket margin
brokerage string True Brokerage amount
collateral string True Collateral calculated based on
uploaded holdings
grcoll string True Valuation of uploaded holding
pre haircut
-------------------------Additional Risk Limits-------
--------------------
turnoverlmt string True
pendordvallmt string True
-------------------------Additional Risk Indicators-
--------------------------
turnover string True Turnover
pendordval string True Pending Order value
-------------------------Margin used detailed
breakup fields-------------------------
rzpnl_e_i string True Current realized PNL (Equity
Intraday)
rzpnl_e_m string True Current realized PNL (Equity
Margin)
rzpnl_e_c string True Current realized PNL (Equity Cash
n Carry)
rzpnl_d_i string True Current realized PNL (Derivative
Intraday)
rzpnl_d_m string True Current realized PNL (Derivative
Margin)
rzpnl_f_i string True Current realized PNL (FX
Intraday)
rzpnl_f_m string True Current realized PNL (FX Margin)
rzpnl_c_i string True Current realized PNL (Commodity
Intraday)
rzpnl_c_m string True Current realized PNL (Commodity
Margin)
uzpnl_e_i string True Current unrealized MTOM
(Equity Intraday)
uzpnl_e_m string True Current unrealized MTOM
(Equity Margin)
uzpnl_e_c string True Current unrealized MTOM
(Equity Cash n Carry)
uzpnl_d_i string True Current unrealized MTOM
(Derivative Intraday)
uzpnl_d_m string True Current unrealized MTOM
(Derivative Margin)
uzpnl_f_i string True Current unrealized MTOM (FX
Intraday)
uzpnl_f_m string True Current unrealized MTOM (FX
Margin)
uzpnl_c_i string True Current unrealized MTOM
(Commodity Intraday)
uzpnl_c_m string True Current unrealized MTOM
(Commodity Margin)
span_d_i string True Span Margin (Derivative
Intraday)
span_d_m string True Span Margin (Derivative Margin)
span_f_i string True Span Margin (FX Intraday)
span_f_m string True Span Margin (FX Margin)
span_c_i string True Span Margin (Commodity
Intraday)
span_c_m string True Span Margin (Commodity
Margin)
expo_d_i string True Exposure Margin (Derivative
Intraday)
expo_d_m string True Exposure Margin (Derivative
Margin)
expo_f_i string True Exposure Margin (FX Intraday)
expo_f_m string True Exposure Margin (FX Margin)
expo_c_i string True Exposure Margin (Commodity
Intraday)
expo_c_m string True Exposure Margin (Commodity
Margin)
premium_d_i string True Option premium (Derivative
Intraday)
premium_d_m string True Option premium (Derivative
Margin)
premium_f_i string True Option premium (FX Intraday)
premium_f_m string True Option premium (FX Margin)
premium_c_i string True Option premium (Commodity
Intraday)
premium_c_m string True Option premium (Commodity
Margin)
varelm_e_i string True Var Elm (Equity Intraday)
varelm_e_m string True Var Elm (Equity Margin)
varelm_e_c string True Var Elm (Equity Cash n Carry)
marprt_e_h string True Covered Product margins (Equity
High leverage)
marprt_e_b string True Covered Product margins (Equity
Bracket Order)
marprt_d_h string True Covered Product margins
(Derivative High leverage)
marprt_d_b string True Covered Product margins
(Derivative Bracket Order)
marprt_f_h string True Covered Product margins (FX
High leverage)
marprt_f_b string True Covered Product margins (FX
Bracket Order)
marprt_c_h string True Covered Product margins
(Commodity High leverage)
marprt_c_b string True Covered Product margins
(Commodity Bracket Order)
scripbskmar_e_i string True Scrip basket margin (Equity
Intraday)
scripbskmar_e_m string True Scrip basket margin (Equity
Margin)
scripbskmar_e_c string True Scrip basket margin (Equity Cash
n Carry)
addscripbskmrg_d_i string True Additional scrip basket margin
(Derivative Intraday)
addscripbskmrg_d_m string True Additional scrip basket margin
(Derivative Margin)
addscripbskmrg_f_i string True Additional scrip basket margin
(FX Intraday)
addscripbskmrg_f_m string True Additional scrip basket margin
(FX Margin)
addscripbskmrg_c_i string True Additional scrip basket margin
(Commodity Intraday)
addscripbskmrg_c_m string True Additional scrip basket margin
(Commodity Margin)
brkage_e_i string True Brokerage (Equity Intraday)
brkage_e_m string True Brokerage (Equity Margin)
brkage_e_c string True Brokerage (Equity CAC)
brkage_e_h string True Brokerage (Equity High Leverage)
brkage_e_b string True Brokerage (Equity Bracket Order)
brkage_d_i string True Brokerage (Derivative Intraday)
brkage_d_m string True Brokerage (Derivative Margin)
brkage_d_h string True Brokerage (Derivative High
Leverage)
brkage_d_b string True Brokerage (Derivative Bracket
Order)
brkage_f_i string True Brokerage (FX Intraday)
brkage_f_m string True Brokerage (FX Margin)
brkage_f_h string True Brokerage (FX High Leverage)
brkage_f_b string True Brokerage (FX Bracket Order)
brkage_c_i string True Brokerage (Commodity Intraday)
brkage_c_m string True Brokerage (Commodity Margin)
brkage_c_h string True Brokerage (Commodity High
Leverage)
brkage_c_b string True Brokerage (Commodity Bracket
Order)
peak_mar string True Peak margin used by the client
request_time string True This will be present only in a
successful response.
emsg string True This will be present only in a
failure response.
searchscrip(exchange, searchtext):
search for scrip or contract and its properties
Param Type Optional Description
exchange string True Exchange NSE / NFO / BSE / CDS
searchtext string True Search Text ie partial or complete text ex: INFY-EQ, INF..
get_security_info(exchange, token):
gets the complete details and its properties
Param Type Optional Description
exchange string True Exchange NSE / NFO / BSE / CDS
token string True token number of the contract
WebSocket API
start_websocket()
starts the websocket
Param Type Optional Description
subscribe_callback function False callback for market updates
order_update_callback function False callback for order updates
socket_open_callback function False callback when socket is open (reconnection also)
socket_close_callback function False callback when socket is closed
subscribe_orders()
get order and trade update callbacks
subscribe([instruments])
send a list of instruments to watch
Param Type Optional Description
instruments list False list of instruments [NSE\
unsubscribe()
send a list of instruments to stop watch
#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
Modify Order
Modify a New Order by providing the OrderNumber
api.modify_order(exchange='NSE', tradingsymbol='INFY-EQ', orderno=orderno,
newquantity=2, newprice_type='LMT', newprice=1505)
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