How to get net Open Position of an trading instrument?

Flawless
Dear Team,

I want to exit all the open positions for a particular trading Instrument by placing market order when my stop loss price triggers.

For that i need Net quantity of open positions for a trading instrument.

I tried below mentioned method:-

public static void getPositions(KiteConnect kiteconnect) throws KiteException {
Position position = kiteconnect.getPositions();
System.out.println(position.netQuantity);

}

The above mentioned code to get position always returns 0. Please tell me how to get the net quantity of a trading symbol?

For example if the trading symbol is DRREDDY and it has -2 (2 Sell Orders Open) as open position, then how to get the value -2 with the above mentioned method?

Is there something I am missing on this, because in this method the trading symbol name is nowhere mentioned.

Do i need to mention DRREDDY anywhere in the above method. If yes please tell me how.

Can u give me sample code with any trading instrument to get the net quantity?

  • sujith
    Hi @Flawless,
    You can get all net positions by accessing position.netPositions. You can check for position data structure here.
  • Flawless
    @sujith ,

    I need only open quantity of buy and sell orders to close that position if market goes against me particular price. Don't want to see full position

    Today i traded in DRREDDY 13 buy orders and 13 Sell Orders.

    Trying to get details of open position by below mentioned method:-

    public static void getPositions(KiteConnect kiteconnect) throws KiteException {
    Position position = kiteconnect.getPositions();
    System.out.println(position.netQuantity);
    System.out.println(position.sellQuantity);
    System.out.println(position.buyQuantity);

    }

    In the above method,

    System.out.println(position.sellQuantity); should return me 13 as sell quantity was 13
    System.out.println(position.buyQuantity); should return me 13 as buy quantity was 13

    both the above lines returns 0 instead of 13.

    Is there any thing wrong in the above code?...please help me with the one liner code to get quantity of open positions with this method.

    Thanks

  • sujith
    getPositions() returns position model which is always empty. Actual data is present inside two arraylist present in position model i.e. position.netPositions and position.dayPositions.

    To get quantity of DRREDDY, you loop through list something like this,

    for (int i = 0; i < position.netPositions.size(); i++){
    Position positionModel = position.netPositions.get(i);
    if(positionModel.tradingsymbol.equals("DRREDDY")){
    int qauntity = positionModel.netQuantity;
    }
    }
  • LUCKYTRADE
    @sujith
    I am also facing the same issue.

    I need to route the script execution to a loop, in which the following condition need to satisfy

    get the net position of the day , net quantity and price at which the share was bought or sold.

    then some condition. Can you help me out.


  • LUCKYTRADE
    @sujith
    position is for banknifty option..
  • sujith
    There is no relation between positions and orderbook. You will have to tag orders and based on that you need to check the condition. You will receive the tags in the orderbook, you can use the same.
    You can check the documentation here.
  • Chetan_Bonde
    If run net position code per min then can i get updated position at every min??
  • Chetan_Bonde
    If run net position code per min then can i get updated position at every min??
  • rakeshr
    We don't recommend high-frequency polling positions. Depending on your requirement, you can make use of Websocket streaming, post-back, order-book APIs, etc to work around instead of polling positions.
Sign In or Register to comment.