Quantity of ModifyOrder partial filled

Hi every one,
Im a bit stuck with ModifyOrder(..., number Quantity) of partially filled order.
Imagine I had 500 shares and I want to exit all position with lowering price if no more filling happens.
Let me explain in short:

  1. PlaceOrder(...."SELL",500,"LMT",1.15..) (1.15-price)
  2. I receive Filled=200, Remaining 300
  3. ModifyOrder(ID_...,,,300(or500???), "LMT", 1.14,...) (1.14-price)
    Which amount shall I use? The Remaining of Initial amount?
    I noticed that IbController keeps the Filled field but what Amount shall I use for complete exit?

After ModifyOrder(ID_...,,,300,,,,) I get following

What is the logic here?

Do I have to put All initial Amount to the ModifyOrder even if it was partially filled or only remaining part?

in my opinion you need to check the positionsize using

ps = ibc.GetPositionSize( symb );

then you adjust the number of shares and send that to ModifyOrder

so if pstn is for instance 500 shares and only 300 have been filled. Then

        ps = ibc.GetPositionSize( symb );
        // calculate number of shares
        nos = pstn - ps;

nos will be 500-300 = 200

pstn = 500
ps = 300
nos = 200 ==> send this to ModifyOrder

Thanks! I tried as you proposed.
(FYI in my question I was going to sell my position in portfolio 500 - not to buy that.)
But it looks like it is not as you and me expected. At least with demo setver. I will explain.
When I modify partly filled order by its ID it keeps Filled as it was before! So IB demo server trade only Ramaining part (Amount-Filled)! (It does not sell the whole Amount! But only remaining part!!!) So right solution is to keek initial Amount (Only if it returns same ID). If it returns New ID - it has to be corrected immediately with Potfolio Amount)!
I will check it in real trade server but I think 99% it will be same logic.

Timing matters a lot when using GetPositionSize() because IB doesn't update the position size, position list, and order status simultaneously. There is a bit of a delay and any of them could be updated first, so if you modify the order you might be using stale data.

2 Likes

yes what I used to do is to first send the original order. Then let the code check the next minute if the order executed by checking the position size. If you wait 1 minute you will get the correct data. I have not used it myself in awhile, not sure if this has improved. But I guess not.

You could build code that checks every minute if the order is completely filled. And stop checking when it is filled.

last piece of code here Order status randomly becomes APIpending, Presubmitted - #4 by empottasch

is what I still use ( meaning the function sendOrder_proc ). This function can for instance be imbedded in code that checks every minute if your order has been completely executed. But it can't be used in High Frequency trading :slight_smile: