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).
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.
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
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 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'])
@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 tocancelthis 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'])
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).
#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.
pykiteconnect just has another wrapper for exiting position which is same as # Second option for canceling orders
above is correct for fetching orders?
exit_order=kite.exit_order(self,'bo',order_id=order['order_id'],parent_order_id=parent_order_id)
Any change in above is required?
orders=kite.orders()
to fetch orderbook.Yes, you can use
kite.cancel_order
to cancel an order andkite.exit_order
to exit a special order.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
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.
orders=kite.orders()
order1=orders[0]
kite.cancel_order('bo',order_id=order1['order_id'],parent_order_id=order1['parent_order_id'])
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'])