## Easy Mean/Median Quantile Creation (Python Code)

I’ve been teaching myself Python, and I’ve had a couple of successes recently where I took a concept or “need” and coded it to completion. Yay, me!

One thing I find myself doing a lot is sorting a batch of data by one column in Excel/Google Sheets, then dividing up another column into quantiles and averaging each quantile for later graphing. This a research technique I use a lot with financial data.

## Peak Crashes: Are They a Shortable Opportunity?

In hindsight, it’s fun to look at stocks that have had a huge surge, only to collapse violently after they peak. And by “fun” I mean sitting on the sidelines watching, as opposed to pulling one’s hair out when you’re long that particular trade.

Above you can see Apple (AAPL) in 2008, where it hit its 52-week high, only to collapse a few days later, for a very significant loss.

Are these moments a shorting opportunity? I aim to find out.

So first, I’ll pick some somewhat arbitrary definitions of a ‘peak crash’ event.

• stock universe is the historical constituents of the Russell 3000 index.

• liquidity filter to eliminate the weird stuff: historical price >\$10, with a median 19-day volume > 100,000 shares.

• The stock must have recently hit its 52-week highest close. I’ve defined this as the highest close of the last 10 trading days is equal to the highest close of the last 250 trading days. Which is close enough to being a year’s worth of trading days.

• The trigger is a drop of 8% or more from one close to the next, within that 10-day period of the highest close being reached.

• I then look at the forward gain/loss for the following 5, 10, 20 and 40 trading days.

• the period explored is 1/1/2000 through 12/22/2015. The end date represents the last ‘peak crash’ event that has the full 40 days of forward data to evaluate as of this writing.

Let’s take a look at the average gain or loss (expressed as a percentage) for each forward-looking period:

Well now, that’s just boring as hell, isn’t it?

A slight mean-reversion tendency for the short term, and a slight downward continuation for the longer terms. The longer the holding period, the deeper the average loss, but I don’t know if these values are statistically significant.

Perhaps these peak-crashes are highly dependent on the overall market health to be a successful short? Let’s take a look at the yearly gain/loss averages for each forward looking period.

Clearly there are some variations over the years!

The most striking takeaway from this chart is that if the year ends in “3”, go long. If the year ends in “1” or “8”, go short.

No, I’m kidding. That’s stupid. But what’s NOT stupid is taking a look at how the peak crashes behave under varying market conditions.

I’ve added another statistic to our data set: the percentage the S&P500 is below its highest close over the last year (ok, 250 trading days). So if the market is closing at a 250-day high, that percentage would be 0. From a code standpoint, I look at the peak-crash trigger, take the highest close of the S&P500 over the last 250 days, and divide it by the current close of the S&P 500. As of the close of 01/28/16, the S&P 500 is 12.9% under its 250-day closing high. Make sense?

This is a simple way to show if the market is doing well or not. There are of course other ways, such as my breadth diffusion indicator discussed a while back. But I wanted to keep this simple.

Here is a chart of all the peak-crash events, sorted by the S&P-percent-under-250-day-highest-close value. Then divided up into vigintiles. I’ve put the the ranges of some select vigintiles so you can get a sense of the values they encompass.

You’ll note that there are many more instances where these peak-crash events happen and the market is very close to its 250-day closing high. In fact, about half of nearly 10,000 events occur when the market is 0-3.5% below the 250-day highest close.

From a shorting standpoint, the action doesn’t really start happening until the market is about 5.9% below its yearly closing high. And while the shorter periods show a pesky tendency to bounce back after such a drop over most market conditions, the 40-day forward period looks promising. Let’s look at that more closely.

There might be a shorting system here. When the market is 6% down or more, these peak-crashes on average end up in a further two-month (40 day) loss. I’ll leave that to the reader to actually refine into a workable system.

And when markets are booming—remember those days?—perhaps a good shorting system might be found in the 10-day forward period (see below). The average loss when the market is less than 2.9% below the 250-day highest close is much smaller, but it’s pretty consistent. This might be worth checking out for when times are good again.

And because charts can be a little boring, here’s a mountain sunset photo, taken on my phone.

## Breadth Diffusion Predicts a Bounce?

Recently I posted a number of articles on various breadth diffusion indicators and their relative effectiveness in predicting the health of the S&P 500. The big winner was the system that compared the number of stocks in the historical constituents of the Russell 3000 that were up 30% or more over the last quarter (60 trading days) vs those were 30% or down over the same period. You can read the whole series here.

The breadth diffusion value is computed as (up30 / (up30 + down30))*100. Suffice it to say, we have been seeing very low numbers recently. The current value is 8.2. So I thought I’d go back and see what those low numbers have foretold in the past.

Looking at the period of 1/1/2000 through 12/31/2015, we have had only twenty times that the diffusion value has dipped below 10. Below you can see all 20 of them, with the diffusion value, plus the 5, 10 and 20 day gain/loss for each instance:

The average 5-day gain/loss of the S&P 500 is 2.9365%, while the average 10-day G/L is 5.4655% and the 20-day G/L average is 6.3975%. The 5-day period following such a low diffusion number has been positive 75% of the time, the 10-day period has been positive 90% of the time, and the 20-day period 80% of the time.

All that to say a bounce seems likely.

## RSI(2)<25 X6!

Today marks the sixth day in the row that the S&P 500’s RSI(2) value was under 25. Since Jan 1 2000, this has happened only 15 times prior to today. So I thought it would be fun to see what the forward return has been after these events. Here’s a handy spreadsheet. I calculated the forward return from the following day’s open to the close of either the 5th or 20th day. I.e. if you were trading based on today’s performance, you’d buy next Monday at open, and exit on Friday’s close (for the 5 day version). I haven’t included those pesky commissions in these numbers.

Note that only five of the 5-day periods were negative, and all but one turned positive over the 20-day period.

Should you buy on Monday? Is 15 data points enough? Is there an Easter Bunny? I can only answer two of those questions.

Update 02/08/16: looks like this was one of those “other” times. As of this writing, the S&P 500 is down over 3% from when this article was written. Ah well.