order_margins api

zuser
Hi,
I am facing some issue with calling order_margins api. It's throwing the following error.
kiteconnect.exceptions.InputException: invalid json

API call code is as follow.
kite.order_margins({'order_id':XXXXX,'exchange':'NFO', 'tradingsymbol':'XXXXXXXX','transaction_type':'SELL','variety':'regular','product':'MIS','order_type':'MARKET','quantity':75,'price':0,'trigger_price':0})

Any example code here would help.
Thanks.
  • sujith
    You can print the exception message to know the exact error message. You can check out the exception structure here.
  • Alogji
    @zuser example code (Based on available margin, how many option lots of both PE and CE can be shorted/written)


  • zuser
    Thanks @Algoji.
    I am able to get the margin info. However the margin of all live trades aren't summing upto the total used margin! :(
  • Alogji
    Alogji edited May 2021
    It is dynamic, so it keeps changing. I have manually verified it when market hours got over. Then number you get using the code exactly matches with margin required for a basket.
  • rakeshr
    However the margin of all live trades aren't summing upto the total used margin!
    It is dynamic, so it keeps changing.
    Yes, that's correct. You can use margin APIs to get an idea of the approx margin required(unless there is huge volatility in the next second and it decreases or increases drastically).
  • akshay_thakker
    Hi everyone, is there a way to get the margin required for a future or option lot independent of the current position/ orderbook? The reason for this requirement is that I have a running tally of margin I have used for a particular setup and the margin that I have available. The code decides the number of new lots to trade by dividing the margin available with the margin per lot but sometimes because the margin queried is way low because of the existing positions or orderbook i get a reject order when I try to place an order. Thanks!
  • rakeshr
    Is there a way to get the margin required for a future or option lot independent of the current position/ orderbook?
    Yes, you can use basket margin API with consider_positions query param as False.
  • Rekha
    I am getting this exception:
    KiteConnect.order_margins() got an unexpected keyword argument 'exchange'

    this is server-side code
    @socketio.on('send_required_margin')
    def send_required_margin(data):
    print(data)
    try:
    margin_data = kite.order_margins(
    exchange = 'NFO',
    tradingsymbol = data.get('tradingsymbol'),
    transaction_type = data.get('transaction_type'),
    variety = data.get('variety'),
    product = data.get('product'),
    order_type = data.get('order_type'),
    quantity = data.get('quantity'),
    price = data.get('price'),
    trigger_price = data.get('trigger_price')
    )
    # Emitting response back to client
    socketio.emit('margin_response', {'success': True, 'margin_data': margin_data})
    except Exception as e:
    print(e)
    socketio.emit('margin_response', {'success': False, 'message': str(e)})



    this is frontend js
    socket.emit('send_required_margin', {
    tradingsymbol: symbol,
    transaction_type: transactionType,
    variety: 'regular',
    product: 'NRML',
    order_type: 'LIMIT',
    quantity: quantity,
    price: price,
    trigger_price: null
    });
  • sujith
    You can refer to the pykiteconnect documentation here.
    You can also check out the example here.
  • pseudo
    What is the difference between initial and final margins from basket_order_margins call?
  • Arockiya_r
    initial margin displays the margin that is required to place all the orders in the basket.
    The final margin displays the eventual margin that will be blocked in the Zerodha account after the order execution considering the hedge benefits.
  • pseudo
    Thank you for the quick response, so I should use initial before placing orders. Was using final and multiplying it with 1.5 times to calculate quantity based on available balance, still many times my orders have been failing due to margin shortfall!
Sign In or Register to comment.