How to enable Python API in Interactive Brokers Trader Workstation

You’re going to want to open Interactive Brokers trader workstation as normal. Then head over to edit>Global Configuration and check that first box.

Enable ActiveX and Socket Clients

The next thing you’ll want to download is the API’s found here – http://interactivebrokers.github.io/

Next, open the command prompt as an administrator and switch directories where you installed it.

d:

D:\TWS API\source\pythonclient

python setup.py install

You will see this if it is completed successfully

Installed c:\program files\python39\lib\site-packages\ibapi-9.76.1-py3.9.egg
Processing dependencies for ibapi==9.76.1
Finished processing dependencies for ibapi==9.76.1

D:\TWS API\source\pythonclient>

I use IntelliJ to to test I’m going to run a simple command

import ibapi

You can see it ran successfully.

So let’s test a little further.

I’m going to run the following Python code

import ibapi

from ibapi.client import EClient
from ibapi.wrapper import EWrapper

class IBapi(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)

app = IBapi()
app.connect('172.16.105.5', 7496, 123)


app.run()

import time
time.sleep(2)
app.disconnect()

This shows it was successful

“C:\Program Files\Python39\python.exe” “G:/My Drive/IdeaProjects/interactive_brokers/interactive_brokers_api_test.py”
ERROR -1 2104 Market data farm connection is OK:usfarm.nj
ERROR -1 2104 Market data farm connection is OK:cashfarm
ERROR -1 2104 Market data farm connection is OK:usfarm
ERROR -1 2106 HMDS data farm connection is OK:euhmds
ERROR -1 2106 HMDS data farm connection is OK:fundfarm
ERROR -1 2106 HMDS data farm connection is OK:ushmds
ERROR -1 2158 Sec-def data farm connection is OK:secdefnj

Shorting stocks to protect your home’s equity without selling your house

Almost everyone in the United States who owns a home has seen a massive increase in the equity of their house. In the Phoenix metro area, we have seen massive gains. For instance, a house I purchased 1-year ago has seen around a 50% increase in value. Today I was discussing with a friend his options for protecting his equity shorting housing stocks rather than selling his primary residence.

Let’s take a look at the Case-Shiller and Zillow estimates for our local Phoenix market.

Phoenix has been one of the hottest real estate markets over the last few years. In addition, we have seen a huge population increase in Arizona.


2020 Population
2010 Population+ People+ %
Utah3,271,6162,763,885507,73118.4
Idaho1,839,1061,567,582271,52417.3
Texas29,145,50525,145,5613,999,94415.9
North Dakota779,094672,591106,50315.8
Nevada3,104,6142,700,551404,06315
Colorado5,773,7145,029,196744,51814.8
District of Columbia689,545601,72387,82214.6
Florida21,538,18718,801,3102,736,87714.6
Washington7,705,2816,724,540980,74114.6
Arizona7,151,5026,392,017759,48511.9
South Carolina5,118,4254,625,364493,06110.7
Georgia10,711,9089,687,6531,024,25510.6
Oregon4,237,2563,831,074406,18210.6
Delaware989,948897,93492,01410.2

But are the prices high? I was to pause for a minute and look at the median sales price overlayed on the median family income. You have to normalize the price of a house. Given that people by houses a fair way to normalize this variable is to make it relative to the income of the average family or individual if you prefer.

You can also use the median sales price of a house as the numerator and income as the denominator. This creates an oscillating indicator in which the peaks show the high price values and the troughs show the low price values. Do keep in mind this does not adjust for interest rates. This is a much more complicated formula so I won’t dive into this deeper. However, I do believe it is extremely relevant. Also, the data available on FRED isn’t updated to 2021 yet. So all of the appreciation we’ve seen in the last year or so is not accounted for in these charts. If I had to guess house prices are up significantly more than income.

I have heard a lot of people talk about selling their houses to capture the appreciation value because they “know” the prices will come back down. This is an extremely speculative comment but it’s one that I’ll entertain. In fact, I’ve thought about selling some houses as well. But know speculating with your primary residence is risky.

I want to outline some basic math here for anyone who is thinking about selling their house and the true cost. Let’s say you have a house that is now worth $400,000. You paid $200,000 initially. You now have $200,000 in equity. However, if you were to sell this house you’re looking at commissions for the buying and selling realtor plus miscellaneous fees. Let’s call this a total of 8%. It’s going to cost you roughly $32,000 to sell this house. Not to mention all of the other stress that comes with moving. This $32,000 represents almost 16% of your equity. You’re going to lose 16% of your equity if you make this move.

So let’s talk about creating a market-neutral position by short-selling residential construction companies in Arizona. I did not perform a comprehensive search for residential construction companies in Arizona. But I was able to pull up 6 publicly traded residential construction companies pretty quickly. Below you will find their 1-year return charts.

So let’s say you want to protect your $200,000 in equity as in the previous example. You could open short-sale positions in all of these stocks evenly. I highly recommended against short-selling one stock with any large percentage of your portfolio. The reason why is the downside loss is infinite. Short selling is extremely dangerous. The math for short selling these stocks is evenly balanced is pretty straightforward. You take your $200,000 and divide it by 6. This gives you $33,333 that you will want to short of each stock. You then take this number and divide it by the share price of the stock. In the last example, Toll Brothers is selling for $63.48. This would mean you would sell short 525 shares of this stock. So what we have essentially done here is make the determination that these stocks represent the value of house prices or that they have a high cointegration to house prices. Therefore if the housing prices go up you will lose money on your short and gain money on your house’s equity. Likewise, if housing prices go down you will make money on your short and lose money on your house’s equity. This puts you in what is called a market-neutral position. This occurs when you find highly cointegrated assets and place trades in opposite directions. Inherently you are long your house because you own it. So what we’re trying to accomplish is a short position equal to the value of your house. Or equal to the value of your equity.

There are also a few other techniques that I will not go into in this article.

  • I would recommend analyzing the short interest ratio of the stocks you plan on shorting. This is the open short interest divided by the daily trading volume.
  • Resaearch out of the money put options. This is a much cheaper way to make this bet with a limited downside risk. But options require a lot of effort and research.
  • Instead of short selling specific stocks you can short sell the entire residential construction industry through different ETFs like XHB, HOMZ, or PKB.