Today my algo didnt work as expected, in that there was a case of double-buy of a same contract. I avoid this by checking the quantities of contract via the API call kite.positions(). Today for some reason, the API returned the value 0, when in fact I had a contract and that resulted in my algo putting a buy order.
I know this because due to this double-buy incident, the algo was stuck in a while loop, therefore, kite.positions() was called repeatedly and I was able to see what values it returned.
Below, is part of the log (quantity_latest_CE is supposed to be 25):
09:23:17 halting till quantity_latest_CE is 25
BANKNIFTY21N1838200CE: {'qty': 50, 'unrealised': -32.5} (Note the "qty" is 50, meaning that the double-buy has happened)
Total: -32.5
09:23:17 halting till quantity_latest_CE is 25
BANKNIFTY21N1838200CE: {'qty': 0, 'value': 1397.499999999999}(!! "qty" is 0 !!)
BANKNIFTY21N1838200CE: {'qty': 0, 'value': 1397.499999999999} (!! "qty" is 0 !!)
Total: 1397.499999999999
------
As you can see, the API returns incorrect values at times. It just so happened today, that it returned a wrong value of 0 just when my algo asked for a contract's quantity.
My question is, is there anyway around it? Do we have to live with this technical bug or "feature"?
Once you place an order, it goes through various internal systems and then finally hit the exchange and then OMS updates the value and the change is propagated to all the connected systems. You can add a delay after placing order before fetching positions to check if a position is created.