0% found this document useful (0 votes)
19 views

ShoonyaAPI-js

Uploaded by

saratbotfire
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

ShoonyaAPI-js

Uploaded by

saratbotfire
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

ShoonyaApi

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

API
ShoonyaApi

 login

Symbols

 searchscrip
 get_security_info
 get_quotes
 get_time_price_series

Orders and Trades

 place_order
 modify_order
 cancel_order
 exit_order
 get_orderbook
 get_singleorderhistory

Holdings and Limits

 get_holdings
 get_positions
 get_limits

Websocket API

 start_websocket
 subscribe
 unsubscribe

Example

 getting started
 Market Functions
 Orders and Trade

<a name=“md-login”></a> 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 dob/pan
vendor_code string False vendor code shared
api_secret string False your secret
imei string False imei identification

<a name=“md-place_order”></a> place_order(buy_or_sell, product_type,exchange,


tradingsymbol, quantity, discloseqty, price_type, price=0.0, trigger_price=None,
retention=‘DAY’, amo=‘NO’, remarks=None)

place an order to oms

Param Type Optional Description


buy_or_sell string False B -> BUY, S -> SELL
C / M / H Product name (Select from ‘prarr’ Array provided
in User Details response, and if same is allowed for selected,
product_type string False
exchange. Show product display name, for user to select, and
send corresponding prd in API call)
exchange string False Exchange NSE / NFO / BSE / CDS
Unique id of contract on which order to be placed. (use url
tradingsymbol string False
encoding to avoid special char error for symbols like M&M
quantity integer False order quantity
discloseqty integer False order disc qty
price_type string False PriceType enum class
price integer False Price in paise, 100.00 is sent as 10000
trigger_price integer False Price in paise
retention string False DAY / IOC / EOS
amo string True Flag for After Market Order, YES/NO
Param Type Optional Description
remarks string True client order id or free text

<a name=“md-modify_order”></a> modify_order(orderno, exchange, tradingsymbol,


newquantity,newprice_type, newprice, newtrigger_price, amo):

modify the quantity pricetype or price of an order

Param Type Optional Description


orderno False
string orderno to be modified
exchange string False Exchange NSE / NFO / BSE / CDS
Unique id of contract on which order to be placed. (use url
tradingsymbol string False
encoding to avoid special char error for symbols like M&M
newquantity integer False new order quantity
newprice_type string False PriceType enum class
newprice integer False Price in paise, 100.00 is sent as 10000
newtrigger_price integer False Price in paise

<a name=“md-cancel_order”></a> cancel_order(orderno)

cancel an order

Param Type Optional Description


orderno string False orderno with status open

<a name=“md-exit_order”></a> 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)

<a name=“md-get_singleorderhistory”></a> single order history(orderno)

history an order

Param Type Optional Description


orderno string False orderno

<a name=“md-get_holdings”></a> 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

<a name=“md-get_positions”></a> get_positions()

retrieves the positions cf and day as a list

Param Type Optional Description


No Parameters

<a name=“md-get_limits”></a> 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


Ok or Limits request success or failure
stat False
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
Total Amount transferred using
payin string True
Payins today
Total amount requested for
payout string True
withdrawal today
-------------------------Cash Additional
Fields-------------------------------
brkcollamt string True Prevalued Collateral Amount
Uncleared Cash (Payin through
unclearedcash string True
cheques)
Additional leverage amount /
daycash string True Amount added to handle system
errors - by broker.
-------------------------Margin
Param Type Optional Description
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 calculated based on
collateral string True
uploaded holdings
Valuation of uploaded holding pre
grcoll string True
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-------------------------
Current realized PNL (Equity
rzpnl_e_i string True
Intraday)
Current realized PNL (Equity
rzpnl_e_m string True
Margin)
Current realized PNL (Equity Cash
rzpnl_e_c string True
n Carry)
Current realized PNL (Derivative
rzpnl_d_i string True
Intraday)
Param Type Optional Description
Current realized PNL (Derivative
rzpnl_d_m string True
Margin)
Current realized PNL (FX
rzpnl_f_i string True
Intraday)
rzpnl_f_m string True Current realized PNL (FX Margin)
Current realized PNL (Commodity
rzpnl_c_i string True
Intraday)
Current realized PNL (Commodity
rzpnl_c_m string True
Margin)
Current unrealized MTOM (Equity
uzpnl_e_i string True
Intraday)
Current unrealized MTOM (Equity
uzpnl_e_m string True
Margin)
Current unrealized MTOM (Equity
uzpnl_e_c string True
Cash n Carry)
Current unrealized MTOM
uzpnl_d_i string True
(Derivative Intraday)
Current unrealized MTOM
uzpnl_d_m string True
(Derivative Margin)
Current unrealized MTOM (FX
uzpnl_f_i string True
Intraday)
Current unrealized MTOM (FX
uzpnl_f_m string True
Margin)
Current unrealized MTOM
uzpnl_c_i string True
(Commodity Intraday)
Current unrealized MTOM
uzpnl_c_m string True
(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 Margin (Commodity
span_c_i string True
Intraday)
span_c_m string True Span Margin (Commodity Margin)
Exposure Margin (Derivative
expo_d_i string True
Intraday)
Exposure Margin (Derivative
expo_d_m string True
Margin)
expo_f_i string True Exposure Margin (FX Intraday)
expo_f_m string True Exposure Margin (FX Margin)
Exposure Margin (Commodity
expo_c_i string True
Intraday)
expo_c_m string True Exposure Margin (Commodity
Param Type Optional Description
Margin)
Option premium (Derivative
premium_d_i string True
Intraday)
Option premium (Derivative
premium_d_m string True
Margin)
premium_f_i string True Option premium (FX Intraday)
premium_f_m string True Option premium (FX Margin)
Option premium (Commodity
premium_c_i string True
Intraday)
Option premium (Commodity
premium_c_m string True
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)
Covered Product margins (Equity
marprt_e_h string True
High leverage)
Covered Product margins (Equity
marprt_e_b string True
Bracket Order)
Covered Product margins
marprt_d_h string True
(Derivative High leverage)
Covered Product margins
marprt_d_b string True
(Derivative Bracket Order)
Covered Product margins (FX
marprt_f_h string True
High leverage)
Covered Product margins (FX
marprt_f_b string True
Bracket Order)
Covered Product margins
marprt_c_h string True
(Commodity High leverage)
Covered Product margins
marprt_c_b string True
(Commodity Bracket Order)
Scrip basket margin (Equity
scripbskmar_e_i string True
Intraday)
Scrip basket margin (Equity
scripbskmar_e_m string True
Margin)
Scrip basket margin (Equity Cash
scripbskmar_e_c string True
n Carry)
Additional scrip basket margin
addscripbskmrg_d_i string True
(Derivative Intraday)
Additional scrip basket margin
addscripbskmrg_d_m string True
(Derivative Margin)
Additional scrip basket margin (FX
addscripbskmrg_f_i string True
Intraday)
addscripbskmrg_f_m string True Additional scrip basket margin (FX
Param Type Optional Description
Margin)
Additional scrip basket margin
addscripbskmrg_c_i string True
(Commodity Intraday)
Additional scrip basket margin
addscripbskmrg_c_m string True
(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)
Brokerage (Derivative High
brkage_d_h string True
Leverage)
Brokerage (Derivative Bracket
brkage_d_b string True
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)
Brokerage (Commodity High
brkage_c_h string True
Leverage)
Brokerage (Commodity Bracket
brkage_c_b string True
Order)
peak_mar string True Peak margin used by the client
This will be present only in a
request_time string True
successful response.
This will be present only in a
emsg string True
failure response.

<a name=“md-searchscrip”></a> 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…

the response is as follows,


Param Type Optional Description
stat string True ok or Not_ok
values string True properties of the scrip
emsg string False Error Message
Param Type Optional Description
exch string True Exchange NSE / NFO / BSE / CDS
tsym string True Trading Symbol is the readable Unique id of contract/scrip
token string True Unique Code of contract/scrip
pp string True price precision, in case of cds its 4 ie 100.1234
ti string True tick size minimum increments of paise for price
ls string True Lot Size

<a name=“md-get_security_info”></a> 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

the response is as follows,

Param Type Optional Description


stat string True ok or Not_ok
values string True properties of the scrip
emsg string False Error Message
Param Type Optional Description
exch string True Exchange NSE / NFO / BSE / CDS
tsym string True Trading Symbol is the readable Unique id of contract/scrip
cname string True Company Name
symnam string True Symbol Name
seg string True Segment
exd string True Expiry Date
instname string True Instrument Name
strprc string True Strike Price
optt string True Option Type
isin string True ISIN
ti string True Tick Size
ls string True Lot Size
pp string True Price Precision
mult string True Multiplier
gp_nd string True GN/GD * PN/PD
prcunt string True Price Units
Param Type Optional Description
prcqqty string True Price Quote Qty
trdunt string True Trade Units
delunt string True Delivery Units
frzqty string True Freeze Qty
gsmind string True GSM indicator
elmbmrg string True ELM Buy Margin
elmsmrg string True ELM Sell Margin
addbmrg string True Additional Long Margin
addsmrg string True Additional Short Margin
splbmrg string True Special Long Margin
splsmrg string True Special Short Margin
delmrg string True Delivery Margin
tenmrg string True Tender Margin
tenstrd string True Tender Start Date
tenendd string True Tender End Date
exestrd string True Exercise Start Date
exeendd string True Exercise End Date
elmmrg string True ELM Margin
varmrg string True VAR Margin
expmrg string True Exposure Margin
token string True Contract Token
prcftr_d string True ((GN / GD) * (PN/PD))

<a name=“md-get_quotes”></a> get_quotes(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

the response is as follows,

Param Type Optional Description


stat string True ok or Not_ok
values string True properties of the scrip
emsg string False Error Message
Param Type Optional Description
exch string True Exchange NSE / NFO / BSE / CDS
tsym string True Trading Symbol is the readable Unique id of contract/scrip
cname string True Company Name
symname string True Symbol Name
Param Type Optional Description
seg string True Segment
instname string True Instrument Name
isin string True ISIN
pp string True Price precision
ls string True Lot Size
ti string True Tick Size
mult string True Multiplier
uc string True Upper circuit limitlc
lc string True Lower circuit limit
prcftr_d string True Price factor((GN / GD) * (PN/PD))
token string True Token
lp string True LTP
o string True Open Price
h string True Day High Price
l string True Day Low Price
v string True Volume
ltq string True Last trade quantity
ltt string True Last trade time
bp1 string True Best Buy Price 1
sp1 string True Best Sell Price 1
bp2 string True Best Buy Price 2
sp2 string True Best Sell Price 2
bp3 string True Best Buy Price 3
sp3 string True Best Sell Price 3
bp4 string True Best Buy Price 4
sp4 string True Best Sell Price 4
bp5 string True Best Buy Price 5
sp5 string True Best Sell Price 5
bq1 string True Best Buy Quantity 1
sq1 string True Best Sell Quantity 1
bq2 string True Best Buy Quantity 2
sq2 string True Best Sell Quantity 2
bq3 string True Best Buy Quantity 3
sq3 string True Best Sell Quantity 3
bq4 string True Best Buy Quantity 4
sq4 string True Best Sell Quantity 4
bq5 string True Best Buy Quantity 5
sq5 string True Best Sell Quantity 5
bo1 string True Best Buy Orders 1
so1 string True Best Sell Orders 1
bo2 string True Best Buy Orders 2
Param Type Optional Description
so2 string True Best Sell Orders 2
bo3 string True Best Buy Orders 3
so3 string True Best Sell Orders 3
bo4 string True Best Buy Orders 4
so4 string True Best Sell Orders 4
bo5 string True Best Buy Orders 5
so5 string True Best Sell Orders 5

<a name=“md-get_time_price_series”></a> get_time_price_series(exchange, token,


starttime, endtime):

gets the chart date for the symbol

Param Type Optional Description


exchange string True Exchange NSE / NFO / BSE / CDS
token string True token number of the contract
starttime string True Start time (seconds since 1 jan 1970)
endtime string True End Time (seconds since 1 jan 1970)

the response is as follows,

Param Type Optional Description


stat string True ok or Not_ok
values string True properties of the scrip
emsg string False Error Message
Param Type Optional Description
time string True DD/MM/CCYY hh:mm:ss
into string True Interval Open
inth string True Interval High
intl string True Interval Low
intc string True Interval Close
intvwap string True Interval vwap
intv string True Interval volume
v string True volume
inoi string True Interval oi change
oi string True oi

<a name=“md-start_websocket”></a> 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

<a name=“md-subscribe_orders”></a> subscribe_orders()

get order and trade update callbacks

<a name=“md-subscribe”></a> subscribe([instruments])

send a list of instruments to watch

Param Type Optional Description


instruments list False list of instruments [NSE|22,CDS|1]

<a name=“md-unsubscribe”></a> unsubscribe()

send a list of instruments to stop watch

<a name=“md-example-basic”></a> Example - Getting


Started
First configure the endpoints in the api_helper constructor.
Thereon provide your credentials and login as follows.

from api_helper import ShoonyaApiPy


import logging

#enable dbug to see request and responses


logging.basicConfig(level=logging.DEBUG)

#start of our program


api = ShoonyaApiPy()

#credentials
user = '< user id>'
u_pwd = '< password >'
factor2 = 'second factor'
vc = 'vendor code'
app_key = 'secret key'
imei = 'uniq identifier'
ret = api.login(userid=user, password=pwd, twoFA=factor2, vendor_code=vc,
api_secret=app_key, imei=imei)
print(ret)

<a name=“md-example-market”></a> Example


Symbol/Contract : Example_market.py
This Example shows API usage for finding scrips and its properties

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')

This will reply as following

{
"stat": "Ok",
"values": [
{
"exch": "NSE",
"token": "18069",
"tsym": "REL100NAV-EQ"
},
{
"exch": "NSE",
"token": "24225",
"tsym": "RELAXO-EQ"
},
{
"exch": "NSE",
"token": "4327",
"tsym": "RELAXOFOOT-EQ"
},
{
"exch": "NSE",
"token": "18068",
"tsym": "RELBANKNAV-EQ"
},
{
"exch": "NSE",
"token": "2882",
"tsym": "RELCAPITAL-EQ"
},
{
"exch": "NSE",
"token": "18070",
"tsym": "RELCONSNAV-EQ"
},
{
"exch": "NSE",
"token": "18071",
"tsym": "RELDIVNAV-EQ"
},
{
"exch": "NSE",
"token": "18072",
"tsym": "RELGOLDNAV-EQ"
},
{
"exch": "NSE",
"token": "2885",
"tsym": "RELIANCE-EQ"
},
{
"exch": "NSE",
"token": "15068",
"tsym": "RELIGARE-EQ"
},
{
"exch": "NSE",
"token": "553",
"tsym": "RELINFRA-EQ"
},
{
"exch": "NSE",
"token": "18074",
"tsym": "RELNV20NAV-EQ"
}
]
}

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')

The response for the same would be

{
"request_time": "17:43:38 31-10-2020",
"stat": "Ok",
"exch": "NSE",
"tsym": "ACC-EQ",
"cname": "ACC LIMITED",
"symname": "ACC",
"seg": "EQT",
"instname": "EQ",
"isin": "INE012A01025",
"pp": "2",
"ls": "1",
"ti": "0.05",
"mult": "1",
"prcftr_d": "(1 / 1 ) * (1 / 1)",
"trdunt": "ACC.BO",
"delunt": "ACC",
"token": "22",
"varmrg": "40.00"
}

Subscribe to a live feed

Subscribe to a single token as follows

api.subscribe('NSE|13')

Subscribe to a list of tokens as follows

api.subscribe(['NSE|22', 'BSE|522032'])

First we need to connect to the WebSocket and then subscribe as follows

feed_opened = False

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

# subscribe to a single token


api.subscribe('NSE|13')

#subscribe to multiple tokens


api.subscribe(['NSE|22', 'BSE|522032'])
<a name=“md-example-orders”></a> Example - Orders and
Trades : example_orders.py
Place Order
Place a Limit order as follows
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')
Place a Market Order as follows
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')
Place a StopLoss Order as follows
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')
Place a Cover Order as follows
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)
Place a Bracket Order as follows
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, bookprofit_price = 1510)

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)

Subscribe to Order Updates


Connecting to the Websocket will automatically subscribe and provide the order updates in the
call back as follows
Note: Feed and Order updates are received from the same websocket and needs to be connected
once only.

feed_opened = False

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 © 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.

You might also like