Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Algorithmic Short Selling with Python

You're reading from  Algorithmic Short Selling with Python

Product type Book
Published in Sep 2021
Publisher Packt
ISBN-13 9781801815192
Pages 376 pages
Edition 1st Edition
Languages
Author (1):
Laurent Bernut Laurent Bernut
Profile icon Laurent Bernut

Table of Contents (17) Chapters

Preface The Stock Market Game 10 Classic Myths About Short Selling Take a Walk on the Wild Short Side Long/Short Methodologies: Absolute and Relative Regime Definition The Trading Edge is a Number, and Here is the Formula Improve Your Trading Edge Position Sizing: Money is Made in the Money Management Module Risk is a Number Refining the Investment Universe The Long/Short Toolbox Signals and Execution Portfolio Management System Other Books You May Enjoy
Index
Appendix: Stock Screening

Regime Definition

During the Napoleonic wars, field surgeons with limited resources had to make quick decisions as to whom would need surgery, who could survive without, and the unfortunate ones for whom nothing could be done. Triage was born out of necessity to allocate limited time and resources in the most efficient and humane way possible. In the stock market, regime is another word for triage. Some are bullish, some are bearish, and some are inconclusive.

Markets tend to "stay wrong" a lot longer than investors tend to stick with you. Segregating stocks into different regime buckets—triaging them—before performing in-depth analysis is an efficient allocation of resources. The objective of this initial triage is not to predict where stocks could, would, or should be headed, but to practice the long-lost art of actively listening to what the market has to say.

Some market participants like to spend time and resources on building bear theses for stocks...

Importing libraries

For this chapter and the rest of the book, we will be working with the pandas, numpy, yfinance, and matplotlib libraries. We will also be working with find_peaks from the ScientificPython library.

So, please remember to import them first:

# Import Libraries
import pandas as pd
import numpy as np
import yfinance as yf
%matplotlib inline
import matplotlib.pyplot as plt
from scipy.signal import find_peaks

Creating a charting function

Before we visually compare various regime methods, let's publish the source code for a colorful charting function called graph_regime_combo. The parameters will gradually make sense as we unveil each method.

The code is as digestible as Japanese mochi rice, a common cause of death by asphyxiation for toddlers, elderly people, and foreigners, like the author, in Japan. The structure is however simple, like the author as well. Everything depends on whether the floor/ceiling method is instantiated in the rg variable, or not. If floor/ceiling is present, then it supersedes everything else. If not, the other two methods (breakout and moving average crossover) are printed. The ax1.fill_between method identifies the boundaries. Read all of them to understand the conditions. The rest is uneventful:

#### Graph Regimes ####
def graph_regime_combo(ticker,df,_c,rg,lo,hi,slo,shi,clg,flr,rg_ch,                       ma_st,ma_mt,ma_lt,lt_lo,lt_hi,st_lo,st_hi...

Breakout/breakdown

"Kites rise highest against the wind—not with it."

– Winston Churchill

This is the oldest and simplest trend-following method. It works for both bull and bear markets. If the price makes a new high over x number of periods, the regime is bullish. If the price makes a fresh low over x number of periods, the regime is bearish. This method is computationally easy to implement.

Popular durations are 252 trading days (which works out as 52 weeks), and 100 and 50 trading days. Below, here is a simple rendition of this regime methodology:

def regime_breakout(df,_h,_l,window):
    hl =  np.where(df[_h] == df[_h].rolling(window).max(),1,
                                np.where(df[_l] == df[_l].                                    rolling(window).min(), -1,np.nan))
    roll_hl = pd.Series(index= df.index, data= hl).fillna(method= 'ffill')
    return roll_hl
 
ticker = '9984.T' # Softbank ticker...

Moving average crossover

Moving averages are another popular regime definition method. This method is so simple and prevalent that even the most hardcore fundamental analysts who claim never to look at charts still like to have a 200-day simple moving average. This method is also computationally easy. There may be further refinements as to the type of moving averages from simple to exponential, triangular, adaptive. Yet, the principle is the same. When the faster moving average is above the slower one, the regime is bullish. When it is below the slower one, the regime is bearish. The code below shows how to calculate the regime with two moving averages using simple and exponential moving averages (SMA and EMA respectively):

#### Regime SMA EMA ####
def regime_sma(df,_c,st,lt):
    '''
    bull +1: sma_st >= sma_lt , bear -1: sma_st <= sma_lt
    '''
    sma_lt = df[_c].rolling(lt).mean()
    sma_st = df[_c].rolling(st).mean()
    rg_sma...

Higher highs/higher lows

This is another popular method. Stocks that trend up make higher highs and higher lows. Conversely, stocks trending down make lower lows and lower highs in that order, and therefore suggest sustained weakness. This method makes intuitive sense. Unfortunately, it is not statistically as robust as it looks. Markets sometimes print lower/higher lows/highs that throw calculations off, before resuming their journey. Secondly, this method requires three conditions to be simultaneously met:

  1. A lower low.
  2. A lower high.
  3. Both lower low and lower high conditions must be met sequentially, which only works in orderly markets.

Those three conditions have to be met consecutively in that precise order for the regime to turn bearish. Markets are random and noisier than people generally assume.

The main advantage of this method are entries and exits. On the long side, buy long on a low and exit on a high. On the short side, sell short on a...

The floor/ceiling method

That method is originally a variation on the higher high/higher low method. Everyone has intuitively used it and yet it is so obvious that no-one has apparently bothered to formalise it. Unlike the higher high/higher low method, only one of the two following conditions has to be fulfilled for the regime to change:

  1. Bearish: A swing high has to be materially lower than the peak.
  2. Bullish: A swing low has to be materially higher than the bottom.

The swings do not even have to be consecutive for the regime to change. For example, markets sometimes shoot up, then retreat and print sideways swings for a while. Those periods are known as consolidation. The regime does not turn bearish until one swing high is markedly below the peak.

The classic definition is always valid regardless of the time frame and the asset class. Lows will be materially higher than the bottom in a bull market. Conversely, highs will be materially lower than...

Methodology comparison

"Learning to choose is hard. Learning to choose well is harder. And learning to choose well in a world of unlimited possibilities is harder still."

– Barry Schwartz on the paradox of choice

In 2004, Barry Schwartz rocked the world with something we have always intuitively felt. The more choices we have, the more stress we experience. We have outlined a few methods. Let's compare them graphically and hope the winner will visually stand out.

Firstly, let's print the floor/ceiling alone. The small dots are level 1. The big dots are level 2. The black triangle is the floor. The shade is the length of the regime. It starts from the first swing low and goes all the way to the right. Even the pandemic "soft patch" did not put a dent in it. This is as stable as it possibly can be:

Figure 5.22: SPY floor/ceiling bullish regime forever

This could come across as unresponsive to market gyrations...

Let the market regime dictate the best strategy

"When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth."

– Sir Arthur Conan Doyle

Over the years, I have come to believe that the two primary determinants of performance are position sizing and market regime. Trade too big and you could be out of business. Trade too small and you do not have a business. Secondly, seasoned market participants usually have several strategies to cope with different market types.

The difficulty is which strategy to use when and more importantly when to fade them. This comes down to regime definition. The floor/ceiling method could potentially change the way you trade markets.

There are two types of strategy: mean reversion and trend following. Mean reversion works best in range-bound markets. The price oscillates around a mean in a semi-predictable fashion. Mean reversion strategies perform poorly in...

Summary

We have examined a few regime methodologies that will help you pick up on signals that a market is going up or down. Regime breakouts and moving average crossovers are staples in the arsenal of trend-following traders. Duration is as much a function of style as what the market happens to reward. Then, we introduced the floor/ceiling methodology. This regime definition method works on absolute and relative series. It is symmetrical and above all more stable than any other methodology. It therefore supersedes everything else.

However, regime definition methodologies are not mutually exclusive. For example, the floor/ceiling method could be used to determine the direction of trades, long or short. Then, regime breakout could be used to enter after consolidation or sideways markets. Finally, the moving average crossover could be used to exit positions.

Having a signal is one thing. Turning it into a profitable strategy with a robust statistical edge is another. There is...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Algorithmic Short Selling with Python
Published in: Sep 2021 Publisher: Packt ISBN-13: 9781801815192
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}