开云体育

ctrl + shift + ? for shortcuts
© 2025 开云体育

Inconsistent behaviour of WHATIF orders


 

I'm trying to use WHATIF-orders to gauge the margin impact of trades before I finally send my orders but the results are more than unreliable. Sometimes it works perfectly and the returned margin impact matches the required margin after the position is opened nicely but
pretty often it doesn't work at all.
I experimented with an order that definitely should have a margin impact - the shortsale of some put options. The what-order returned zero for both the change in initial margin and maintenance margin. I started with just one contract - result zero. Then I started increasing the position size. Two contracts - still zero.
3 contracts - now I suddenly got a nonzero maintenance margin change although it was much too low. When I went to 4 contracts the margin increased but still it was not what I would expect (or what IB would require from me).

Anybody experienced similar?


 

Hi, I use WHATIF orders via API everyday and it always worked fine. One leg or multi-leg combos, everything OK.
Try to build the same order on TWS, and see the margin impact. You should get exactly the same with API.
If not the case, look for API error messages, maybe they will tell you what's wrong


 

Hmm Ok thanks, then there must be a bug in my code. I will debug it again :-)


 

The code I use, with dummy parameters:

IContractOrd is IBApi.Contract
...
IOrderLabo is IBApi.Order
stOrderState is STOpenOrderWhatIf
IOrderLabo.totalQuantity=3
IOrderLabo.lmtPrice=10.50
IOrderLabo.action="SELL"
IOrderLabo.orderType="LMT"
IOrderLabo.tif="GTC"
IOrderLabo.account=accountNo
IOrderLabo.transmit=Vrai??? //mandatory for WhatIf order
IOrderLabo.WhatIf=Vrai
IBClient.ClientSocket.placeOrder(NextOrderId,IContractOrd,IOrderLabo)


 

oups! Vrai means True (in french)


 

The only differences to my code that I can spot directly is that I use MKT orders. Also I don't explicitly set the transmit flag (I think it is true by default). Further I already experimented with setting the tif to either DAY or GTC but it didn't make any difference.
After all my code can't be dead wrong since sometimes it works perfectly.

I will come back when I found the culprit.

However thanks for your comments! Really appreciated!


 

@Francois G: One more question. Do the whatif orders work for you also when the market is closed (like on weekends)? I'm asking because I ran the program on the weekend and yesterday before the market opened and it gave me wrong results. Now I rerun the same program (no changes) and it gives me correct results! Well and this although the market is closed again (US options). Maybe it is because I try it on the paper trading account?


 

Despair, I just tried now (07:29 AM UTC) markets closed, and it works fine, either for single leg or combo whatif orders.
See attached screenshot, one is for single legs, one is for combo. I use WHATIF orders to get the margin requirements before I transmit the order.
Did you track error messages? There can be errors code 110: 'The price does not conform to the minimum price variation for this contract'.
I remember when coding my app that this error forced me to manage 'minimum price increment' through marketRuleId, for normal orders AND whatif orders.





 

This is on a paper trading account, and WHATIF orders always worked fine.


 

@Francois: There is no error message. Everything is returned as it should just that both change in initial margin and change in maintenance margin are returned as zero.

I tested it now all week and since the market opened on Monday it works perfectly. No problems at all. It just did not work during the weekend and Monday morning before the market opened. Have you tested your code on a weekend?


 

I guess yes, as I'm workaholic... But no matter, if it works fine when markets are open, then it works fine!


 

I will test it again this weekend. Since it is now working flawlessly there is nothing to debug :-)?


 

On Mon, Apr 26, 2021 at 05:31 PM, Despair wrote:
I started with just one contract - result zero. Then I started increasing the position size. Two contracts - still zero.
3 contracts - now I suddenly got a nonzero maintenance margin change although it was much too low. When I went to 4 contracts the margin increased but still it was not what I would expect (or what IB would require from me).
Could it be possible that you already have some positions, for example short calls, and selling 1-2 puts would not increase the margin requirement.? but when you are selling 3-4, it brings more risk on the other direction and therefore the margin increases.?


 

If you're on portfolio margin then the behavior is completely consistent. Not every trade increases margin if it's considered to be risk reducing by IB's PM model.
Now, if you're seeing a disparity between the response from your API app and TWS "Check Margin" window, you have a separate problem.


 

This is exactly the problem. On portfolio margin IB sets the margin requirement according to their risk model and not according to some fixed rules. Meaning every new positions margin is depending on which positions are already open. So using WHAT-IF orders simply doesn't work for positions
build from many legs.
Instead of using WHAT-IF orders I now calculate the margin according to this rules:

Most often the margin requirement of the whole position is <= the sum of the legs according to the rules. It only happened once so far that the requirement was higher (probably there was too much concentrated risk).