erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.75k stars 723 forks source link

In order to obtain the desired position your Equity with Loan Value [1000] must exceed the new total Initial Margin of [1500]. #704

Closed Amaziinq closed 4 months ago

Amaziinq commented 4 months ago

It's not an issue of the library, but a question. How do you guys handle this error when placing an order? I get it, but I still want to place an order for maximum available size.

When I have the same error while manually using TWS, they offer to automatically reduce order size to match margin requirements.

But when using API, I cannot find such option. How can we do it? Cannot believe whatIf order is the only way.

Best regards

mattsta commented 4 months ago

You're right about whatIf being the only way to really deal with this problem.

IBKR has some of the most restrictive margin requirements, so each symbol/instrument has different real time margin metadata based on volatility and your individual account holdings.

IBKR doesn't provide a way to look up the margin details for an instrument just as a standalone operation. You can only preview your margin settings with a full whatIf order using the API.

For example (assuming US Reg-T margin account), currently for me, SMCI is showing about 85% long initial margin, COIN is showing 100% long initial margin (so, not margin-able), while more "regular" single stocks usually have 33% long initial margin, and ETFs typically have the best rate of 25% long initial margin — but those 33% long initial margin rates can usually be reduced to 25% (long) or 30% (short) if you hold opposite side protective positions (e.g. hold related puts on SPY/QQQ/AAPL when buying AAPL stock or hold related calls when shorting stock).

Also, the long/short margin isn't symmetric. For example, currently I'm seeing for COIN 100% long initial margin but 200% short initial margin; SMCI is 85% long initial margin but 40% initial short margin; TSLA is 55% initial long margin but only 35% initial short margin (so it's cheaper to short some of these than go long according to IBKR's risk calculations).

So, if you really want to "max out" your account, opening positions with a spread order having a ratio of 100 long stock to 1 OTM put (or 100 short stock to 1 OTM call) will let you use your full buying power (but if you are truly at full buying power, it's obviously dangerous because just one cent of loss starts the IBKR auto-liquidation warning timer; also if your puts or calls expire while you are still holding your underlyings, you can get liquidated unless you buy more offsetting puts/calls).

Overall, the safest way to manage these orders is to always run whatIf first to verify the margin limits instead of relying on order submission errors (though, sometimes even whatIf isn't 100% accurate and the actual order still errors out anyway). Also, some instruments have different margin requirements based on time of day (e.g. futures have lower margin during liquid trading hours/RTH than the GTH hours). You could try keeping a local database/cache mapping symbols to long/short margin requirements based on order preview output to avoid needing the extra whatIf latency before every order (sometimes the whatIf system is down or slow... I see it respond anywhere from 5 ms to 45 ms to 3 seconds or just never).

Amaziinq commented 4 months ago

You're right about whatIf being the only way to really deal with this problem.

IBKR has some of the most restrictive margin requirements, so each symbol/instrument has different real time margin metadata based on volatility and your individual account holdings.

IBKR doesn't provide a way to look up the margin details for an instrument just as a standalone operation. You can only preview your margin settings with a full whatIf order using the API.

For example (assuming US Reg-T margin account), currently for me, SMCI is showing about 85% long initial margin, COIN is showing 100% long initial margin (so, not margin-able), while more "regular" single stocks usually have 33% long initial margin, and ETFs typically have the best rate of 25% long initial margin — but those 33% long initial margin rates can usually be reduced to 25% (long) or 30% (short) if you hold opposite side protective positions (e.g. hold related puts on SPY/QQQ/AAPL when buying AAPL stock or hold related calls when shorting stock).

Also, the long/short margin isn't symmetric. For example, currently I'm seeing for COIN 100% long initial margin but 200% short initial margin; SMCI is 85% long initial margin but 40% initial short margin; TSLA is 55% initial long margin but only 35% initial short margin (so it's cheaper to short some of these than go long according to IBKR's risk calculations).

So, if you really want to "max out" your account, opening positions with a spread order having a ratio of 100 long stock to 1 OTM put (or 100 short stock to 1 OTM call) will let you use your full buying power (but if you are truly at full buying power, it's obviously dangerous because just one cent of loss starts the IBKR auto-liquidation warning timer; also if your puts or calls expire while you are still holding your underlyings, you can get liquidated unless you buy more offsetting puts/calls).

Overall, the safest way to manage these orders is to always run whatIf first to verify the margin limits instead of relying on order submission errors (though, sometimes even whatIf isn't 100% accurate and the actual order still errors out anyway). Also, some instruments have different margin requirements based on time of day (e.g. futures have lower margin during liquid trading hours/RTH than the GTH hours). You could try keeping a local database/cache mapping symbols to long/short margin requirements based on order preview output to avoid needing the extra whatIf latency before every order (sometimes the whatIf system is down or slow... I see it respond anywhere from 5 ms to 45 ms to 3 seconds or just never).

Dear @mattsta, Appreciate you taking time to explain in detail to me, I am geniunely curious about the issue and still looking for a solution. Recently I requested if IB API has some checkbox which could enable us to still submit the order for max avaliable size in case we don't have enough margin for the basic one (there is such option in TWS when submit an oversized order manually). For now, will use whatIf. Best regards and have a good day!