Margin calculation for bracket and cover order

sujith
sujith edited February 24 in General
Following is the logic for margin calculation,
# Bracket and cover order Margins calculation:-

# Equity:-

co_lower = co_lower/100
co_upper = co_upper/100

trigger = price - (co_upper * price)

if stoploss < trigger:
stoploss = trigger
else:
trigger = stoploss

x = 0

if transaction_type == 'buy':
x = (price - trigger) * quantity
else:
x = (trigger - price) * quantity


y = co_lower * price * quantity

margin = x > y ? x : y
margin = margin + (margin * 0.2)

#NFO

if 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/100

trigger = price - (co_upper * price)

if stoploss < trigger:
stoploss = trigger
else:
trigger = stoploss

x = 0

if transaction_type == 'buy':
x = (price - trigger) * quantity
else:
x = (trigger - price) * quantity

y = co_lower * price * quantity

margin = x > y ? x : y
margin = (margin * 0.20) + margin

#MCX

co_lower = 0.01
co_upper = 0.019

trigger = price - (co_upper * price)

if stoploss < trigger:
stoploss = trigger
else:
trigger = stoploss

x = 0

if transaction_type == 'buy':
x = (price - trigger) * quantity
else:
x = (trigger - price) * quantity

y = co_lower * price * quantity

margin = x > y ? x : y
margin = margin + (margin * 0.4)

#CDS

co_lower = co_lower/100
co_upper = co_upper/100

trigger = price - (co_upper * price)

if stoploss < trigger:
stoploss = trigger
else:
trigger = stoploss

x = 0

if transaction_type == 'buy':
x = (price - trigger) * quantity
else:
x = (trigger - price) * quantity

y = co_lower * price * quantity

margin = x > y ? x : y
margin = margin + (margin * 0.2)
  • puneetbijnor4020
    This condition is for buy only.For sell make opposite.
    if stoploss < trigger:
    stoploss = trigger
    else:
    trigger = stoploss
  • puneetbijnor4020
    this conditional operation is not available in python.
    margin = x > y ? x : y

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

    Am I missing something here? How have you initialized the stoploss value?
    trigger = price - (co_upper * price)

    if stoploss < trigger:
    stoploss = trigger
    else:
    trigger = stoploss
  • sujith
    It is the user entered value.
  • sudipto
    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?
  • sujith
    It is 2.5% for index option sell so the formula will be like
    margin = (strike_price + price) * quantity * 0.025
  • sujith
    I have updated the formula in the above post also. Thank you for pointing out.
  • nawaz1001
    nawaz1001 edited March 26
    How do I differentiate the calculation for intraday and delivery orders?
  • sudipto
    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?
  • sujith
    @sudipto,
    The margin requirement has been updated recently.
    You can know more here,
  • sudipto
    @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.
  • arundonni17
    Hi @sujith
    Is there any code or api to calculate the normal margins required when we short options?
  • rakeshr
    @arundonni17
    No, we don't have any API to calculate margins required for option shorting, you will have to manually use margin calculator.
  • arundonni17
    arundonni17 edited June 11
    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?
  • rakeshr
    @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. For the time being, you will have to manually use a margin calculator.
  • Habib
    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.
  • sujith
    @Habib,
    You can refer to this thread.
  • Habib
    @sujith ,
    Thanks for your response. I will try that.
  • ashishjindal89
    @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?
  • ashishjindal89
    ashishjindal89 edited June 30
    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.
  • rakeshr
    @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
  • ashishjindal89
    @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?
  • rakeshr
    @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}
  • ashishjindal89
    @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?
  • rakeshr
    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.
  • ashishjindal89
    @rakeshr @sujith Can you please confirm this
    sujith used margin + 0.2 * margin. Where does this 0.2 come? is it a constant?
  • rakeshr
    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.
  • ashishjindal89
    @rakeshr thanks for your help.
This discussion has been closed.