Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Number of stocks in each exchange

Fundamentals.exchange_id in pipeline is often different from the security object exchange attribute.
Fundamentals.exchange_id is None for 30% of them here.
For one thing it means for example one can't rely on Fundamentals to filter for NASDAQ only because that would miss 588 of them this day.

'''
2019-04-18 05:45  
 - - - -   pipeline   - - - -  
 Stocks           8708  
   Exchng        Count  
      NYS         2907  
      NAS         2838  
     None         2655  
      ASE          299  
     ARCX            8  
     BATS            1  
 - - - - from symbols - - - -  
 Stocks           8708  
 Exchange        Count  
     NYSE         4981  
   NASDAQ         3427  
     BATS          300  
 - - - -   pairings   - - - -  
   Pairing       Count  
  NYS    NYSE     2906  
  NAS  NASDAQ     2838  
 None    NYSE     1768  
 None  NASDAQ      588  
 None    BATS      299  
  ASE    NYSE      299  
 ARCX    NYSE        8  
 BATS    BATS        1  
  NYS  NASDAQ        1  
'''

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline.data import Fundamentals  
from quantopian.pipeline.filters import Q500US, Q1500US, Q3000US, QTradableStocksUS

def initialize(context):  
    '''  
    ALL  
    QTU  
    Q3000  
    Q1500  
    Q500  
    '''  
    # The one to use this time, copy/paste:  
    context.collection = 'ALL'

    attach_pipeline(make_pipeline(context), 'pipeline')

def make_pipeline(c):  
    if   c.collection == 'ALL'  : limit = None  
    elif c.collection == 'QTU'  : limit = QTradableStocksUS()  
    elif c.collection == 'Q3000': limit = Q3000US()  
    elif c.collection == 'Q1500': limit = Q1500US()  
    elif c.collection == 'Q500' : limit = Q500US()

    return Pipeline(  
        screen  = limit,  
        columns = {  
            'exchange': Fundamentals.exchange_id.latest  
        }  
    )

def before_trading_start(context, data):  
    out = pipeline_output('pipeline')

    log.info(' - - - -   pipeline   - - - - \nStocks  {}\nExchng Count\nNone    {}\n{}'.format(  
        len(out),  
        len(out[out.exchange == None]),  
        out.exchange.value_counts(),  
    ))


    log.info(' - - - - from symbols - - - - ')  
    count   = 0  
    exchngs = {}  
    pairs   = {}

    for s in out.index:  
        count += 1  
        if str(s.exchange) in exchngs:  
            exchngs[str(s.exchange)] += 1  
            # example  
            if 0 and str(s.exchange) and out.exchange[s] not in str(s.exchange):  
                print('{}       pipe {}  sid.exchange {}'.format(  
                    s.symbol.rjust(6),  
                    out.exchange[s],  
                    str(s.exchange),  
                ))  
        else:  
            exchngs[str(s.exchange)]  = 1

        pairstring = str(out.exchange[s]).rjust(7) + ' ' + str(s.exchange).rjust(7)  
        if pairstring in pairs:  
            pairs[pairstring] += 1  
        else:  
            pairs[pairstring]  = 1

    log.info('Stocks       {}'.format(count))  
    log.info('Exchange    Count')  
    for e in exchngs:  
        log.info('{}   {}'.format(e.rjust(8), str(exchngs[e]).rjust(6)))


    content = ''  
    for p in sorted((value,key) for (key,value) in pairs.items()):  
        content += p[1] + '    ' + str(p[0]).rjust(5) + '\n'  
    log.info(' - - - -   pairings   - - - - \n     Pairing       Count\n{}'.format(content))  
3 responses

The Morningstar Fundamentals dataset only has data for stocks. It doesn't include data for ETFs, warrants, and some other types of securities. Therefore there won't be Morningstar exchange_id data for a number of securities. However, all the securities in the QTradableStocksUS universe have an exchange_id. (One more reason to limit algos to QTradableStocksUS). See the attached notebook.

The exchange_id from Morningstar may at times be different than the symbol object. The exchange_id is 'point-in-time' accurate just like all the other dataset data. That would have been the exchange which a security traded on as of the pipeline date. However, the symbol exchange data is just the current exchange and doesn't get back populated if the exchange changed.

One should really use the Morningstar exchange_id and NOT the exchange from the symbol object. There should be accurate point-in-time exchange data for all the securities in the QTradableStocksUS universe.

Hope that helps?

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.

Thanks for the response and notebook.
What is special about CBOE, the sole BATS?

The BATS exchange primarily focuses on options, FX, futures, and ETPs (Exchange Traded Products) such as ETFs and ETNs. Take a look at their equity listing here https://markets.cboe.com/us/equities/listings/listed_products/ . CBOE is the public company that owns BATS and is traded on the BATS exchange (https://en.wikipedia.org/wiki/Cboe_Global_Markets ). If a public company owns an exchange I guess they feel compelled to trade their own stock there. From what I can tell, CBOE is the only common stock traded on BATS. It's not the sole security traded on BATS, it's just the sole non-ETF/ETN product.

As a check, look at the above notebook. Go down to the Qgrid cell and filter for 'exchange_from_symbol = BATS'. Notice all the ETFs that are there. So, CBOE isn't the sole BATS listing, it's just the sole common stock listing.

Good you pointed that out @Blue