zerodha / pykiteconnect

The official Python client library for the Kite Connect trading APIs
MIT License
996 stars 473 forks source link

basket_order_margins giving wrong initial margin #163

Closed dolaram closed 1 year ago

dolaram commented 1 year ago

For the basket order, the initial margin given by the kite basket_order_margins API is 151450.58 and for the same basket, 64K is the margin reported in the web application.
Parameters passed to the basket_order_margins: [{'exchange': 'NFO', 'tradingsymbol': 'BANKNIFTY22O2039600CE', 'transaction_type': 'BUY', 'variety': 'regular', 'product': 'MIS', 'order_type': 'MARKET', 'quantity': 25, 'price': 0, 'trigger_price': 0}, {'exchange': 'NFO', 'tradingsymbol': 'BANKNIFTY22O2039400PE', 'transaction_type': 'BUY', 'variety': 'regular', 'product': 'MIS', 'order_type': 'MARKET', 'quantity': 25, 'price': 0, 'trigger_price': 0}, {'exchange': 'NFO', 'tradingsymbol': 'BANKNIFTY22O2039500CE', 'transaction_type': 'SELL', 'variety': 'regular', 'product': 'MIS', 'order_type': 'MARKET', 'quantity': 25, 'price': 0, 'trigger_price': 0}, {'exchange': 'NFO', 'tradingsymbol': 'BANKNIFTY22O2039500PE', 'transaction_type': 'SELL', 'variety': 'regular', 'product': 'MIS', 'order_type': 'MARKET', 'quantity': 25, 'price': 0, 'trigger_price': 0}]

Response from the API call: {'initial': {'type': '', 'tradingsymbol': '', 'exchange': '', 'span': 90852.84000000001, 'exposure': 41515.240000000005, 'option_premium': 19082.5, 'additional': 0, 'bo': 0, 'cash': 0, 'var': 0, 'pnl': {'realised': 0, 'unrealised': 0}, 'leverage': 0, 'total': 151450.58000000002}, 'final': {'type': '', 'tradingsymbol': '', 'exchange': '', 'span': 2630.420000000013, 'exposure': 41515.240000000005, 'option_premium': 9987.5, 'additional': 0, 'bo': 0, 'cash': 0, 'var': 0, 'pnl': {'realised': 0, 'unrealised': 0}, 'leverage': 0, 'total': 54133.16000000002}, 'orders': [{'type': 'equity', 'tradingsymbol': 'BANKNIFTY22O2039600CE', 'exchange': 'NFO', 'span': 0, 'exposure': 0, 'option_premium': 9522.5, 'additional': 0, 'bo': 0, 'cash': 0, 'var': 0, 'pnl': {'realised': 0, 'unrealised': 0}, 'leverage': 1, 'total': 9522.5}, {'type': 'equity', 'tradingsymbol': 'BANKNIFTY22O2039400PE', 'exchange': 'NFO', 'span': 0, 'exposure': 0, 'option_premium': 9560, 'additional': 0, 'bo': 0, 'cash': 0, 'var': 0, 'pnl': {'realised': 0, 'unrealised': 0}, 'leverage': 1, 'total': 9560}, {'type': 'equity', 'tradingsymbol': 'BANKNIFTY22O2039500CE', 'exchange': 'NFO', 'span': 90852.84000000001, 'exposure': 20759.258, 'option_premium': 0, 'additional': 0, 'bo': 0, 'cash': 0, 'var': 0, 'pnl': {'realised': 0, 'unrealised': 0}, 'leverage': 1, 'total': 111612.09800000001}, {'type': 'equity', 'tradingsymbol': 'BANKNIFTY22O2039500PE', 'exchange': 'NFO', 'span': 0, 'exposure': 20755.982000000004, 'option_premium': 0, 'additional': 0, 'bo': 0, 'cash': 0, 'var': 0, 'pnl': {'realised': 0, 'unrealised': 0}, 'leverage': 1, 'total': 20755.982000000004}]}

ranjanrak commented 1 year ago

I just placed the same set of basket orders and it's showing fine for me.

DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "POST /margins/basket?consider_positions=True&mode=compact HTTP/1.1" 200 None
INFO:Required margin for basket order in compact form: {'initial': {'type': '', 'tradingsymbol': '', 'exchange': '', 
**'total': 62115.236000000004**}, 'final': {'type': '', 'tradingsymbol': '', 'exchange': '', 'total': 41986.486000000004},
 'orders': [{'type': 'equity', 'tradingsymbol': 'BANKNIFTY22O2039600CE', 'exchange': 'NFO', 'total': 11875}, 
{'type': 'equity', 'tradingsymbol': 'BANKNIFTY22O2039400PE', 'exchange': 'NFO', 'total': 5846.25}, 
{'type': 'equity', 'tradingsymbol': 'BANKNIFTY22O2039500CE', 'exchange': 'NFO', 'total': 23579.842}, 
{'type': 'equity', 'tradingsymbol': 'BANKNIFTY22O2039500PE', 'exchange': 'NFO', 'total': 20814.144}]}

You need to re-check if you are sending consider_positions=false while making a basket_order_margins API request, as basket order margin by default considers user open positions.

dolaram commented 1 year ago

Thanks, Ranjan for the quick reply. For some of the algo it has worked properly for some it has not. Even after market hours with no position open, it was not showing proper margin for some time. Yesterday and today margin reported was correct. Maybe it was an intermittent issue.

Thanks for the support.