Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Short Only Algo

I'm trying to create a short only algo. However, it simply isn't conducting any trades. Recreated the problem with the attached algorithm. When MAX_SHORT_SIZE = 0 and MAX_LONG_SIZE = 0.05, I get a long-only algorithm that trades correctly.

However, when I make MAX_SHORT_SIZE = -0.05 and MAX_LONG_SIZE = 0, it doesn't conduct any trading. It still runs without errors. If anyone can help I'd really appreciate it.

5 responses

You probably know all this but in .rank(), all values fed to MaximizeAlpha are positive, just needs some negative for shorting. One route:

'testingFactor': testingFactor.demean()

... to shift the middle of ranks to 0. Or like this backtest.

Another thing you can do to understand this is, in your version with : MAX_SHORT_SIZE = -0.05 and MAX_LONG_SIZE = 0
... place a minus sign in front of the input to opt. That makes all of the ranks negative and you'll see trades.

opt.MaximizeAlpha( -context.output['testingFactor'] )

In multiplying them all by -1, the highest positive becomes the lowest negative. Instead they can all be shifted downward to negative to see what happens like this. So if the top rank is 2063, it would become 0 actually. Rank 2062 would become -1, 2061 becomes -2 etc. The -= operation is shorthand for context.output.testingFactor = context.output.testingFactor - context.output.testingFactor.max()

def rebalance(context, data):  
    context.output.testingFactor -= context.output.testingFactor.max()  

I added log_data(), helpful to see what pipeline is delivering, and becomes more elaborate when more than one column

Thanks for the help. In the actual algo where this is an issue I ended up doing the following which worked:

context.output['testingFactor'] = - context.output['factorToBeTested'].rank(method='average', ascending = True)  

Strange though, I've built algos that have ranks ranging from 1 (lowest, shorts) to 3000 (highest, longs). These algos still work as they are intended, and short as need be without any negative ranks.

I also didn’t think the Optimizer needed negative numbers to short...?

Same. If there wasn't a change in MaximizeAlpha maybe I have a false memory. I'm ok with its behavior if as intended.

I see, that's the case adding opt.DollarNeutral(), can be all positive and still do shorting. Goes all-in up to PositionConcentration highs long, lows short until it runs out of MaxGrossExposure room even if all are positive or negative, the almost-automatic-demean we were thinking of. I'm talking about the case of plus/minus values for PositionConcentration. With one of them at 0, that would be 1 or 0 stocks, it stops. DollarNeutral rules.