How to square off all positions at once for Covered orders-

samant
Hi, I already read a thread on this stating "For bo, co positions, you need to loop through orderbook and find the pending orders with parent order id and send an exit position request." But didn't quite understand what you mean by "send an exit position request" I am placing covered orders and by 3 PM I need to exit all positions. All my intraday positions are short positions ( all COs). Will the following code not work to exit all positions ?
try {
Map> positions = kiteConnect.getPositions();
List posList=positions.get("day");

Position position=null;
for ( int i=0;iSquared off "+position.tradingSymbol+" quantity "+position.sellQuantity);
}
} catch (JSONException | IOException | KiteException e) {
sb.append(e.getMessage());
}
  • samant
    Somehow the code didn't paste properly. I wanted to know whether following code will work for exiting all open positions, that were taken with COs.
    try {
    Map> positions = kiteConnect.getPositions();
    List posList=positions.get("day");

    Position position=null;
    for ( int i=0;i<posList.size();i++){
    position=posList.get(i);
    OrderParams orderParams = new OrderParams();
    orderParams.quantity = position.sellQuantity;
    orderParams.orderType = Constants.ORDER_TYPE_MARKET;
    orderParams.tradingsymbol = position.tradingSymbol;
    orderParams.product = Constants.PRODUCT_MIS;
    orderParams.exchange = Constants.EXCHANGE_NFO;
    orderParams.transactionType = Constants.TRANSACTION_TYPE_BUY;
    orderParams.validity = Constants.VALIDITY_DAY;
    Order order = kiteConnect.placeOrder(orderParams, Constants.VARIETY_REGULAR);

    }
    } catch (JSONException | IOException | KiteException e) {

    }
  • sujith
    You can refer to this thread.
  • samant
    Hi Sujith,

    Yes I had referred to that thread before putting this comment. What was not clear to me from that thread was, the meaning of "For bo, co positions, you need to loop through orderbook and find the pending orders with parent order id and send an exit position request."
    My question was- I place MIS CO market orders from 9:15 onwards and those are all NIFTY OPTION SHORT orders. So all orders get placed and each order has a corrosponding stoploss order waiting to be triggered. Now at 3:10, I want to explicitly close all my open positions and cancel the open orders ( The stoploss orders that are waiting to fire).
    Is the following code good enough to achieve this ?

    try {
    Map> positions = kiteConnect.getPositions();
    List posList=positions.get("day");

    Position position=null;
    for ( int i=0;i<posList.size();i++){
    position=posList.get(i);
    OrderParams orderParams = new OrderParams();
    orderParams.quantity = position.sellQuantity;
    orderParams.orderType = Constants.ORDER_TYPE_MARKET;
    orderParams.tradingsymbol = position.tradingSymbol;
    orderParams.product = Constants.PRODUCT_MIS;
    orderParams.exchange = Constants.EXCHANGE_NFO;
    orderParams.transactionType = Constants.TRANSACTION_TYPE_BUY;
    orderParams.validity = Constants.VALIDITY_DAY;
    Order order = kiteConnect.placeOrder(orderParams, Constants.VARIETY_REGULAR);

    }
    } catch (JSONException | IOException | KiteException e) {

    }

    Thanks
  • sujith
    For cover and bracket orders, there is already a pending child order, the system just has to modify that to a market order.
    For an end-user to exit co or bo, one has to send an exit order request, this can be done by sending a cancel order request with parent order id as mentioned here.

    If you place another MIS order to exit cover and bracket order then RMS will think there are 2 open positions and they will exit from the admin terminal and place another counter order for closing MIS order. End of the day you will be charged Rs 100 for call & trade.
  • samant
    Thanks Sujith. Sorry for pestering you, but I still didn't understand this part of the documentation : "one has to send an exit order request" which I don't see any API in Java object kiteConnect for doing the following .
    # Exit / cancel a bracket order.
    curl --request DELETE \
    "https://api.kite.trade/orders/bo/151220000000000?parent_order_id=151210000000000" \
    -H "X-Kite-Version: 3" \
    -H "Authorization: token api_key:access_token" \

    Should I use kiteConnect.cancelOrder(orderId, parentOrderId, variety) ?
    If Yes, and if I am trying to exit the placed orders and cancle the open stoploss orders of the CO, what should be value of the param variety ? Constants.VARIETY_CO?
  • sujith
    @samant,
    You can check out the example here.
  • samant
    Thanks Sujith. It worked, but it lead to another functional issue. I'll open another ticket for that though. This may be closed now.
This discussion has been closed.