Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to conditionally Long/Short securities in IDE using Optimize API

Hi,
I have been trying to test an algorithm based on Fundamental and Techincal factors. The algorithm is structured something like this:

Fundamental Factors: used for ranking stocks
Technical Factors: used for deciding when to take a position

Till now I have been using Techincal Factors as a Screen in my pipeline and I forced my algorithm to go 'Long Only' by using LongOnly constraint in Optimize API. For example:

combined_factor = e.zscore() + r.zscore()

return Pipeline(  
        columns={  
             'combined_factor': combined_factor},  
        screen= QTradableStocksUS() & combined_factor.notnull() & sector.notnull()  
        & screen2 & screen3 & screen4  
    )  

where:

screen2= USEquityPricing.close.latest >SMA_50  
screen3= SMA_50 > SMA_150  
screen4= SMA_150 > SMA_200  

Now I want to logic for going SHORT. But for that, I want to pick the bottom quantile as per factor ranking and use different Technical Indicators. Example of conditions to go short:

bottom_scores= combined_factor.bottom(200)

 screen2= USEquityPricing.close.latest <SMA_50  
 screen3= SMA_50 < SMA_150  
 screen4= SMA_150 < SMA_200

return Pipeline(  
        columns={  
             'combined_factor': combined_factor},  
        screen= QTradableStocksUS() & combined_factor.notnull() & sector.notnull()  
        & bottom_scores & screen2 & screen3 & screen4  
    )  

I have not been able to find a solution as to what I can do to implement both Long and Short logic while using the Optimize API.

To put it differently- I want to apply conditions so that my algo knows which stocks to go long and which ones to go short and Optimize the portfolio.

Thanks in advance!

2 responses

First off, when using order_optimal_portfolio there are two objectives one can use. Either maximize total alpha based upon a series of individual alphas or specify weights based upon a series of target weights. They both take a series of securities as an input and they both will try to long any securities with positive values and short those with negative values.
Here's a general approach:

  1. set columns in the pipeline for securities one would like to long and short. These are based upon filters and can be different for the longs and the shorts.
  2. set columns in the pipeline indicating the 'alpha' or 'weight' for each long and short. This could be one column or separate columns. The key is that the values for the shorts need to be negative and the longs positive.
  3. run the pipeline and extract two series of 'alphas' or 'weights' (one for the longs and one for the shorts).
  4. concatenate these two series into a single 'alpha' or 'weight' series to use for the objective
  5. add any constraints
  6. execute order_optimal_portfolio

Hope that points you towards a solution. See the attached ago for this in action.

This is absolutely brilliant Dan! Thanks for taking the time out to reply. Lemme try this in my algo. I will write back if I face any other issues.

Thanks again
Simran