Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to switch longs to shorts, vice-versa

Referring to Lecture 38 - Example: Long-Short Equity Algorithm, I tried to switch the longs to shorts and shorts to longs on lines 121 and 122, but when I ran the backtest, it didn't change anything. The resulting backtest was the same which can't be right. I basically just wanted to invert the original strategy and bet against it in the backtest just to see what happens.

I tried changing this...

longs = combined_factor.top(TOTAL_POSITIONS//2, mask=universe)  
shorts = combined_factor.bottom(TOTAL_POSITIONS//2, mask=universe)

to this...

shorts = combined_factor.top(TOTAL_POSITIONS//2, mask=universe)  
longs = combined_factor.bottom(TOTAL_POSITIONS//2, mask=universe)
6 responses

Easiest way would be to put a minus sign in front of the ‘combined_factor’. I don’t really recommend random factor flipping though, unless you have a good economic rationale for it (e.g you got the sign wrong to begin with).

Joakim Arvidsson
Easiest way would be to put a minus sign in front of the ‘combined_factor’. I don’t really recommend random factor flipping though, unless you have a good economic rationale for it (e.g you got the sign wrong to begin with).

This is what it looks like when I do that...

longs = -combined_factor.top(TOTAL_POSITIONS//2, mask=universe)  
shorts = -combined_factor.bottom(TOTAL_POSITIONS//2, mask=universe)  

If I do that, I get a TypeError.

TypeError: bad operand type for unary -: 'NumExprFilter'  
USER ALGORITHM:122, in make_pipeline  
longs = -combined_factor.top(TOTAL_POSITIONS//2, mask=universe)  

I wouldn't recommend random factor flipping either but my goal here is to understand the code and how to manipulate it. Being able to switch longs to short and vice-versa would be useful.

Sorry, not there. Here, like this:


combined_factor = -(  
        value_winsorized.zscore() +  
        quality_winsorized.zscore() +  
        sentiment_score_winsorized.zscore()  
    )

Notice the minus sign in front of the parenthesis? You could do it on the individual factors instead too if you prefer.

The top and bottom slicing is not really required, it’s only if you don’t want to score (and trade) the full universe. You wouldn’t use a minus sign there though.

Fair enough on playing around trying things to see what happens. That’sa great way to learn and how I learned a lot of this stuff.

Thanks for the help. The backtest output is definitely different but it didn't inversely mirror the original long/short strategy. The graph shapes look very little alike. I was expecting the same shape graph, just flipped inversely. Guessing the pipeline is outputting completely different trades that are not inverse to the original strategy.

Did you disable the default trading cost model? If not, that's probably why. Without the trading friction (slippage mostly) it should mirror inversely I believe.

Put the below in your Initialize function to try:

def initialize(context):  
    set_commission(commission.PerShare(cost=0.000, min_trade_cost=0))  
    set_slippage(slippage.FixedSlippage(spread=0))  

That's looks to be the reason. Thanks for that. Works now.