Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
More fun with zScore. Ranked, sliced and served lukewarm

It appears that the use of a composite index, from whatever source -- in this case the zScore, of a group of ETFs, can indicate, in general, when it's time to be in or out of the market. Taken as a general indicator, the zMean in the algo seems fairly useful for risk on / risk off type determination.

Boosting the leverage boosts the returns as well as boosting the drawdowns, effectively doubling the volatility, as would be expected. Setting Leverage to 2.0 also boost the Sharpe though; I wonder why?

The use of an entry unit is also a point worth noting. The zScore ranked securities are purchased in linear ratio fashion with the highest percent given to the highest zScored security. The entry unit ensures that maximum leverage is never exceeded.
As an aside, adding back in pandas series items, as determined from open positions, took me forever to figure out. I call-out this code as it was a personal triumph (limited though it may be). It allows the open positions to join the best zScores as part of the next entry loop.

My dislike for Python is slowly ebbing...

zScores0 = zScores[topCount:] # best  
zScoresX = zScores[:topCount] # worst  
--- snip ---  
openPositions = [stock for stock in context.portfolio.positions if context.portfolio.positions[stock].amount != 0.0]  
lowZExisting  = zScoresX[[stock in openPositions for stock in zScoresX.index]]  
zScores0  = lowZExisting.append(zScores0)  

[You'll have to excuse the bloated UpdateState method. I neglected to trim that down from another strategy. Apologies.]

4 responses

Setting Leverage to 2.0 also boost the Sharpe though

Probably because Quantopian subtracts the risk-free rate from theirs? Numerator will grow faster than denominator.

My dislike for Python is slowly ebbing...

Not me! I rely so heavily on strong typing as I get older, I can't keep all the balls in the air anymore!

Strongly typed, I hear ya. Maybe someone will write a TypeScript like version for Python. TypeScript, in case you're unaware, is a Microsoft wrapper/precompiler around Javascript. Created by the dude who created Delphi, Turbo Pascal and C#!

Maybe that's why python is so inaccessible; no easy way to infer object type, property type, method return type, etc. I mean, who wouldn't miss Visual Studio?

(Inaccessible, not to mention the tendency for many to write obscure, cryptic code... not that any here would ever do so.)

played around a bit to make it minute test compatible. I played with the parameters and I found that ZScoreMinToFlatten = -0.5 works best. Why do you choose -0.2? Just Curious

this one I mean