Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to filter by multiple sectors?

I can filter for stocks based in a certain sector by doing this:

.filter(fundamentals.asset_classification.morningstar_sector_code == 311)

This filters stocks that are in the technology sector.

How would I go about filtering for more than one sector? i.e. how would I get all stocks in the technology sector and the industrials sector (sector_code == 310)?

I guess it would be something along the lines of:

.filter(fundamentals.asset_classification.morningstar_sector_code == 311 and fundamentals.asset_classification.morningstar_sector_code == 310)

thanks any help appreciated.

4 responses

We use SQL Alchemy as the ORM with fundamentals. The relevant docs for what you're trying to do can be found here: http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html#common-filter-operators

The AND you are using is not valid SQL Alchemy syntax. What I think SQLAlchemy is doing here is successfully parsing the first portion of the filter (filtering for the sector value = 311) and executing that portion of your filter. The second portion (starting with the 'and') is not executed because it's not recognizing the 'and'.

If you want to do an 'and' like that, you could simply chain two filter() calls together.

I don't think you want to do that since you are stringing together two potential values for the same column.

You probably want an 'in' statement.

Something like:

.filter(fundamentals.asset_classification.morningstar_sector_code.in_([311, 310]))
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.

One quick follow up, there are SQLAlchemy certain methods and operators that have been whitelisted for use with get_fundamentals. From the Quantopian docs:

Within Quantopian, specific SQLAlchemy methods are whitelisted for
your use within the Quantopian IDE. These methods are filter,
filter_by, first, offset, order_by, limit, exists, group_by, having.

Some SQLAlchemy column operators have been whitelisted use within the
Quantopian IDE: asc, desc, nullsfirst, nullslast, label, in_, like,
is_, contains (and corresponding negating operators)

Thanks Josh.

I found two ways to get this done for anybody that reads this in future but there is a problem with one of the methods:

.filter(or_(fundamentals.asset_classification.morningstar_sector_code == 310, fundamentals.asset_classification.morningstar_sector_code == 311))
.filter((fundamentals.asset_classification.morningstar_sector_code == 310) | (fundamentals.asset_classification.morningstar_sector_code == 311))

The first method raises an error that the module or_ does not exist. Is this because the or_ module has not been whitelisted for use in Quantopian?

The second method works with no problem.

Kane, the module hasn't yet been whitelisted for the IDE. Nice work-around in the meantime!

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.