Can we get margins for multiple instrument tokens in one call, like we can get ltp, currently I know only getting one symbol at a time through kite.order_margins(order_param_single_sell)
to clarify, need help on this with python code, saw the documentation for basket orders, but couldnt see python code for it..maybe am not well informed..but do need help
But you can just use the order_margins. Just pass in a list of orders. Basket orders api is for enjoying margin benefit and it will give bunch of useless information if you want to trade in different stocks or derivatives. And it seems you want margin for different orders of different stocks or derivatives. So just use order margins api.
Yes, you can use order_margin with list of orders. Refer an example here.
I ran this example for basket order margin. Changed the symbols to NIFTY21NOV18000CE and NIFTY21NOV18000PE, but forgot to change the quantity from 75 and 150 to 50 and 100 (multiple of NIFTY lot size). I still managed to get a margin. Of course it is not meaningful. How is it possible?
Order margins is calculated per quantity. Margin is calculating the risk which only depends on the base quantity(i.e 1 here), not on the allowed trading lot(which can change or be any number). So, you can fetch margin for any quantity multiplier of its base quantity i.e 1 qty.
Getting following error with multi margin (trying for all Stock Futures symbols as test run):
DataException: Unknown Content-Type (text/plain; charset=utf-8) with response: (b'Error when parsing request')
Following is the code: order_param_multi = [] for symbol in futures_symbols: #this is list of stock futures tradingsymbols taken from dump lot_size = df.loc[df.tradingsymbol == symbol, "lot_size"].iloc[0]
File "C:\Users\Zerodha\AppData\Local\Temp/ipykernel_14104/3982996301.py", line 1, in margin_detail = kite.order_margins(order_param_multi)
File "C:\ProgramData\Anaconda3\lib\site-packages\kiteconnect\connect.py", line 764, in order_margins return self._post("order.margins", params=params, is_json=True)
File "C:\ProgramData\Anaconda3\lib\site-packages\kiteconnect\connect.py", line 840, in _post return self._request(route, "POST", url_args=url_args, params=params, is_json=is_json, query_params=query_params)
File "C:\ProgramData\Anaconda3\lib\site-packages\kiteconnect\connect.py", line 918, in _request raise ex.DataException("Unknown Content-Type ({content_type}) with response: ({content})".format(
DataException: Unknown Content-Type (text/plain; charset=utf-8) with response: (b'Error when parsing request')
order_margins method by default considers existing positions and open orders. You can refer to method detail here. So, you don't have to send consider_positions param in order. You can refer here for all order param.
following are 5 tradingsymbols, found in fno dump, returning error on calling individual margins, tried after removing these also, still persists the same error...
This is a spread contracts i.e spread over two months 30DEC27JAN, you can ignore these. We will remove such spread contracts from the instrument file. For the rest other contracts, I just tried and it worked perfectly fine.
However, I could figure out that if I send less than 240 tradingsymbols, it works fine, >= 240 symbols at a time returns error. Have worked out my solution accordingly.
DataException: Unknown Content-Type (text/plain; charset=utf-8) with response: (b'Error when parsing request')
Following is the code:
order_param_multi = []
for symbol in futures_symbols: #this is list of stock futures tradingsymbols taken from dump
lot_size = df.loc[df.tradingsymbol == symbol, "lot_size"].iloc[0]
order_param_multi.append(
{"exchange": "NFO",
"tradingsymbol": symbol,
"transaction_type": "SELL",
"variety": "regular",
"product": "MIS",
"order_type": "MARKET",
"quantity": int(lot_size),
"consider_positions" : False
})
margin_detail = kite.order_margins(order_param_multi)
margin_detail = kite.order_margins(order_param_multi)
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443
DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "POST /margins/orders HTTP/1.1" 400 26
Traceback (most recent call last):
File "C:\Users\Zerodha\AppData\Local\Temp/ipykernel_14104/3982996301.py", line 1, in
margin_detail = kite.order_margins(order_param_multi)
File "C:\ProgramData\Anaconda3\lib\site-packages\kiteconnect\connect.py", line 764, in order_margins
return self._post("order.margins", params=params, is_json=True)
File "C:\ProgramData\Anaconda3\lib\site-packages\kiteconnect\connect.py", line 840, in _post
return self._request(route, "POST", url_args=url_args, params=params, is_json=is_json, query_params=query_params)
File "C:\ProgramData\Anaconda3\lib\site-packages\kiteconnect\connect.py", line 918, in _request
raise ex.DataException("Unknown Content-Type ({content_type}) with response: ({content})".format(
DataException: Unknown Content-Type (text/plain; charset=utf-8) with response: (b'Error when parsing request')
order_margins
method by default considers existing positions and open orders. You can refer to method detail here. So, you don't have to sendconsider_positions
param in order. You can refer here for all order param.['AMBUJACEM30DEC2127JAN22FUT',
'FINNIFTY21D07FUT',
'FINNIFTY21DECFUT',
'FINNIFTY22JANFUT',
'PAGEIND22JANFUT']
For the rest other contracts, I just tried and it worked perfectly fine.
[{'type': 'equity', 'tradingsymbol': 'PAGEIND22JANFUT', 'exchange': 'NFO', 'span':
37940.700500000006, 'exposure': 0, 'option_premium': 0, 'additional': 0, 'bo': 0, 'cash': 0, 'var': 0, 'pnl': {'realised':
0, 'unrealised': 0}, 'leverage': 1, 'total': 37940.700500000006}, {'type': 'equity', 'tradingsymbol':
'FINNIFTY21DECFUT', 'exchange': 'NFO', 'span': 13149.089, 'exposure': 0, 'option_premium': 0, 'additional': 0, 'bo':
0, 'cash': 0, 'var': 0, 'pnl': {'realised': 0, 'unrealised': 0}, 'leverage': 1, 'total': 13149.089}, {'type': 'equity',
'tradingsymbol': 'FINNIFTY21D07FUT', 'exchange': 'NFO', 'span': 13119.900000000001, 'exposure': 0,
'option_premium': 0, 'additional': 0, 'bo': 0, 'cash': 0, 'var': 0, 'pnl': {'realised': 0, 'unrealised': 0}, 'leverage': 1,
'total': 13119.900000000001}]
Can you check, if you are using an updated version of python kite connect client lib? Using
pip show kiteconnect
Name: kiteconnect
Version: 3.9.4
However, I could figure out that if I send less than 240 tradingsymbols, it works fine, >= 240 symbols at a time returns error. Have worked out my solution accordingly.
thank you.