Getting ArrayIndexOutOfBoundsException while iterating Hashmap Returned by getPositions()

AbhijeetTeli
Hey Guys,
I working on a scenario where i am infinitely looping through few Scripts in worker thread, and once the XYZ condition met, i will pass the specific script further to place an Cover Order, but before placing the order i need to check if there is any order is currently open,

So i have called getPositions() method which returns HashMap . then i will iterate through HashMap to check if Trading Symbol already present in Positions arrayslist, if Yes then check for netqty==0 then i am good place the order.

Since i am testing this now, and i know i have placed two cover orders for CIPLA, when i am iterating through Hashmap i am getting below exception.

Code Snippet -

Map> position = kiteConnect.getPositions();
System.out.println("Map Size - "+position.size() );
for (int i=0 ;i<position.size() ;i++) {
System.out.println("Position No - "+i);
System.out.println("Symbol - "+position.get("net").get(i).tradingSymbol);
System.out.println("Qty - "+position.get("net").get(i).netQuantity);
System.out.println("Buy Price - "+position.get("net").get(i).buyPrice);
System.out.println("Sell Price - "+position.get("net").get(i).sellPrice);
System.out.println("PnL - "+position.get("net").get(i).pnl);
System.out.println("------------------------------------");
}

Here is the output -

Fetching position
Map Size - 2

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at java.util.Arrays$ArrayList.get(Arrays.java:3841)
at tests.Test.getPositions(Test.java:51)
at tests.Test.main(Test.java:99)
Position No- 0
Symbol - CIPLA
Qty - 0
Buy Price - 636.375
Sell Price - 637.75
PnL - 2.75
------------------------------------
Position - 1

So question is if Map size is 2 then why it thrown indexoutofbound while pulling first element.


Please advise...

Thanks,
Abhijeet

  • sujith
    You can check out the raw response here.
    The response will always have two maps one is for net positions and another for day positions. Having two maps doesn't mean it contains elements always. Array will have positions if there are some active positions(closed or open) in your account.
  • AbhijeetTeli
    Thanks for response @sujith ,

    Ok checked size for "net" and "day" both are 1 and 1 hence Map size is 2
    So in order to make this work, can i safely iterate through size of "net" in my for loop ? I just need to check if
    Position is open (netqty==0), Assuming net will always contain open position right ?

    for (int i=0 ;i<position.get("net").size() ;i++)
    {.....TODO......}

    Another basic question - What is the difference between net and day elements ?


    Thanks,
    Abhijeet
  • sujith
    Yes, you can add a size check before doing any operations.
    Net will have all the positions including day and overnight positions whereas day will have only today's positions.
  • AbhijeetTeli
    OK Sounds good,
    As per my current logic, All orders will be intraday so i am not worried about overnight positions.

    Thanks for your support, We are good to close this thread.

    Regards,
    Abhijeet
This discussion has been closed.