Hi, I was wondering how can I check initialMarginRequirement for specific security programmatically before executing the order and calculating it's quantity? What Security Margin Model does live trading algo implement. How can I access IB's initial margin requirements?
Thanky you in advance for help!
Gurumeher Sawhney
The security margin model has a few methods that can help determine the margin requirements. The method GetInitialMarginRequiredForOrder(Security security, Order order) gets the total margin required to execute the specified order in units of the account currency including fees. If there is no order, a more general method GetInitialMarginRequirement(Security security) gets the percentage of an order's absolute cost that must be held in free cash in order to place the order.
Ilshat Garipov
Gurumeher Sawhney thank you!
But as I understand GetInitialMarginRequiredForOrder is a protected method and can only be called within a class itself or its ancestors.
At the same time, Securities[symbol].MarginModel property is of IBuyingPowerModel type.
So to use any method of SecurityMarginModel interface implementation I need to cast that to a proper type then call a public method?
In live trading mode - paper account through IB - how do I need to cast the MarginModel property to call the public methods, I wonder?
Gurumeher Sawhney
You're right, since Python is not strongly-typed it was easier to access those methods. When trading equities the initial margin requirement is simply 1/leverage. The IBuyingPowerModel type has similar methods such as HasSufficientBuyingPowerForOrder(SecurityPortfolioManager portfolio, Security security, Order order) and GetBuyingPower(SecurityPortfolioManager portfolio, Security security, OrderDirection direction) which will return the values that are needed.
Purpledawn
Hmmm, for position sizing I'm trying to get the maximum qty of a security I can trade at a specific (limit) price - which I would assume to be:Â
security.MarginModel.GetBuyingPower(...) / securityMarginCost
whereÂ
securityMarginCost = security.Price * security.SymbolProperties.ContractMultiplier * initialMarginRequirement
The problem, as mentioned above, is both GetInitialMarginRequirement and GetInitialMarginRequiredForOrder were made (why??!) protected [in .NET]
 the GetLeverage method suggestion above is incorrect as GetLeverage returns 1/MaintenanceMargin, not InitialMargin. GetMaximumOrderQuantityForTargetValueResult targets an account %ge and depends on existing holdings, market price, etc which doesn't really help in this situation.
Is there a canonical way to determine how much of a security I can buy at a given price, taking into account buying power and security leverage?
Â
Michael Manus
search for CalculateOrderQuantity maybe that helps
Purpledawn
Thanks Michael!
For posterity, OpeningBreakoutAlgorithm.cs provides an example of risk based position sizing with CalculateOrderQuantity, which indirectly uses GetMaximumOrderQuantityForTargetValueResult with a provided max leverage.
The current market price is used determine the allowed size, but that could be adjusted for a limit price if significantly different, I guess.
Ilshat Garipov
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!