Keyboard Shortcuts
Likes
Search
How do I find out the margin requirements for a US stock through the TWS API?
Hi, I'd like to find out the margin requirements for US stocks via the TWS API in python. On the Trader Workstation, of I look the Description of a Stock, it has a tab about margins, and lists the initial and maintenance margin requirements for long and short positions - see screenshot attached. I'd like to get the same information via the TWS API in python. When requesting the contract details via the API, this information is not contained in the returned ContractDetails object. Here's a sample code using the ib_async wrapper: qqq = Stock(symbol='META', exchange='SMART', currency='USD') qqq_details = ib.reqContractDetails(qqq) pprint(qqq_details) here's the result of the printout: [ContractDetails(contract=Contract(secType='STK', conId=107113386, symbol='META', exchange='SMART', primaryExchange='NASDAQ', currency='USD', localSymbol='META', tradingClass='NMS'), marketName='NMS', minTick=0.01, orderTypes='ACTIVETIM,AD,ADJUST,ALERT,ALGO ,ALLOC,AON,AVGCOST,BASKET,BENC HPX,CASHQTY,COND,CONDORDER,DAR KONLY,DARKPOLL,DAY,DEACT,DEACT DIS,DEACTEOD,DIS,DUR,GAT,GTC,G TD,GTT,HID,IBKRATS,ICE,IMB,IOC ,LIT,LMT,LOC,MIDPX,MIT,MKT,MOC ,MTL,NGCOMB,NODARK,NONALGO,OCA ,OPG,OPGREROUT,PEGBENCH,PEGMID ,POSTATS,POSTONLY,PREOPGRTH,PR ICECHK,REL,REL2MID,RELPCTOFS,R PI,RTH,SCALE,SCALEODD,SCALERST ,SIZECHK,SMARTSTG,SNAPMID,SNAP MKT,SNAPREL,STP,STPLMT,SWEEP,T RAIL,TRAILLIT,TRAILLMT,TRAILMI T,WHATIF', validExchanges='SMART,AMEX,NYSE,CBOE,PHLX,ISE, CHX,ARCA,NASDAQ,DRCTEDGE,BEX,B ATS,EDGEA,BYX,IEX,EDGX,FOXRIVE R,PEARL,NYSENAT,LTSE,MEMX,IBEO S,OVERNIGHT,TPLUS0,PSX', priceMagnifier=1, underConId=0, longName='META PLATFORMS INC-CLASS A', contractMonth='', industry='Communications', category='Internet', subcategory='Internet Content-Entmnt', timeZoneId='US/Eastern', tradingHours='20241016:0400-20241016:2000;20241017:0400-20241017:2000;20241018:0400-20241018:2000;20241019:CLOSED;20241020:CLOSED;20241021:0400-20241021:2000', liquidHours='20241016:0930-20241016:1600;20241017:0930-20241017:1600;20241018:0930-20241018:1600;20241019:CLOSED;20241020:CLOSED;20241021:0930-20241021:1600', evRule='', evMultiplier=0, mdSizeMultiplier=1, aggGroup=1, underSymbol='', underSecType='', marketRuleIds='26,26,26,26,26,26,26,26,26,26, 26,26,26,26,26,26,26,26,26,26, 26,26,26,26,26', secIdList=[TagValue(tag='ISIN', value='US30303M1027')], realExpirationDate='', lastTradeTime='', stockType='COMMON', minSize=0.0001, sizeIncrement=0.0001, suggestedSizeIncrement=100.0, cusip='', ratings='', descAppend='', bondType='', couponType='', callable=False, putable=False, coupon=0, convertible=False, maturity='', issueDate='', nextOptionDate='', nextOptionType='', nextOptionPartial=False, notes='')] |
@rossh_yh?
I assume you wanted to suggest the flag as described in the "" or ""
?
@?kos Mar¨®y
The margin information you are asking for is very generic and probably identical for all STK instruments on large exchanges in the US. That information is not available through TWS API and has only marginal value. The actual margin required for a trade is much more nuanced and depends on the risk your account and positions pose at the time of trade. Factors include the account type (cash, margin, IRA, ...), the combination of positions currently in the account, and the overall market situation. In fact, under certain situations, account margin requirements may be reduced when you add a position that reduces the overall risk. And, consequently, required margin increases when you close it. With TWS API you can monitor the various current account level margin requirements in real time by subscribing to . The actual margin impact for any given order can be checked, as rossh_yh? pointed out, by placing that order with the WhatIf flag set to true. You may want to search the group's archive since we had several discussion about this. ´³¨¹°ù²µ±ð²Ô ?
?
?
On Sat, Nov 2, 2024 at 11:04 AM, rossh_yh wrote:
|
Well, it sounds very anti-intuitive that one cannot determine if a trade will go through by using data available at the time of entering a trade - but has to 'reverse engineer' by issuing 'what if' trades and seeing their result. Is this really the case? On Sat, Nov 2, 2024 at 6:35?PM ´³¨¹°ù²µ±ð²Ô Reinold via <TwsApiOnGroupsIo=[email protected]> wrote:
|
I don't think that it is counter-intuitive. Whether an order would go through may depend on many parameters. For example what other positions are already in your portfolio, and how much free cash you have available. Besides the size of the intended order. The margin required is therefore depending on the situation and can only be analyzed by submitting a "what-if order". |
On Sun, Nov 24, 2024 at 8:25?AM J G via <windmill_1965=[email protected]> wrote:
yes, and all this information is available before entering the trade, and I should be able to deduct the result myself, without a 'what if' order. ideally for example based on values in accountSummary(), such as?AvailableFunds-S ? |
This is where your assumptions are incorrect. The actual margin required for changes in positions (buy or sell) depends on an account level risk model that you do not know all parameters of and that you will not be able to rebuild. The information from the instrument description page you mentioned describes only a very small portion of the model, and for US stocks it always says the same:
So you'd need no API call if you think you can determine your margin requirements from those constant percentages. That TWS screen has a "More" link that points to various margin related resources at IBKR. It also mentions the ability in TWS to monitor your real time margin requirements with the "check margin" feature, which happens to be identical to placing a WhatIf order through the API. Keep in mind that our group has not designed the API nor can we make any changes to it. We can help each other with experience and advice about how to use it. If you do not like our advice and the current API "WhatIf" order placement feature, you need to continue your discussion directly with IBKR. ´³¨¹°ù²µ±ð²Ô ?
On Sun, Nov 24, 2024 at 02:38 AM, ?kos Mar¨®y wrote:
? |
On Sun, Nov 24, 2024 at 4:14?PM ´³¨¹°ù²µ±ð²Ô Reinold via <TwsApiOnGroupsIo=[email protected]> wrote:
I don't need or want to rebuild this, or re-calculate this, I just want to know what IB has calculated in this regard. these are all based on the account's properties and not related?to the asset I'm trying to take. The accountSummary endpoint seems the closest?to contain this information:?? . Looking at it, AvailableFunds-S seems to come closest in relation to stocks. ?
It's actually not the same for all stocks or ETFs. For example, for QQQ it's 25% for a long?position, but for TQQQ / SQQQ, it's 75% for a long position. hence the need to have this available via an API call, which apparently doesn't exist and the size of the trade is about: <funds available for trading> / margin_requirement, e.g. $100k / 25% = $100k / 0.25 = $400k ? where the missing link is <funds available for trading>, something akin to AvailableFunds-S in account properties
it also has a page that basically displays the account properties for the account ?
|
Margin by itself is not additive as it is calculated under the maximum norm. The closest thing that works in real-time is CME's SPAN mechanism. There's some documentation on the client systems wiki if you want to implement something similar, although it would require simulating all scenarios (up, down, sideways, high vol, low vol).
?
Margin(A) + Margin(B) != Margin (A+B)
?
With the SPAN methodology, the risk vectors are additive:
Risk(A+B)[k] = Risk(A)[k] + Risk(B)[k]
?
So margin can always be deduced:
Margin(A+B) = max ( Risk(A+B)[k] for k in [1..16] ) = max ( Risk(A)[k]+Risk(B)[k] for k=1..16 )
?
IB is doing something similar on their side. |