Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How do I backtest using a list of specific stocks for my algorithm ?

Hi wonderful Quantopians!

I have a pretty straightforward model. It uses a ranking filter of fundamentals and AVG Dollar Volume strategy when purchasing US stocks. The model uses a bucket of quality assets available for purchase once every week.

I want to test my model using only a list of a certain amount of specific stocks(For example 2-10 different stocks). I would need to remove the filters I have in my model just in case. However, I cannot seem to figure out the simple code needed to make my model purchase from only a list instead or such a large universe.

I keep coming across errors in the IDE, would love some guidance on this one.

def initialize(context):  
    # Universe Selection  
        market = (  
        MarketCap().percentile_between(MIN_Market_Cap_Percentile, 100)  
        & Sector().notnull()  
        & builtin.USEquityPricing.close.latest.notnull()  
        & (builtin.USEquityPricing.volume.latest > 0)  
         )

        monthly_top_volume = (  
        AverageDollarVolume(window_length=LiquidityHistory)  
        .top(MarketSize, mask=market)  
        .downsample(Market_Data_Frequency)  
         )  

  universe = monthly_top_volume & market  

The above code is my universe, when I seem to transform it to a list it does not seem to work. I'm clearly doing something wrong. You can see the rest of my code in the backtest!

Cheers everyone and thanks ahead.

1 response

To limit a pipeline to specific securities one can use either the StaticAssets or StaticSids filters (https://www.quantopian.com/docs/api-reference/pipeline-api-reference#quantopian.pipeline.filters.StaticAssets).

Remember to import the filters first and then simply set your universe to the specific securities you wish.

from quantopian.pipeline.filters import StaticAssets, StaticSids  
# Here we limit out universe to four assets  
universe = StaticAssets(symbols('AAPL', 'IBM', 'C', 'FB'))

Limiting the universe in this way may help debugging but be cautious to not read too much into the results. As an example, the above algo has a min and max position size constraint. With only four assets, the entire portfolio may not be allocated and leave a very small gross exposure (ie leverage). Also, the constraint on sector exposure may be impossible to satisfy (or be greatly skewed) if, for example, the chosen static assets are from just one or two sectors.

See the attached backtest. Simply added the two lines above to the original code.

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.