It's probably easiest to use the pandas sum
method to get a rank or score for assets where the score is the sum of a number of conditions. It's a little involved (a custom factor) to sum conditions inside of pipeline. However, outside of pipeline it can be done with a single command.
First, put all your conditions (which will be True or False) into the pipeline output. Next, simply run the pipeline and sum
those conditions in the resulting dataframe. Something like this
# Define all the conditions for scoring
return_gt_1 = Returns(window_length=5) > .1
rerturns_top_50 = Returns(window_length=5).top(50, mask=base_universe)
price_gt_5 = EquityPricing.close.latest > 5
dollar_volume_gt_mean = AverageDollarVolume(window_length=10).demean(mask=(base_universe)) > 0
# Place the conditions into the pipeline output
return Pipeline(
columns={
'Weeklyreturn_gt_1': return_gt_1,
'rerturns_top_50': rerturns_top_50,
'price_gt_5': price_gt_5,
'dollar_volume_gt_mean': dollar_volume_gt_mean,
},
screen=base_universe
)
After running the pipeline, add a column for score
like this
# Add a column for 'score' to the dataframe
# It will be the sum of the true conditions. Use 'sum' since True=1
pipe_output['score'] = pipe_output.sum(axis=1)
One can then see all the conditions and the final score in a neat dataframe. One could plot the scores or possibly a histogram to check distribution.
Check out the attached notebook.
Hope this helps. Good luck.
Disclaimer
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.