I'm trying to figure out a way to check IB margin requirements through script, or to place a "What If" order as described below through the IB API so that I can see if a trade is possible before execution, and resize accordingly. The issue is that IB changes its margin constantly and if an order is rejected, the script basically just shutsdown without an ability to resize that I can see.
Thanks!
From IB:
Checking Margin Changes
From the API it is possible to check how a specified trade execution is expected to change the account margin requirements for an account in real time. This is done by creating an Order object which has the IBApi.Order.WhatIf flag set to true. By default the whatif boolean in Order has a false value, but if set to True in an Order object with is passed to IBApi.EClient.placeOrder, instead of sending the order to a destination the IB server it will undergo a credit check for the expected post-trade margin requirement. The estimated post-trade margin requirement is returned to the IBApi.OrderState object.
This is equivalent to creating a order ticket in TWS, clicking "Preview", and viewing the information in the "Margin Impact" panel.
See also: TWS Margin Impact
Comment