Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Algorithm To Isolate Common/Specific Returns

I recently coded an algorithm which (I think) can be used to isolate either a specific or common return. To isolate a common return you simply put your algorithm logic into the pipeline and let it run. To isolate a specific return you add in your entire algorithm, reverse the weights (negatives go to positive and vice versa) essentially shorting this algorithm and the outcome should be the specific returns.

It's not perfect because I:
a) Don't know exactly how Quantopian's risk model works
b) Haven't run the sector exposures of the stocks I added to mimic the factor returns, leaving some gapping.

It works in backtesting with a very simple fundamental long-short algorithm

The specific returns are caused (I believe) by the errors I outlined above.

4 responses

For example,

Common Returns Unconstrained

Common Returns Constrained

It would be better if the algorithm was good, but the constraint (when applied) does significantly impact common returns. Would be more effective if I had more knowledge regarding what Quantopian used to model the factors.

1) Can anyone say why this goes to -10 with use_norm??
That switch independently normalizes positive values 0 to 1 and negative values -1 to 0.

2) Does weight-flipping in any way offer a quick hint about specific and/or common returns?
When using order_optimal_portfolio, one can add a minus sign in front of the input weights (and notice the minus sign here)
like objective = opt.MaximizeAlpha( -pipeline_data.alpha ) thus flipping/reversing long and short. Sometimes a negative return will become positive, sometimes more negative. A positive return could go either up or down also presumably.
Here, that would look like order_target_percent(s, -weight). Related?

Intriguing code by Quant Trader to be aiming for zero returns for understanding.

With this version I'm primarily hoping it might help in testing changes and someone might share their findings.

Several things about this:
- context.use_norm = 1 # use 0 for off
- Log preview to see pipe contents, look to bottom of this code now for example. Some fundamentals floor are zero (affects norm()).
- Vertical alignment using white space (the start of the code will take an extra 1/10,000th of a second or so, otherwise no difference).
- Short variable names for readability, less typing, fast editing.
- Custom chart values somewhat atypical.
- dropna() from pipeline_output once
- Easily adjust percentile_between hi (high) and lo (low)
- Fundamentals instead of morningstar

See also: Isolating Specific Returns

Can also be used to convert an algorithm from long (or short) only to a long-short portfolio with limited side effects. See below a S&P500 long-short mimic.

I set the leverage to 2 because I found out that when the leverage was set to 1, the returns and volatility were both reduced, however, the volatility was reduced by more than the returns. That meant I could leverage up to the original volatility levels but with extra returns, essentially giving me an extra 1-2% of returns for 'free'.

Does this work better than constraining the style risks via the optimizer? Or does it do something fundamentally different? (I guess you can also update the code now that the whitepaper is released which gives exact specifications of the different styles)