Identifying Long/Short Opportunities With Python

Thiago Thaylor
2 min readOct 15, 2020

The financial market is full of different kinds of assets. It is a very difficult task to predict if any of them is going to increase or decrease in price. One way to get around this difficulty is through non-directional strategies, like Long /Short. That way it is possible to make money trading assets, regardless of the market’s direction.

The Long/Short Strategy

The basic idea behind this strategy is to take two assets that are linked to each other in a way that when one goes up in price you expect that the other goes up as well. These prices in general don’t move together in a perfect way. For example, if ALPHA and BETA are two highly correlated stocks and an investor decide to sell a great amount of stocks ALPHA, the price of ALPHA can go down faster and deeper than BETA, this delay in the price movement of these stocks can be a good opportunity to go long ALPHA and short in BETA.

The Costs

Depending on the asset you are trading, the cost can vary a lot, especially due to the short side. You must consider that some assets need to be borrowed in order to be sold, and some considered as “hard to borrow” assets can have a very high cost and make your long/short strategy impracticable.

The Risks

A good long/short system is very assertive and can bring you great money most of the time. However, you must consider the risk of detachment between the assets, that is very dangerous, and only one occurrence can bring you big losses. So you must be prepared to cut your losses as soon as you realize the trade is not going well.

Due to the risk of detachment, it is a good practice to avoid assets like penny stocks or stocks with very low liquidity in which the price can go crazy from one day to another.

The code

There are different ways of modeling a long/short indicator. Here I make a python code available for you, that generates a chart that shows the percentage difference between any two assets at the yahoo database.

After running the code you should see the following chart:

The relative spread between ITSA4.SA and ITUB3.SA

The mean value (in green) will be always zero, and the last value (in orange) represents the deviation from the average (the zero). In this example, the price of ‘ITSA4.SA’ is 5.8% (deviation is 0.058) more expensive than ‘ITUB3.SA’ considering the historical deviation data.

Your task now is to find eligible related assets where their prices are historically contained in a range. The idea is to sell the most expensive and buy the cheapest. Good luck!

Disclaimer

The assets shown in this article are just examples and must not be considered as some kind of recommendation.

--

--