Auto square off At market 100 BO order

Ashok121
I need to auto square off 100 BO executed and non executed order at 2 pm everyday.

I know it can be done through order id and parent id.

But how to fetch order id and parent id for multiple orders? as manual process to get the same is difficult.

Can you give me Python coding to fetch order id and parent id?
Tagged:
  • sujith
    You need to do two things, one is canceling pending orders and square off bracket order positions.
    You need to make sure you won't cancel second leg orders. You need to exit second leg orders.
    The difference between canceling and exit is sending parent order id in the request. If you don't send parent order id then it is canceling an order, if you send a parent order id then it is exiting order.

    To square off at 2:00 PM every day, you need to first fetch orderbook and find pending orders using the status. If an order has a parent order id then you need to send exit order request or else you need to send a cancel request(for first leg order).
  • sujith
    In pykiteconnect, you can check out fetch orderbook and cancel order here.
  • Ashok121
    Ashok121 edited March 2018
    For fetching orders and cancelling/exit orders, I could code below for python:

    #For fetching orders:
    orders=kite.orders()

    #First Option for exiting/cancelling orders:
    cancel_order=kite.cancel_order('bo',order_id=order['order_id'],parent_order_id=order['parent_order_id'])
    exit_order=kite.exit_order(self,'bo',order_id=order['order_id'],parent_order_id=parent_order_id)

    # Second option for canceling orders:
    "https://api.kite.trade/orders/bo/XXX?api_key=xxx&access_token=yyy&parent_order_id=YYY"

    Please advise me further if anything is not correct.
  • sujith
    A bo in the URL denotes order is a variety of type bracket order and you send parent order id for exiting BO position.

    pykiteconnect just has another wrapper for exiting position which is same as # Second option for canceling orders
  • Ashok121
    orders=kite.orders()

    above is correct for fetching orders?
  • Ashok121
    cancel_order=kite.cancel_order('bo',order_id=order['order_id'],parent_order_id=order['parent_order_id'])
    exit_order=kite.exit_order(self,'bo',order_id=order['order_id'],parent_order_id=parent_order_id)

    Any change in above is required?
  • sujith
    Yes, you can use orders=kite.orders() to fetch orderbook.
    Yes, you can use kite.cancel_order to cancel an order and kite.exit_order to exit a special order.
  • Ashok121
    I run following on python:
    orders=kite.orders()
    kite.cancel_order('bo',order_id=order['order_id'],parent_order_id=order['parent_order_id'])
    kite.exit_order('bo',order_id=order['order_id'],parent_order_id=parent_order_id)

    I got following error:

    (kite3) 14:21 ~/krish $ python time.py
    Traceback (most recent call last):
    File "time.py", line 18, in
    kite.cancel_order('bo',order_id=order['order_id'],parent_order_id=order['parent_order_id'])
    NameError: name 'order' is not defined

  • zartimus
    @Ashok121
    orders=kite.orders()
    kite.cancel_order('bo',order_id=order['order_id'],parent_order_id=order['parent_order_id'])
    kite.exit_order('bo',order_id=order['order_id'],parent_order_id=parent_order_id)
    Now, the above exception `NameError` occurs just because you do not have the variable `order` defined in the globals or locals. orders = kite.orders() will give you a list of orders. Now you can access individual order as orders[0]['order_id']
  • Ashok121
    thanks for your response.

    say I have 5 order_id and 2 parent_id. In that case my coding will be:

    orders=kite.orders()
    orders[0]['order_id']
    orders[1]['order_id']
    orders[2]['order_id']
    orders[3]['order_id']
    orders[4]['order_id']
    orders[0]['parent_order_id']
    orders[1]['parent_order_id']
    kite.cancel_order('bo',order_id=order['order_id'],parent_order_id=order['parent_order_id'])
    kite.exit_order('bo',order_id=order['order_id'],parent_order_id=parent_order_id)

    Please correct me if anything wrong.
  • zartimus
    zartimus edited March 2018
    @Ashok121 Where are your order, parent_order_id variables assigned?
    orders=kite.orders()
    order1=orders[0]
    kite.cancel_order('bo',order_id=order1['order_id'],parent_order_id=order1['parent_order_id'])
  • Ashok121
    @zartimus
    Thank you for the clarity.
    One more time, I will disturb you to clarify & get corrected below, if required:

    I put one bracket order which is yet to be executed & it is pending in order book. Now to cancel this pending order, my coding will be:
    orders=kite.orders()
    order1=orders[0]
    print(order1)
    kite.cancel_order('bo',order_id=order1['order_id'],parent_order_id=none)

    I put one bracket order which is executed and second leg orders (target & SL) are pending in order book. Now to exit any of second leg orders, my coding will be:
    orders=kite.orders()
    order2=orders[1]
    print(order2)
    kite.cancel_order('bo',order_id=order2['order_id'],parent_order_id=order2['parent_order_id'])
  • sujith
    Yes, the above code is correct.
Sign In or Register to comment.