# Margin calculation for bracket and cover order

edited February 2020 in General
Following is the logic for margin calculation,
``# Bracket and cover order Margins calculation:-# Equity:-co_lower = co_lower/100co_upper = co_upper/100trigger = price - (co_upper * price)if stoploss < trigger:	stoploss = triggerelse:	trigger = stoplossx = 0if transaction_type == 'buy':	x = (price - trigger) * quantityelse:	x = (trigger - price) * quantityy = co_lower * price * quantitymargin = x > y ? x : ymargin = margin + (margin * 0.2)#NFOif options:	# For NIFTY and BANKNIFTY 	if 'NIFTY' in tradingsymbol and transaction_type == 'buy':		margin = price * quantity * 0.70	else:		margin = (strike_price + price) * quantity * 0.025#Futures		else:	co_lower =  co_lower/100	co_upper = co_upper/100trigger = price - (co_upper * price)if stoploss < trigger:	stoploss = triggerelse:	trigger = stoplossx = 0if transaction_type == 'buy':	x = (price - trigger) * quantityelse:	x = (trigger - price) * quantityy = co_lower * price * quantitymargin = x > y ? x : ymargin = (margin * 0.20) + margin#MCXco_lower = 0.01co_upper = 0.019trigger = price - (co_upper * price)if stoploss < trigger: 	stoploss = triggerelse:	trigger = stoplossx = 0if transaction_type == 'buy':	x = (price - trigger) * quantityelse:	x = (trigger - price) * quantityy = co_lower * price * quantitymargin = x > y ? x : ymargin = margin + (margin * 0.4)#CDSco_lower = co_lower/100co_upper = co_upper/100trigger = price - (co_upper * price)if stoploss < trigger:	stoploss = triggerelse:	trigger = stoplossx = 0if transaction_type == 'buy':	x = (price - trigger) * quantityelse:		x = (trigger - price) * quantityy = co_lower * price * quantitymargin = x > y ? x : ymargin = margin + (margin * 0.2)``
• This condition is for buy only.For sell make opposite.
if stoploss < trigger:
stoploss = trigger
else:
trigger = stoploss
• this conditional operation is not available in python.
margin = x > y ? x : y

try this:
margin=max(x,y)
• or margin=x if x>y else y
• Thanks. Can you please elaborate what does the variables co_lower and co_upper mean?
• Hi @Shaha,
You can check out this thread for margins API.
• Hi @sujith ,

Am I missing something here? How have you initialized the stoploss value?
``trigger = price - (co_upper * price)if stoploss < trigger:	stoploss = triggerelse:	trigger = stoploss``
• It is the user entered value.
• For NFO Option - sell, the formula is given as

margin = (strike_price + price) * quantity * 0.25

For Strike_price = 12000, price=1000, quantity=75 from the above formula one gets a value of Rs. 2,43,750
The same values entered in your formula calculator on your site gives Rs. 24,375
i.e. exactly one tenth the value. What am I missing here?
• It is 2.5% for index option sell so the formula will be like
margin = (strike_price + price) * quantity * 0.025
• I have updated the formula in the above post also. Thank you for pointing out.
• edited March 2020
How do I differentiate the calculation for intraday and delivery orders?
• For NFO futures, your formula on the last line of the function has the following line...

margin = (margin * 0.20) + margin;

when I compare this to the actual margin you are blocking, it seems that there is an error. By the above calculation one NFO Nifty contract should block a margin of Rs. 50K, but you actually block only 42K right now.

It seems there is an error and that this line should be more like what is given below because when I use this then the margin calculated and the actual margin you block matches closely.

margin = (margin * 0.002) + margin;

Am I getting something wrong here, or is there an error in the calculation formula given to us?
• @sudipto,
The margin requirement has been updated recently.
You can know more here,
• @sujith
I am aware of the margin requirement changes. I read the bulletin every day morning. This has got nothing to do with the margin requirement change, I am just pointing out an error in the basic formula for calculating the margin as given by you above in this post itself.
To verify the same, just enter the current co_upper and co_lower in the formula and check for yourself.
• Hi @sujith
Is there any code or api to calculate the normal margins required when we short options?
• @arundonni17
No, we don't have any API to calculate margins required for option shorting, you will have to manually use margin calculator.
• edited June 2020
But is there any other way that I can calculate approximate margins required to short BANKNIFTY weekly options through some code? From the previous trades that I have checked we may required 50-60K to short 1 lot of BANKNIFTY weekly expiry option. Is that a fair estimate?
• edited December 2022
@arundonni17
Margin calculation for all F&O contracts is based on intra-day and eod/bod span files from the respective exchange and there are lot of other values considered too while calculating the same. Overall, it's complex calculation.
We are working on margin computation APIs at our end for API clients as well. The same, we recently offered on the Kite web.
Update: Margin APIs is added. Go through the documentation here.
• Hi, how do I get the co_lower and co_upper values referred in the above code dynamically per segment just before placing the order? Can someone guide me? Thanks.
• @Habib,
You can refer to this thread.
• @sujith ,
Thanks for your response. I will try that.
• @sujith
why have you added this line margin = margin + (margin * 0.2) ?
and from where will we get this value of 0.2? Is it a constant?
• edited June 2020
If I am understanding it right, if I have stoploss of 1% and co_upper & co_lower are almost always greater than 1%, then I would get a leverage of 1/co_lower ?
In equity segment.
• @ashishjindal89
if I have stoploss of 1% and co_upper &co_lower are almost always greater than 1%, then I would get a leverage of 1/co_lower ?
Yes, correct. CO Leverage multiplier explanation is same as mis_multiplier explanation as stated here, in the condition of `trigger = stoploss`
• @rakeshr
I have one doubt. I was going through @sujith's explaination at the top of this thread. After looking at the response of https://api.kite.trade/margins/equity , i observed that co_upper is always lower than co_lower. So if co_upper < co_lower, then the leverage would always be 1/co_lower regardless of my stoploss. ( i am representing stoploss as percentage )
There are three cases :-
1. stoploss < co_upper < co_lower :-- in this case trigger will be same as stoploss. and since stoploss < co_lower, my leverage would be 1/max(stoploss,co_lower) which is 1/co_lower
2. co_upper < stoploss < co_lower :-- In this case trigger will be co_upper and since co_upper < co_lower, my leverage would again be 1/co_lower.
3. co_upper < co_lower < stoploss . This would be same as case 2.

So, in short if co_upper < co_lower, i can safely assume that leverage i would get is 1/co_lower regardless of my stoploss. Am I missing something here?
• @ashishjindal89
i observed that co_upper is always lower than co_lower
No, not always. RMS does change this for few scrip like for example as below.
`{"margin": 0, "co_lower": 5.95, "mis_multiplier": 10.0, "tradingsymbol": "BOSCHLTD", "co_upper": 10.0, "nrml_margin": 0, "mis_margin": 9.91}`
• @rakeshr yup got it. Just wanted to confirm for the case where co_upper < co_lower, in that case leverage would always be 1/co_lower right?
• Just wanted to confirm for the case where co_upper < co_lower, in that case leverage would always be 1/co_lower right?
Yeah, correct.
• @rakeshr @sujith Can you please confirm this
sujith used margin + 0.2 * margin. Where does this 0.2 come? is it a constant?
• Where does this 0.2 come? is it a constant?
Constant derived by the RMS team. Might changes in the future, if any we will update here.
• @rakeshr thanks for your help.
This discussion has been closed.