Build your own portfolio downside protection strategy in Python

If you can’t beat the market at least you can protect from it using Python

With lower transaction costs CPPI and TIPP strategies among the simplest ways to implement downside protection algorithms for your portfolio. Here’s a simple guide in Python.

Radu Nedelcu
The Startup

--

One of the most straightforward strategies to apply when building a portfolio is diversification. Diversification allows you to enjoy a portfolio that gives you back maximum reward for the risks you take.

You can increase exposure to different regions (North-America, Europe, Asia, etc.) or different sectors (industrials, pharmaceutical, financial, etc.) or different currencies or invest in different asset classes(equity, fixed income, alternative investments, cash instruments).

Diversification works well on average in most market condition but it fails for example in extreme bear markets like the one induced by the COVID-19 lockdown.

Photo by Natalie Rhea Riggs on Unsplash

Welcome CPPI

Black and Jones have introduced constant proportion portfolio investment (CPPI) in 1987. It allows you to dynamically allocate assets to get performance by exposing yourself to upside while protecting against downside. It’s a strategy that works well without having to introduce derivatives like options in your portfolio.

Photo by Alex Block on Unsplash

How does it work?

You will allocate a portion of your portfolio to the risky assets in multiple M the difference between your assets value (AV) and a given floor (F).

http://www.deltaquants.com/Introduction-to-risks-in-CPPI-products

For example given a multiplier M=4 and assuming your current asset value (AV) is at 100%, and you want to keep its value at a minimum 85% (floor F=85%). You can invest 4x($100-$85)=4x$15=$60 into your risky assets and the rest of $40 to safe assets.

Not a silver bullet..

Implement this carefully and trade frequently and you can almost guarantee you will never go below the floor. Trade rarely, once a quarter, and there might be cases when the losses you make in the risky portfolio between two trade dates can bring your portfolio value below the floor. This will happen if the losses in your risky portfolio compared to your safe assets are higher than 1 over the multiplier M.

For example, if your multiplier M is 4,1/4=0.25, so if in a quarter your losses in the risky portfolio are higher than 25% in relation to your safe assets you will break the floor.

How to do it in Python?

I will show you the CPPI strategy using the S&P 500 Index data. This is the index data source, and it also contains more details about the columns used.

Let’s import the S&P 500 Index data.

Retrieving, indexing and calculating monthly returns for S&P 500 Index data

Next, we need to define what is a safe asset. It could be a coupon bearing bond, zero coupon bond or a high yield savings account, but for reference (and because we can find this data easier) we can take the 3 month U.S. Treasury Bill which on average is 4.29% whereas at the time I’m writing this it’s 0.13%. For exercise purposes I will pick 2% or 0.02 which is a sensible value.

I will also pick $100 as my portfolio starting value and a floor of 70% — so will not accept my portfolio to drop below 70% of its value. Will pick the multiplier m to be 6 (multiplier values between 3 and 8 are very common).

The CPPI algorithm has 5 main steps

  1. Calculate the cushion value: C=CPPI - F
  2. Calculate the allocation to the risky asset: E=C x m
  3. Calculate the allocation in the safe asset which is the remainder: B=CPPI-E
  4. Compute the growth of your risky and safe allocations:

CPPI = E*(1 + risky_price_change)+ B*(1 + safe_asset_return)

*safe_asset_return can be a coupon or the price change of a zero-coupon bond

Please excuse the formatting. It’s done on purpose to reflect the steps aforementioned.

Run it and plot the results.

As you can see in the chart below, the CPPI strategy can protect against downside. The 2002 dot-com bubble and the 2008 subprime loans market crashes the portfolio value preserved above the floor (70% of the initial value in this case).

Looking at the weight allocations relative to the portfolio value, we can see how the allocation to risk assets dropped while the allocation to safe assets increased during market downturns.

Risk vs Safe allocation

One drawback is that once your portfolio value reaches new heights (after 2013), the chance of falling and breaching the initially set floor value reduces — there is almost no chance it can breach it in the near term. As a consequence, you would end up fully invested in the risky assets.

In one scenario you might be thinking if setting the floor at $70 when the portfolio value is above $150 or more, is a sensible thing. It would make more sense to update the floor along with our portfolio value.

Welcome TIPP

Estep and Kritzman developed TIPP (Time — Invariant Protection Portfolio) with origins in CPPI, in1988. Essentially with TIPP we update the floor relative to the previous maximum portfolio value and not the initial value of the portfolio.

We will also update the code so that we can adjust the rebalancing periods. Remember the wider the gap between trade points the higher the probability to break the floor.

Please excuse the formatting. It’s done on purpose to reflect the steps aforementioned.

This time we will run it for data from 1990 and we will set the constant multiplier M=4.

As expected, floor value increases and always sits at 70% level from the maximum previous portfolio value. So for example, if in 2008 just before market downturn my portfolio value was roughly $500, the floor will be set at $350 level until 2014 when we have a new peak and hence a new floor value.

Conclusion

CPPI and TIPP are basic downside insurance algorithms. They can be used like a structured product where you can consider an index or ETF as your risky asset, bond, savings account or even cash as your safe asset.

Both CPPI and TIPP are limited by the transaction costs that occur when rebalancing frequently, however nowadays, trading applications charge almost nothing for transactions so these strategies become more practical.

Feel free to play around with the multiplier and test this algorithm on bigger time frames and various products other than S&P 500 Index or the default 2% safe asset return.

--

--

Radu Nedelcu
The Startup

Financial Systems Developer and Technical Writer