Stock selection with the mad CAPM and liquidity filtering

This entry is part of 23 in the series Numpy Strategies

Numpy Strategies 0.0.5

The Capital Asset Pricing Model ( CAPM ) usually uses variance or standard deviation as a risk metric. I invented a slight modification of the model, which I call the mad CAPM ( well OK maybe I did not invent it, but I don’t know if anyone else did ). M.A.D here is median absolute deviation not to be confused with mutual assured destruction or the other thing. It should be a more robust statistical measure of risk. The liquidity filter works almost the same way as the CAPM, but instead of trying to maximize expected return, the goal is to maximize liquidity. Liquidity is after all a good thing. So the plan for today is:

  1. Screen stocks with the mad CAPM.
  2. Screen stocks with the liquidity filter.
  3. Profit!

Kurn’s revenge Part Deux

Kurn had a nice Thanksgiving dinner at his friend’s house. Thanksgiving dinner is a traditional Klingon dinner at the end of the work week. Traditionally, Klingons would then say thanks about all the good things that happened during the week. They had a wonderful time feasting on gagh, bloodwine and Rokeg blood pie.

Kurn told his friends about his Numpy portfolio 4. For some reason his strategy had picked mostly energy ETF’s, probably those had the most normal returns. Kurn’s portfolio was still slightly in the black. He explained his trailing stops setup as well.

Kurn closed two of his positions and adjusted a few of his stops. On his way home Kurn got shot at by two hired killers on gliders. Kurn saw it coming and he always made sure he got good cover anyway. Kurn dispatched one of the assasins to Stovokor and let the other fly away with a phaser wound to the chest.

The mad CAPM

The median absolute deviation of a sample is calculated by:

  1. Finding the median.
  2. Subtracting the median in the previous step from each element.
  3. Taking the absolute value of each difference.
  4. Determining the median of the absolute values.

The code.

1
2
3
4
def mad( arr ):
   mdn = median( arr )
 
   return median( abs( arr - mdn ) )

and unit test.

1
2
3
4
...
      actual = mad( [ 1, 1, 2, 2, 4, 6, 9 ] )
      assert actual == 1, 'incorrect mad' + str(actual)
...

Liquidity filter

The liquidity is estimated by the geometric mean of the daily volumes. So I apply the mad CAPM with this liquidity figure instead of the expected returns.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
...
   ev = geomean( returns[ 0 ] )
 
   evs.append( ev )
 
   madC = mad( returns[ 0 ] )
   mads.append( madC )
 
   liq = geomean( v ) 
   liqs.append( liq )
 
   beforeLastReturn = c[ len( c ) - 2 ] / c[ len( c ) - 3 ] - 1
 
   if beforeLastReturn > 0:
      continue
 
   t = file.replace('.csv', ''), ev, madC, liq
   records.append( t )
 
 
( a,b,residuals ) = fitline( mads, evs )
( aLiq, bLiq, residuals ) = fitline( mads, liqs )
 
for t in records:
   symbol, evC, madC, liq = t
 
   if evC > a * madC + b:
      if liq > aLiq * madC + bLiq:
...

A Vulcan portfolio ( a story in past future probable parallel tense )

Challenge your preconceptions, or they will challenge you.

Many, many centuries into the future, in a galaxy far away, in a parallel universe lived or would live the Vulcan Sarek. Sarek used to be or will be a member of the Vulcan High Command. Due to a disease, which caused or will cause Sarek to lose control of his emotions, he was/will be relieved of his duties.

Sarek in his unstable emotional state decided/will decide to gamble it all on the intergalactic stock markets. An archaelogist told/will tell Sarek about an ancient Earth science fiction blog, that had some interesting investing ideas. Made impatient by his disease Sarek just applied those ideas. Most of it revolved around an ancient model. This model was/will be already replaced many centuries ago/into the future by the Hyper Dimensionsional Super String Model, but Sarek did/will not care. The model also had/will have a liquidity constraint. However such a constraint was/will be completely unnecessary since the intergalactic stock market has/had/will have zillions participants. And so Numpy portfolio 5 was born.

numpy5

Sarek also did some effort to setup weekly manual trailing stops.

Live long and prosper.

THE END

Random links of interest

If you liked this post and are interested in NumPy check out NumPy Beginner’s Guide by yours truly.

Series NavigationSecret Transitions of a Markov Chain
By the author of NumPy Beginner's Guide, NumPy Cookbook and Instant Pygame. If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
Share
This entry was posted in programming and tagged , , . Bookmark the permalink.