Shorting stocks can be a profitable strategy for experienced traders, but it can also be challenging for inexperienced traders to execute. In this article, we will explore the difficulties of shorting stocks and provide solutions for dealing with changing initial margin.

Why is Shorting Stocks Challenging?

Shorting stocks is challenging for a number of reasons. Firstly, stocks tend to gain value over time, which makes it harder to find successful shorting strategies. Additionally, some stocks may be unavailable for shorting. Brokers have to lend stocks from owners to make them available for shorting, meaning that not all stocks are available to be shorted. At Interactive Brokers you can find this kind of information in TWS platform – where you can see green circle with “Shortable” below chosen ticker. Alternatively, you can check Interactive Brokers short-selling stocks list. Secondly, brokers may adjust initial margin requirements, particularly when a stock is volatile, which can lead to unexpected issues with orders being cancelled. Lastly, borrowing fees must be paid for keeping short positions overnight, which can make shorting a less attractive strategy for inexperienced traders.

How to Deal with Changing Initial Margin?

One of the main challenges of shorting stocks is dealing with changing initial margin. When a trader places a short order, the broker may require a higher initial margin if the stock is volatile or if the trader is taking a large position. If the trader does not have enough capital to cover the required initial margin, the order may be cancelled, which can lead to missed opportunities for profit.

In such cases you will get your order being cancelled with error message: IN ORDER TO OBTAIN THE DESIRED POSITION YOUR EQUITY WITH LOAN VALUE MUST EXCEED THE INITIAL MARGIN

When using Python and Interactive Brokers API I really recommend great Python library ib_insync which is basically wrapper around IB APIs with high level methods. It will let you read your trading account data, stream market data, fetch history data or place, modify and cancel orders.

Within a standart solution using Python and ib_insync, your order placing code would look something like this:

aapl_contract = Stock('AAPL', 'SMART', 'USD')
aapl_order = MarketOrder('SELL', 200)
ib.qualifyContracts(aapl_contract)
aapl_trade = ib.placeOrder(aapl_contract, aapl_order)

Above code snippet will create new short position on AAPL with quantity of 200 stocks sold at market price. When placing order manually, you can easily deal with insufficient funds due to high initial margin. But when trading portfolio of strategies, you need to automate the process.

There are two main solutions for dealing with changing initial margin when using the Interactive Brokers API and Python programming language.

Solution 1: Wait and Check

The first solution involves waiting after the order is executed and filled and then checking its status. If the order is cancelled due to insufficient funds, the trader can resubmit the order with a smaller size.

With ib_insync you can get access to trade log – audit trail with changes made to submitted order, where you can check for reason, why your order is being cancelled. To solve this particular problem, you can use code similar to this:

aapl_contract = Stock('AAPL', 'SMART', 'USD')
aapl_order = MarketOrder('SELL', 200)
ib.qualifyContracts(aapl_contract)
aapl_trade = ib.placeOrder(aapl_contract, aapl_order)

# wait 3 seconds for IB to process and execute your order
ib.sleep(3)

# check only if order was cancelled
if trade.orderStatus.status == 'Cancelled':
    # iterate through all log records for your order
    for log in trade.logs:
        # check for specific error code and error message
        if log.errorCode == 201 and 'MUST EXCEED THE INITIAL MARGIN'.lower() in log.message.lower():
            # resubmit order with half of the quantity
            aapl_half_order = MarketOrder('SELL', 100)
            aapl_trade = ib.placeOrder(aapl_contract, aapl_half_order)

As you can see, you can adjust logic to resubmit order with some percent of original quantity. While being very simple, such solution will be quite robust and will work very well.

Solution 2: Check for Precise Initial Margin

The second solution involves checking the precise initial margin required from the error message when the order is cancelled. The required initial margin can be extracted from the error message and used to calculate the percentage of the previous order size that can be resubmitted. This solution is more precise and can be automated using Python programming language.

YOUR ORDER IS NOT ACCEPTED. IN ORDER TO OBTAIN THE DESIRED POSITION YOUR EQUITY WITH LOAN VALUE [10000.00 USD] MUST EXCEED THE INITIAL MARGIN [20000.00 USD]

Here’s how to implement the second solution using Python and ib_insync:

order_size = 200
aapl_contract = Stock('AAPL', 'SMART', 'USD')
aapl_order = MarketOrder('SELL', order_size)
ib.qualifyContracts(aapl_contract)
aapl_trade = ib.placeOrder(aapl_contract, aapl_order)

# wait 3 seconds for IB to process and execute your order
ib.sleep(3)

# check only if order was cancelled
if trade.orderStatus.status == 'Cancelled':
# iterate through all log records for your order
for log in trade.logs:
# check for specific error code and error message
if log.errorCode == 201 and 'MUST EXCEED THE INITIAL MARGIN'.lower() in log.message.lower():
# extract values from message
match = re.findall(r'(\d+\.\d+)', log.message)
current_eq = float(match[0])
required_eq = float(match[1])
# compute required percent of previous size
percent = math.floor((value / required) * 10) / 10
# resubmit order with ajdusted quantity
aapl_half_order = MarketOrder('SELL', math.floor(order_size * percent))
aapl_trade = ib.placeOrder(aapl_contract, aapl_half_order)

Conclusion

Shorting stocks can be a challenging but potentially rewarding strategy for traders. Inexperienced traders should be aware of the difficulties of shorting and be prepared to deal with them.