Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Unable to sort from rank

Hi,
I have been trying for a while to sort and rank a dataframe that I reduced to 1 column for clarity.
I tried many different ways to sort, before ranking, or afterwards. Adding a column as a rank.

I tried the ranking in before_trading_start and moved it to my scheduled_function trading.
This has not changed anything either.

Here is my code.

What I my doing wrong ?!? Thanks
Matt

LOGS ----------------------------
2016-03-01 PRINT 2016-02-29 00:00:00+00:00
Equity(32270 [SSO]) 3
Equity(37513 [ERX]) 1
Equity(37852 [EDC]) 2
2016-03-02 PRINT 2016-03-01 00:00:00+00:00
Equity(32270 [SSO]) 2
Equity(37513 [ERX]) 1
Equity(37852 [EDC]) 3
2016-03-03 PRINT 2016-03-02 00:00:00+00:00
Equity(32270 [SSO]) 1
Equity(37513 [ERX]) 2
Equity(37852 [EDC]) 3
2016-03-04 PRINT 2016-03-03 00:00:00+00:00
Equity(32270 [SSO]) 1
Equity(37513 [ERX]) 2
Equity(37852 [EDC]) 3

2 responses

Hi Matt, everything looks good here except lines 59 and 60. On line 59 you should specify that the DataFrame should be sorted by the column, not by the index. So it could look like this:

momentum_sorted = momentum_rank.sort(columns=momentum_rank.columns[0], ascending=False)  

Then when you want to take the first 5 of that, you should do momentum_sorted = momentum_sorted.iloc[:5], not momentum_sorted = momentum_rank.iloc[:5]. Let me know if these changes solve the problem.

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.

Great !!
Thank you so much Nathan.
My next step is to sort using Stochastic instead of WMA.
Working on it.
Thanks