Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Rebalance not Exiting Positions Properly

I'm having an issue with this simple algo that uses a custom dataset. The csv data has a only 6 dates and a list of 24-30 stocks for each date. The list of stocks changes at each date, and I want it to rebalance to match the list for each date. However, it appears that the algo does not exit positions that are no longer in the most recent date list, and adds new positions in the new list for the new date. Can anyone help me debug my code so my long count will not go over the amount of stocks in the most recent date list? Thanks!

2 responses

A feature of self-serve data is that it will always 'forward fill'. It seems the CSV data file is structured something like this. With 24-30 stocks, but generally similar. (Do correct me if that's not the case)

DATE      SYMBOL     WEIGHT  
1-15-2015,  AAA,       25  
1-15-2015,  AAB,       25  
1-15-2015,  AAC,       25  
1-15-2015,  AAD,       25  
5-15-2015,  BBA,       50  
5-15-2015,  BBB,       50


It appears the intent is to open positions for AAA, AAB, AAC, AAD on 1-15-2015 with weights of .25. Then, on 5-15-2015, close those positions and open BBA, BBB with weights of .50. However, since data is forward filled, what will happen is AAA, AAB, AAC, AAD will still have weights of .25 on 5-15-2015 and will not close. The new securities will be appended but existing securities will never change.

So, assuming this is the challenge, what to do? The most straight forward approach is to modify the CSV file. If that is an option, then the format something like this would work as intended.

DATE      SYMBOL     WEIGHT  
1-15-2015,  AAA,       25  
1-15-2015,  AAB,       25  
1-15-2015,  AAC,       25  
1-15-2015,  AAD,       25  
5-15-2015,  AAA,       00  
5-15-2015,  AAB,       00  
5-15-2015,  AAC,       00  
5-15-2015,  AAD,       00  
5-15-2015,  BBA,       50  
5-15-2015,  BBB,       50

If modifying the CSV file isn't an option then one could select just those weights with 'asof_dates' equal to the max date. Something like this

def before_trading_start(context,data):  
    pipe_data = pipeline_output('my_pipeline')

    latest_update = pipe_data.date.max()  
    context.weights = pipe_data.query('date == @latest_update').weights  

Hope I interpreted the problem correctly.

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.

Dan,

Perfect interpretation of my issue. Thank you for the thorough answer! I appreciate the help