Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help in designing a simple strategy

Hi!
I want to have a simple strategy where I buy with equal weights the top 1000 securities by market cap and rebalance every quarter.
So each security will have 0.001 of my capital (with the whole capital being 1).
Unfortunately with this code I'm having a bit of problems.
Can anyone help me?

7 responses

In particular I'm not sure if I trade the top 1000 stocks or more.
If I put

screen=top_mkt_cap  

It gives me an error, but in the pipeline returned there are 1000 securities

Now it seems to work better, but I still am not sure if it always receives 1000 stocks to trade. Probably not, since having as a screen just the top securities by mkt cap gives this error ValueError: Cannot convert NA to integer.
If I do the intersection of QTU()&top 1000 stocks by mkt cap I get like 750 stocks and no errors...

To help debug the problem, you might want to try it in the notebooks environment.

When I run this code I see there are 1000 stocks.

from quantopian.pipeline import Pipeline  
from quantopian.pipeline.filters import QTradableStocksUS  
from quantopian.pipeline.data import Fundamentals  
#from quantopian.pipeline.domain import US_EQUITIES  
from quantopian.research import run_pipeline

def make_pipeline():  
    # Gets the latest market cap.  
    mkt_cap = Fundamentals.market_cap.latest  
    screen = mkt_cap.top(1000)#, mask=(QTradableStocksUS()))  
    return Pipeline(  
    columns={  
        'mkt_cap': mkt_cap,  
    },  
    #domain=US_EQUITIES,  
    screen=screen,  
)

df = run_pipeline(make_pipeline(),'2020-08-24','2020-08-24')  
df.shape  

Let's see how many there are in my 2011-2012 period

@Emiliano Your algo looks good and it's doing what I think you intended. I moved the logging around a bit (didn't change any code) and, looking at the logs, most of the time there are 1000 positions in the portfolio the day after a trading day. There are a few occasions where there are 1001 positions. This is because the algo tried to close a position for a delisted stock but couldn't. After a few days those positions are automatically closed by the backtester anyway so the total positions do go back down to 1000.

There are quite a few days where there are less than 1000 positions. The quantity starts at 1000 right after a trading day and then steadily drops for the following three months and then jumps back to 1000 after the algo reballances. This happens because the backtester again automatically closes delisted stock positions. Cash is adjusted into the portfolio (based upon the last known stock price) each time this happens. Actually similar to what a broker would do in real trading.

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.

from quantopian.pipeline import Pipeline  
from quantopian.pipeline.filters import QTradableStocksUS  
from quantopian.pipeline.data import Fundamentals  
#from quantopian.pipeline.domain import US_EQUITIES  
from quantopian.research import run_pipeline

def make_pipeline():  
    # Gets the latest market cap.  
    mkt_cap = Fundamentals.market_cap.latest  
    screen = mkt_cap.top(1000)#, mask=(QTradableStocksUS()))  
    return Pipeline(  
    columns={  
        'mkt_cap': mkt_cap,  
    },  
    #domain=US_EQUITIES,  
    screen=screen,  
)

df = run_pipeline(make_pipeline(),'2011-04-01','2011-04-01')  
df.shape  

(1000, 1)

so they are the same..well anyway I get the ValueError: Cannot convert NA to integer when not using the mask

@Dan thank you very much!
I don't know what you changed but the error has now disappeared (ValueError: Cannot convert NA to integer)...
I'm running on my same timeframe, too.
GREAT!
Thanks